http_SUITE.erl 44 KB

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