http_SUITE.erl 40 KB

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