echo_h.erl 3.2 KB

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