resp_h.erl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. %% This module echoes back the value the test is interested in.
  2. -module(resp_h).
  3. %% @todo Probably should have a separate handler for errors,
  4. %% so that we can dialyze all the other correct calls.
  5. -dialyzer({nowarn_function, do/3}).
  6. -export([init/2]).
  7. init(Req, Opts) ->
  8. do(cowboy_req:binding(key, Req), Req, Opts).
  9. do(<<"set_resp_cookie3">>, Req0, Opts) ->
  10. Req = case cowboy_req:binding(arg, Req0) of
  11. undefined ->
  12. cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0);
  13. <<"multiple">> ->
  14. Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0),
  15. cowboy_req:set_resp_cookie(<<"yourcookie">>, <<"yourvalue">>, Req1);
  16. <<"overwrite">> ->
  17. Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0),
  18. cowboy_req:set_resp_cookie(<<"mycookie">>, <<"overwrite">>, Req1)
  19. end,
  20. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  21. do(<<"set_resp_cookie4">>, Req0, Opts) ->
  22. Req = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0,
  23. #{path => cowboy_req:path(Req0)}),
  24. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  25. do(<<"set_resp_header">>, Req0, Opts) ->
  26. Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
  27. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  28. do(<<"set_resp_header_server">>, Req0, Opts) ->
  29. Req = cowboy_req:set_resp_header(<<"server">>, <<"nginx">>, Req0),
  30. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  31. do(<<"set_resp_headers">>, Req0, Opts) ->
  32. Req = cowboy_req:set_resp_headers(#{
  33. <<"content-type">> => <<"text/plain">>,
  34. <<"content-encoding">> => <<"compress">>
  35. }, Req0),
  36. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  37. do(<<"resp_header_defined">>, Req0, Opts) ->
  38. Req1 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
  39. <<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1),
  40. <<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1, default),
  41. {ok, cowboy_req:reply(200, #{}, "OK", Req0), Opts};
  42. do(<<"resp_header_default">>, Req, Opts) ->
  43. undefined = cowboy_req:resp_header(<<"content-type">>, Req),
  44. default = cowboy_req:resp_header(<<"content-type">>, Req, default),
  45. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  46. do(<<"resp_headers">>, Req0, Opts) ->
  47. Req1 = cowboy_req:set_resp_header(<<"server">>, <<"nginx">>, Req0),
  48. Req = cowboy_req:set_resp_headers(#{
  49. <<"content-type">> => <<"text/plain">>,
  50. <<"content-encoding">> => <<"compress">>
  51. }, Req1),
  52. Headers = cowboy_req:resp_headers(Req),
  53. true = maps:is_key(<<"server">>, Headers),
  54. true = maps:is_key(<<"content-type">>, Headers),
  55. true = maps:is_key(<<"content-encoding">>, Headers),
  56. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  57. do(<<"resp_headers_empty">>, Req, Opts) ->
  58. #{} = cowboy_req:resp_headers(Req),
  59. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  60. do(<<"set_resp_body">>, Req0, Opts) ->
  61. Arg = cowboy_req:binding(arg, Req0),
  62. Req1 = case Arg of
  63. <<"sendfile0">> ->
  64. AppFile = code:where_is_file("cowboy.app"),
  65. cowboy_req:set_resp_body({sendfile, 0, 0, AppFile}, Req0);
  66. <<"sendfile">> ->
  67. AppFile = code:where_is_file("cowboy.app"),
  68. cowboy_req:set_resp_body({sendfile, 0, filelib:file_size(AppFile), AppFile}, Req0);
  69. _ ->
  70. cowboy_req:set_resp_body(<<"OK">>, Req0)
  71. end,
  72. Req = case Arg of
  73. <<"override">> ->
  74. cowboy_req:reply(200, #{}, <<"OVERRIDE">>, Req1);
  75. _ ->
  76. cowboy_req:reply(200, Req1)
  77. end,
  78. {ok, Req, Opts};
  79. do(<<"has_resp_header">>, Req0, Opts) ->
  80. false = cowboy_req:has_resp_header(<<"content-type">>, Req0),
  81. Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
  82. true = cowboy_req:has_resp_header(<<"content-type">>, Req),
  83. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  84. do(<<"has_resp_body">>, Req0, Opts) ->
  85. case cowboy_req:binding(arg, Req0) of
  86. <<"sendfile">> ->
  87. %% @todo Cases for sendfile. Note that sendfile 0 is unallowed.
  88. false = cowboy_req:has_resp_body(Req0),
  89. Req = cowboy_req:set_resp_body({sendfile, 0, 10, code:where_is_file("cowboy.app")}, Req0),
  90. true = cowboy_req:has_resp_body(Req),
  91. {ok, cowboy_req:reply(200, #{}, <<"OK">>, Req), Opts};
  92. undefined ->
  93. false = cowboy_req:has_resp_body(Req0),
  94. Req = cowboy_req:set_resp_body(<<"OK">>, Req0),
  95. true = cowboy_req:has_resp_body(Req),
  96. {ok, cowboy_req:reply(200, #{}, Req), Opts}
  97. end;
  98. do(<<"delete_resp_header">>, Req0, Opts) ->
  99. %% We try to delete first even though it hasn't been set to
  100. %% make sure this noop is possible.
  101. Req1 = cowboy_req:delete_resp_header(<<"content-type">>, Req0),
  102. false = cowboy_req:has_resp_header(<<"content-type">>, Req1),
  103. Req2 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req1),
  104. true = cowboy_req:has_resp_header(<<"content-type">>, Req2),
  105. Req = cowboy_req:delete_resp_header(<<"content-type">>, Req2),
  106. false = cowboy_req:has_resp_header(<<"content-type">>, Req),
  107. {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
  108. do(<<"inform2">>, Req0, Opts) ->
  109. case cowboy_req:binding(arg, Req0) of
  110. <<"binary">> ->
  111. cowboy_req:inform(<<"102 On my way">>, Req0);
  112. <<"error">> ->
  113. ct_helper:ignore(cowboy_req, inform, 3),
  114. cowboy_req:inform(ok, Req0);
  115. <<"twice">> ->
  116. cowboy_req:inform(102, Req0),
  117. cowboy_req:inform(102, Req0);
  118. Status ->
  119. cowboy_req:inform(binary_to_integer(Status), Req0)
  120. end,
  121. Req = cowboy_req:reply(200, Req0),
  122. {ok, Req, Opts};
  123. do(<<"inform3">>, Req0, Opts) ->
  124. Headers = #{<<"ext-header">> => <<"ext-value">>},
  125. case cowboy_req:binding(arg, Req0) of
  126. <<"binary">> ->
  127. cowboy_req:inform(<<"102 On my way">>, Headers, Req0);
  128. <<"error">> ->
  129. ct_helper:ignore(cowboy_req, inform, 3),
  130. cowboy_req:inform(ok, Headers, Req0);
  131. <<"twice">> ->
  132. cowboy_req:inform(102, Headers, Req0),
  133. cowboy_req:inform(102, Headers, Req0);
  134. Status ->
  135. cowboy_req:inform(binary_to_integer(Status), Headers, Req0)
  136. end,
  137. Req = cowboy_req:reply(200, Req0),
  138. {ok, Req, Opts};
  139. do(<<"reply2">>, Req0, Opts) ->
  140. Req = case cowboy_req:binding(arg, Req0) of
  141. <<"binary">> ->
  142. cowboy_req:reply(<<"200 GOOD">>, Req0);
  143. <<"error">> ->
  144. ct_helper:ignore(cowboy_req, reply, 4),
  145. cowboy_req:reply(ok, Req0);
  146. <<"twice">> ->
  147. ct_helper:ignore(cowboy_req, reply, 4),
  148. Req1 = cowboy_req:reply(200, Req0),
  149. cowboy_req:reply(200, Req1);
  150. Status ->
  151. cowboy_req:reply(binary_to_integer(Status), Req0)
  152. end,
  153. {ok, Req, Opts};
  154. do(<<"reply3">>, Req0, Opts) ->
  155. Req = case cowboy_req:binding(arg, Req0) of
  156. <<"error">> ->
  157. ct_helper:ignore(cowboy_req, reply, 4),
  158. cowboy_req:reply(200, ok, Req0);
  159. Status ->
  160. cowboy_req:reply(binary_to_integer(Status),
  161. #{<<"content-type">> => <<"text/plain">>}, Req0)
  162. end,
  163. {ok, Req, Opts};
  164. do(<<"reply4">>, Req0, Opts) ->
  165. Req = case cowboy_req:binding(arg, Req0) of
  166. <<"error">> ->
  167. ct_helper:ignore(erlang, iolist_size, 1),
  168. cowboy_req:reply(200, #{}, ok, Req0);
  169. Status ->
  170. cowboy_req:reply(binary_to_integer(Status), #{}, <<"OK">>, Req0)
  171. end,
  172. {ok, Req, Opts};
  173. do(<<"stream_reply2">>, Req0, Opts) ->
  174. case cowboy_req:binding(arg, Req0) of
  175. <<"binary">> ->
  176. Req = cowboy_req:stream_reply(<<"200 GOOD">>, Req0),
  177. stream_body(Req),
  178. {ok, Req, Opts};
  179. <<"error">> ->
  180. ct_helper:ignore(cowboy_req, stream_reply, 3),
  181. Req = cowboy_req:stream_reply(ok, Req0),
  182. stream_body(Req),
  183. {ok, Req, Opts};
  184. <<"204">> ->
  185. Req = cowboy_req:stream_reply(204, Req0),
  186. {ok, Req, Opts};
  187. <<"304">> ->
  188. Req = cowboy_req:stream_reply(304, Req0),
  189. {ok, Req, Opts};
  190. Status ->
  191. Req = cowboy_req:stream_reply(binary_to_integer(Status), Req0),
  192. stream_body(Req),
  193. {ok, Req, Opts}
  194. end;
  195. do(<<"stream_reply3">>, Req0, Opts) ->
  196. Req = case cowboy_req:binding(arg, Req0) of
  197. <<"error">> ->
  198. ct_helper:ignore(cowboy_req, stream_reply, 3),
  199. cowboy_req:stream_reply(200, ok, Req0);
  200. Status ->
  201. cowboy_req:stream_reply(binary_to_integer(Status),
  202. #{<<"content-type">> => <<"text/plain">>}, Req0)
  203. end,
  204. stream_body(Req),
  205. {ok, Req, Opts};
  206. do(<<"stream_body">>, Req0, Opts) ->
  207. case cowboy_req:binding(arg, Req0) of
  208. <<"fin0">> ->
  209. Req = cowboy_req:stream_reply(200, Req0),
  210. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  211. cowboy_req:stream_body(<<>>, fin, Req),
  212. {ok, Req, Opts};
  213. <<"multiple">> ->
  214. Req = cowboy_req:stream_reply(200, Req0),
  215. cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
  216. cowboy_req:stream_body(<<"world">>, nofin, Req),
  217. cowboy_req:stream_body(<<"!">>, fin, Req),
  218. {ok, Req, Opts};
  219. <<"loop">> ->
  220. Req = cowboy_req:stream_reply(200, Req0),
  221. _ = [cowboy_req:stream_body(<<0:1000000/unit:8>>, nofin, Req)
  222. || _ <- lists:seq(1, 32)],
  223. {ok, Req, Opts};
  224. <<"nofin">> ->
  225. Req = cowboy_req:stream_reply(200, Req0),
  226. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  227. {ok, Req, Opts};
  228. <<"sendfile">> ->
  229. AppFile = code:where_is_file("cowboy.app"),
  230. AppSize = filelib:file_size(AppFile),
  231. Req = cowboy_req:stream_reply(200, Req0),
  232. cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
  233. cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
  234. cowboy_req:stream_body(<<" interspersed ">>, nofin, Req),
  235. cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
  236. cowboy_req:stream_body(<<" world!">>, fin, Req),
  237. {ok, Req, Opts};
  238. <<"sendfile_fin">> ->
  239. AppFile = code:where_is_file("cowboy.app"),
  240. AppSize = filelib:file_size(AppFile),
  241. Req = cowboy_req:stream_reply(200, Req0),
  242. cowboy_req:stream_body(<<"Hello! ">>, nofin, Req),
  243. cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, fin, Req),
  244. {ok, Req, Opts};
  245. _ ->
  246. %% Call stream_body without initiating streaming.
  247. cowboy_req:stream_body(<<0:800000>>, fin, Req0),
  248. {ok, Req0, Opts}
  249. end;
  250. do(<<"stream_body_content_length">>, Req0, Opts) ->
  251. case cowboy_req:binding(arg, Req0) of
  252. <<"fin0">> ->
  253. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  254. Req = cowboy_req:stream_reply(200, Req1),
  255. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  256. cowboy_req:stream_body(<<>>, fin, Req),
  257. {ok, Req, Opts};
  258. <<"multiple">> ->
  259. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  260. Req = cowboy_req:stream_reply(200, Req1),
  261. cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
  262. cowboy_req:stream_body(<<"world">>, nofin, Req),
  263. cowboy_req:stream_body(<<"!">>, fin, Req),
  264. {ok, Req, Opts};
  265. <<"nofin">> ->
  266. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  267. Req = cowboy_req:stream_reply(200, Req1),
  268. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  269. {ok, Req, Opts};
  270. <<"nofin-error">> ->
  271. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  272. Req = cowboy_req:stream_reply(200, Req1),
  273. cowboy_req:stream_body(<<"Hello">>, nofin, Req),
  274. {ok, Req, Opts}
  275. end;
  276. do(<<"stream_events">>, Req0, Opts) ->
  277. case cowboy_req:binding(arg, Req0) of
  278. %%<<"single">>
  279. %%<<"list">>
  280. <<"single">> ->
  281. Req = cowboy_req:stream_reply(200,
  282. #{<<"content-type">> => <<"text/event-stream">>},
  283. Req0),
  284. cowboy_req:stream_events(#{
  285. event => <<"add_comment">>,
  286. data => <<"Comment text.\nWith many lines.">>
  287. }, fin, Req),
  288. {ok, Req, Opts};
  289. <<"list">> ->
  290. Req = cowboy_req:stream_reply(200,
  291. #{<<"content-type">> => <<"text/event-stream">>},
  292. Req0),
  293. cowboy_req:stream_events([
  294. #{
  295. event => <<"add_comment">>,
  296. data => <<"Comment text.\nWith many lines.">>
  297. },
  298. #{
  299. comment => <<"Set retry higher\nwith many lines also.">>,
  300. retry => 10000
  301. },
  302. #{
  303. id => <<"123">>,
  304. event => <<"add_comment">>,
  305. data => <<"Closing!">>
  306. }
  307. ], fin, Req),
  308. {ok, Req, Opts};
  309. <<"multiple">> ->
  310. Req = cowboy_req:stream_reply(200,
  311. #{<<"content-type">> => <<"text/event-stream">>},
  312. Req0),
  313. cowboy_req:stream_events(#{
  314. event => <<"add_comment">>,
  315. data => <<"Comment text.\nWith many lines.">>
  316. }, nofin, Req),
  317. cowboy_req:stream_events(#{
  318. comment => <<"Set retry higher\nwith many lines also.">>,
  319. retry => 10000
  320. }, nofin, Req),
  321. cowboy_req:stream_events(#{
  322. id => <<"123">>,
  323. event => <<"add_comment">>,
  324. data => <<"Closing!">>
  325. }, fin, Req),
  326. {ok, Req, Opts}
  327. end;
  328. do(<<"stream_trailers">>, Req0, Opts) ->
  329. case cowboy_req:binding(arg, Req0) of
  330. <<"large">> ->
  331. Req = cowboy_req:stream_reply(200, #{
  332. <<"trailer">> => <<"grpc-status">>
  333. }, Req0),
  334. cowboy_req:stream_body(<<0:800000>>, nofin, Req),
  335. cowboy_req:stream_trailers(#{
  336. <<"grpc-status">> => <<"0">>
  337. }, Req),
  338. {ok, Req, Opts};
  339. _ ->
  340. Req = cowboy_req:stream_reply(200, #{
  341. <<"trailer">> => <<"grpc-status">>
  342. }, Req0),
  343. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  344. cowboy_req:stream_trailers(#{
  345. <<"grpc-status">> => <<"0">>
  346. }, Req),
  347. {ok, Req, Opts}
  348. end;
  349. do(<<"push">>, Req, Opts) ->
  350. case cowboy_req:binding(arg, Req) of
  351. <<"method">> ->
  352. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  353. #{method => <<"HEAD">>});
  354. <<"origin">> ->
  355. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  356. #{scheme => <<"ftp">>, host => <<"127.0.0.1">>, port => 21});
  357. <<"qs">> ->
  358. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  359. #{qs => <<"server=cowboy&version=2.0">>});
  360. _ ->
  361. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req),
  362. %% The text/plain mime is not defined by default, so a 406 will be returned.
  363. cowboy_req:push("/static/plain.txt", #{<<"accept">> => <<"text/plain">>}, Req)
  364. end,
  365. {ok, cowboy_req:reply(200, Req), Opts}.
  366. stream_body(Req) ->
  367. _ = [cowboy_req:stream_body(<<0:800000>>, nofin, Req) || _ <- lists:seq(1,9)],
  368. cowboy_req:stream_body(<<0:800000>>, fin, Req).