cowboy_req.erl 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. %% Copyright (c) 2011-2017, 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(cowboy_req).
  16. %% Request.
  17. -export([method/1]).
  18. -export([version/1]).
  19. -export([peer/1]).
  20. -export([sock/1]).
  21. -export([cert/1]).
  22. -export([scheme/1]).
  23. -export([host/1]).
  24. -export([host_info/1]).
  25. -export([port/1]).
  26. -export([path/1]).
  27. -export([path_info/1]).
  28. -export([qs/1]).
  29. -export([parse_qs/1]).
  30. -export([match_qs/2]).
  31. -export([uri/1]).
  32. -export([uri/2]).
  33. -export([binding/2]).
  34. -export([binding/3]).
  35. -export([bindings/1]).
  36. -export([header/2]).
  37. -export([header/3]).
  38. -export([headers/1]).
  39. -export([parse_header/2]).
  40. -export([parse_header/3]).
  41. -export([parse_cookies/1]).
  42. -export([match_cookies/2]).
  43. %% Request body.
  44. -export([has_body/1]).
  45. -export([body_length/1]).
  46. -export([read_body/1]).
  47. -export([read_body/2]).
  48. -export([read_urlencoded_body/1]).
  49. -export([read_urlencoded_body/2]).
  50. %% @todo read_and_match_urlencoded_body?
  51. %% Multipart.
  52. -export([read_part/1]).
  53. -export([read_part/2]).
  54. -export([read_part_body/1]).
  55. -export([read_part_body/2]).
  56. %% Response.
  57. -export([set_resp_cookie/3]).
  58. -export([set_resp_cookie/4]).
  59. -export([resp_header/2]).
  60. -export([resp_header/3]).
  61. -export([resp_headers/1]).
  62. -export([set_resp_header/3]).
  63. -export([set_resp_headers/2]).
  64. -export([has_resp_header/2]).
  65. -export([delete_resp_header/2]).
  66. -export([set_resp_body/2]).
  67. %% @todo set_resp_body/3 with a ContentType or even Headers argument, to set content headers.
  68. -export([has_resp_body/1]).
  69. -export([inform/2]).
  70. -export([inform/3]).
  71. -export([reply/2]).
  72. -export([reply/3]).
  73. -export([reply/4]).
  74. -export([stream_reply/2]).
  75. -export([stream_reply/3]).
  76. %% @todo stream_body/2 (nofin)
  77. -export([stream_body/3]).
  78. %% @todo stream_event/2,3
  79. -export([stream_trailers/2]).
  80. -export([push/3]).
  81. -export([push/4]).
  82. %% Internal.
  83. -export([response_headers/2]).
  84. %% @todo Get rid of this type, use cow_cookie directly.
  85. -type cookie_opts() :: map().
  86. -export_type([cookie_opts/0]).
  87. -type read_body_opts() :: #{
  88. length => non_neg_integer() | infinity,
  89. period => non_neg_integer(),
  90. timeout => timeout()
  91. }.
  92. -export_type([read_body_opts/0]).
  93. %% While sendfile allows a Len of 0 that means "everything past Offset",
  94. %% Cowboy expects the real length as it is used as metadata.
  95. %% @todo We should probably explicitly reject it.
  96. -type resp_body() :: iodata()
  97. | {sendfile, non_neg_integer(), non_neg_integer(), file:name_all()}.
  98. -export_type([resp_body/0]).
  99. -type push_opts() :: #{
  100. method => binary(),
  101. scheme => binary(),
  102. host => binary(),
  103. port => binary(),
  104. qs => binary()
  105. }.
  106. -export_type([push_opts/0]).
  107. -type req() :: map(). %% @todo #{
  108. % ref := ranch:ref(),
  109. % pid := pid(),
  110. % streamid := cowboy_stream:streamid(),
  111. % peer := {inet:ip_address(), inet:port_number()},
  112. %
  113. % method := binary(), %% case sensitive
  114. % version := cowboy:http_version() | atom(),
  115. % scheme := binary(), %% <<"http">> or <<"https">>
  116. % host := binary(), %% lowercase; case insensitive
  117. % port := inet:port_number(),
  118. % path := binary(), %% case sensitive
  119. % qs := binary(), %% case sensitive
  120. % headers := cowboy:http_headers(),
  121. %
  122. % host_info => cowboy_router:tokens(),
  123. % path_info => cowboy_router:tokens(),
  124. % bindings => cowboy_router:bindings(),
  125. %
  126. % has_body := boolean(),
  127. % has_read_body => true,
  128. % body_length := undefined | non_neg_integer()
  129. %
  130. %% @todo resp_*
  131. %}.
  132. -export_type([req/0]).
  133. %% Request.
  134. -spec method(req()) -> binary().
  135. method(#{method := Method}) ->
  136. Method.
  137. -spec version(req()) -> cowboy:http_version().
  138. version(#{version := Version}) ->
  139. Version.
  140. -spec peer(req()) -> {inet:ip_address(), inet:port_number()}.
  141. peer(#{peer := Peer}) ->
  142. Peer.
  143. -spec sock(req()) -> {inet:ip_address(), inet:port_number()}.
  144. sock(#{sock := Sock}) ->
  145. Sock.
  146. -spec cert(req()) -> binary() | undefined.
  147. cert(#{cert := Cert}) ->
  148. Cert.
  149. -spec scheme(req()) -> binary().
  150. scheme(#{scheme := Scheme}) ->
  151. Scheme.
  152. -spec host(req()) -> binary().
  153. host(#{host := Host}) ->
  154. Host.
  155. %% @todo The host_info is undefined if cowboy_router isn't used. Do we want to crash?
  156. -spec host_info(req()) -> cowboy_router:tokens() | undefined.
  157. host_info(#{host_info := HostInfo}) ->
  158. HostInfo.
  159. -spec port(req()) -> inet:port_number().
  160. port(#{port := Port}) ->
  161. Port.
  162. -spec path(req()) -> binary().
  163. path(#{path := Path}) ->
  164. Path.
  165. %% @todo The path_info is undefined if cowboy_router isn't used. Do we want to crash?
  166. -spec path_info(req()) -> cowboy_router:tokens() | undefined.
  167. path_info(#{path_info := PathInfo}) ->
  168. PathInfo.
  169. -spec qs(req()) -> binary().
  170. qs(#{qs := Qs}) ->
  171. Qs.
  172. %% @todo Might be useful to limit the number of keys.
  173. -spec parse_qs(req()) -> [{binary(), binary() | true}].
  174. parse_qs(#{qs := Qs}) ->
  175. try
  176. cow_qs:parse_qs(Qs)
  177. catch _:_ ->
  178. erlang:raise(exit, {request_error, qs,
  179. 'Malformed query string; application/x-www-form-urlencoded expected.'
  180. }, erlang:get_stacktrace())
  181. end.
  182. -spec match_qs(cowboy:fields(), req()) -> map().
  183. match_qs(Fields, Req) ->
  184. case filter(Fields, kvlist_to_map(Fields, parse_qs(Req))) of
  185. {ok, Map} ->
  186. Map;
  187. {error, Errors} ->
  188. exit({request_error, {match_qs, Errors},
  189. 'Query string validation constraints failed for the reasons provided.'})
  190. end.
  191. -spec uri(req()) -> iodata().
  192. uri(Req) ->
  193. uri(Req, #{}).
  194. -spec uri(req(), map()) -> iodata().
  195. uri(#{scheme := Scheme0, host := Host0, port := Port0,
  196. path := Path0, qs := Qs0}, Opts) ->
  197. Scheme = case maps:get(scheme, Opts, Scheme0) of
  198. S = undefined -> S;
  199. S -> iolist_to_binary(S)
  200. end,
  201. Host = maps:get(host, Opts, Host0),
  202. Port = maps:get(port, Opts, Port0),
  203. Path = maps:get(path, Opts, Path0),
  204. Qs = maps:get(qs, Opts, Qs0),
  205. Fragment = maps:get(fragment, Opts, undefined),
  206. [uri_host(Scheme, Scheme0, Port, Host), uri_path(Path), uri_qs(Qs), uri_fragment(Fragment)].
  207. uri_host(_, _, _, undefined) -> <<>>;
  208. uri_host(Scheme, Scheme0, Port, Host) ->
  209. case iolist_size(Host) of
  210. 0 -> <<>>;
  211. _ -> [uri_scheme(Scheme), <<"//">>, Host, uri_port(Scheme, Scheme0, Port)]
  212. end.
  213. uri_scheme(undefined) -> <<>>;
  214. uri_scheme(Scheme) ->
  215. case iolist_size(Scheme) of
  216. 0 -> Scheme;
  217. _ -> [Scheme, $:]
  218. end.
  219. uri_port(_, _, undefined) -> <<>>;
  220. uri_port(undefined, <<"http">>, 80) -> <<>>;
  221. uri_port(undefined, <<"https">>, 443) -> <<>>;
  222. uri_port(<<"http">>, _, 80) -> <<>>;
  223. uri_port(<<"https">>, _, 443) -> <<>>;
  224. uri_port(_, _, Port) ->
  225. [$:, integer_to_binary(Port)].
  226. uri_path(undefined) -> <<>>;
  227. uri_path(Path) -> Path.
  228. uri_qs(undefined) -> <<>>;
  229. uri_qs(Qs) ->
  230. case iolist_size(Qs) of
  231. 0 -> Qs;
  232. _ -> [$?, Qs]
  233. end.
  234. uri_fragment(undefined) -> <<>>;
  235. uri_fragment(Fragment) ->
  236. case iolist_size(Fragment) of
  237. 0 -> Fragment;
  238. _ -> [$#, Fragment]
  239. end.
  240. -ifdef(TEST).
  241. uri1_test() ->
  242. <<"http://localhost/path">> = iolist_to_binary(uri(#{
  243. scheme => <<"http">>, host => <<"localhost">>, port => 80,
  244. path => <<"/path">>, qs => <<>>})),
  245. <<"http://localhost:443/path">> = iolist_to_binary(uri(#{
  246. scheme => <<"http">>, host => <<"localhost">>, port => 443,
  247. path => <<"/path">>, qs => <<>>})),
  248. <<"http://localhost:8080/path">> = iolist_to_binary(uri(#{
  249. scheme => <<"http">>, host => <<"localhost">>, port => 8080,
  250. path => <<"/path">>, qs => <<>>})),
  251. <<"http://localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(#{
  252. scheme => <<"http">>, host => <<"localhost">>, port => 8080,
  253. path => <<"/path">>, qs => <<"dummy=2785">>})),
  254. <<"https://localhost/path">> = iolist_to_binary(uri(#{
  255. scheme => <<"https">>, host => <<"localhost">>, port => 443,
  256. path => <<"/path">>, qs => <<>>})),
  257. <<"https://localhost:8443/path">> = iolist_to_binary(uri(#{
  258. scheme => <<"https">>, host => <<"localhost">>, port => 8443,
  259. path => <<"/path">>, qs => <<>>})),
  260. <<"https://localhost:8443/path?dummy=2785">> = iolist_to_binary(uri(#{
  261. scheme => <<"https">>, host => <<"localhost">>, port => 8443,
  262. path => <<"/path">>, qs => <<"dummy=2785">>})),
  263. ok.
  264. uri2_test() ->
  265. Req = #{
  266. scheme => <<"http">>, host => <<"localhost">>, port => 8080,
  267. path => <<"/path">>, qs => <<"dummy=2785">>
  268. },
  269. <<"http://localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{})),
  270. %% Disable individual components.
  271. <<"//localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => undefined})),
  272. <<"/path?dummy=2785">> = iolist_to_binary(uri(Req, #{host => undefined})),
  273. <<"http://localhost/path?dummy=2785">> = iolist_to_binary(uri(Req, #{port => undefined})),
  274. <<"http://localhost:8080?dummy=2785">> = iolist_to_binary(uri(Req, #{path => undefined})),
  275. <<"http://localhost:8080/path">> = iolist_to_binary(uri(Req, #{qs => undefined})),
  276. <<"http://localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{fragment => undefined})),
  277. <<"http://localhost:8080">> = iolist_to_binary(uri(Req, #{path => undefined, qs => undefined})),
  278. <<>> = iolist_to_binary(uri(Req, #{host => undefined, path => undefined, qs => undefined})),
  279. %% Empty values.
  280. <<"//localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => <<>>})),
  281. <<"//localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => ""})),
  282. <<"//localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => [<<>>]})),
  283. <<"/path?dummy=2785">> = iolist_to_binary(uri(Req, #{host => <<>>})),
  284. <<"/path?dummy=2785">> = iolist_to_binary(uri(Req, #{host => ""})),
  285. <<"/path?dummy=2785">> = iolist_to_binary(uri(Req, #{host => [<<>>]})),
  286. <<"http://localhost:8080?dummy=2785">> = iolist_to_binary(uri(Req, #{path => <<>>})),
  287. <<"http://localhost:8080?dummy=2785">> = iolist_to_binary(uri(Req, #{path => ""})),
  288. <<"http://localhost:8080?dummy=2785">> = iolist_to_binary(uri(Req, #{path => [<<>>]})),
  289. <<"http://localhost:8080/path">> = iolist_to_binary(uri(Req, #{qs => <<>>})),
  290. <<"http://localhost:8080/path">> = iolist_to_binary(uri(Req, #{qs => ""})),
  291. <<"http://localhost:8080/path">> = iolist_to_binary(uri(Req, #{qs => [<<>>]})),
  292. <<"http://localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{fragment => <<>>})),
  293. <<"http://localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{fragment => ""})),
  294. <<"http://localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{fragment => [<<>>]})),
  295. %% Port is integer() | undefined.
  296. {'EXIT', _} = (catch iolist_to_binary(uri(Req, #{port => <<>>}))),
  297. {'EXIT', _} = (catch iolist_to_binary(uri(Req, #{port => ""}))),
  298. {'EXIT', _} = (catch iolist_to_binary(uri(Req, #{port => [<<>>]}))),
  299. %% Update components.
  300. <<"https://localhost:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => "https"})),
  301. <<"http://example.org:8080/path?dummy=2785">> = iolist_to_binary(uri(Req, #{host => "example.org"})),
  302. <<"http://localhost:123/path?dummy=2785">> = iolist_to_binary(uri(Req, #{port => 123})),
  303. <<"http://localhost:8080/custom?dummy=2785">> = iolist_to_binary(uri(Req, #{path => "/custom"})),
  304. <<"http://localhost:8080/path?smart=42">> = iolist_to_binary(uri(Req, #{qs => "smart=42"})),
  305. <<"http://localhost:8080/path?dummy=2785#intro">> = iolist_to_binary(uri(Req, #{fragment => "intro"})),
  306. %% Interesting combinations.
  307. <<"http://localhost/path?dummy=2785">> = iolist_to_binary(uri(Req, #{port => 80})),
  308. <<"https://localhost/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => "https", port => 443})),
  309. ok.
  310. -endif.
  311. -spec binding(atom(), req()) -> any() | undefined.
  312. binding(Name, Req) ->
  313. binding(Name, Req, undefined).
  314. -spec binding(atom(), req(), Default) -> any() | Default when Default::any().
  315. binding(Name, #{bindings := Bindings}, Default) when is_atom(Name) ->
  316. case Bindings of
  317. #{Name := Value} -> Value;
  318. _ -> Default
  319. end;
  320. binding(Name, _, Default) when is_atom(Name) ->
  321. Default.
  322. -spec bindings(req()) -> cowboy_router:bindings().
  323. bindings(#{bindings := Bindings}) ->
  324. Bindings;
  325. bindings(_) ->
  326. #{}.
  327. -spec header(binary(), req()) -> binary() | undefined.
  328. header(Name, Req) ->
  329. header(Name, Req, undefined).
  330. -spec header(binary(), req(), Default) -> binary() | Default when Default::any().
  331. header(Name, #{headers := Headers}, Default) ->
  332. maps:get(Name, Headers, Default).
  333. -spec headers(req()) -> cowboy:http_headers().
  334. headers(#{headers := Headers}) ->
  335. Headers.
  336. -spec parse_header(binary(), Req) -> any() when Req::req().
  337. parse_header(Name = <<"content-length">>, Req) ->
  338. parse_header(Name, Req, 0);
  339. parse_header(Name = <<"cookie">>, Req) ->
  340. parse_header(Name, Req, []);
  341. parse_header(Name, Req) ->
  342. parse_header(Name, Req, undefined).
  343. -spec parse_header(binary(), Req, any()) -> any() when Req::req().
  344. parse_header(Name, Req, Default) ->
  345. try
  346. parse_header(Name, Req, Default, parse_header_fun(Name))
  347. catch _:_ ->
  348. erlang:raise(exit, {request_error, {header, Name},
  349. 'Malformed header. Please consult the relevant specification.'
  350. }, erlang:get_stacktrace())
  351. end.
  352. parse_header_fun(<<"accept">>) -> fun cow_http_hd:parse_accept/1;
  353. parse_header_fun(<<"accept-charset">>) -> fun cow_http_hd:parse_accept_charset/1;
  354. parse_header_fun(<<"accept-encoding">>) -> fun cow_http_hd:parse_accept_encoding/1;
  355. parse_header_fun(<<"accept-language">>) -> fun cow_http_hd:parse_accept_language/1;
  356. parse_header_fun(<<"authorization">>) -> fun cow_http_hd:parse_authorization/1;
  357. parse_header_fun(<<"connection">>) -> fun cow_http_hd:parse_connection/1;
  358. parse_header_fun(<<"content-length">>) -> fun cow_http_hd:parse_content_length/1;
  359. parse_header_fun(<<"content-type">>) -> fun cow_http_hd:parse_content_type/1;
  360. parse_header_fun(<<"cookie">>) -> fun cow_cookie:parse_cookie/1;
  361. parse_header_fun(<<"expect">>) -> fun cow_http_hd:parse_expect/1;
  362. parse_header_fun(<<"if-match">>) -> fun cow_http_hd:parse_if_match/1;
  363. parse_header_fun(<<"if-modified-since">>) -> fun cow_http_hd:parse_if_modified_since/1;
  364. parse_header_fun(<<"if-none-match">>) -> fun cow_http_hd:parse_if_none_match/1;
  365. parse_header_fun(<<"if-unmodified-since">>) -> fun cow_http_hd:parse_if_unmodified_since/1;
  366. parse_header_fun(<<"range">>) -> fun cow_http_hd:parse_range/1;
  367. parse_header_fun(<<"sec-websocket-extensions">>) -> fun cow_http_hd:parse_sec_websocket_extensions/1;
  368. parse_header_fun(<<"sec-websocket-protocol">>) -> fun cow_http_hd:parse_sec_websocket_protocol_req/1;
  369. parse_header_fun(<<"upgrade">>) -> fun cow_http_hd:parse_upgrade/1;
  370. parse_header_fun(<<"x-forwarded-for">>) -> fun cow_http_hd:parse_x_forwarded_for/1.
  371. parse_header(Name, Req, Default, ParseFun) ->
  372. case header(Name, Req) of
  373. undefined -> Default;
  374. Value -> ParseFun(Value)
  375. end.
  376. -spec parse_cookies(req()) -> [{binary(), binary()}].
  377. parse_cookies(Req) ->
  378. parse_header(<<"cookie">>, Req).
  379. -spec match_cookies(cowboy:fields(), req()) -> map().
  380. match_cookies(Fields, Req) ->
  381. case filter(Fields, kvlist_to_map(Fields, parse_cookies(Req))) of
  382. {ok, Map} ->
  383. Map;
  384. {error, Errors} ->
  385. exit({request_error, {match_cookies, Errors},
  386. 'Cookie validation constraints failed for the reasons provided.'})
  387. end.
  388. %% Request body.
  389. -spec has_body(req()) -> boolean().
  390. has_body(#{has_body := HasBody}) ->
  391. HasBody.
  392. %% The length may not be known if HTTP/1.1 with a transfer-encoding;
  393. %% or HTTP/2 with no content-length header. The length is always
  394. %% known once the body has been completely read.
  395. -spec body_length(req()) -> undefined | non_neg_integer().
  396. body_length(#{body_length := Length}) ->
  397. Length.
  398. -spec read_body(Req) -> {ok, binary(), Req} | {more, binary(), Req} when Req::req().
  399. read_body(Req) ->
  400. read_body(Req, #{}).
  401. -spec read_body(Req, read_body_opts()) -> {ok, binary(), Req} | {more, binary(), Req} when Req::req().
  402. read_body(Req=#{has_body := false}, _) ->
  403. {ok, <<>>, Req};
  404. read_body(Req=#{has_read_body := true}, _) ->
  405. {ok, <<>>, Req};
  406. read_body(Req=#{pid := Pid, streamid := StreamID}, Opts) ->
  407. Length = maps:get(length, Opts, 8000000),
  408. Period = maps:get(period, Opts, 15000),
  409. Timeout = maps:get(timeout, Opts, Period + 1000),
  410. Ref = make_ref(),
  411. Pid ! {{Pid, StreamID}, {read_body, Ref, Length, Period}},
  412. receive
  413. {request_body, Ref, nofin, Body} ->
  414. {more, Body, Req};
  415. {request_body, Ref, fin, BodyLength, Body} ->
  416. {ok, Body, set_body_length(Req, BodyLength)}
  417. after Timeout ->
  418. exit(timeout)
  419. end.
  420. set_body_length(Req=#{headers := Headers}, BodyLength) ->
  421. Req#{
  422. headers => Headers#{<<"content-length">> => integer_to_binary(BodyLength)},
  423. body_length => BodyLength,
  424. has_read_body => true
  425. }.
  426. -spec read_urlencoded_body(Req) -> {ok, [{binary(), binary() | true}], Req} when Req::req().
  427. read_urlencoded_body(Req) ->
  428. read_urlencoded_body(Req, #{length => 64000, period => 5000}).
  429. -spec read_urlencoded_body(Req, read_body_opts()) -> {ok, [{binary(), binary() | true}], Req} when Req::req().
  430. read_urlencoded_body(Req0, Opts) ->
  431. case read_body(Req0, Opts) of
  432. {ok, Body, Req} ->
  433. try
  434. {ok, cow_qs:parse_qs(Body), Req}
  435. catch _:_ ->
  436. erlang:raise(exit, {request_error, urlencoded_body,
  437. 'Malformed body; application/x-www-form-urlencoded expected.'
  438. }, erlang:get_stacktrace())
  439. end;
  440. {more, Body, _} ->
  441. Length = maps:get(length, Opts, 64000),
  442. if
  443. byte_size(Body) < Length ->
  444. exit({request_error, timeout,
  445. 'The request body was not received within the configured time.'});
  446. true ->
  447. exit({request_error, payload_too_large,
  448. 'The request body is larger than allowed by configuration.'})
  449. end
  450. end.
  451. %% Multipart.
  452. -spec read_part(Req)
  453. -> {ok, cow_multipart:headers(), Req} | {done, Req}
  454. when Req::req().
  455. read_part(Req) ->
  456. read_part(Req, #{length => 64000, period => 5000}).
  457. -spec read_part(Req, read_body_opts())
  458. -> {ok, #{binary() => binary()}, Req} | {done, Req}
  459. when Req::req().
  460. read_part(Req, Opts) ->
  461. case maps:is_key(multipart, Req) of
  462. true ->
  463. {Data, Req2} = stream_multipart(Req, Opts, headers),
  464. read_part(Data, Opts, Req2);
  465. false ->
  466. read_part(init_multipart(Req), Opts)
  467. end.
  468. read_part(Buffer, Opts, Req=#{multipart := {Boundary, _}}) ->
  469. try cow_multipart:parse_headers(Buffer, Boundary) of
  470. more ->
  471. {Data, Req2} = stream_multipart(Req, Opts, headers),
  472. read_part(<< Buffer/binary, Data/binary >>, Opts, Req2);
  473. {more, Buffer2} ->
  474. {Data, Req2} = stream_multipart(Req, Opts, headers),
  475. read_part(<< Buffer2/binary, Data/binary >>, Opts, Req2);
  476. {ok, Headers0, Rest} ->
  477. Headers = maps:from_list(Headers0),
  478. %% Reject multipart content containing duplicate headers.
  479. true = map_size(Headers) =:= length(Headers0),
  480. {ok, Headers, Req#{multipart => {Boundary, Rest}}};
  481. %% Ignore epilogue.
  482. {done, _} ->
  483. {done, Req#{multipart => done}}
  484. catch _:_ ->
  485. erlang:raise(exit, {request_error, {multipart, headers},
  486. 'Malformed body; multipart expected.'
  487. }, erlang:get_stacktrace())
  488. end.
  489. -spec read_part_body(Req)
  490. -> {ok, binary(), Req} | {more, binary(), Req}
  491. when Req::req().
  492. read_part_body(Req) ->
  493. read_part_body(Req, #{}).
  494. -spec read_part_body(Req, read_body_opts())
  495. -> {ok, binary(), Req} | {more, binary(), Req}
  496. when Req::req().
  497. read_part_body(Req, Opts) ->
  498. case maps:is_key(multipart, Req) of
  499. true ->
  500. read_part_body(<<>>, Opts, Req, <<>>);
  501. false ->
  502. read_part_body(init_multipart(Req), Opts)
  503. end.
  504. read_part_body(Buffer, Opts, Req=#{multipart := {Boundary, _}}, Acc) ->
  505. Length = maps:get(length, Opts, 8000000),
  506. case byte_size(Acc) > Length of
  507. true ->
  508. {more, Acc, Req#{multipart => {Boundary, Buffer}}};
  509. false ->
  510. {Data, Req2} = stream_multipart(Req, Opts, body),
  511. case cow_multipart:parse_body(<< Buffer/binary, Data/binary >>, Boundary) of
  512. {ok, Body} ->
  513. read_part_body(<<>>, Opts, Req2, << Acc/binary, Body/binary >>);
  514. {ok, Body, Rest} ->
  515. read_part_body(Rest, Opts, Req2, << Acc/binary, Body/binary >>);
  516. done ->
  517. {ok, Acc, Req2};
  518. {done, Body} ->
  519. {ok, << Acc/binary, Body/binary >>, Req2};
  520. {done, Body, Rest} ->
  521. {ok, << Acc/binary, Body/binary >>,
  522. Req2#{multipart => {Boundary, Rest}}}
  523. end
  524. end.
  525. init_multipart(Req) ->
  526. {<<"multipart">>, _, Params} = parse_header(<<"content-type">>, Req),
  527. case lists:keyfind(<<"boundary">>, 1, Params) of
  528. {_, Boundary} ->
  529. Req#{multipart => {Boundary, <<>>}};
  530. false ->
  531. exit({request_error, {multipart, boundary},
  532. 'Missing boundary parameter for multipart media type.'})
  533. end.
  534. stream_multipart(Req=#{multipart := done}, _, _) ->
  535. {<<>>, Req};
  536. stream_multipart(Req=#{multipart := {_, <<>>}}, Opts, Type) ->
  537. case read_body(Req, Opts) of
  538. {more, Data, Req2} ->
  539. {Data, Req2};
  540. %% We crash when the data ends unexpectedly.
  541. {ok, <<>>, _} ->
  542. exit({request_error, {multipart, Type},
  543. 'Malformed body; multipart expected.'});
  544. {ok, Data, Req2} ->
  545. {Data, Req2}
  546. end;
  547. stream_multipart(Req=#{multipart := {Boundary, Buffer}}, _, _) ->
  548. {Buffer, Req#{multipart => {Boundary, <<>>}}}.
  549. %% Response.
  550. -spec set_resp_cookie(iodata(), iodata(), Req)
  551. -> Req when Req::req().
  552. set_resp_cookie(Name, Value, Req) ->
  553. set_resp_cookie(Name, Value, Req, #{}).
  554. %% The cookie name cannot contain any of the following characters:
  555. %% =,;\s\t\r\n\013\014
  556. %%
  557. %% The cookie value cannot contain any of the following characters:
  558. %% ,; \t\r\n\013\014
  559. %% @todo Fix the cookie_opts() type.
  560. -spec set_resp_cookie(binary(), iodata(), Req, cookie_opts())
  561. -> Req when Req::req().
  562. set_resp_cookie(Name, Value, Req, Opts) ->
  563. Cookie = cow_cookie:setcookie(Name, Value, maps:to_list(Opts)),
  564. RespCookies = maps:get(resp_cookies, Req, #{}),
  565. Req#{resp_cookies => RespCookies#{Name => Cookie}}.
  566. %% @todo We could add has_resp_cookie and delete_resp_cookie now.
  567. -spec set_resp_header(binary(), iodata(), Req)
  568. -> Req when Req::req().
  569. set_resp_header(Name, Value, Req=#{resp_headers := RespHeaders}) ->
  570. Req#{resp_headers => RespHeaders#{Name => Value}};
  571. set_resp_header(Name,Value, Req) ->
  572. Req#{resp_headers => #{Name => Value}}.
  573. -spec set_resp_headers(cowboy:http_headers(), Req)
  574. -> Req when Req::req().
  575. set_resp_headers(Headers, Req=#{resp_headers := RespHeaders}) ->
  576. Req#{resp_headers => maps:merge(RespHeaders, Headers)};
  577. set_resp_headers(Headers, Req) ->
  578. Req#{resp_headers => Headers}.
  579. -spec resp_header(binary(), req()) -> binary() | undefined.
  580. resp_header(Name, Req) ->
  581. resp_header(Name, Req, undefined).
  582. -spec resp_header(binary(), req(), Default)
  583. -> binary() | Default when Default::any().
  584. resp_header(Name, #{resp_headers := Headers}, Default) ->
  585. maps:get(Name, Headers, Default);
  586. resp_header(_, #{}, Default) ->
  587. Default.
  588. -spec resp_headers(req()) -> cowboy:http_headers().
  589. resp_headers(#{resp_headers := RespHeaders}) ->
  590. RespHeaders;
  591. resp_headers(#{}) ->
  592. #{}.
  593. -spec set_resp_body(resp_body(), Req) -> Req when Req::req().
  594. set_resp_body(Body, Req) ->
  595. Req#{resp_body => Body}.
  596. -spec has_resp_header(binary(), req()) -> boolean().
  597. has_resp_header(Name, #{resp_headers := RespHeaders}) ->
  598. maps:is_key(Name, RespHeaders);
  599. has_resp_header(_, _) ->
  600. false.
  601. -spec has_resp_body(req()) -> boolean().
  602. has_resp_body(#{resp_body := {sendfile, _, _, _}}) ->
  603. true;
  604. has_resp_body(#{resp_body := RespBody}) ->
  605. iolist_size(RespBody) > 0;
  606. has_resp_body(_) ->
  607. false.
  608. -spec delete_resp_header(binary(), Req)
  609. -> Req when Req::req().
  610. delete_resp_header(Name, Req=#{resp_headers := RespHeaders}) ->
  611. Req#{resp_headers => maps:remove(Name, RespHeaders)}.
  612. -spec inform(cowboy:http_status(), req()) -> ok.
  613. inform(Status, Req) ->
  614. inform(Status, #{}, Req).
  615. -spec inform(cowboy:http_status(), cowboy:http_headers(), req()) -> ok.
  616. inform(_, _, #{has_sent_resp := _}) ->
  617. error(function_clause); %% @todo Better error message.
  618. inform(Status, Headers, #{pid := Pid, streamid := StreamID})
  619. when is_integer(Status); is_binary(Status) ->
  620. Pid ! {{Pid, StreamID}, {inform, Status, Headers}},
  621. ok.
  622. -spec reply(cowboy:http_status(), Req) -> Req when Req::req().
  623. reply(Status, Req) ->
  624. reply(Status, #{}, Req).
  625. -spec reply(cowboy:http_status(), cowboy:http_headers(), Req)
  626. -> Req when Req::req().
  627. reply(Status, Headers, Req=#{resp_body := Body}) ->
  628. reply(Status, Headers, Body, Req);
  629. reply(Status, Headers, Req) ->
  630. reply(Status, Headers, <<>>, Req).
  631. -spec reply(cowboy:http_status(), cowboy:http_headers(), resp_body(), Req)
  632. -> Req when Req::req().
  633. reply(_, _, _, #{has_sent_resp := _}) ->
  634. error(function_clause); %% @todo Better error message.
  635. reply(Status, Headers, {sendfile, _, 0, _}, Req)
  636. when is_integer(Status); is_binary(Status) ->
  637. do_reply(Status, Headers#{
  638. <<"content-length">> => <<"0">>
  639. }, <<>>, Req);
  640. reply(Status, Headers, SendFile = {sendfile, _, Len, _}, Req)
  641. when is_integer(Status); is_binary(Status) ->
  642. do_reply(Status, Headers#{
  643. <<"content-length">> => integer_to_binary(Len)
  644. }, SendFile, Req);
  645. reply(Status, Headers, Body, Req)
  646. when is_integer(Status); is_binary(Status) ->
  647. do_reply(Status, Headers#{
  648. <<"content-length">> => integer_to_binary(iolist_size(Body))
  649. }, Body, Req).
  650. %% Don't send any body for HEAD responses. While the protocol code is
  651. %% supposed to enforce this rule, we prefer to avoid copying too much
  652. %% data around if we can avoid it.
  653. do_reply(Status, Headers, _, Req=#{pid := Pid, streamid := StreamID, method := <<"HEAD">>}) ->
  654. Pid ! {{Pid, StreamID}, {response, Status, response_headers(Headers, Req), <<>>}},
  655. done_replying(Req, true);
  656. do_reply(Status, Headers, Body, Req=#{pid := Pid, streamid := StreamID}) ->
  657. Pid ! {{Pid, StreamID}, {response, Status, response_headers(Headers, Req), Body}},
  658. done_replying(Req, true).
  659. done_replying(Req, HasSentResp) ->
  660. maps:without([resp_cookies, resp_headers, resp_body], Req#{has_sent_resp => HasSentResp}).
  661. -spec stream_reply(cowboy:http_status(), Req) -> Req when Req::req().
  662. stream_reply(Status, Req) ->
  663. stream_reply(Status, #{}, Req).
  664. -spec stream_reply(cowboy:http_status(), cowboy:http_headers(), Req)
  665. -> Req when Req::req().
  666. stream_reply(_, _, #{has_sent_resp := _}) ->
  667. error(function_clause);
  668. stream_reply(Status, Headers=#{}, Req=#{pid := Pid, streamid := StreamID})
  669. when is_integer(Status); is_binary(Status) ->
  670. Pid ! {{Pid, StreamID}, {headers, Status, response_headers(Headers, Req)}},
  671. done_replying(Req, headers).
  672. -spec stream_body(iodata(), fin | nofin, req()) -> ok.
  673. %% Error out if headers were not sent.
  674. %% Don't send any body for HEAD responses.
  675. stream_body(_, _, #{method := <<"HEAD">>, has_sent_resp := headers}) ->
  676. ok;
  677. %% Don't send a message if the data is empty, except for the
  678. %% very last message with IsFin=fin.
  679. stream_body(Data, IsFin=nofin, #{pid := Pid, streamid := StreamID, has_sent_resp := headers}) ->
  680. case iolist_size(Data) of
  681. 0 -> ok;
  682. _ ->
  683. Pid ! {{Pid, StreamID}, {data, IsFin, Data}},
  684. ok
  685. end;
  686. stream_body(Data, IsFin, #{pid := Pid, streamid := StreamID, has_sent_resp := headers}) ->
  687. Pid ! {{Pid, StreamID}, {data, IsFin, Data}},
  688. ok.
  689. -spec stream_trailers(cowboy:http_headers(), req()) -> ok.
  690. stream_trailers(Trailers, #{pid := Pid, streamid := StreamID, has_sent_resp := headers}) ->
  691. Pid ! {{Pid, StreamID}, {trailers, Trailers}},
  692. ok.
  693. -spec push(binary(), cowboy:http_headers(), req()) -> ok.
  694. push(Path, Headers, Req) ->
  695. push(Path, Headers, Req, #{}).
  696. %% @todo Optimization: don't send anything at all for HTTP/1.0 and HTTP/1.1.
  697. %% @todo Path, Headers, Opts, everything should be in proper binary,
  698. %% or normalized when creating the Req object.
  699. -spec push(iodata(), cowboy:http_headers(), req(), push_opts()) -> ok.
  700. push(Path, Headers, #{pid := Pid, streamid := StreamID,
  701. scheme := Scheme0, host := Host0, port := Port0}, Opts) ->
  702. Method = maps:get(method, Opts, <<"GET">>),
  703. Scheme = maps:get(scheme, Opts, Scheme0),
  704. Host = maps:get(host, Opts, Host0),
  705. Port = maps:get(port, Opts, Port0),
  706. Qs = maps:get(qs, Opts, <<>>),
  707. Pid ! {{Pid, StreamID}, {push, Method, Scheme, Host, Port, Path, Qs, Headers}},
  708. ok.
  709. %% Internal.
  710. %% @todo What about set-cookie headers set through set_resp_header or reply?
  711. -spec response_headers(Headers, req()) -> Headers when Headers::cowboy:http_headers().
  712. response_headers(Headers0, Req) ->
  713. RespHeaders = maps:get(resp_headers, Req, #{}),
  714. Headers = maps:merge(#{
  715. <<"date">> => cowboy_clock:rfc1123(),
  716. <<"server">> => <<"Cowboy">>
  717. }, maps:merge(RespHeaders, Headers0)),
  718. %% The set-cookie header is special; we can only send one cookie per header.
  719. %% We send the list of values for many cookies in one key of the map,
  720. %% and let the protocols deal with it directly.
  721. case maps:get(resp_cookies, Req, undefined) of
  722. undefined -> Headers;
  723. RespCookies -> Headers#{<<"set-cookie">> => maps:values(RespCookies)}
  724. end.
  725. %% Create map, convert keys to atoms and group duplicate keys into lists.
  726. %% Keys that are not found in the user provided list are entirely skipped.
  727. %% @todo Can probably be done directly while parsing.
  728. kvlist_to_map(Fields, KvList) ->
  729. Keys = [case K of
  730. {Key, _} -> Key;
  731. {Key, _, _} -> Key;
  732. Key -> Key
  733. end || K <- Fields],
  734. kvlist_to_map(Keys, KvList, #{}).
  735. kvlist_to_map(_, [], Map) ->
  736. Map;
  737. kvlist_to_map(Keys, [{Key, Value}|Tail], Map) ->
  738. try binary_to_existing_atom(Key, utf8) of
  739. Atom ->
  740. case lists:member(Atom, Keys) of
  741. true ->
  742. case maps:find(Atom, Map) of
  743. {ok, MapValue} when is_list(MapValue) ->
  744. kvlist_to_map(Keys, Tail,
  745. Map#{Atom => [Value|MapValue]});
  746. {ok, MapValue} ->
  747. kvlist_to_map(Keys, Tail,
  748. Map#{Atom => [Value, MapValue]});
  749. error ->
  750. kvlist_to_map(Keys, Tail,
  751. Map#{Atom => Value})
  752. end;
  753. false ->
  754. kvlist_to_map(Keys, Tail, Map)
  755. end
  756. catch error:badarg ->
  757. kvlist_to_map(Keys, Tail, Map)
  758. end.
  759. filter(Fields, Map0) ->
  760. filter(Fields, Map0, #{}).
  761. %% Loop through fields, if value is missing and no default,
  762. %% record the error; else if value is missing and has a
  763. %% default, set default; otherwise apply constraints. If
  764. %% constraint fails, record the error.
  765. %%
  766. %% When there is an error at the end, crash.
  767. filter([], Map, Errors) ->
  768. case maps:size(Errors) of
  769. 0 -> {ok, Map};
  770. _ -> {error, Errors}
  771. end;
  772. filter([{Key, Constraints}|Tail], Map, Errors) ->
  773. filter_constraints(Tail, Map, Errors, Key, maps:get(Key, Map), Constraints);
  774. filter([{Key, Constraints, Default}|Tail], Map, Errors) ->
  775. case maps:find(Key, Map) of
  776. {ok, Value} ->
  777. filter_constraints(Tail, Map, Errors, Key, Value, Constraints);
  778. error ->
  779. filter(Tail, Map#{Key => Default}, Errors)
  780. end;
  781. filter([Key|Tail], Map, Errors) ->
  782. case maps:is_key(Key, Map) of
  783. true ->
  784. filter(Tail, Map, Errors);
  785. false ->
  786. filter(Tail, Map, Errors#{Key => required})
  787. end.
  788. filter_constraints(Tail, Map, Errors, Key, Value0, Constraints) ->
  789. case cowboy_constraints:validate(Value0, Constraints) of
  790. {ok, Value} ->
  791. filter(Tail, Map#{Key => Value}, Errors);
  792. {error, Reason} ->
  793. filter(Tail, Map, Errors#{Key => Reason})
  794. end.