http_SUITE.erl 37 KB

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