cowboy_req.erl 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. %% Copyright (c) 2011-2012, 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. %% @doc HTTP request manipulation API.
  16. %%
  17. %% The functions in this module try to follow this pattern for their
  18. %% return types:
  19. %% <dl>
  20. %% <dt>access:</dt>
  21. %% <dd><em>{Value, Req}</em></dd>
  22. %% <dt>action:</dt>
  23. %% <dd><em>{Result, Req} | {Result, Value, Req} | {error, atom()}</em></dd>
  24. %% <dt>modification:</dt>
  25. %% <dd><em>Req</em></dd>
  26. %% <dt>question (<em>has_*</em> or <em>is_*</em>):</dt>
  27. %% <dd><em>boolean()</em></dd>
  28. %% </dl>
  29. %%
  30. %% Exceptions include <em>chunk/2</em> which always returns <em>'ok'</em>,
  31. %% <em>to_list/1</em> which returns a list of key/values,
  32. %% and <em>transport/1</em> which returns <em>{ok, Transport, Socket}</em>.
  33. %%
  34. %% Also note that all body reading functions perform actions, as Cowboy
  35. %% doesn't read the request body until they are called.
  36. %%
  37. %% Whenever <em>Req</em> is returned, it should always be kept in place of
  38. %% the one given as argument in your function call, because it keeps
  39. %% track of the request and response state. Doing so allows Cowboy to do
  40. %% some lazy evaluation and cache results when possible.
  41. -module(cowboy_req).
  42. %% Request API.
  43. -export([new/13]).
  44. -export([method/1]).
  45. -export([version/1]).
  46. -export([peer/1]).
  47. -export([peer_addr/1]).
  48. -export([host/1]).
  49. -export([host_info/1]).
  50. -export([port/1]).
  51. -export([path/1]).
  52. -export([path_info/1]).
  53. -export([qs/1]).
  54. -export([qs_val/2]).
  55. -export([qs_val/3]).
  56. -export([qs_vals/1]).
  57. -export([fragment/1]).
  58. -export([host_url/1]).
  59. -export([url/1]).
  60. -export([binding/2]).
  61. -export([binding/3]).
  62. -export([bindings/1]).
  63. -export([header/2]).
  64. -export([header/3]).
  65. -export([headers/1]).
  66. -export([parse_header/2]).
  67. -export([parse_header/3]).
  68. -export([cookie/2]).
  69. -export([cookie/3]).
  70. -export([cookies/1]).
  71. -export([meta/2]).
  72. -export([meta/3]).
  73. -export([set_meta/3]).
  74. %% Request body API.
  75. -export([has_body/1]).
  76. -export([body_length/1]).
  77. -export([init_stream/4]).
  78. -export([stream_body/1]).
  79. -export([skip_body/1]).
  80. -export([body/1]).
  81. -export([body/2]).
  82. -export([body_qs/1]).
  83. -export([multipart_data/1]).
  84. -export([multipart_skip/1]).
  85. %% Response API.
  86. -export([set_resp_cookie/4]).
  87. -export([set_resp_header/3]).
  88. -export([set_resp_body/2]).
  89. -export([set_resp_body_fun/3]).
  90. -export([has_resp_header/2]).
  91. -export([has_resp_body/1]).
  92. -export([delete_resp_header/2]).
  93. -export([reply/2]).
  94. -export([reply/3]).
  95. -export([reply/4]).
  96. -export([chunked_reply/2]).
  97. -export([chunked_reply/3]).
  98. -export([chunk/2]).
  99. -export([upgrade_reply/3]).
  100. -export([ensure_response/2]).
  101. %% Private setter/getter API.
  102. -export([get/2]).
  103. -export([set/2]).
  104. -export([set_bindings/4]).
  105. %% Misc API.
  106. -export([compact/1]).
  107. -export([lock/1]).
  108. -export([to_list/1]).
  109. -export([transport/1]).
  110. -ifdef(TEST).
  111. -include_lib("eunit/include/eunit.hrl").
  112. -endif.
  113. -type resp_body_fun() :: fun(() -> {sent, non_neg_integer()}).
  114. -record(http_req, {
  115. %% Transport.
  116. socket = undefined :: undefined | inet:socket(),
  117. transport = undefined :: undefined | module(),
  118. connection = keepalive :: keepalive | close,
  119. %% Request.
  120. pid = undefined :: pid(),
  121. method = <<"GET">> :: binary(),
  122. version = {1, 1} :: cowboy_http:version(),
  123. peer = undefined :: undefined | {inet:ip_address(), inet:port_number()},
  124. host = undefined :: undefined | binary(),
  125. host_info = undefined :: undefined | cowboy_dispatcher:tokens(),
  126. port = undefined :: undefined | inet:port_number(),
  127. path = undefined :: binary(),
  128. path_info = undefined :: undefined | cowboy_dispatcher:tokens(),
  129. qs = undefined :: binary(),
  130. qs_vals = undefined :: undefined | list({binary(), binary() | true}),
  131. fragment = undefined :: binary(),
  132. bindings = undefined :: undefined | cowboy_dispatcher:bindings(),
  133. headers = [] :: cowboy_http:headers(),
  134. p_headers = [] :: [any()], %% @todo Improve those specs.
  135. cookies = undefined :: undefined | [{binary(), binary()}],
  136. meta = [] :: [{atom(), any()}],
  137. %% Request body.
  138. body_state = waiting :: waiting | done | {stream, fun(), any(), fun()},
  139. multipart = undefined :: undefined | {non_neg_integer(), fun()},
  140. buffer = <<>> :: binary(),
  141. %% Response.
  142. resp_state = waiting :: locked | waiting | chunks | done,
  143. resp_headers = [] :: cowboy_http:headers(),
  144. resp_body = <<>> :: iodata() | {non_neg_integer(), resp_body_fun()},
  145. %% Functions.
  146. onresponse = undefined :: undefined | cowboy_protocol:onresponse_fun()
  147. }).
  148. -opaque req() :: #http_req{}.
  149. -export_type([req/0]).
  150. %% Request API.
  151. %% @doc Create a new HTTP Req object.
  152. %%
  153. %% This function takes care of setting the owner's pid to self().
  154. %% @private
  155. %%
  156. %% Since we always need to parse the Connection header, we do it
  157. %% in an optimized way and add the parsed value to p_headers' cache.
  158. -spec new(inet:socket(), module(), binary(), binary(), binary(), binary(),
  159. cowboy_http:version(), cowboy_http:headers(), binary(),
  160. inet:port_number() | undefined, binary(), boolean(),
  161. undefined | cowboy_protocol:onresponse_fun())
  162. -> req().
  163. new(Socket, Transport, Method, Path, Query, Fragment,
  164. Version, Headers, Host, Port, Buffer, CanKeepalive,
  165. OnResponse) ->
  166. Req = #http_req{socket=Socket, transport=Transport, pid=self(),
  167. method=Method, path=Path, qs=Query, fragment=Fragment, version=Version,
  168. headers=Headers, host=Host, port=Port, buffer=Buffer,
  169. onresponse=OnResponse},
  170. case CanKeepalive of
  171. false ->
  172. Req#http_req{connection=close};
  173. true ->
  174. case lists:keyfind(<<"connection">>, 1, Headers) of
  175. false when Version =:= {1, 1} ->
  176. Req; %% keepalive
  177. false ->
  178. Req#http_req{connection=close};
  179. {_, ConnectionHeader} ->
  180. Tokens = parse_connection_before(ConnectionHeader, []),
  181. Connection = connection_to_atom(Tokens),
  182. Req#http_req{connection=Connection,
  183. p_headers=[{<<"connection">>, Tokens}]}
  184. end
  185. end.
  186. %% @doc Return the HTTP method of the request.
  187. -spec method(Req) -> {binary(), Req} when Req::req().
  188. method(Req) ->
  189. {Req#http_req.method, Req}.
  190. %% @doc Return the HTTP version used for the request.
  191. -spec version(Req) -> {cowboy_http:version(), Req} when Req::req().
  192. version(Req) ->
  193. {Req#http_req.version, Req}.
  194. %% @doc Return the peer address and port number of the remote host.
  195. -spec peer(Req)
  196. -> {{inet:ip_address(), inet:port_number()}, Req} when Req::req().
  197. peer(Req=#http_req{socket=Socket, transport=Transport, peer=undefined}) ->
  198. {ok, Peer} = Transport:peername(Socket),
  199. {Peer, Req#http_req{peer=Peer}};
  200. peer(Req) ->
  201. {Req#http_req.peer, Req}.
  202. %% @doc Returns the peer address calculated from headers.
  203. -spec peer_addr(Req) -> {inet:ip_address(), Req} when Req::req().
  204. peer_addr(Req = #http_req{}) ->
  205. {RealIp, Req1} = header(<<"x-real-ip">>, Req),
  206. {ForwardedForRaw, Req2} = header(<<"x-forwarded-for">>, Req1),
  207. {{PeerIp, _PeerPort}, Req3} = peer(Req2),
  208. ForwardedFor = case ForwardedForRaw of
  209. undefined ->
  210. undefined;
  211. ForwardedForRaw ->
  212. case re:run(ForwardedForRaw, "^(?<first_ip>[^\\,]+)",
  213. [{capture, [first_ip], binary}]) of
  214. {match, [FirstIp]} -> FirstIp;
  215. _Any -> undefined
  216. end
  217. end,
  218. {ok, PeerAddr} = if
  219. is_binary(RealIp) -> inet_parse:address(binary_to_list(RealIp));
  220. is_binary(ForwardedFor) -> inet_parse:address(binary_to_list(ForwardedFor));
  221. true -> {ok, PeerIp}
  222. end,
  223. {PeerAddr, Req3}.
  224. %% @doc Return the host binary string.
  225. -spec host(Req) -> {binary(), Req} when Req::req().
  226. host(Req) ->
  227. {Req#http_req.host, Req}.
  228. %% @doc Return the extra host information obtained from partially matching
  229. %% the hostname using <em>'...'</em>.
  230. -spec host_info(Req)
  231. -> {cowboy_dispatcher:tokens() | undefined, Req} when Req::req().
  232. host_info(Req) ->
  233. {Req#http_req.host_info, Req}.
  234. %% @doc Return the port used for this request.
  235. -spec port(Req) -> {inet:port_number(), Req} when Req::req().
  236. port(Req) ->
  237. {Req#http_req.port, Req}.
  238. %% @doc Return the path binary string.
  239. -spec path(Req) -> {binary(), Req} when Req::req().
  240. path(Req) ->
  241. {Req#http_req.path, Req}.
  242. %% @doc Return the extra path information obtained from partially matching
  243. %% the patch using <em>'...'</em>.
  244. -spec path_info(Req)
  245. -> {cowboy_dispatcher:tokens() | undefined, Req} when Req::req().
  246. path_info(Req) ->
  247. {Req#http_req.path_info, Req}.
  248. %% @doc Return the raw query string directly taken from the request.
  249. -spec qs(Req) -> {binary(), Req} when Req::req().
  250. qs(Req) ->
  251. {Req#http_req.qs, Req}.
  252. %% @equiv qs_val(Name, Req, undefined)
  253. -spec qs_val(binary(), Req)
  254. -> {binary() | true | undefined, Req} when Req::req().
  255. qs_val(Name, Req) when is_binary(Name) ->
  256. qs_val(Name, Req, undefined).
  257. %% @doc Return the query string value for the given key, or a default if
  258. %% missing.
  259. -spec qs_val(binary(), Req, Default)
  260. -> {binary() | true | Default, Req} when Req::req(), Default::any().
  261. qs_val(Name, Req=#http_req{qs=RawQs, qs_vals=undefined}, Default)
  262. when is_binary(Name) ->
  263. QsVals = cowboy_http:x_www_form_urlencoded(RawQs),
  264. qs_val(Name, Req#http_req{qs_vals=QsVals}, Default);
  265. qs_val(Name, Req, Default) ->
  266. case lists:keyfind(Name, 1, Req#http_req.qs_vals) of
  267. {Name, Value} -> {Value, Req};
  268. false -> {Default, Req}
  269. end.
  270. %% @doc Return the full list of query string values.
  271. -spec qs_vals(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
  272. qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) ->
  273. QsVals = cowboy_http:x_www_form_urlencoded(RawQs),
  274. qs_vals(Req#http_req{qs_vals=QsVals});
  275. qs_vals(Req=#http_req{qs_vals=QsVals}) ->
  276. {QsVals, Req}.
  277. %% @doc Return the raw fragment directly taken from the request.
  278. -spec fragment(Req) -> {binary(), Req} when Req::req().
  279. fragment(Req) ->
  280. {Req#http_req.fragment, Req}.
  281. %% @doc Return the request URL as a binary without the path and query string.
  282. %%
  283. %% The URL includes the scheme, host and port only.
  284. %% @see cowboy_req:url/1
  285. -spec host_url(Req) -> {binary(), Req} when Req::req().
  286. host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
  287. TransportName = Transport:name(),
  288. Secure = case TransportName of
  289. ssl -> <<"s">>;
  290. _ -> <<>>
  291. end,
  292. PortBin = case {TransportName, Port} of
  293. {ssl, 443} -> <<>>;
  294. {tcp, 80} -> <<>>;
  295. _ -> << ":", (list_to_binary(integer_to_list(Port)))/binary >>
  296. end,
  297. {<< "http", Secure/binary, "://", Host/binary, PortBin/binary >>, Req}.
  298. %% @doc Return the full request URL as a binary.
  299. %%
  300. %% The URL includes the scheme, host, port, path, query string and fragment.
  301. -spec url(Req) -> {binary(), Req} when Req::req().
  302. url(Req=#http_req{path=Path, qs=QS, fragment=Fragment}) ->
  303. {HostURL, Req2} = host_url(Req),
  304. QS2 = case QS of
  305. <<>> -> <<>>;
  306. _ -> << "?", QS/binary >>
  307. end,
  308. Fragment2 = case Fragment of
  309. <<>> -> <<>>;
  310. _ -> << "#", Fragment/binary >>
  311. end,
  312. {<< HostURL/binary, Path/binary, QS2/binary, Fragment2/binary >>, Req2}.
  313. %% @equiv binding(Name, Req, undefined)
  314. -spec binding(atom(), Req) -> {binary() | undefined, Req} when Req::req().
  315. binding(Name, Req) when is_atom(Name) ->
  316. binding(Name, Req, undefined).
  317. %% @doc Return the binding value for the given key obtained when matching
  318. %% the host and path against the dispatch list, or a default if missing.
  319. -spec binding(atom(), Req, Default)
  320. -> {binary() | Default, Req} when Req::req(), Default::any().
  321. binding(Name, Req, Default) when is_atom(Name) ->
  322. case lists:keyfind(Name, 1, Req#http_req.bindings) of
  323. {Name, Value} -> {Value, Req};
  324. false -> {Default, Req}
  325. end.
  326. %% @doc Return the full list of binding values.
  327. -spec bindings(Req) -> {list({atom(), binary()}), Req} when Req::req().
  328. bindings(Req) ->
  329. {Req#http_req.bindings, Req}.
  330. %% @equiv header(Name, Req, undefined)
  331. -spec header(binary(), Req)
  332. -> {binary() | undefined, Req} when Req::req().
  333. header(Name, Req) ->
  334. header(Name, Req, undefined).
  335. %% @doc Return the header value for the given key, or a default if missing.
  336. -spec header(binary(), Req, Default)
  337. -> {binary() | Default, Req} when Req::req(), Default::any().
  338. header(Name, Req, Default) ->
  339. case lists:keyfind(Name, 1, Req#http_req.headers) of
  340. {Name, Value} -> {Value, Req};
  341. false -> {Default, Req}
  342. end.
  343. %% @doc Return the full list of headers.
  344. -spec headers(Req) -> {cowboy_http:headers(), Req} when Req::req().
  345. headers(Req) ->
  346. {Req#http_req.headers, Req}.
  347. %% @doc Semantically parse headers.
  348. %%
  349. %% When the value isn't found, a proper default value for the type
  350. %% returned is used as a return value.
  351. %% @see parse_header/3
  352. -spec parse_header(binary(), Req)
  353. -> {ok, any(), Req} | {undefined, binary(), Req}
  354. | {error, badarg} when Req::req().
  355. parse_header(Name, Req=#http_req{p_headers=PHeaders}) ->
  356. case lists:keyfind(Name, 1, PHeaders) of
  357. false -> parse_header(Name, Req, parse_header_default(Name));
  358. {Name, Value} -> {ok, Value, Req}
  359. end.
  360. %% @doc Default values for semantic header parsing.
  361. -spec parse_header_default(binary()) -> any().
  362. parse_header_default(<<"transfer-encoding">>) -> [<<"identity">>];
  363. parse_header_default(_Name) -> undefined.
  364. %% @doc Semantically parse headers.
  365. %%
  366. %% When the header is unknown, the value is returned directly without parsing.
  367. -spec parse_header(binary(), Req, any())
  368. -> {ok, any(), Req} | {undefined, binary(), Req}
  369. | {error, badarg} when Req::req().
  370. parse_header(Name, Req, Default) when Name =:= <<"accept">> ->
  371. parse_header(Name, Req, Default,
  372. fun (Value) ->
  373. cowboy_http:list(Value, fun cowboy_http:media_range/2)
  374. end);
  375. parse_header(Name, Req, Default) when Name =:= <<"accept-charset">> ->
  376. parse_header(Name, Req, Default,
  377. fun (Value) ->
  378. cowboy_http:nonempty_list(Value, fun cowboy_http:conneg/2)
  379. end);
  380. parse_header(Name, Req, Default) when Name =:= <<"accept-encoding">> ->
  381. parse_header(Name, Req, Default,
  382. fun (Value) ->
  383. cowboy_http:list(Value, fun cowboy_http:conneg/2)
  384. end);
  385. parse_header(Name, Req, Default) when Name =:= <<"accept-language">> ->
  386. parse_header(Name, Req, Default,
  387. fun (Value) ->
  388. cowboy_http:nonempty_list(Value, fun cowboy_http:language_range/2)
  389. end);
  390. parse_header(Name, Req, Default) when Name =:= <<"content-length">> ->
  391. parse_header(Name, Req, Default, fun cowboy_http:digits/1);
  392. parse_header(Name, Req, Default) when Name =:= <<"content-type">> ->
  393. parse_header(Name, Req, Default, fun cowboy_http:content_type/1);
  394. parse_header(Name, Req, Default) when Name =:= <<"expect">> ->
  395. parse_header(Name, Req, Default,
  396. fun (Value) ->
  397. cowboy_http:nonempty_list(Value, fun cowboy_http:expectation/2)
  398. end);
  399. parse_header(Name, Req, Default)
  400. when Name =:= <<"if-match">>; Name =:= <<"if-none-match">> ->
  401. parse_header(Name, Req, Default, fun cowboy_http:entity_tag_match/1);
  402. parse_header(Name, Req, Default)
  403. when Name =:= <<"if-modified-since">>;
  404. Name =:= <<"if-unmodified-since">> ->
  405. parse_header(Name, Req, Default, fun cowboy_http:http_date/1);
  406. %% @todo Extension parameters.
  407. parse_header(Name, Req, Default) when Name =:= <<"transfer-encoding">> ->
  408. parse_header(Name, Req, Default,
  409. fun (Value) ->
  410. cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
  411. end);
  412. parse_header(Name, Req, Default) when Name =:= <<"upgrade">> ->
  413. parse_header(Name, Req, Default,
  414. fun (Value) ->
  415. cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
  416. end);
  417. parse_header(Name, Req, Default) ->
  418. {Value, Req2} = header(Name, Req, Default),
  419. {undefined, Value, Req2}.
  420. parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) ->
  421. case header(Name, Req) of
  422. {undefined, Req2} ->
  423. {ok, Default, Req2#http_req{p_headers=[{Name, Default}|PHeaders]}};
  424. {Value, Req2} ->
  425. case Fun(Value) of
  426. {error, badarg} ->
  427. {error, badarg};
  428. P ->
  429. {ok, P, Req2#http_req{p_headers=[{Name, P}|PHeaders]}}
  430. end
  431. end.
  432. %% @equiv cookie(Name, Req, undefined)
  433. -spec cookie(binary(), Req)
  434. -> {binary() | true | undefined, Req} when Req::req().
  435. cookie(Name, Req) when is_binary(Name) ->
  436. cookie(Name, Req, undefined).
  437. %% @doc Return the cookie value for the given key, or a default if
  438. %% missing.
  439. -spec cookie(binary(), Req, Default)
  440. -> {binary() | true | Default, Req} when Req::req(), Default::any().
  441. cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
  442. case header(<<"cookie">>, Req) of
  443. {undefined, Req2} ->
  444. {Default, Req2#http_req{cookies=[]}};
  445. {RawCookie, Req2} ->
  446. Cookies = cowboy_cookies:parse_cookie(RawCookie),
  447. cookie(Name, Req2#http_req{cookies=Cookies}, Default)
  448. end;
  449. cookie(Name, Req, Default) ->
  450. case lists:keyfind(Name, 1, Req#http_req.cookies) of
  451. {Name, Value} -> {Value, Req};
  452. false -> {Default, Req}
  453. end.
  454. %% @doc Return the full list of cookie values.
  455. -spec cookies(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
  456. cookies(Req=#http_req{cookies=undefined}) ->
  457. case header(<<"cookie">>, Req) of
  458. {undefined, Req2} ->
  459. {[], Req2#http_req{cookies=[]}};
  460. {RawCookie, Req2} ->
  461. Cookies = cowboy_cookies:parse_cookie(RawCookie),
  462. cookies(Req2#http_req{cookies=Cookies})
  463. end;
  464. cookies(Req=#http_req{cookies=Cookies}) ->
  465. {Cookies, Req}.
  466. %% @equiv meta(Name, Req, undefined)
  467. -spec meta(atom(), Req) -> {any() | undefined, Req} when Req::req().
  468. meta(Name, Req) ->
  469. meta(Name, Req, undefined).
  470. %% @doc Return metadata information about the request.
  471. %%
  472. %% Metadata information varies from one protocol to another. Websockets
  473. %% would define the protocol version here, while REST would use it to
  474. %% indicate which media type, language and charset were retained.
  475. -spec meta(atom(), Req, any()) -> {any(), Req} when Req::req().
  476. meta(Name, Req, Default) ->
  477. case lists:keyfind(Name, 1, Req#http_req.meta) of
  478. {Name, Value} -> {Value, Req};
  479. false -> {Default, Req}
  480. end.
  481. %% @doc Set metadata information.
  482. %%
  483. %% You can use this function to attach information about the request.
  484. %%
  485. %% If the value already exists it will be overwritten.
  486. -spec set_meta(atom(), any(), Req) -> Req when Req::req().
  487. set_meta(Name, Value, Req=#http_req{meta=Meta}) ->
  488. Req#http_req{meta=[{Name, Value}|lists:keydelete(Name, 1, Meta)]}.
  489. %% Request Body API.
  490. %% @doc Return whether the request message has a body.
  491. -spec has_body(Req) -> {boolean(), Req} when Req::req().
  492. has_body(Req) ->
  493. Has = lists:keymember(<<"content-length">>, 1, Req#http_req.headers) orelse
  494. lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers),
  495. {Has, Req}.
  496. %% @doc Return the request message body length, if known.
  497. %%
  498. %% The length may not be known if Transfer-Encoding is not identity,
  499. %% and the body hasn't been read at the time of the call.
  500. -spec body_length(Req) -> {undefined | non_neg_integer(), Req} when Req::req().
  501. body_length(Req) ->
  502. case lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers) of
  503. true ->
  504. {undefined, Req};
  505. false ->
  506. {ok, Length, Req2} = parse_header(<<"content-length">>, Req, 0),
  507. {Length, Req2}
  508. end.
  509. %% @doc Initialize body streaming and set custom decoding functions.
  510. %%
  511. %% Calling this function is optional. It should only be used if you
  512. %% need to override the default behavior of Cowboy. Otherwise you
  513. %% should call stream_body/1 directly.
  514. %%
  515. %% Two decodings happen. First a decoding function is applied to the
  516. %% transferred data, and then another is applied to the actual content.
  517. %%
  518. %% Transfer encoding is generally used for chunked bodies. The decoding
  519. %% function uses a state to keep track of how much it has read, which is
  520. %% also initialized through this function.
  521. %%
  522. %% Content encoding is generally used for compression.
  523. %%
  524. %% Standard encodings can be found in cowboy_http.
  525. -spec init_stream(fun(), any(), fun(), Req) -> {ok, Req} when Req::req().
  526. init_stream(TransferDecode, TransferState, ContentDecode, Req) ->
  527. {ok, Req#http_req{body_state=
  528. {stream, TransferDecode, TransferState, ContentDecode}}}.
  529. %% @doc Stream the request's body.
  530. %%
  531. %% This is the most low level function to read the request body.
  532. %%
  533. %% In most cases, if they weren't defined before using stream_body/4,
  534. %% this function will guess which transfer and content encodings were
  535. %% used for building the request body, and configure the decoding
  536. %% functions that will be used when streaming.
  537. %%
  538. %% It then starts streaming the body, returning {ok, Data, Req}
  539. %% for each streamed part, and {done, Req} when it's finished streaming.
  540. -spec stream_body(Req) -> {ok, binary(), Req}
  541. | {done, Req} | {error, atom()} when Req::req().
  542. stream_body(Req=#http_req{body_state=waiting,
  543. version=Version, transport=Transport, socket=Socket}) ->
  544. case parse_header(<<"expect">>, Req) of
  545. {ok, [<<"100-continue">>], Req1} ->
  546. HTTPVer = cowboy_http:version_to_binary(Version),
  547. Transport:send(Socket,
  548. << HTTPVer/binary, " ", (status(100))/binary, "\r\n\r\n" >>);
  549. {ok, undefined, Req1} ->
  550. ok
  551. end,
  552. case parse_header(<<"transfer-encoding">>, Req1) of
  553. {ok, [<<"chunked">>], Req2} ->
  554. stream_body(Req2#http_req{body_state=
  555. {stream, fun cowboy_http:te_chunked/2, {0, 0},
  556. fun cowboy_http:ce_identity/1}});
  557. {ok, [<<"identity">>], Req2} ->
  558. {Length, Req3} = body_length(Req2),
  559. case Length of
  560. 0 ->
  561. {done, Req3#http_req{body_state=done}};
  562. Length ->
  563. stream_body(Req3#http_req{body_state=
  564. {stream, fun cowboy_http:te_identity/2, {0, Length},
  565. fun cowboy_http:ce_identity/1}})
  566. end
  567. end;
  568. stream_body(Req=#http_req{buffer=Buffer, body_state={stream, _, _, _}})
  569. when Buffer =/= <<>> ->
  570. transfer_decode(Buffer, Req#http_req{buffer= <<>>});
  571. stream_body(Req=#http_req{body_state={stream, _, _, _}}) ->
  572. stream_body_recv(Req);
  573. stream_body(Req=#http_req{body_state=done}) ->
  574. {done, Req}.
  575. -spec stream_body_recv(Req)
  576. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  577. stream_body_recv(Req=#http_req{
  578. transport=Transport, socket=Socket, buffer=Buffer}) ->
  579. %% @todo Allow configuring the timeout.
  580. case Transport:recv(Socket, 0, 5000) of
  581. {ok, Data} -> transfer_decode(<< Buffer/binary, Data/binary >>, Req);
  582. {error, Reason} -> {error, Reason}
  583. end.
  584. -spec transfer_decode(binary(), Req)
  585. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  586. transfer_decode(Data, Req=#http_req{
  587. body_state={stream, TransferDecode, TransferState, ContentDecode}}) ->
  588. case TransferDecode(Data, TransferState) of
  589. {ok, Data2, TransferState2} ->
  590. content_decode(ContentDecode, Data2, Req#http_req{body_state=
  591. {stream, TransferDecode, TransferState2, ContentDecode}});
  592. {ok, Data2, Rest, TransferState2} ->
  593. content_decode(ContentDecode, Data2, Req#http_req{
  594. buffer=Rest, body_state=
  595. {stream, TransferDecode, TransferState2, ContentDecode}});
  596. %% @todo {header(s) for chunked
  597. more ->
  598. stream_body_recv(Req#http_req{buffer=Data});
  599. {done, Length, Rest} ->
  600. Req2 = transfer_decode_done(Length, Rest, Req),
  601. {done, Req2};
  602. {done, Data2, Length, Rest} ->
  603. Req2 = transfer_decode_done(Length, Rest, Req),
  604. content_decode(ContentDecode, Data2, Req2);
  605. {error, Reason} ->
  606. {error, Reason}
  607. end.
  608. -spec transfer_decode_done(non_neg_integer(), binary(), Req)
  609. -> Req when Req::req().
  610. transfer_decode_done(Length, Rest, Req=#http_req{
  611. headers=Headers, p_headers=PHeaders}) ->
  612. Headers2 = lists:keystore(<<"content-length">>, 1, Headers,
  613. {<<"content-length">>, list_to_binary(integer_to_list(Length))}),
  614. %% At this point we just assume TEs were all decoded.
  615. Headers3 = lists:keydelete(<<"transfer-encoding">>, 1, Headers2),
  616. PHeaders2 = lists:keystore(<<"content-length">>, 1, PHeaders,
  617. {<<"content-length">>, Length}),
  618. PHeaders3 = lists:keydelete(<<"transfer-encoding">>, 1, PHeaders2),
  619. Req#http_req{buffer=Rest, body_state=done,
  620. headers=Headers3, p_headers=PHeaders3}.
  621. %% @todo Probably needs a Rest.
  622. -spec content_decode(fun(), binary(), Req)
  623. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  624. content_decode(ContentDecode, Data, Req) ->
  625. case ContentDecode(Data) of
  626. {ok, Data2} -> {ok, Data2, Req};
  627. {error, Reason} -> {error, Reason}
  628. end.
  629. %% @doc Return the full body sent with the request.
  630. -spec body(Req) -> {ok, binary(), Req} | {error, atom()} when Req::req().
  631. body(Req) ->
  632. read_body(infinity, Req, <<>>).
  633. %% @doc Return the full body sent with the request as long as the body
  634. %% length doesn't go over MaxLength.
  635. %%
  636. %% This is most useful to quickly be able to get the full body while
  637. %% avoiding filling your memory with huge request bodies when you're
  638. %% not expecting it.
  639. -spec body(non_neg_integer() | infinity, Req)
  640. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  641. body(MaxLength, Req) ->
  642. read_body(MaxLength, Req, <<>>).
  643. -spec read_body(non_neg_integer() | infinity, Req, binary())
  644. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  645. read_body(MaxLength, Req, Acc) when MaxLength > byte_size(Acc) ->
  646. case stream_body(Req) of
  647. {ok, Data, Req2} ->
  648. read_body(MaxLength, Req2, << Acc/binary, Data/binary >>);
  649. {done, Req2} ->
  650. {ok, Acc, Req2};
  651. {error, Reason} ->
  652. {error, Reason}
  653. end.
  654. -spec skip_body(Req) -> {ok, Req} | {error, atom()} when Req::req().
  655. skip_body(Req) ->
  656. case stream_body(Req) of
  657. {ok, _, Req2} -> skip_body(Req2);
  658. {done, Req2} -> {ok, Req2};
  659. {error, Reason} -> {error, Reason}
  660. end.
  661. %% @doc Return the full body sent with the request, parsed as an
  662. %% application/x-www-form-urlencoded string. Essentially a POST query string.
  663. %% @todo We need an option to limit the size of the body for QS too.
  664. -spec body_qs(Req)
  665. -> {ok, [{binary(), binary() | true}], Req} | {error, atom()}
  666. when Req::req().
  667. body_qs(Req) ->
  668. case body(Req) of
  669. {ok, Body, Req2} ->
  670. {ok, cowboy_http:x_www_form_urlencoded(Body), Req2};
  671. {error, Reason} ->
  672. {error, Reason}
  673. end.
  674. %% Multipart Request API.
  675. %% @doc Return data from the multipart parser.
  676. %%
  677. %% Use this function for multipart streaming. For each part in the request,
  678. %% this function returns <em>{headers, Headers}</em> followed by a sequence of
  679. %% <em>{body, Data}</em> tuples and finally <em>end_of_part</em>. When there
  680. %% is no part to parse anymore, <em>eof</em> is returned.
  681. %%
  682. %% If the request Content-Type is not a multipart one, <em>{error, badarg}</em>
  683. %% is returned.
  684. -spec multipart_data(Req)
  685. -> {headers, cowboy_http:headers(), Req} | {body, binary(), Req}
  686. | {end_of_part | eof, Req} when Req::req().
  687. multipart_data(Req=#http_req{body_state=waiting}) ->
  688. {ok, {<<"multipart">>, _SubType, Params}, Req2} =
  689. parse_header(<<"content-type">>, Req),
  690. {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
  691. {ok, Length, Req3} = parse_header(<<"content-length">>, Req2),
  692. multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)});
  693. multipart_data(Req=#http_req{multipart={Length, Cont}}) ->
  694. multipart_data(Req, Length, Cont());
  695. multipart_data(Req=#http_req{body_state=done}) ->
  696. {eof, Req}.
  697. %% @todo Typespecs.
  698. multipart_data(Req, Length, {headers, Headers, Cont}) ->
  699. {headers, Headers, Req#http_req{multipart={Length, Cont}}};
  700. multipart_data(Req, Length, {body, Data, Cont}) ->
  701. {body, Data, Req#http_req{multipart={Length, Cont}}};
  702. multipart_data(Req, Length, {end_of_part, Cont}) ->
  703. {end_of_part, Req#http_req{multipart={Length, Cont}}};
  704. multipart_data(Req, 0, eof) ->
  705. {eof, Req#http_req{body_state=done, multipart=undefined}};
  706. multipart_data(Req=#http_req{socket=Socket, transport=Transport},
  707. Length, eof) ->
  708. %% We just want to skip so no need to stream data here.
  709. {ok, _Data} = Transport:recv(Socket, Length, 5000),
  710. {eof, Req#http_req{body_state=done, multipart=undefined}};
  711. multipart_data(Req, Length, {more, Parser}) when Length > 0 ->
  712. case stream_body(Req) of
  713. {ok, << Data:Length/binary, Buffer/binary >>, Req2} ->
  714. multipart_data(Req2#http_req{buffer=Buffer}, 0, Parser(Data));
  715. {ok, Data, Req2} ->
  716. multipart_data(Req2, Length - byte_size(Data), Parser(Data))
  717. end.
  718. %% @doc Skip a part returned by the multipart parser.
  719. %%
  720. %% This function repeatedly calls <em>multipart_data/1</em> until
  721. %% <em>end_of_part</em> or <em>eof</em> is parsed.
  722. -spec multipart_skip(Req) -> {ok, Req} when Req::req().
  723. multipart_skip(Req) ->
  724. case multipart_data(Req) of
  725. {end_of_part, Req2} -> {ok, Req2};
  726. {eof, Req2} -> {ok, Req2};
  727. {_, _, Req2} -> multipart_skip(Req2)
  728. end.
  729. %% Response API.
  730. %% @doc Add a cookie header to the response.
  731. -spec set_resp_cookie(binary(), binary(),
  732. [cowboy_cookies:cookie_option()], Req) -> Req when Req::req().
  733. set_resp_cookie(Name, Value, Options, Req) ->
  734. {HeaderName, HeaderValue} = cowboy_cookies:cookie(Name, Value, Options),
  735. set_resp_header(HeaderName, HeaderValue, Req).
  736. %% @doc Add a header to the response.
  737. -spec set_resp_header(binary(), iodata(), Req)
  738. -> Req when Req::req().
  739. set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) ->
  740. Req#http_req{resp_headers=[{Name, Value}|RespHeaders]}.
  741. %% @doc Add a body to the response.
  742. %%
  743. %% The body set here is ignored if the response is later sent using
  744. %% anything other than reply/2 or reply/3. The response body is expected
  745. %% to be a binary or an iolist.
  746. -spec set_resp_body(iodata(), Req) -> Req when Req::req().
  747. set_resp_body(Body, Req) ->
  748. Req#http_req{resp_body=Body}.
  749. %% @doc Add a body function to the response.
  750. %%
  751. %% The response body may also be set to a content-length - stream-function pair.
  752. %% If the response body is of this type normal response headers will be sent.
  753. %% After the response headers has been sent the body function is applied.
  754. %% The body function is expected to write the response body directly to the
  755. %% socket using the transport module.
  756. %%
  757. %% If the body function crashes while writing the response body or writes fewer
  758. %% bytes than declared the behaviour is undefined. The body set here is ignored
  759. %% if the response is later sent using anything other than `reply/2' or
  760. %% `reply/3'.
  761. %%
  762. %% @see cowboy_req:transport/1.
  763. -spec set_resp_body_fun(non_neg_integer(), resp_body_fun(), Req)
  764. -> Req when Req::req().
  765. set_resp_body_fun(StreamLen, StreamFun, Req) ->
  766. Req#http_req{resp_body={StreamLen, StreamFun}}.
  767. %% @doc Return whether the given header has been set for the response.
  768. -spec has_resp_header(binary(), req()) -> boolean().
  769. has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
  770. lists:keymember(Name, 1, RespHeaders).
  771. %% @doc Return whether a body has been set for the response.
  772. -spec has_resp_body(req()) -> boolean().
  773. has_resp_body(#http_req{resp_body={Length, _}}) ->
  774. Length > 0;
  775. has_resp_body(#http_req{resp_body=RespBody}) ->
  776. iolist_size(RespBody) > 0.
  777. %% Remove a header previously set for the response.
  778. -spec delete_resp_header(binary(), Req)
  779. -> Req when Req::req().
  780. delete_resp_header(Name, Req=#http_req{resp_headers=RespHeaders}) ->
  781. RespHeaders2 = lists:keydelete(Name, 1, RespHeaders),
  782. Req#http_req{resp_headers=RespHeaders2}.
  783. %% @equiv reply(Status, [], [], Req)
  784. -spec reply(cowboy_http:status(), Req) -> {ok, Req} when Req::req().
  785. reply(Status, Req=#http_req{resp_body=Body}) ->
  786. reply(Status, [], Body, Req).
  787. %% @equiv reply(Status, Headers, [], Req)
  788. -spec reply(cowboy_http:status(), cowboy_http:headers(), Req)
  789. -> {ok, Req} when Req::req().
  790. reply(Status, Headers, Req=#http_req{resp_body=Body}) ->
  791. reply(Status, Headers, Body, Req).
  792. %% @doc Send a reply to the client.
  793. -spec reply(cowboy_http:status(), cowboy_http:headers(),
  794. iodata() | {non_neg_integer() | resp_body_fun()}, Req)
  795. -> {ok, Req} when Req::req().
  796. reply(Status, Headers, Body, Req=#http_req{
  797. version=Version, connection=Connection,
  798. method=Method, resp_state=waiting, resp_headers=RespHeaders}) ->
  799. RespConn = response_connection(Headers, Connection),
  800. HTTP11Headers = case Version of
  801. {1, 1} -> [{<<"connection">>, atom_to_connection(Connection)}];
  802. _ -> []
  803. end,
  804. case Body of
  805. {ContentLength, BodyFun} ->
  806. {RespType, Req2} = response(Status, Headers, RespHeaders, [
  807. {<<"content-length">>, integer_to_list(ContentLength)},
  808. {<<"date">>, cowboy_clock:rfc1123()},
  809. {<<"server">>, <<"Cowboy">>}
  810. |HTTP11Headers], <<>>, Req),
  811. if RespType =/= hook, Method =/= <<"HEAD">> -> BodyFun();
  812. true -> ok
  813. end;
  814. _ ->
  815. {_, Req2} = response(Status, Headers, RespHeaders, [
  816. {<<"content-length">>, integer_to_list(iolist_size(Body))},
  817. {<<"date">>, cowboy_clock:rfc1123()},
  818. {<<"server">>, <<"Cowboy">>}
  819. |HTTP11Headers],
  820. case Method of <<"HEAD">> -> <<>>; _ -> Body end,
  821. Req)
  822. end,
  823. {ok, Req2#http_req{connection=RespConn, resp_state=done,
  824. resp_headers=[], resp_body= <<>>}}.
  825. %% @equiv chunked_reply(Status, [], Req)
  826. -spec chunked_reply(cowboy_http:status(), Req) -> {ok, Req} when Req::req().
  827. chunked_reply(Status, Req) ->
  828. chunked_reply(Status, [], Req).
  829. %% @doc Initiate the sending of a chunked reply to the client.
  830. %% @see cowboy_req:chunk/2
  831. -spec chunked_reply(cowboy_http:status(), cowboy_http:headers(), Req)
  832. -> {ok, Req} when Req::req().
  833. chunked_reply(Status, Headers, Req=#http_req{
  834. version=Version, connection=Connection,
  835. resp_state=waiting, resp_headers=RespHeaders}) ->
  836. RespConn = response_connection(Headers, Connection),
  837. HTTP11Headers = case Version of
  838. {1, 1} -> [
  839. {<<"connection">>, atom_to_connection(Connection)},
  840. {<<"transfer-encoding">>, <<"chunked">>}];
  841. _ -> []
  842. end,
  843. {_, Req2} = response(Status, Headers, RespHeaders, [
  844. {<<"date">>, cowboy_clock:rfc1123()},
  845. {<<"server">>, <<"Cowboy">>}
  846. |HTTP11Headers], <<>>, Req),
  847. {ok, Req2#http_req{connection=RespConn, resp_state=chunks,
  848. resp_headers=[], resp_body= <<>>}}.
  849. %% @doc Send a chunk of data.
  850. %%
  851. %% A chunked reply must have been initiated before calling this function.
  852. -spec chunk(iodata(), req()) -> ok | {error, atom()}.
  853. chunk(_Data, #http_req{method= <<"HEAD">>}) ->
  854. ok;
  855. chunk(Data, #http_req{socket=Socket, transport=Transport, version={1, 0}}) ->
  856. Transport:send(Socket, Data);
  857. chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) ->
  858. Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
  859. <<"\r\n">>, Data, <<"\r\n">>]).
  860. %% @doc Send an upgrade reply.
  861. %% @private
  862. -spec upgrade_reply(cowboy_http:status(), cowboy_http:headers(), Req)
  863. -> {ok, Req} when Req::req().
  864. upgrade_reply(Status, Headers, Req=#http_req{
  865. resp_state=waiting, resp_headers=RespHeaders}) ->
  866. {_, Req2} = response(Status, Headers, RespHeaders, [
  867. {<<"connection">>, <<"Upgrade">>}
  868. ], <<>>, Req),
  869. {ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
  870. %% @doc Ensure the response has been sent fully.
  871. %% @private
  872. -spec ensure_response(req(), cowboy_http:status()) -> ok.
  873. %% The response has already been fully sent to the client.
  874. ensure_response(#http_req{resp_state=done}, _) ->
  875. ok;
  876. %% No response has been sent but everything apparently went fine.
  877. %% Reply with the status code found in the second argument.
  878. ensure_response(Req=#http_req{resp_state=waiting}, Status) ->
  879. _ = reply(Status, [], [], Req),
  880. ok;
  881. %% Terminate the chunked body for HTTP/1.1 only.
  882. ensure_response(#http_req{method= <<"HEAD">>, resp_state=chunks}, _) ->
  883. ok;
  884. ensure_response(#http_req{version={1, 0}, resp_state=chunks}, _) ->
  885. ok;
  886. ensure_response(#http_req{socket=Socket, transport=Transport,
  887. resp_state=chunks}, _) ->
  888. Transport:send(Socket, <<"0\r\n\r\n">>),
  889. ok.
  890. %% Private setter/getter API.
  891. %% @private
  892. -spec get(atom(), req()) -> any(); ([atom()], req()) -> any().
  893. get(List, Req) when is_list(List) ->
  894. [g(Atom, Req) || Atom <- List];
  895. get(Atom, Req) when is_atom(Atom) ->
  896. g(Atom, Req).
  897. g(bindings, #http_req{bindings=Ret}) -> Ret;
  898. g(body_state, #http_req{body_state=Ret}) -> Ret;
  899. g(buffer, #http_req{buffer=Ret}) -> Ret;
  900. g(connection, #http_req{connection=Ret}) -> Ret;
  901. g(cookies, #http_req{cookies=Ret}) -> Ret;
  902. g(fragment, #http_req{fragment=Ret}) -> Ret;
  903. g(headers, #http_req{headers=Ret}) -> Ret;
  904. g(host, #http_req{host=Ret}) -> Ret;
  905. g(host_info, #http_req{host_info=Ret}) -> Ret;
  906. g(meta, #http_req{meta=Ret}) -> Ret;
  907. g(method, #http_req{method=Ret}) -> Ret;
  908. g(multipart, #http_req{multipart=Ret}) -> Ret;
  909. g(onresponse, #http_req{onresponse=Ret}) -> Ret;
  910. g(p_headers, #http_req{p_headers=Ret}) -> Ret;
  911. g(path, #http_req{path=Ret}) -> Ret;
  912. g(path_info, #http_req{path_info=Ret}) -> Ret;
  913. g(peer, #http_req{peer=Ret}) -> Ret;
  914. g(pid, #http_req{pid=Ret}) -> Ret;
  915. g(port, #http_req{port=Ret}) -> Ret;
  916. g(qs, #http_req{qs=Ret}) -> Ret;
  917. g(qs_vals, #http_req{qs_vals=Ret}) -> Ret;
  918. g(resp_body, #http_req{resp_body=Ret}) -> Ret;
  919. g(resp_headers, #http_req{resp_headers=Ret}) -> Ret;
  920. g(resp_state, #http_req{resp_state=Ret}) -> Ret;
  921. g(socket, #http_req{socket=Ret}) -> Ret;
  922. g(transport, #http_req{transport=Ret}) -> Ret;
  923. g(version, #http_req{version=Ret}) -> Ret.
  924. %% @private
  925. -spec set([{atom(), any()}], Req) -> Req when Req::req().
  926. set([], Req) -> Req;
  927. set([{bindings, Val}|Tail], Req) -> set(Tail, Req#http_req{bindings=Val});
  928. set([{body_state, Val}|Tail], Req) -> set(Tail, Req#http_req{body_state=Val});
  929. set([{buffer, Val}|Tail], Req) -> set(Tail, Req#http_req{buffer=Val});
  930. set([{connection, Val}|Tail], Req) -> set(Tail, Req#http_req{connection=Val});
  931. set([{cookies, Val}|Tail], Req) -> set(Tail, Req#http_req{cookies=Val});
  932. set([{fragment, Val}|Tail], Req) -> set(Tail, Req#http_req{fragment=Val});
  933. set([{headers, Val}|Tail], Req) -> set(Tail, Req#http_req{headers=Val});
  934. set([{host, Val}|Tail], Req) -> set(Tail, Req#http_req{host=Val});
  935. set([{host_info, Val}|Tail], Req) -> set(Tail, Req#http_req{host_info=Val});
  936. set([{meta, Val}|Tail], Req) -> set(Tail, Req#http_req{meta=Val});
  937. set([{method, Val}|Tail], Req) -> set(Tail, Req#http_req{method=Val});
  938. set([{multipart, Val}|Tail], Req) -> set(Tail, Req#http_req{multipart=Val});
  939. set([{onresponse, Val}|Tail], Req) -> set(Tail, Req#http_req{onresponse=Val});
  940. set([{p_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{p_headers=Val});
  941. set([{path, Val}|Tail], Req) -> set(Tail, Req#http_req{path=Val});
  942. set([{path_info, Val}|Tail], Req) -> set(Tail, Req#http_req{path_info=Val});
  943. set([{peer, Val}|Tail], Req) -> set(Tail, Req#http_req{peer=Val});
  944. set([{pid, Val}|Tail], Req) -> set(Tail, Req#http_req{pid=Val});
  945. set([{port, Val}|Tail], Req) -> set(Tail, Req#http_req{port=Val});
  946. set([{qs, Val}|Tail], Req) -> set(Tail, Req#http_req{qs=Val});
  947. set([{qs_vals, Val}|Tail], Req) -> set(Tail, Req#http_req{qs_vals=Val});
  948. set([{resp_body, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_body=Val});
  949. set([{resp_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_headers=Val});
  950. set([{resp_state, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_state=Val});
  951. set([{socket, Val}|Tail], Req) -> set(Tail, Req#http_req{socket=Val});
  952. set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val});
  953. set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}).
  954. %% @private
  955. -spec set_bindings(cowboy_dispatcher:tokens(), cowboy_dispatcher:tokens(),
  956. cowboy_dispatcher:bindings(), Req) -> Req when Req::req().
  957. set_bindings(HostInfo, PathInfo, Bindings, Req) ->
  958. Req#http_req{host_info=HostInfo, path_info=PathInfo,
  959. bindings=Bindings}.
  960. %% Misc API.
  961. %% @doc Compact the request data by removing all non-system information.
  962. %%
  963. %% This essentially removes the host and path info, query string, bindings,
  964. %% headers and cookies.
  965. %%
  966. %% Use it when you really need to save up memory, for example when having
  967. %% many concurrent long-running connections.
  968. -spec compact(Req) -> Req when Req::req().
  969. compact(Req) ->
  970. Req#http_req{host_info=undefined,
  971. path_info=undefined, qs_vals=undefined,
  972. bindings=undefined, headers=[],
  973. p_headers=[], cookies=[]}.
  974. %% @doc Prevent any further responses.
  975. %% @private
  976. -spec lock(Req) -> Req when Req::req().
  977. lock(Req) ->
  978. Req#http_req{resp_state=locked}.
  979. %% @doc Convert the Req object to a list of key/values.
  980. -spec to_list(req()) -> [{atom(), any()}].
  981. to_list(Req) ->
  982. lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))).
  983. %% @doc Return the transport module and socket associated with a request.
  984. %%
  985. %% This exposes the same socket interface used internally by the HTTP protocol
  986. %% implementation to developers that needs low level access to the socket.
  987. %%
  988. %% It is preferred to use this in conjuction with the stream function support
  989. %% in `set_resp_body_fun/3' if this is used to write a response body directly
  990. %% to the socket. This ensures that the response headers are set correctly.
  991. -spec transport(req()) -> {ok, module(), inet:socket()}.
  992. transport(#http_req{transport=Transport, socket=Socket}) ->
  993. {ok, Transport, Socket}.
  994. %% Internal.
  995. -spec response(cowboy_http:status(), cowboy_http:headers(),
  996. cowboy_http:headers(), cowboy_http:headers(), iodata(), Req)
  997. -> {normal | hook, Req} when Req::req().
  998. response(Status, Headers, RespHeaders, DefaultHeaders, Body, Req=#http_req{
  999. socket=Socket, transport=Transport, version=Version,
  1000. pid=ReqPid, onresponse=OnResponse}) ->
  1001. FullHeaders = response_merge_headers(Headers, RespHeaders, DefaultHeaders),
  1002. Req2 = case OnResponse of
  1003. undefined -> Req;
  1004. OnResponse -> OnResponse(Status, FullHeaders, Body,
  1005. %% Don't call 'onresponse' from the hook itself.
  1006. Req#http_req{resp_headers=[], resp_body= <<>>,
  1007. onresponse=undefined})
  1008. end,
  1009. ReplyType = case Req2#http_req.resp_state of
  1010. waiting ->
  1011. HTTPVer = cowboy_http:version_to_binary(Version),
  1012. StatusLine = << HTTPVer/binary, " ",
  1013. (status(Status))/binary, "\r\n" >>,
  1014. HeaderLines = [[Key, <<": ">>, Value, <<"\r\n">>]
  1015. || {Key, Value} <- FullHeaders],
  1016. Transport:send(Socket, [StatusLine, HeaderLines, <<"\r\n">>, Body]),
  1017. ReqPid ! {?MODULE, resp_sent},
  1018. normal;
  1019. _ ->
  1020. hook
  1021. end,
  1022. {ReplyType, Req2}.
  1023. -spec response_connection(cowboy_http:headers(), keepalive | close)
  1024. -> keepalive | close.
  1025. response_connection([], Connection) ->
  1026. Connection;
  1027. response_connection([{Name, Value}|Tail], Connection) ->
  1028. case Name of
  1029. <<"connection">> ->
  1030. Tokens = parse_connection_before(Value, []),
  1031. connection_to_atom(Tokens);
  1032. _ ->
  1033. response_connection(Tail, Connection)
  1034. end.
  1035. -spec response_merge_headers(cowboy_http:headers(), cowboy_http:headers(),
  1036. cowboy_http:headers()) -> cowboy_http:headers().
  1037. response_merge_headers(Headers, RespHeaders, DefaultHeaders) ->
  1038. Headers2 = [{Key, Value} || {Key, Value} <- Headers],
  1039. merge_headers(
  1040. merge_headers(Headers2, RespHeaders),
  1041. DefaultHeaders).
  1042. -spec merge_headers(cowboy_http:headers(), cowboy_http:headers())
  1043. -> cowboy_http:headers().
  1044. %% Merge headers by prepending the tuples in the second list to the
  1045. %% first list. It also handles Set-Cookie properly, which supports
  1046. %% duplicated entries. Notice that, while the RFC2109 does allow more
  1047. %% than one cookie to be set per Set-Cookie header, we are following
  1048. %% the implementation of common web servers and applications which
  1049. %% return many distinct headers per each Set-Cookie entry to avoid
  1050. %% issues with clients/browser which may not support it.
  1051. merge_headers(Headers, []) ->
  1052. Headers;
  1053. merge_headers(Headers, [{<<"set-cookie">>, Value}|Tail]) ->
  1054. merge_headers([{<<"set-cookie">>, Value}|Headers], Tail);
  1055. merge_headers(Headers, [{Name, Value}|Tail]) ->
  1056. Headers2 = case lists:keymember(Name, 1, Headers) of
  1057. true -> Headers;
  1058. false -> [{Name, Value}|Headers]
  1059. end,
  1060. merge_headers(Headers2, Tail).
  1061. -spec atom_to_connection(keepalive) -> <<_:80>>;
  1062. (close) -> <<_:40>>.
  1063. atom_to_connection(keepalive) ->
  1064. <<"keep-alive">>;
  1065. atom_to_connection(close) ->
  1066. <<"close">>.
  1067. %% Optimized parsing functions for the Connection header.
  1068. parse_connection_before(<<>>, Acc) ->
  1069. lists:reverse(Acc);
  1070. parse_connection_before(<< C, Rest/bits >>, Acc)
  1071. when C =:= $,; C =:= $\s; C =:= $\t ->
  1072. parse_connection_before(Rest, Acc);
  1073. parse_connection_before(Buffer, Acc) ->
  1074. parse_connection(Buffer, Acc, <<>>).
  1075. %% An evil block of code appeared!
  1076. parse_connection(<<>>, Acc, <<>>) ->
  1077. lists:reverse(Acc);
  1078. parse_connection(<<>>, Acc, Token) ->
  1079. lists:reverse([Token|Acc]);
  1080. parse_connection(<< C, Rest/bits >>, Acc, Token)
  1081. when C =:= $,; C =:= $\s; C =:= $\t ->
  1082. parse_connection_before(Rest, [Token|Acc]);
  1083. parse_connection(<< C, Rest/bits >>, Acc, Token) ->
  1084. case C of
  1085. $A -> parse_connection(Rest, Acc, << Token/binary, $a >>);
  1086. $B -> parse_connection(Rest, Acc, << Token/binary, $b >>);
  1087. $C -> parse_connection(Rest, Acc, << Token/binary, $c >>);
  1088. $D -> parse_connection(Rest, Acc, << Token/binary, $d >>);
  1089. $E -> parse_connection(Rest, Acc, << Token/binary, $e >>);
  1090. $F -> parse_connection(Rest, Acc, << Token/binary, $f >>);
  1091. $G -> parse_connection(Rest, Acc, << Token/binary, $g >>);
  1092. $H -> parse_connection(Rest, Acc, << Token/binary, $h >>);
  1093. $I -> parse_connection(Rest, Acc, << Token/binary, $i >>);
  1094. $J -> parse_connection(Rest, Acc, << Token/binary, $j >>);
  1095. $K -> parse_connection(Rest, Acc, << Token/binary, $k >>);
  1096. $L -> parse_connection(Rest, Acc, << Token/binary, $l >>);
  1097. $M -> parse_connection(Rest, Acc, << Token/binary, $m >>);
  1098. $N -> parse_connection(Rest, Acc, << Token/binary, $n >>);
  1099. $O -> parse_connection(Rest, Acc, << Token/binary, $o >>);
  1100. $P -> parse_connection(Rest, Acc, << Token/binary, $p >>);
  1101. $Q -> parse_connection(Rest, Acc, << Token/binary, $q >>);
  1102. $R -> parse_connection(Rest, Acc, << Token/binary, $r >>);
  1103. $S -> parse_connection(Rest, Acc, << Token/binary, $s >>);
  1104. $T -> parse_connection(Rest, Acc, << Token/binary, $t >>);
  1105. $U -> parse_connection(Rest, Acc, << Token/binary, $u >>);
  1106. $V -> parse_connection(Rest, Acc, << Token/binary, $v >>);
  1107. $W -> parse_connection(Rest, Acc, << Token/binary, $w >>);
  1108. $X -> parse_connection(Rest, Acc, << Token/binary, $x >>);
  1109. $Y -> parse_connection(Rest, Acc, << Token/binary, $y >>);
  1110. $Z -> parse_connection(Rest, Acc, << Token/binary, $z >>);
  1111. C -> parse_connection(Rest, Acc, << Token/binary, C >>)
  1112. end.
  1113. %% @doc Walk through a tokens list and return whether
  1114. %% the connection is keepalive or closed.
  1115. %%
  1116. %% We don't match on "keep-alive" since it is the default value.
  1117. -spec connection_to_atom([binary()]) -> keepalive | close.
  1118. connection_to_atom([]) ->
  1119. keepalive;
  1120. connection_to_atom([<<"close">>|_]) ->
  1121. close;
  1122. connection_to_atom([_|Tail]) ->
  1123. connection_to_atom(Tail).
  1124. -spec status(cowboy_http:status()) -> binary().
  1125. status(100) -> <<"100 Continue">>;
  1126. status(101) -> <<"101 Switching Protocols">>;
  1127. status(102) -> <<"102 Processing">>;
  1128. status(200) -> <<"200 OK">>;
  1129. status(201) -> <<"201 Created">>;
  1130. status(202) -> <<"202 Accepted">>;
  1131. status(203) -> <<"203 Non-Authoritative Information">>;
  1132. status(204) -> <<"204 No Content">>;
  1133. status(205) -> <<"205 Reset Content">>;
  1134. status(206) -> <<"206 Partial Content">>;
  1135. status(207) -> <<"207 Multi-Status">>;
  1136. status(226) -> <<"226 IM Used">>;
  1137. status(300) -> <<"300 Multiple Choices">>;
  1138. status(301) -> <<"301 Moved Permanently">>;
  1139. status(302) -> <<"302 Found">>;
  1140. status(303) -> <<"303 See Other">>;
  1141. status(304) -> <<"304 Not Modified">>;
  1142. status(305) -> <<"305 Use Proxy">>;
  1143. status(306) -> <<"306 Switch Proxy">>;
  1144. status(307) -> <<"307 Temporary Redirect">>;
  1145. status(400) -> <<"400 Bad Request">>;
  1146. status(401) -> <<"401 Unauthorized">>;
  1147. status(402) -> <<"402 Payment Required">>;
  1148. status(403) -> <<"403 Forbidden">>;
  1149. status(404) -> <<"404 Not Found">>;
  1150. status(405) -> <<"405 Method Not Allowed">>;
  1151. status(406) -> <<"406 Not Acceptable">>;
  1152. status(407) -> <<"407 Proxy Authentication Required">>;
  1153. status(408) -> <<"408 Request Timeout">>;
  1154. status(409) -> <<"409 Conflict">>;
  1155. status(410) -> <<"410 Gone">>;
  1156. status(411) -> <<"411 Length Required">>;
  1157. status(412) -> <<"412 Precondition Failed">>;
  1158. status(413) -> <<"413 Request Entity Too Large">>;
  1159. status(414) -> <<"414 Request-URI Too Long">>;
  1160. status(415) -> <<"415 Unsupported Media Type">>;
  1161. status(416) -> <<"416 Requested Range Not Satisfiable">>;
  1162. status(417) -> <<"417 Expectation Failed">>;
  1163. status(418) -> <<"418 I'm a teapot">>;
  1164. status(422) -> <<"422 Unprocessable Entity">>;
  1165. status(423) -> <<"423 Locked">>;
  1166. status(424) -> <<"424 Failed Dependency">>;
  1167. status(425) -> <<"425 Unordered Collection">>;
  1168. status(426) -> <<"426 Upgrade Required">>;
  1169. status(428) -> <<"428 Precondition Required">>;
  1170. status(429) -> <<"429 Too Many Requests">>;
  1171. status(431) -> <<"431 Request Header Fields Too Large">>;
  1172. status(500) -> <<"500 Internal Server Error">>;
  1173. status(501) -> <<"501 Not Implemented">>;
  1174. status(502) -> <<"502 Bad Gateway">>;
  1175. status(503) -> <<"503 Service Unavailable">>;
  1176. status(504) -> <<"504 Gateway Timeout">>;
  1177. status(505) -> <<"505 HTTP Version Not Supported">>;
  1178. status(506) -> <<"506 Variant Also Negotiates">>;
  1179. status(507) -> <<"507 Insufficient Storage">>;
  1180. status(510) -> <<"510 Not Extended">>;
  1181. status(511) -> <<"511 Network Authentication Required">>;
  1182. status(B) when is_binary(B) -> B.
  1183. %% Tests.
  1184. -ifdef(TEST).
  1185. url_test() ->
  1186. {<<"http://localhost/path">>, _ } =
  1187. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=80,
  1188. path= <<"/path">>, qs= <<>>, fragment= <<>>, pid=self()}),
  1189. {<<"http://localhost:443/path">>, _} =
  1190. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=443,
  1191. path= <<"/path">>, qs= <<>>, fragment= <<>>, pid=self()}),
  1192. {<<"http://localhost:8080/path">>, _} =
  1193. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=8080,
  1194. path= <<"/path">>, qs= <<>>, fragment= <<>>, pid=self()}),
  1195. {<<"http://localhost:8080/path?dummy=2785">>, _} =
  1196. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=8080,
  1197. path= <<"/path">>, qs= <<"dummy=2785">>, fragment= <<>>,
  1198. pid=self()}),
  1199. {<<"http://localhost:8080/path?dummy=2785#fragment">>, _} =
  1200. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=8080,
  1201. path= <<"/path">>, qs= <<"dummy=2785">>, fragment= <<"fragment">>,
  1202. pid=self()}),
  1203. {<<"https://localhost/path">>, _} =
  1204. url(#http_req{transport=ranch_ssl, host= <<"localhost">>, port=443,
  1205. path= <<"/path">>, qs= <<>>, fragment= <<>>, pid=self()}),
  1206. {<<"https://localhost:8443/path">>, _} =
  1207. url(#http_req{transport=ranch_ssl, host= <<"localhost">>, port=8443,
  1208. path= <<"/path">>, qs= <<>>, fragment= <<>>, pid=self()}),
  1209. {<<"https://localhost:8443/path?dummy=2785">>, _} =
  1210. url(#http_req{transport=ranch_ssl, host= <<"localhost">>, port=8443,
  1211. path= <<"/path">>, qs= <<"dummy=2785">>, fragment= <<>>,
  1212. pid=self()}),
  1213. {<<"https://localhost:8443/path?dummy=2785#fragment">>, _} =
  1214. url(#http_req{transport=ranch_ssl, host= <<"localhost">>, port=8443,
  1215. path= <<"/path">>, qs= <<"dummy=2785">>, fragment= <<"fragment">>,
  1216. pid=self()}),
  1217. ok.
  1218. parse_connection_test_() ->
  1219. %% {Binary, Result}
  1220. Tests = [
  1221. {<<"close">>, [<<"close">>]},
  1222. {<<"ClOsE">>, [<<"close">>]},
  1223. {<<"Keep-Alive">>, [<<"keep-alive">>]},
  1224. {<<"keep-alive, Upgrade">>, [<<"keep-alive">>, <<"upgrade">>]}
  1225. ],
  1226. [{B, fun() -> R = parse_connection_before(B, []) end}
  1227. || {B, R} <- Tests].
  1228. connection_to_atom_test_() ->
  1229. %% {Tokens, Result}
  1230. Tests = [
  1231. {[<<"close">>], close},
  1232. {[<<"keep-alive">>], keepalive},
  1233. {[<<"keep-alive">>, <<"upgrade">>], keepalive}
  1234. ],
  1235. [{lists:flatten(io_lib:format("~p", [T])),
  1236. fun() -> R = connection_to_atom(T) end} || {T, R} <- Tests].
  1237. merge_headers_test() ->
  1238. Left0 = [{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
  1239. Right0 = [{<<"set-cookie">>,<<"foo=bar">>},{<<"content-length">>,<<"11">>}],
  1240. ?assertMatch(
  1241. [{<<"set-cookie">>,<<"foo=bar">>},
  1242. {<<"content-length">>,<<"13">>},
  1243. {<<"server">>,<<"Cowboy">>}],
  1244. merge_headers(Left0, Right0)),
  1245. Left1 = [{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
  1246. Right1 = [{<<"set-cookie">>,<<"foo=bar">>},{<<"set-cookie">>,<<"bar=baz">>}],
  1247. ?assertMatch(
  1248. [{<<"set-cookie">>,<<"bar=baz">>},
  1249. {<<"set-cookie">>,<<"foo=bar">>},
  1250. {<<"content-length">>,<<"13">>},
  1251. {<<"server">>,<<"Cowboy">>}],
  1252. merge_headers(Left1, Right1)),
  1253. ok.
  1254. -endif.