http_SUITE.erl 41 KB

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