http_SUITE.erl 40 KB

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