cowboy_req.erl 31 KB

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