http_SUITE.erl 45 KB

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