resp_h.erl 12 KB

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