cowboy_req.erl 30 KB

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