resp_h.erl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. <<"nofin">> ->
  220. Req = cowboy_req:stream_reply(200, Req0),
  221. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  222. {ok, Req, Opts};
  223. <<"sendfile">> ->
  224. AppFile = code:where_is_file("cowboy.app"),
  225. AppSize = filelib:file_size(AppFile),
  226. Req = cowboy_req:stream_reply(200, Req0),
  227. cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
  228. cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
  229. cowboy_req:stream_body(<<" interspersed ">>, nofin, Req),
  230. cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
  231. cowboy_req:stream_body(<<" world!">>, fin, Req),
  232. {ok, Req, Opts};
  233. <<"sendfile_fin">> ->
  234. AppFile = code:where_is_file("cowboy.app"),
  235. AppSize = filelib:file_size(AppFile),
  236. Req = cowboy_req:stream_reply(200, Req0),
  237. cowboy_req:stream_body(<<"Hello! ">>, nofin, Req),
  238. cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, fin, Req),
  239. {ok, Req, Opts};
  240. _ ->
  241. %% Call stream_body without initiating streaming.
  242. cowboy_req:stream_body(<<0:800000>>, fin, Req0),
  243. {ok, Req0, Opts}
  244. end;
  245. do(<<"stream_body_content_length">>, Req0, Opts) ->
  246. case cowboy_req:binding(arg, Req0) of
  247. <<"fin0">> ->
  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 world!">>, nofin, Req),
  251. cowboy_req:stream_body(<<>>, fin, Req),
  252. {ok, Req, Opts};
  253. <<"multiple">> ->
  254. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  255. Req = cowboy_req:stream_reply(200, Req1),
  256. cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
  257. cowboy_req:stream_body(<<"world">>, nofin, Req),
  258. cowboy_req:stream_body(<<"!">>, fin, Req),
  259. {ok, Req, Opts};
  260. <<"nofin">> ->
  261. Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
  262. Req = cowboy_req:stream_reply(200, Req1),
  263. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  264. {ok, Req, Opts};
  265. <<"nofin-error">> ->
  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">>, nofin, Req),
  269. {ok, Req, Opts}
  270. end;
  271. do(<<"stream_events">>, Req0, Opts) ->
  272. case cowboy_req:binding(arg, Req0) of
  273. %%<<"single">>
  274. %%<<"list">>
  275. <<"single">> ->
  276. Req = cowboy_req:stream_reply(200,
  277. #{<<"content-type">> => <<"text/event-stream">>},
  278. Req0),
  279. cowboy_req:stream_events(#{
  280. event => <<"add_comment">>,
  281. data => <<"Comment text.\nWith many lines.">>
  282. }, fin, Req),
  283. {ok, Req, Opts};
  284. <<"list">> ->
  285. Req = cowboy_req:stream_reply(200,
  286. #{<<"content-type">> => <<"text/event-stream">>},
  287. Req0),
  288. cowboy_req:stream_events([
  289. #{
  290. event => <<"add_comment">>,
  291. data => <<"Comment text.\nWith many lines.">>
  292. },
  293. #{
  294. comment => <<"Set retry higher\nwith many lines also.">>,
  295. retry => 10000
  296. },
  297. #{
  298. id => <<"123">>,
  299. event => <<"add_comment">>,
  300. data => <<"Closing!">>
  301. }
  302. ], fin, Req),
  303. {ok, Req, Opts};
  304. <<"multiple">> ->
  305. Req = cowboy_req:stream_reply(200,
  306. #{<<"content-type">> => <<"text/event-stream">>},
  307. Req0),
  308. cowboy_req:stream_events(#{
  309. event => <<"add_comment">>,
  310. data => <<"Comment text.\nWith many lines.">>
  311. }, nofin, Req),
  312. cowboy_req:stream_events(#{
  313. comment => <<"Set retry higher\nwith many lines also.">>,
  314. retry => 10000
  315. }, nofin, Req),
  316. cowboy_req:stream_events(#{
  317. id => <<"123">>,
  318. event => <<"add_comment">>,
  319. data => <<"Closing!">>
  320. }, fin, Req),
  321. {ok, Req, Opts}
  322. end;
  323. do(<<"stream_trailers">>, Req0, Opts) ->
  324. case cowboy_req:binding(arg, Req0) of
  325. <<"large">> ->
  326. Req = cowboy_req:stream_reply(200, #{
  327. <<"trailer">> => <<"grpc-status">>
  328. }, Req0),
  329. cowboy_req:stream_body(<<0:800000>>, nofin, Req),
  330. cowboy_req:stream_trailers(#{
  331. <<"grpc-status">> => <<"0">>
  332. }, Req),
  333. {ok, Req, Opts};
  334. _ ->
  335. Req = cowboy_req:stream_reply(200, #{
  336. <<"trailer">> => <<"grpc-status">>
  337. }, Req0),
  338. cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
  339. cowboy_req:stream_trailers(#{
  340. <<"grpc-status">> => <<"0">>
  341. }, Req),
  342. {ok, Req, Opts}
  343. end;
  344. do(<<"push">>, Req, Opts) ->
  345. case cowboy_req:binding(arg, Req) of
  346. <<"method">> ->
  347. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  348. #{method => <<"HEAD">>});
  349. <<"origin">> ->
  350. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  351. #{scheme => <<"ftp">>, host => <<"127.0.0.1">>, port => 21});
  352. <<"qs">> ->
  353. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
  354. #{qs => <<"server=cowboy&version=2.0">>});
  355. _ ->
  356. cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req),
  357. %% The text/plain mime is not defined by default, so a 406 will be returned.
  358. cowboy_req:push("/static/plain.txt", #{<<"accept">> => <<"text/plain">>}, Req)
  359. end,
  360. {ok, cowboy_req:reply(200, Req), Opts}.
  361. stream_body(Req) ->
  362. _ = [cowboy_req:stream_body(<<0:800000>>, nofin, Req) || _ <- lists:seq(1,9)],
  363. cowboy_req:stream_body(<<0:800000>>, fin, Req).