http_SUITE.erl 48 KB

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