http_SUITE.erl 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. %% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(http_SUITE).
  16. -compile(export_all).
  17. -import(cowboy_test, [config/2]).
  18. -import(cowboy_test, [gun_open/1]).
  19. -import(cowboy_test, [gun_monitor_open/1]).
  20. -import(cowboy_test, [gun_monitor_open/2]).
  21. -import(cowboy_test, [gun_is_gone/2]).
  22. -import(cowboy_test, [raw_open/1]).
  23. -import(cowboy_test, [raw_send/2]).
  24. -import(cowboy_test, [raw_recv_head/1]).
  25. -import(cowboy_test, [raw_expect_recv/2]).
  26. %% ct.
  27. all() ->
  28. [
  29. {group, http},
  30. {group, https},
  31. {group, http_compress},
  32. {group, https_compress},
  33. {group, onrequest},
  34. {group, onresponse},
  35. {group, onresponse_capitalize},
  36. {group, parse_host},
  37. {group, set_env}
  38. ].
  39. groups() ->
  40. Tests = cowboy_test:all(?MODULE) -- [
  41. onrequest, onrequest_reply, onrequest_hook,
  42. onresponse_crash, onresponse_reply, onresponse_capitalize,
  43. parse_host, set_env_dispatch
  44. ],
  45. [
  46. {http, [parallel], Tests},
  47. {https, [parallel], Tests},
  48. {http_compress, [parallel], Tests},
  49. {https_compress, [parallel], Tests},
  50. {onrequest, [parallel], [
  51. onrequest,
  52. onrequest_reply
  53. ]},
  54. {onresponse, [parallel], [
  55. onresponse_crash,
  56. onresponse_reply
  57. ]},
  58. {onresponse_capitalize, [parallel], [
  59. onresponse_capitalize
  60. ]},
  61. {parse_host, [], [
  62. parse_host
  63. ]},
  64. {set_env, [], [
  65. set_env_dispatch
  66. ]}
  67. ].
  68. init_per_suite(Config) ->
  69. cowboy_test:start([cowboy, gun]),
  70. Dir = config(priv_dir, Config) ++ "/static",
  71. ct_helper:create_static_dir(Dir),
  72. [{static_dir, Dir}|Config].
  73. end_per_suite(Config) ->
  74. Dir = config(static_dir, Config),
  75. ct_helper:delete_static_dir(Dir),
  76. cowboy_test:stop([cowboy, gun]).
  77. init_tcp_group(Ref, ProtoOpts, Config) ->
  78. Transport = ranch_tcp,
  79. {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
  80. {env, [{dispatch, init_dispatch(Config)}]},
  81. {max_keepalive, 50},
  82. {timeout, 500}
  83. |ProtoOpts]),
  84. Port = ranch:get_port(Ref),
  85. [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
  86. init_ssl_group(Ref, ProtoOpts, Config) ->
  87. Transport = ranch_ssl,
  88. {_, Cert, Key} = ct_helper:make_certs(),
  89. Opts = [{cert, Cert}, {key, Key}],
  90. {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
  91. {env, [{dispatch, init_dispatch(Config)}]},
  92. {max_keepalive, 50},
  93. {timeout, 500}
  94. |ProtoOpts]),
  95. Port = ranch:get_port(Ref),
  96. [{type, ssl}, {port, Port}, {opts, Opts}, {transport, Transport}|Config].
  97. init_per_group(http, Config) ->
  98. init_tcp_group(http, [], Config);
  99. init_per_group(https, Config) ->
  100. init_ssl_group(https, [], Config);
  101. init_per_group(http_compress, Config) ->
  102. init_tcp_group(http_compress, [{compress, true}], Config);
  103. init_per_group(https_compress, Config) ->
  104. init_ssl_group(https_compress, [{compress, true}], Config);
  105. %% Most, if not all of these, should be in separate test suites.
  106. init_per_group(onrequest, Config) ->
  107. Transport = ranch_tcp,
  108. {ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
  109. {env, [{dispatch, init_dispatch(Config)}]},
  110. {max_keepalive, 50},
  111. {onrequest, fun do_onrequest_hook/1},
  112. {timeout, 500}
  113. ]),
  114. Port = ranch:get_port(onrequest),
  115. [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
  116. init_per_group(onresponse, Config) ->
  117. Transport = ranch_tcp,
  118. {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
  119. {env, [{dispatch, init_dispatch(Config)}]},
  120. {max_keepalive, 50},
  121. {onresponse, fun do_onresponse_hook/4},
  122. {timeout, 500}
  123. ]),
  124. Port = ranch:get_port(onresponse),
  125. [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
  126. init_per_group(onresponse_capitalize, Config) ->
  127. Transport = ranch_tcp,
  128. {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
  129. {env, [{dispatch, init_dispatch(Config)}]},
  130. {max_keepalive, 50},
  131. {onresponse, fun do_onresponse_capitalize_hook/4},
  132. {timeout, 500}
  133. ]),
  134. Port = ranch:get_port(onresponse_capitalize),
  135. [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
  136. init_per_group(parse_host, Config) ->
  137. Transport = ranch_tcp,
  138. Dispatch = cowboy_router:compile([
  139. {'_', [
  140. {"/req_attr", http_req_attr, []}
  141. ]}
  142. ]),
  143. {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
  144. {env, [{dispatch, Dispatch}]},
  145. {max_keepalive, 50},
  146. {timeout, 500}
  147. ]),
  148. Port = ranch:get_port(http),
  149. [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
  150. init_per_group(set_env, Config) ->
  151. Transport = ranch_tcp,
  152. {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
  153. {env, [{dispatch, []}]},
  154. {max_keepalive, 50},
  155. {timeout, 500}
  156. ]),
  157. Port = ranch:get_port(set_env),
  158. [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
  159. end_per_group(Name, _) ->
  160. cowboy:stop_listener(Name),
  161. ok.
  162. %% Dispatch configuration.
  163. init_dispatch(Config) ->
  164. cowboy_router:compile([
  165. {"localhost", [
  166. {"/chunked_response", http_chunked, []},
  167. {"/streamed_response", http_streamed, []},
  168. {"/init_shutdown", http_init_shutdown, []},
  169. {"/long_polling", http_long_polling, []},
  170. {"/headers/dupe", http_handler,
  171. [{headers, [{<<"connection">>, <<"close">>}]}]},
  172. {"/set_resp/header", http_set_resp,
  173. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  174. {"/set_resp/overwrite", http_set_resp,
  175. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  176. {"/set_resp/body", http_set_resp,
  177. [{body, <<"A flameless dance does not equal a cycle">>}]},
  178. {"/stream_body/set_resp", http_stream_body,
  179. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  180. {"/stream_body/set_resp_close",
  181. http_stream_body, [
  182. {reply, set_resp_close},
  183. {body, <<"stream_body_set_resp_close">>}]},
  184. {"/stream_body/set_resp_chunked",
  185. http_stream_body, [
  186. {reply, set_resp_chunked},
  187. {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]},
  188. {"/static/[...]", cowboy_static,
  189. {dir, config(static_dir, Config)}},
  190. {"/static_mimetypes_function/[...]", cowboy_static,
  191. {dir, config(static_dir, Config),
  192. [{mimetypes, ?MODULE, do_mimetypes_text_html}]}},
  193. {"/handler_errors", http_errors, []},
  194. {"/static_attribute_etag/[...]", cowboy_static,
  195. {dir, config(static_dir, Config)}},
  196. {"/static_function_etag/[...]", cowboy_static,
  197. {dir, config(static_dir, Config),
  198. [{etag, ?MODULE, do_etag_gen}]}},
  199. {"/static_specify_file/[...]", cowboy_static,
  200. {file, config(static_dir, Config) ++ "/style.css"}},
  201. {"/multipart", http_multipart, []},
  202. {"/multipart/large", http_multipart_stream, []},
  203. {"/echo/body", http_echo_body, []},
  204. {"/echo/body_qs", http_body_qs, []},
  205. {"/param_all", rest_param_all, []},
  206. {"/bad_accept", rest_simple_resource, []},
  207. {"/bad_content_type", rest_patch_resource, []},
  208. {"/simple", rest_simple_resource, []},
  209. {"/forbidden_post", rest_forbidden_resource, [true]},
  210. {"/simple_post", rest_forbidden_resource, [false]},
  211. {"/missing_get_callbacks", rest_missing_callbacks, []},
  212. {"/missing_put_callbacks", rest_missing_callbacks, []},
  213. {"/nodelete", rest_nodelete_resource, []},
  214. {"/post_charset", rest_post_charset_resource, []},
  215. {"/postonly", rest_postonly_resource, []},
  216. {"/patch", rest_patch_resource, []},
  217. {"/resetags", rest_resource_etags, []},
  218. {"/rest_expires", rest_expires, []},
  219. {"/rest_empty_resource", rest_empty_resource, []},
  220. {"/loop_recv", http_loop_recv, []},
  221. {"/loop_stream_recv", http_loop_stream_recv, []},
  222. {"/loop_timeout", http_loop_timeout, []},
  223. {"/", http_handler, []}
  224. ]}
  225. ]).
  226. %% Callbacks.
  227. do_etag_gen(_, _, _) ->
  228. {strong, <<"etag">>}.
  229. do_mimetypes_text_html(_) ->
  230. <<"text/html">>.
  231. %% Convenience functions.
  232. do_raw(Data, Config) ->
  233. Client = raw_open(Config),
  234. ok = raw_send(Client, Data),
  235. case catch raw_recv_head(Client) of
  236. {'EXIT', _} -> closed;
  237. Resp -> element(2, cow_http:parse_status_line(Resp))
  238. end.
  239. do_get(Path, Config) ->
  240. ConnPid = gun_open(Config),
  241. Ref = gun:get(ConnPid, Path),
  242. {response, _, Status, _} = gun:await(ConnPid, Ref),
  243. gun:close(ConnPid),
  244. Status.
  245. %% Tests.
  246. check_raw_status(Config) ->
  247. Huge = [$0 || _ <- lists:seq(1, 5000)],
  248. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  249. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  250. ResponsePacket =
  251. "HTTP/1.0 302 Found\r
  252. Location: http://www.google.co.il/\r
  253. Cache-Control: private\r
  254. Content-Type: text/html; charset=UTF-8\r
  255. Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com\r
  256. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  257. Server: gws\r
  258. Content-Length: 221\r
  259. X-XSS-Protection: 1; mode=block\r
  260. X-Frame-Options: SAMEORIGIN\r
  261. \r
  262. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  263. <TITLE>302 Moved</TITLE></HEAD><BODY>
  264. <H1>302 Moved</H1>
  265. The document has moved
  266. <A HREF=\"http://www.google.co.il/\">here</A>.
  267. </BODY></HTML>",
  268. Tests = [
  269. {102, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  270. "Content-Length: 5000\r\n\r\n", 0:5000/unit:8 >>},
  271. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  272. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  273. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  274. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  275. {200, <<"POST /loop_recv HTTP/1.1\r\nHost: localhost\r\n"
  276. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  277. {400, "\n"},
  278. {400, "Garbage\r\n\r\n"},
  279. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  280. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  281. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  282. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  283. {505, ResponsePacket},
  284. {408, "GET / HTTP/1.1\r\n"},
  285. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  286. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  287. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  288. {414, Huge},
  289. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  290. {500, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  291. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  292. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  293. {closed, ""},
  294. {closed, "\r\n"},
  295. {closed, "\r\n\r\n"},
  296. {closed, "GET / HTTP/1.1"}
  297. ],
  298. _ = [{Status, Packet} = begin
  299. Ret = do_raw(Packet, Config),
  300. {Ret, Packet}
  301. end || {Status, Packet} <- Tests],
  302. ok.
  303. check_status(Config) ->
  304. Tests = [
  305. {102, "/long_polling"},
  306. {200, "/"},
  307. {200, "/simple"},
  308. {204, "/loop_timeout"},
  309. {400, "/static/%2f"},
  310. {400, "/static/%2e"},
  311. {400, "/static/%2e%2e"},
  312. {403, "/static/directory"},
  313. {403, "/static/directory/"},
  314. {403, "/static/unreadable"},
  315. {404, "/not/found"},
  316. {404, "/static/not_found"},
  317. {500, "/handler_errors?case=handle_before_reply"},
  318. {500, "/handler_errors?case=init_before_reply"},
  319. {666, "/init_shutdown"}
  320. ],
  321. _ = [{Status, URL} = begin
  322. Ret = do_get(URL, Config),
  323. {Ret, URL}
  324. end || {Status, URL} <- Tests].
  325. chunked_response(Config) ->
  326. ConnPid = gun_open(Config),
  327. Ref = gun:get(ConnPid, "/chunked_response"),
  328. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  329. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  330. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  331. ok.
  332. %% Check if sending requests whose size is around the MTU breaks something.
  333. echo_body(Config) ->
  334. MTU = ct_helper:get_loopback_mtu(),
  335. _ = [begin
  336. Body = list_to_binary(lists:duplicate(Size, $a)),
  337. ConnPid = gun_open(Config),
  338. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  339. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  340. {ok, Body} = gun:await_body(ConnPid, Ref)
  341. end || Size <- lists:seq(MTU - 500, MTU)],
  342. ok.
  343. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  344. echo_body_max_length(Config) ->
  345. ConnPid = gun_open(Config),
  346. Ref = gun:post(ConnPid, "/echo/body", [], << 0:8000008 >>),
  347. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  348. ok.
  349. % check if body_qs echo's back results
  350. echo_body_qs(Config) ->
  351. ConnPid = gun_open(Config),
  352. Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>),
  353. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  354. {ok, <<"67890">>} = gun:await_body(ConnPid, Ref),
  355. ok.
  356. echo_body_qs_max_length(Config) ->
  357. ConnPid = gun_open(Config),
  358. Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:15996/unit:8 >>),
  359. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  360. ok.
  361. error_chain_handle_after_reply(Config) ->
  362. {ConnPid, MRef} = gun_monitor_open(Config),
  363. Ref1 = gun:get(ConnPid, "/"),
  364. Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
  365. {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
  366. {response, nofin, 200, _} = gun:await(ConnPid, Ref2, MRef),
  367. gun_is_gone(ConnPid, MRef).
  368. error_chain_handle_before_reply(Config) ->
  369. {ConnPid, MRef} = gun_monitor_open(Config),
  370. Ref1 = gun:get(ConnPid, "/"),
  371. Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_before_reply"),
  372. {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
  373. {response, fin, 500, _} = gun:await(ConnPid, Ref2, MRef),
  374. gun_is_gone(ConnPid, MRef).
  375. error_handle_after_reply(Config) ->
  376. {ConnPid, MRef} = gun_monitor_open(Config),
  377. Ref = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
  378. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  379. gun_is_gone(ConnPid, MRef).
  380. error_init_after_reply(Config) ->
  381. {ConnPid, MRef} = gun_monitor_open(Config),
  382. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  383. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  384. gun_is_gone(ConnPid, MRef).
  385. error_init_reply_handle_error(Config) ->
  386. {ConnPid, MRef} = gun_monitor_open(Config),
  387. Ref = gun:get(ConnPid, "/handler_errors?case=init_reply_handle_error"),
  388. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  389. gun_is_gone(ConnPid, MRef).
  390. headers_dupe(Config) ->
  391. {ConnPid, MRef} = gun_monitor_open(Config),
  392. Ref = gun:get(ConnPid, "/headers/dupe"),
  393. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  394. %% Ensure that only one connection header was received.
  395. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  396. gun_is_gone(ConnPid, MRef).
  397. http10_chunkless(Config) ->
  398. {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
  399. Ref = gun:get(ConnPid, "/chunked_response"),
  400. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  401. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  402. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef),
  403. gun_is_gone(ConnPid, MRef).
  404. http10_hostless(Config) ->
  405. Port10 = config(port, Config) + 10,
  406. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  407. ranch:start_listener(Name, 5,
  408. config(transport, Config), config(opts, Config) ++ [{port, Port10}],
  409. cowboy_protocol, [
  410. {env, [{dispatch, cowboy_router:compile([
  411. {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
  412. {max_keepalive, 50},
  413. {timeout, 500}]
  414. ),
  415. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  416. [{port, Port10}|Config]),
  417. cowboy:stop_listener(http10).
  418. keepalive_max(Config) ->
  419. {ConnPid, MRef} = gun_monitor_open(Config),
  420. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  421. || _ <- lists:seq(1, 49)],
  422. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  423. _ = [begin
  424. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  425. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  426. end || Ref <- Refs],
  427. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
  428. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  429. gun_is_gone(ConnPid, MRef).
  430. keepalive_nl(Config) ->
  431. ConnPid = gun_open(Config),
  432. Refs = [begin
  433. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  434. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  435. Ref
  436. end || _ <- lists:seq(1, 10)],
  437. _ = [begin
  438. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  439. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  440. end || Ref <- Refs],
  441. ok.
  442. keepalive_stream_loop(Config) ->
  443. ConnPid = gun_open(Config),
  444. Refs = [begin
  445. Ref = gun:post(ConnPid, "/loop_stream_recv",
  446. [{<<"transfer-encoding">>, <<"chunked">>}]),
  447. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  448. || ID <- lists:seq(1, 250)],
  449. gun:data(ConnPid, Ref, fin, <<>>),
  450. Ref
  451. end || _ <- lists:seq(1, 10)],
  452. _ = [begin
  453. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  454. end || Ref <- Refs],
  455. ok.
  456. multipart(Config) ->
  457. ConnPid = gun_open(Config),
  458. Body = <<
  459. "This is a preamble."
  460. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  461. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  462. "\r\n--OHai--\r\n"
  463. "This is an epilogue."
  464. >>,
  465. Ref = gun:post(ConnPid, "/multipart",
  466. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  467. Body),
  468. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  469. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  470. Parts = binary_to_term(RespBody),
  471. Parts = [
  472. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  473. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  474. ],
  475. ok.
  476. multipart_large(Config) ->
  477. ConnPid = gun_open(Config),
  478. Boundary = "----------",
  479. Big = << 0:9000000/unit:8 >>,
  480. Bigger = << 0:9999999/unit:8 >>,
  481. Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
  482. "--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
  483. "--", Boundary, "--\r\n"],
  484. Ref = gun:post(ConnPid, "/multipart/large",
  485. [{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
  486. Body),
  487. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  488. ok.
  489. do_nc(Config, Input) ->
  490. Cat = os:find_executable("cat"),
  491. Nc = os:find_executable("nc"),
  492. case {Cat, Nc} of
  493. {false, _} ->
  494. {skip, {notfound, cat}};
  495. {_, false} ->
  496. {skip, {notfound, nc}};
  497. _Good ->
  498. %% Throw garbage at the server then check if it's still up.
  499. StrPort = integer_to_list(config(port, Config)),
  500. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  501. || _ <- lists:seq(1, 100)],
  502. 200 = do_get("/", Config)
  503. end.
  504. nc_rand(Config) ->
  505. do_nc(Config, "/dev/urandom").
  506. nc_zero(Config) ->
  507. do_nc(Config, "/dev/zero").
  508. onrequest(Config) ->
  509. ConnPid = gun_open(Config),
  510. Ref = gun:get(ConnPid, "/"),
  511. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  512. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  513. {ok, <<"http_handler">>} = gun:await_body(ConnPid, Ref),
  514. ok.
  515. onrequest_reply(Config) ->
  516. ConnPid = gun_open(Config),
  517. Ref = gun:get(ConnPid, "/?reply=1"),
  518. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  519. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  520. {ok, <<"replied!">>} = gun:await_body(ConnPid, Ref),
  521. ok.
  522. %% Hook for the above onrequest tests.
  523. do_onrequest_hook(Req) ->
  524. case cowboy_req:qs_val(<<"reply">>, Req) of
  525. {undefined, Req2} ->
  526. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  527. {_, Req2} ->
  528. {ok, Req3} = cowboy_req:reply(
  529. 200, [], <<"replied!">>, Req2),
  530. Req3
  531. end.
  532. onresponse_capitalize(Config) ->
  533. Client = raw_open(Config),
  534. ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  535. Data = raw_recv_head(Client),
  536. false = nomatch =:= binary:match(Data, <<"Content-Length">>),
  537. ok.
  538. %% Hook for the above onresponse_capitalize test.
  539. do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  540. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  541. || {N, V} <- Headers],
  542. {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
  543. Req2.
  544. onresponse_crash(Config) ->
  545. ConnPid = gun_open(Config),
  546. Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
  547. {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
  548. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  549. onresponse_reply(Config) ->
  550. ConnPid = gun_open(Config),
  551. Ref = gun:get(ConnPid, "/"),
  552. {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
  553. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  554. ok.
  555. %% Hook for the above onresponse tests.
  556. do_onresponse_hook(_, Headers, _, Req) ->
  557. {ok, Req2} = cowboy_req:reply(
  558. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  559. Req2.
  560. parse_host(Config) ->
  561. ConnPid = gun_open(Config),
  562. Tests = [
  563. {<<"example.org:8080">>, <<"example.org\n8080">>},
  564. {<<"example.org">>, <<"example.org\n80">>},
  565. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  566. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  567. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  568. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  569. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  570. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  571. ],
  572. [begin
  573. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  574. [{<<"host">>, Host}]),
  575. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  576. {ok, Body} = gun:await_body(ConnPid, Ref)
  577. end || {Host, Body} <- Tests],
  578. ok.
  579. pipeline(Config) ->
  580. ConnPid = gun_open(Config),
  581. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  582. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  583. ok.
  584. pipeline_long_polling(Config) ->
  585. ConnPid = gun_open(Config),
  586. Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)],
  587. _ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  588. ok.
  589. rest_param_all(Config) ->
  590. ConnPid = gun_open(Config),
  591. %% Accept without param.
  592. Ref1 = gun:get(ConnPid, "/param_all",
  593. [{<<"accept">>, <<"text/plain">>}]),
  594. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  595. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  596. %% Accept with param.
  597. Ref2 = gun:get(ConnPid, "/param_all",
  598. [{<<"accept">>, <<"text/plain;level=1">>}]),
  599. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  600. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  601. %% Accept with param and quality.
  602. Ref3 = gun:get(ConnPid, "/param_all",
  603. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  604. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  605. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  606. Ref4 = gun:get(ConnPid, "/param_all",
  607. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  608. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  609. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  610. %% Without Accept.
  611. Ref5 = gun:get(ConnPid, "/param_all"),
  612. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  613. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  614. %% Content-Type without param.
  615. Ref6 = gun:put(ConnPid, "/param_all",
  616. [{<<"content-type">>, <<"text/plain">>}]),
  617. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  618. %% Content-Type with param.
  619. Ref7 = gun:put(ConnPid, "/param_all",
  620. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  621. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  622. ok.
  623. rest_bad_accept(Config) ->
  624. ConnPid = gun_open(Config),
  625. Ref = gun:get(ConnPid, "/bad_accept",
  626. [{<<"accept">>, <<"1">>}]),
  627. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  628. ok.
  629. rest_bad_content_type(Config) ->
  630. ConnPid = gun_open(Config),
  631. Ref = gun:patch(ConnPid, "/bad_content_type",
  632. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  633. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  634. ok.
  635. rest_expires(Config) ->
  636. ConnPid = gun_open(Config),
  637. Ref = gun:get(ConnPid, "/rest_expires"),
  638. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  639. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  640. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  641. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  642. ok.
  643. rest_keepalive(Config) ->
  644. ConnPid = gun_open(Config),
  645. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  646. _ = [begin
  647. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  648. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  649. end || Ref <- Refs],
  650. ok.
  651. rest_keepalive_post(Config) ->
  652. ConnPid = gun_open(Config),
  653. Refs = [{
  654. gun:post(ConnPid, "/forbidden_post",
  655. [{<<"content-type">>, <<"text/plain">>}]),
  656. gun:post(ConnPid, "/simple_post",
  657. [{<<"content-type">>, <<"text/plain">>}])
  658. } || _ <- lists:seq(1, 5)],
  659. _ = [begin
  660. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  661. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
  662. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  663. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
  664. end || {Ref1, Ref2} <- Refs],
  665. ok.
  666. rest_missing_get_callbacks(Config) ->
  667. ConnPid = gun_open(Config),
  668. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  669. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  670. ok.
  671. rest_missing_put_callbacks(Config) ->
  672. ConnPid = gun_open(Config),
  673. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  674. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  675. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  676. ok.
  677. rest_nodelete(Config) ->
  678. ConnPid = gun_open(Config),
  679. Ref = gun:delete(ConnPid, "/nodelete"),
  680. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  681. ok.
  682. rest_options_default(Config) ->
  683. ConnPid = gun_open(Config),
  684. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  685. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  686. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  687. ok.
  688. rest_patch(Config) ->
  689. Tests = [
  690. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  691. {422, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  692. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  693. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  694. ],
  695. ConnPid = gun_open(Config),
  696. _ = [begin
  697. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  698. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  699. end || {Status, Headers, Body} <- Tests],
  700. ok.
  701. rest_post_charset(Config) ->
  702. ConnPid = gun_open(Config),
  703. Ref = gun:post(ConnPid, "/post_charset",
  704. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  705. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  706. ok.
  707. rest_postonly(Config) ->
  708. ConnPid = gun_open(Config),
  709. Ref = gun:post(ConnPid, "/postonly",
  710. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  711. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  712. ok.
  713. rest_resource_get_etag(Config, Type) ->
  714. rest_resource_get_etag(Config, Type, []).
  715. rest_resource_get_etag(Config, Type, Headers) ->
  716. ConnPid = gun_open(Config),
  717. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  718. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  719. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  720. false -> {Status, false};
  721. {<<"etag">>, ETag} -> {Status, ETag}
  722. end.
  723. rest_resource_etags(Config) ->
  724. Tests = [
  725. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  726. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  727. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  728. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  729. {500, false, "binary-strong-unquoted"},
  730. {500, false, "binary-weak-unquoted"}
  731. ],
  732. _ = [{Status, ETag, Type} = begin
  733. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  734. {Ret, RespETag, Type}
  735. end || {Status, ETag, Type} <- Tests].
  736. rest_resource_etags_if_none_match(Config) ->
  737. Tests = [
  738. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  739. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  740. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  741. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  742. ],
  743. _ = [{Status, Type} = begin
  744. {Ret, _} = rest_resource_get_etag(Config, Type,
  745. [{<<"if-none-match">>, ETag}]),
  746. {Ret, Type}
  747. end || {Status, ETag, Type} <- Tests].
  748. set_env_dispatch(Config) ->
  749. ConnPid1 = gun_open(Config),
  750. Ref1 = gun:get(ConnPid1, "/"),
  751. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  752. ok = cowboy:set_env(set_env, dispatch,
  753. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  754. ConnPid2 = gun_open(Config),
  755. Ref2 = gun:get(ConnPid2, "/"),
  756. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  757. ok.
  758. set_resp_body(Config) ->
  759. ConnPid = gun_open(Config),
  760. Ref = gun:get(ConnPid, "/set_resp/body"),
  761. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  762. {ok, <<"A flameless dance does not equal a cycle">>}
  763. = gun:await_body(ConnPid, Ref),
  764. ok.
  765. set_resp_header(Config) ->
  766. ConnPid = gun_open(Config),
  767. Ref = gun:get(ConnPid, "/set_resp/header"),
  768. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  769. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  770. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  771. ok.
  772. set_resp_overwrite(Config) ->
  773. ConnPid = gun_open(Config),
  774. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  775. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  776. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  777. ok.
  778. slowloris(Config) ->
  779. Client = raw_open(Config),
  780. try
  781. [begin
  782. ok = raw_send(Client, [C]),
  783. receive after 25 -> ok end
  784. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  785. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  786. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  787. error(failure)
  788. catch error:{badmatch, _} ->
  789. ok
  790. end.
  791. slowloris2(Config) ->
  792. Client = raw_open(Config),
  793. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  794. receive after 300 -> ok end,
  795. ok = raw_send(Client, "Host: localhost\r\n"),
  796. receive after 300 -> ok end,
  797. Data = raw_recv_head(Client),
  798. {_, 408, _, _} = cow_http:parse_status_line(Data),
  799. ok.
  800. static_attribute_etag(Config) ->
  801. ConnPid = gun_open(Config),
  802. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  803. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  804. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  805. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  806. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  807. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  808. true = ETag =/= undefined,
  809. ok.
  810. static_function_etag(Config) ->
  811. ConnPid = gun_open(Config),
  812. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  813. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  814. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  815. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  816. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  817. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  818. true = ETag =/= undefined,
  819. ok.
  820. static_mimetypes_function(Config) ->
  821. ConnPid = gun_open(Config),
  822. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  823. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  824. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  825. ok.
  826. static_specify_file(Config) ->
  827. ConnPid = gun_open(Config),
  828. Ref = gun:get(ConnPid, "/static_specify_file"),
  829. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  830. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  831. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  832. ok.
  833. static_specify_file_catchall(Config) ->
  834. ConnPid = gun_open(Config),
  835. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  836. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  837. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  838. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  839. ok.
  840. static_test_file(Config) ->
  841. ConnPid = gun_open(Config),
  842. Ref = gun:get(ConnPid, "/static/unknown"),
  843. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  844. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  845. ok.
  846. static_test_file_css(Config) ->
  847. ConnPid = gun_open(Config),
  848. Ref = gun:get(ConnPid, "/static/style.css"),
  849. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  850. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  851. ok.
  852. stream_body_set_resp(Config) ->
  853. ConnPid = gun_open(Config),
  854. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  855. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  856. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  857. ok.
  858. stream_body_set_resp_close(Config) ->
  859. {ConnPid, MRef} = gun_monitor_open(Config),
  860. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  861. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  862. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
  863. gun_is_gone(ConnPid, MRef).
  864. stream_body_set_resp_chunked(Config) ->
  865. ConnPid = gun_open(Config),
  866. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  867. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  868. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  869. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  870. ok.
  871. stream_body_set_resp_chunked10(Config) ->
  872. {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
  873. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  874. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  875. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  876. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
  877. gun_is_gone(ConnPid, MRef).
  878. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  879. streamed_response(Config) ->
  880. Client = raw_open(Config),
  881. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  882. Data = raw_recv_head(Client),
  883. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  884. {Headers, Rest2} = cow_http:parse_headers(Rest),
  885. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  886. Rest2Size = byte_size(Rest2),
  887. ok = case <<"streamed_handler\r\nworks fine!">> of
  888. Rest2 -> ok;
  889. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  890. end.
  891. te_chunked(Config) ->
  892. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  893. ConnPid = gun_open(Config),
  894. Ref = gun:post(ConnPid, "/echo/body",
  895. [{<<"transfer-encoding">>, <<"chunked">>}], Body),
  896. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  897. {ok, Body} = gun:await_body(ConnPid, Ref),
  898. ok.
  899. do_body_to_chunks(_, <<>>, Acc) ->
  900. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  901. do_body_to_chunks(ChunkSize, Body, Acc) ->
  902. BodySize = byte_size(Body),
  903. ChunkSize2 = case BodySize < ChunkSize of
  904. true -> BodySize;
  905. false -> ChunkSize
  906. end,
  907. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  908. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  909. do_body_to_chunks(ChunkSize, Rest,
  910. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  911. te_chunked_chopped(Config) ->
  912. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  913. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  914. ConnPid = gun_open(Config),
  915. Ref = gun:post(ConnPid, "/echo/body",
  916. [{<<"transfer-encoding">>, <<"chunked">>}]),
  917. _ = [begin
  918. ok = gun:dbg_send_raw(ConnPid, << C >>),
  919. receive after 10 -> ok end
  920. end || << C >> <= Body2],
  921. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  922. {ok, Body} = gun:await_body(ConnPid, Ref),
  923. ok.
  924. te_chunked_delayed(Config) ->
  925. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  926. Chunks = do_body_to_chunks(50, Body, []),
  927. ConnPid = gun_open(Config),
  928. Ref = gun:post(ConnPid, "/echo/body",
  929. [{<<"transfer-encoding">>, <<"chunked">>}]),
  930. _ = [begin
  931. ok = gun:dbg_send_raw(ConnPid, Chunk),
  932. receive after 10 -> ok end
  933. end || Chunk <- Chunks],
  934. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  935. {ok, Body} = gun:await_body(ConnPid, Ref),
  936. ok.
  937. te_chunked_split_body(Config) ->
  938. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  939. Chunks = do_body_to_chunks(50, Body, []),
  940. ConnPid = gun_open(Config),
  941. Ref = gun:post(ConnPid, "/echo/body",
  942. [{<<"transfer-encoding">>, <<"chunked">>}]),
  943. _ = [begin
  944. case Chunk of
  945. <<"0\r\n\r\n">> ->
  946. ok = gun:dbg_send_raw(ConnPid, Chunk);
  947. _ ->
  948. [Size, ChunkBody, <<>>] =
  949. binary:split(Chunk, [<<"\r\n">>], [global]),
  950. PartASize = random:uniform(byte_size(ChunkBody)),
  951. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  952. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  953. receive after 10 -> ok end,
  954. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  955. end
  956. end || Chunk <- Chunks],
  957. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  958. {ok, Body} = gun:await_body(ConnPid, Ref),
  959. ok.
  960. te_chunked_split_crlf(Config) ->
  961. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  962. Chunks = do_body_to_chunks(50, Body, []),
  963. ConnPid = gun_open(Config),
  964. Ref = gun:post(ConnPid, "/echo/body",
  965. [{<<"transfer-encoding">>, <<"chunked">>}]),
  966. _ = [begin
  967. %% Split in the newline just before the end of the chunk.
  968. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  969. << Chunk2:Len/binary, End/binary >> = Chunk,
  970. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  971. receive after 10 -> ok end,
  972. ok = gun:dbg_send_raw(ConnPid, End)
  973. end || Chunk <- Chunks],
  974. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  975. {ok, Body} = gun:await_body(ConnPid, Ref),
  976. ok.
  977. te_identity(Config) ->
  978. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  979. ConnPid = gun_open(Config),
  980. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  981. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  982. {ok, Body} = gun:await_body(ConnPid, Ref),
  983. ok.