echo_h.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. %% This module echoes back the value the test is interested in.
  2. -module(echo_h).
  3. -export([init/2]).
  4. init(Req, Opts) ->
  5. case cowboy_req:binding(arg, Req) of
  6. undefined ->
  7. echo(cowboy_req:binding(key, Req), Req, Opts);
  8. Arg ->
  9. echo_arg(Arg, Req, Opts)
  10. end.
  11. echo(<<"read_body">>, Req0, Opts) ->
  12. case Opts of
  13. #{crash := true} -> ct_helper:ignore(cowboy_req, read_body, 2);
  14. _ -> ok
  15. end,
  16. {_, Body, Req} = case cowboy_req:path(Req0) of
  17. <<"/100-continue", _/bits>> ->
  18. cowboy_req:inform(100, Req0),
  19. cowboy_req:read_body(Req0);
  20. <<"/full", _/bits>> -> read_body(Req0, <<>>);
  21. <<"/length", _/bits>> ->
  22. {_, _, Req1} = read_body(Req0, <<>>),
  23. Length = cowboy_req:body_length(Req1),
  24. {ok, integer_to_binary(Length), Req1};
  25. <<"/opts", _/bits>> -> cowboy_req:read_body(Req0, Opts);
  26. _ -> cowboy_req:read_body(Req0)
  27. end,
  28. {ok, cowboy_req:reply(200, #{}, Body, Req), Opts};
  29. echo(<<"read_urlencoded_body">>, Req0, Opts) ->
  30. Path = cowboy_req:path(Req0),
  31. case {Path, Opts} of
  32. {<<"/opts", _/bits>>, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_body, 2);
  33. {_, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_urlencoded_body, 2);
  34. _ -> ok
  35. end,
  36. {ok, Body, Req} = case Path of
  37. <<"/opts", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts);
  38. <<"/crash", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts);
  39. _ -> cowboy_req:read_urlencoded_body(Req0)
  40. end,
  41. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Body), Req), Opts};
  42. echo(<<"uri">>, Req, Opts) ->
  43. Value = case cowboy_req:path_info(Req) of
  44. [<<"origin">>] -> cowboy_req:uri(Req, #{host => undefined});
  45. [<<"protocol-relative">>] -> cowboy_req:uri(Req, #{scheme => undefined});
  46. [<<"no-qs">>] -> cowboy_req:uri(Req, #{qs => undefined});
  47. [<<"no-path">>] -> cowboy_req:uri(Req, #{path => undefined, qs => undefined});
  48. [<<"set-port">>] -> cowboy_req:uri(Req, #{port => 123});
  49. [] -> cowboy_req:uri(Req)
  50. end,
  51. {ok, cowboy_req:reply(200, #{}, Value, Req), Opts};
  52. echo(<<"match">>, Req, Opts) ->
  53. [Type|Fields0] = cowboy_req:path_info(Req),
  54. Fields = [binary_to_atom(F, latin1) || F <- Fields0],
  55. Value = case Type of
  56. <<"qs">> -> cowboy_req:match_qs(Fields, Req);
  57. <<"cookies">> -> cowboy_req:match_cookies(Fields, Req)
  58. end,
  59. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts};
  60. echo(What, Req, Opts) ->
  61. Key = binary_to_atom(What, latin1),
  62. Value = case cowboy_req:path(Req) of
  63. <<"/direct/",_/bits>> -> maps:get(Key, Req);
  64. _ -> cowboy_req:Key(Req)
  65. end,
  66. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
  67. echo_arg(Arg0, Req, Opts) ->
  68. F = binary_to_atom(cowboy_req:binding(key, Req), latin1),
  69. Arg = case F of
  70. binding -> binary_to_atom(Arg0, latin1);
  71. _ -> Arg0
  72. end,
  73. Value = case cowboy_req:binding(default, Req) of
  74. undefined -> cowboy_req:F(Arg, Req);
  75. Default -> cowboy_req:F(Arg, Req, Default)
  76. end,
  77. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
  78. read_body(Req0, Acc) ->
  79. case cowboy_req:read_body(Req0) of
  80. {ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req};
  81. {more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>)
  82. end.
  83. value_to_iodata(V) when is_integer(V) -> integer_to_binary(V);
  84. value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1);
  85. value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~999999p", [V]);
  86. value_to_iodata(V) -> V.