cowboy_req.erl 28 KB

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