echo_h.erl 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. <<"/full", _/bits>> -> read_body(Req0, <<>>);
  18. <<"/opts", _/bits>> -> cowboy_req:read_body(Req0, Opts);
  19. _ -> cowboy_req:read_body(Req0)
  20. end,
  21. {ok, cowboy_req:reply(200, #{}, Body, Req), Opts};
  22. echo(<<"read_urlencoded_body">>, Req0, Opts) ->
  23. Path = cowboy_req:path(Req0),
  24. case {Path, Opts} of
  25. {<<"/opts", _/bits>>, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_body, 2);
  26. {_, #{crash := true}} -> ct_helper:ignore(cowboy_req, read_urlencoded_body, 2);
  27. _ -> ok
  28. end,
  29. {ok, Body, Req} = case Path of
  30. <<"/opts", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts);
  31. <<"/crash", _/bits>> -> cowboy_req:read_urlencoded_body(Req0, Opts);
  32. _ -> cowboy_req:read_urlencoded_body(Req0)
  33. end,
  34. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Body), Req), Opts};
  35. echo(<<"uri">>, Req, Opts) ->
  36. Value = case cowboy_req:path_info(Req) of
  37. [<<"origin">>] -> cowboy_req:uri(Req, #{host => undefined});
  38. [<<"protocol-relative">>] -> cowboy_req:uri(Req, #{scheme => undefined});
  39. [<<"no-qs">>] -> cowboy_req:uri(Req, #{qs => undefined});
  40. [<<"no-path">>] -> cowboy_req:uri(Req, #{path => undefined, qs => undefined});
  41. [<<"set-port">>] -> cowboy_req:uri(Req, #{port => 123});
  42. [] -> cowboy_req:uri(Req)
  43. end,
  44. {ok, cowboy_req:reply(200, #{}, Value, Req), Opts};
  45. echo(<<"match">>, Req, Opts) ->
  46. [Type|Fields0] = cowboy_req:path_info(Req),
  47. Fields = [binary_to_atom(F, latin1) || F <- Fields0],
  48. Value = case Type of
  49. <<"qs">> -> cowboy_req:match_qs(Fields, Req);
  50. <<"cookies">> -> cowboy_req:match_cookies(Fields, Req)
  51. end,
  52. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts};
  53. echo(What, Req, Opts) ->
  54. F = binary_to_atom(What, latin1),
  55. Value = cowboy_req:F(Req),
  56. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
  57. echo_arg(Arg0, Req, Opts) ->
  58. F = binary_to_atom(cowboy_req:binding(key, Req), latin1),
  59. Arg = case F of
  60. binding -> binary_to_atom(Arg0, latin1);
  61. _ -> Arg0
  62. end,
  63. Value = case cowboy_req:binding(default, Req) of
  64. undefined -> cowboy_req:F(Arg, Req);
  65. Default -> cowboy_req:F(Arg, Req, Default)
  66. end,
  67. {ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
  68. read_body(Req0, Acc) ->
  69. case cowboy_req:read_body(Req0) of
  70. {ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req};
  71. {more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>)
  72. end.
  73. value_to_iodata(V) when is_integer(V) -> integer_to_binary(V);
  74. value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1);
  75. value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~p", [V]);
  76. value_to_iodata(V) -> V.