resp_h.erl 13 KB

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