resp_h.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. %% This module echoes back the value the test is interested in.
  2. -module(resp_h).
  3. -export([init/2]).
  4. init(Req, Opts) ->
  5. do(cowboy_req:binding(key, Req), Req, Opts).
  6. do(<<"set_resp_cookie3">>, Req0, Opts) ->
  7. Req = case cowboy_req:binding(arg, Req0) of
  8. undefined ->
  9. cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0);
  10. <<"multiple">> ->
  11. Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0),
  12. cowboy_req:set_resp_cookie(<<"yourcookie">>, <<"yourvalue">>, Req1);
  13. <<"overwrite">> ->
  14. Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0),
  15. cowboy_req:set_resp_cookie(<<"mycookie">>, <<"overwrite">>, Req1)
  16. end,
  17. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  18. do(<<"set_resp_cookie4">>, Req0, Opts) ->
  19. Req = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0,
  20. #{path => cowboy_req:path(Req0)}),
  21. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  22. do(<<"set_resp_header">>, Req0, Opts) ->
  23. Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
  24. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  25. do(<<"set_resp_headers">>, Req0, Opts) ->
  26. Req = cowboy_req:set_resp_headers(#{
  27. <<"content-type">> => <<"text/plain">>,
  28. <<"content-encoding">> => <<"compress">>
  29. }, Req0),
  30. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  31. do(<<"resp_header_defined">>, Req0, Opts) ->
  32. Req1 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
  33. <<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1),
  34. <<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1, default),
  35. {ok, cowboy_req:reply(200, #{}, "OK", Req0), Opts};
  36. do(<<"resp_header_default">>, Req, Opts) ->
  37. undefined = cowboy_req:resp_header(<<"content-type">>, Req),
  38. default = cowboy_req:resp_header(<<"content-type">>, Req, default),
  39. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  40. do(<<"resp_headers">>, Req0, Opts) ->
  41. Req1 = cowboy_req:set_resp_header(<<"server">>, <<"nginx">>, Req0),
  42. Req = cowboy_req:set_resp_headers(#{
  43. <<"content-type">> => <<"text/plain">>,
  44. <<"content-encoding">> => <<"compress">>
  45. }, Req1),
  46. Headers = cowboy_req:resp_headers(Req),
  47. true = maps:is_key(<<"server">>, Headers),
  48. true = maps:is_key(<<"content-type">>, Headers),
  49. true = maps:is_key(<<"content-encoding">>, Headers),
  50. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  51. do(<<"resp_headers_empty">>, Req, Opts) ->
  52. #{} = cowboy_req:resp_headers(Req),
  53. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  54. do(<<"set_resp_body">>, Req0, Opts) ->
  55. Arg = cowboy_req:binding(arg, Req0),
  56. Req1 = case Arg of
  57. <<"sendfile0">> ->
  58. AppFile = code:where_is_file("cowboy.app"),
  59. cowboy_req:set_resp_body({sendfile, 0, 0, AppFile}, Req0);
  60. <<"sendfile">> ->
  61. AppFile = code:where_is_file("cowboy.app"),
  62. cowboy_req:set_resp_body({sendfile, 0, filelib:file_size(AppFile), AppFile}, Req0);
  63. _ ->
  64. cowboy_req:set_resp_body(<<"OK">>, Req0)
  65. end,
  66. Req = case Arg of
  67. <<"override">> ->
  68. cowboy_req:reply(200, #{}, <<"OVERRIDE">>, Req1);
  69. _ ->
  70. cowboy_req:reply(200, Req1)
  71. end,
  72. {ok, Req, Opts};
  73. do(<<"has_resp_header">>, Req0, Opts) ->
  74. false = cowboy_req:has_resp_header(<<"content-type">>, Req0),
  75. Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
  76. true = cowboy_req:has_resp_header(<<"content-type">>, Req),
  77. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  78. do(<<"has_resp_body">>, Req0, Opts) ->
  79. case cowboy_req:binding(arg, Req0) of
  80. <<"sendfile">> ->
  81. %% @todo Cases for sendfile. Note that sendfile 0 is unallowed.
  82. false = cowboy_req:has_resp_body(Req0),
  83. Req = cowboy_req:set_resp_body({sendfile, 0, 10, code:where_is_file("cowboy.app")}, Req0),
  84. true = cowboy_req:has_resp_body(Req),
  85. {ok, cowboy_req:reply(200, #{}, <<"OK">>, Req), Opts};
  86. undefined ->
  87. false = cowboy_req:has_resp_body(Req0),
  88. Req = cowboy_req:set_resp_body(<<"OK">>, Req0),
  89. true = cowboy_req:has_resp_body(Req),
  90. {ok, cowboy_req:reply(200, #{}, Req), Opts}
  91. end;
  92. do(<<"delete_resp_header">>, Req0, Opts) ->
  93. %% We try to delete first even though it hasn't been set to
  94. %% make sure this noop is possible.
  95. Req1 = cowboy_req:delete_resp_header(<<"content-type">>, Req0),
  96. false = cowboy_req:has_resp_header(<<"content-type">>, Req1),
  97. Req2 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req1),
  98. true = cowboy_req:has_resp_header(<<"content-type">>, Req2),
  99. Req = cowboy_req:delete_resp_header(<<"content-type">>, Req2),
  100. false = cowboy_req:has_resp_header(<<"content-type">>, Req),
  101. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  102. do(<<"inform2">>, Req0, Opts) ->
  103. case cowboy_req:binding(arg, Req0) of
  104. <<"binary">> ->
  105. cowboy_req:inform(<<"102 On my way">>, Req0);
  106. <<"error">> ->
  107. ct_helper:ignore(cowboy_req, inform, 3),
  108. cowboy_req:inform(ok, Req0);
  109. <<"twice">> ->
  110. cowboy_req:inform(102, Req0),
  111. cowboy_req:inform(102, Req0);
  112. Status ->
  113. cowboy_req:inform(binary_to_integer(Status), Req0)
  114. end,
  115. Req = cowboy_req:reply(200, Req0),
  116. {ok, Req, Opts};
  117. do(<<"inform3">>, Req0, Opts) ->
  118. Headers = #{<<"ext-header">> => <<"ext-value">>},
  119. case cowboy_req:binding(arg, Req0) of
  120. <<"binary">> ->
  121. cowboy_req:inform(<<"102 On my way">>, Headers, Req0);
  122. <<"error">> ->
  123. ct_helper:ignore(cowboy_req, inform, 3),
  124. cowboy_req:inform(ok, Headers, Req0);
  125. <<"twice">> ->
  126. cowboy_req:inform(102, Headers, Req0),
  127. cowboy_req:inform(102, Headers, Req0);
  128. Status ->
  129. cowboy_req:inform(binary_to_integer(Status), Headers, Req0)
  130. end,
  131. Req = cowboy_req:reply(200, Req0),
  132. {ok, Req, Opts};
  133. do(<<"reply2">>, Req0, Opts) ->
  134. Req = case cowboy_req:binding(arg, Req0) of
  135. <<"binary">> ->
  136. cowboy_req:reply(<<"200 GOOD">>, Req0);
  137. <<"error">> ->
  138. ct_helper:ignore(cowboy_req, reply, 4),
  139. cowboy_req:reply(ok, Req0);
  140. <<"twice">> ->
  141. ct_helper:ignore(cowboy_req, reply, 4),
  142. Req1 = cowboy_req:reply(200, Req0),
  143. cowboy_req:reply(200, Req1);
  144. Status ->
  145. cowboy_req:reply(binary_to_integer(Status), Req0)
  146. end,
  147. {ok, Req, Opts};
  148. do(<<"reply3">>, Req0, Opts) ->
  149. Req = case cowboy_req:binding(arg, Req0) of
  150. <<"error">> ->
  151. ct_helper:ignore(cowboy_req, reply, 4),
  152. cowboy_req:reply(200, ok, Req0);
  153. Status ->
  154. cowboy_req:reply(binary_to_integer(Status),
  155. #{<<"content-type">> => <<"text/plain">>}, Req0)
  156. end,
  157. {ok, Req, Opts};
  158. do(<<"reply4">>, Req0, Opts) ->
  159. Req = case cowboy_req:binding(arg, Req0) of
  160. <<"error">> ->
  161. ct_helper:ignore(erlang, iolist_size, 1),
  162. cowboy_req:reply(200, #{}, ok, Req0);
  163. Status ->
  164. cowboy_req:reply(binary_to_integer(Status), #{}, <<"OK">>, Req0)
  165. end,
  166. {ok, Req, Opts};
  167. do(<<"stream_reply2">>, Req0, Opts) ->
  168. case cowboy_req:binding(arg, Req0) of
  169. <<"binary">> ->
  170. Req = cowboy_req:stream_reply(<<"200 GOOD">>, Req0),
  171. stream_body(Req),
  172. {ok, Req, Opts};
  173. <<"error">> ->
  174. ct_helper:ignore(cowboy_req, stream_reply, 3),
  175. Req = cowboy_req:stream_reply(ok, Req0),
  176. stream_body(Req),
  177. {ok, Req, Opts};
  178. <<"204">> ->
  179. Req = cowboy_req:stream_reply(204, Req0),
  180. {ok, Req, Opts};
  181. Status ->
  182. Req = cowboy_req:stream_reply(binary_to_integer(Status), Req0),
  183. stream_body(Req),
  184. {ok, Req, Opts}
  185. end;
  186. do(<<"stream_reply3">>, Req0, Opts) ->
  187. Req = case cowboy_req:binding(arg, Req0) of
  188. <<"error">> ->
  189. ct_helper:ignore(cowboy_req, stream_reply, 3),
  190. cowboy_req:stream_reply(200, ok, Req0);
  191. Status ->
  192. cowboy_req:stream_reply(binary_to_integer(Status),
  193. #{<<"content-type">> => <<"text/plain">>}, Req0)
  194. end,
  195. stream_body(Req),
  196. {ok, Req, Opts};
  197. do(<<"stream_body">>, Req0, Opts) ->
  198. case cowboy_req:binding(arg, Req0) of
  199. <<"fin0">> ->
  200. Req = cowboy_req:stream_reply(200, Req0),
  201. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  202. cowboy_req:stream_body(<<>>, fin, Req),
  203. {ok, Req, Opts};
  204. <<"multiple">> ->
  205. Req = cowboy_req:stream_reply(200, Req0),
  206. cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
  207. cowboy_req:stream_body(<<"world">>, nofin, Req),
  208. cowboy_req:stream_body(<<"!">>, fin, Req),
  209. {ok, Req, Opts};
  210. <<"nofin">> ->
  211. Req = cowboy_req:stream_reply(200, Req0),
  212. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  213. {ok, Req, Opts};
  214. _ ->
  215. %% Call stream_body without initiating streaming.
  216. cowboy_req:stream_body(<<0:800000>>, fin, Req0),
  217. {ok, Req0, Opts}
  218. end;
  219. do(<<"stream_body_content_length">>, Req0, Opts) ->
  220. case cowboy_req:binding(arg, Req0) of
  221. <<"fin0">> ->
  222. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  223. Req = cowboy_req:stream_reply(200, Req1),
  224. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  225. cowboy_req:stream_body(<<>>, fin, Req),
  226. {ok, Req, Opts};
  227. <<"multiple">> ->
  228. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  229. Req = cowboy_req:stream_reply(200, Req1),
  230. cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
  231. cowboy_req:stream_body(<<"world">>, nofin, Req),
  232. cowboy_req:stream_body(<<"!">>, fin, Req),
  233. {ok, Req, Opts};
  234. <<"nofin">> ->
  235. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  236. Req = cowboy_req:stream_reply(200, Req1),
  237. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  238. {ok, Req, Opts};
  239. <<"nofin-error">> ->
  240. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  241. Req = cowboy_req:stream_reply(200, Req1),
  242. cowboy_req:stream_body(<<"Hello">>, nofin, Req),
  243. {ok, Req, Opts}
  244. end;
  245. do(<<"stream_trailers">>, Req0, Opts) ->
  246. case cowboy_req:binding(arg, Req0) of
  247. <<"large">> ->
  248. Req = cowboy_req:stream_reply(200, #{
  249. <<"trailer">> => <<"grpc-status">>
  250. }, Req0),
  251. cowboy_req:stream_body(<<0:800000>>, nofin, Req),
  252. cowboy_req:stream_trailers(#{
  253. <<"grpc-status">> => <<"0">>
  254. }, Req),
  255. {ok, Req, Opts};
  256. _ ->
  257. Req = cowboy_req:stream_reply(200, #{
  258. <<"trailer">> => <<"grpc-status">>
  259. }, Req0),
  260. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  261. cowboy_req:stream_trailers(#{
  262. <<"grpc-status">> => <<"0">>
  263. }, Req),
  264. {ok, Req, Opts}
  265. end;
  266. do(<<"push">>, Req, Opts) ->
  267. case cowboy_req:binding(arg, Req) of
  268. <<"method">> ->
  269. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  270. #{method => <<"HEAD">>});
  271. <<"origin">> ->
  272. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  273. #{scheme => <<"ftp">>, host => <<"127.0.0.1">>, port => 21});
  274. <<"qs">> ->
  275. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  276. #{qs => <<"server=cowboy&version=2.0">>});
  277. _ ->
  278. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req),
  279. %% The text/plain mime is not defined by default, so a 406 will be returned.
  280. cowboy_req:push("/static/plain.txt", #{<<"accept">> => <<"text/plain">>}, Req)
  281. end,
  282. {ok, cowboy_req:reply(200, Req), Opts}.
  283. stream_body(Req) ->
  284. _ = [cowboy_req:stream_body(<<0:800000>>, nofin, Req) || _ <- lists:seq(1,9)],
  285. cowboy_req:stream_body(<<0:800000>>, fin, Req).