http_SUITE.erl 44 KB

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