resp_h.erl 13 KB

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