cowboy_req.erl 29 KB

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