resp_h.erl 14 KB

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