http_SUITE.erl 39 KB

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