http_SUITE.erl 37 KB

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