resp_h.erl 12 KB

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