echo_h.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. <<"/delay", _/bits>> ->
  21. timer:sleep(500),
  22. cowboy_req:read_body(Req0);
  23. <<"/full", _/bits>> -> read_body(Req0, <<>>);
  24. <<"/length", _/bits>> ->
  25. {_, _, Req1} = read_body(Req0, <<>>),
  26. Length = cowboy_req:body_length(Req1),
  27. {ok, integer_to_binary(Length), Req1};
  28. <<"/opts", _/bits>> -> cowboy_req:read_body(Req0, Opts);
  29. _ -> cowboy_req:read_body(Req0)
  30. end,
  31. {ok, cowboy_req:reply(200, #{}, Body, Req), Opts};
  32. echo(<<"read_urlencoded_body">>, Req0, Opts) ->
  33. Path = cowboy_req:path(Req0),
  34. case {Path, Opts} of
  35. {<<"/opts", _/bits>>, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_body, 2);
  36. {_, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_urlencoded_body, 2);
  37. _ -> ok
  38. end,
  39. {ok, Body, Req} = case Path of
  40. <<"/opts", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts);
  41. <<"/crash", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts);
  42. _ -> cowboy_req:read_urlencoded_body(Req0)
  43. end,
  44. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Body), Req), Opts};
  45. echo(<<"read_and_match_urlencoded_body">>, Req0, Opts) ->
  46. Path = cowboy_req:path(Req0),
  47. case {Path, Opts} of
  48. {<<"/opts", _/bits>>, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_body, 2);
  49. {_, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_urlencoded_body, 2);
  50. _ -> ok
  51. end,
  52. {ok, Body, Req} = case Path of
  53. <<"/opts", _/bits>> -> cowboy_req:read_and_match_urlencoded_body([], Req0, Opts);
  54. <<"/crash", _/bits>> -> cowboy_req:read_and_match_urlencoded_body([], Req0, Opts);
  55. _ -> cowboy_req:read_and_match_urlencoded_body([], Req0)
  56. end,
  57. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Body), Req), Opts};
  58. echo(<<"uri">>, Req, Opts) ->
  59. Value = case cowboy_req:path_info(Req) of
  60. [<<"origin">>] -> cowboy_req:uri(Req, #{host => undefined});
  61. [<<"protocol-relative">>] -> cowboy_req:uri(Req, #{scheme => undefined});
  62. [<<"no-qs">>] -> cowboy_req:uri(Req, #{qs => undefined});
  63. [<<"no-path">>] -> cowboy_req:uri(Req, #{path => undefined, qs => undefined});
  64. [<<"set-port">>] -> cowboy_req:uri(Req, #{port => 123});
  65. _ -> cowboy_req:uri(Req)
  66. end,
  67. {ok, cowboy_req:reply(200, #{}, Value, Req), Opts};
  68. echo(<<"match">>, Req, Opts) ->
  69. [Type|Fields0] = cowboy_req:path_info(Req),
  70. Fields = [binary_to_atom(F, latin1) || F <- Fields0],
  71. Value = case Type of
  72. <<"qs">> -> cowboy_req:match_qs(Fields, Req);
  73. <<"cookies">> -> cowboy_req:match_cookies(Fields, Req);
  74. <<"body_qs">> ->
  75. %% Note that the Req should not be discarded but for the
  76. %% purpose of this test this has no ill impacts.
  77. {ok, Match, _} = cowboy_req:read_and_match_urlencoded_body(Fields, Req),
  78. Match
  79. end,
  80. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts};
  81. echo(What, Req, Opts) ->
  82. Key = binary_to_atom(What, latin1),
  83. Value = case cowboy_req:path(Req) of
  84. <<"/direct/",_/bits>> -> maps:get(Key, Req);
  85. _ -> cowboy_req:Key(Req)
  86. end,
  87. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
  88. echo_arg(Arg0, Req, Opts) ->
  89. F = binary_to_atom(cowboy_req:binding(key, Req), latin1),
  90. Arg = case F of
  91. binding -> binary_to_atom(Arg0, latin1);
  92. _ -> Arg0
  93. end,
  94. Value = case cowboy_req:binding(default, Req) of
  95. undefined -> cowboy_req:F(Arg, Req);
  96. Default -> cowboy_req:F(Arg, Req, Default)
  97. end,
  98. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
  99. read_body(Req0, Acc) ->
  100. case cowboy_req:read_body(Req0) of
  101. {ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req};
  102. {more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>)
  103. end.
  104. value_to_iodata(V) when is_integer(V) -> integer_to_binary(V);
  105. value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1);
  106. value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~999999p", [V]);
  107. value_to_iodata(V) -> V.