cowboy_req.erl 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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) -> {undefined | binary(), Req} when Req::req().
  286. host_url(Req=#http_req{port=undefined}) ->
  287. {undefined, Req};
  288. host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
  289. TransportName = Transport:name(),
  290. Secure = case TransportName of
  291. ssl -> <<"s">>;
  292. _ -> <<>>
  293. end,
  294. PortBin = case {TransportName, Port} of
  295. {ssl, 443} -> <<>>;
  296. {tcp, 80} -> <<>>;
  297. _ -> << ":", (list_to_binary(integer_to_list(Port)))/binary >>
  298. end,
  299. {<< "http", Secure/binary, "://", Host/binary, PortBin/binary >>, Req}.
  300. %% @doc Return the full request URL as a binary.
  301. %%
  302. %% The URL includes the scheme, host, port, path, query string and fragment.
  303. -spec url(Req) -> {undefined | binary(), Req} when Req::req().
  304. url(Req=#http_req{}) ->
  305. {HostURL, Req2} = host_url(Req),
  306. url2(HostURL, Req2).
  307. url2(undefined, Req=#http_req{}) ->
  308. {undefined, Req};
  309. url2(HostURL, Req=#http_req{path=Path, qs=QS, fragment=Fragment}) ->
  310. QS2 = case QS of
  311. <<>> -> <<>>;
  312. _ -> << "?", QS/binary >>
  313. end,
  314. Fragment2 = case Fragment of
  315. <<>> -> <<>>;
  316. _ -> << "#", Fragment/binary >>
  317. end,
  318. {<< HostURL/binary, Path/binary, QS2/binary, Fragment2/binary >>, Req}.
  319. %% @equiv binding(Name, Req, undefined)
  320. -spec binding(atom(), Req) -> {binary() | undefined, Req} when Req::req().
  321. binding(Name, Req) when is_atom(Name) ->
  322. binding(Name, Req, undefined).
  323. %% @doc Return the binding value for the given key obtained when matching
  324. %% the host and path against the dispatch list, or a default if missing.
  325. -spec binding(atom(), Req, Default)
  326. -> {binary() | Default, Req} when Req::req(), Default::any().
  327. binding(Name, Req, Default) when is_atom(Name) ->
  328. case lists:keyfind(Name, 1, Req#http_req.bindings) of
  329. {Name, Value} -> {Value, Req};
  330. false -> {Default, Req}
  331. end.
  332. %% @doc Return the full list of binding values.
  333. -spec bindings(Req) -> {list({atom(), binary()}), Req} when Req::req().
  334. bindings(Req) ->
  335. {Req#http_req.bindings, Req}.
  336. %% @equiv header(Name, Req, undefined)
  337. -spec header(binary(), Req)
  338. -> {binary() | undefined, Req} when Req::req().
  339. header(Name, Req) ->
  340. header(Name, Req, undefined).
  341. %% @doc Return the header value for the given key, or a default if missing.
  342. -spec header(binary(), Req, Default)
  343. -> {binary() | Default, Req} when Req::req(), Default::any().
  344. header(Name, Req, Default) ->
  345. case lists:keyfind(Name, 1, Req#http_req.headers) of
  346. {Name, Value} -> {Value, Req};
  347. false -> {Default, Req}
  348. end.
  349. %% @doc Return the full list of headers.
  350. -spec headers(Req) -> {cowboy_http:headers(), Req} when Req::req().
  351. headers(Req) ->
  352. {Req#http_req.headers, Req}.
  353. %% @doc Semantically parse headers.
  354. %%
  355. %% When the value isn't found, a proper default value for the type
  356. %% returned is used as a return value.
  357. %% @see parse_header/3
  358. -spec parse_header(binary(), Req)
  359. -> {ok, any(), Req} | {undefined, binary(), Req}
  360. | {error, badarg} when Req::req().
  361. parse_header(Name, Req=#http_req{p_headers=PHeaders}) ->
  362. case lists:keyfind(Name, 1, PHeaders) of
  363. false -> parse_header(Name, Req, parse_header_default(Name));
  364. {Name, Value} -> {ok, Value, Req}
  365. end.
  366. %% @doc Default values for semantic header parsing.
  367. -spec parse_header_default(binary()) -> any().
  368. parse_header_default(<<"transfer-encoding">>) -> [<<"identity">>];
  369. parse_header_default(_Name) -> undefined.
  370. %% @doc Semantically parse headers.
  371. %%
  372. %% When the header is unknown, the value is returned directly without parsing.
  373. -spec parse_header(binary(), Req, any())
  374. -> {ok, any(), Req} | {undefined, binary(), Req}
  375. | {error, badarg} when Req::req().
  376. parse_header(Name, Req, Default) when Name =:= <<"accept">> ->
  377. parse_header(Name, Req, Default,
  378. fun (Value) ->
  379. cowboy_http:list(Value, fun cowboy_http:media_range/2)
  380. end);
  381. parse_header(Name, Req, Default) when Name =:= <<"accept-charset">> ->
  382. parse_header(Name, Req, Default,
  383. fun (Value) ->
  384. cowboy_http:nonempty_list(Value, fun cowboy_http:conneg/2)
  385. end);
  386. parse_header(Name, Req, Default) when Name =:= <<"accept-encoding">> ->
  387. parse_header(Name, Req, Default,
  388. fun (Value) ->
  389. cowboy_http:list(Value, fun cowboy_http:conneg/2)
  390. end);
  391. parse_header(Name, Req, Default) when Name =:= <<"accept-language">> ->
  392. parse_header(Name, Req, Default,
  393. fun (Value) ->
  394. cowboy_http:nonempty_list(Value, fun cowboy_http:language_range/2)
  395. end);
  396. parse_header(Name, Req, Default) when Name =:= <<"content-length">> ->
  397. parse_header(Name, Req, Default, fun cowboy_http:digits/1);
  398. parse_header(Name, Req, Default) when Name =:= <<"content-type">> ->
  399. parse_header(Name, Req, Default, fun cowboy_http:content_type/1);
  400. parse_header(Name, Req, Default) when Name =:= <<"expect">> ->
  401. parse_header(Name, Req, Default,
  402. fun (Value) ->
  403. cowboy_http:nonempty_list(Value, fun cowboy_http:expectation/2)
  404. end);
  405. parse_header(Name, Req, Default)
  406. when Name =:= <<"if-match">>; Name =:= <<"if-none-match">> ->
  407. parse_header(Name, Req, Default, fun cowboy_http:entity_tag_match/1);
  408. parse_header(Name, Req, Default)
  409. when Name =:= <<"if-modified-since">>;
  410. Name =:= <<"if-unmodified-since">> ->
  411. parse_header(Name, Req, Default, fun cowboy_http:http_date/1);
  412. %% @todo Extension parameters.
  413. parse_header(Name, Req, Default) when Name =:= <<"transfer-encoding">> ->
  414. parse_header(Name, Req, Default,
  415. fun (Value) ->
  416. cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
  417. end);
  418. parse_header(Name, Req, Default) when Name =:= <<"upgrade">> ->
  419. parse_header(Name, Req, Default,
  420. fun (Value) ->
  421. cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
  422. end);
  423. parse_header(Name, Req, Default) ->
  424. {Value, Req2} = header(Name, Req, Default),
  425. {undefined, Value, Req2}.
  426. parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) ->
  427. case header(Name, Req) of
  428. {undefined, Req2} ->
  429. {ok, Default, Req2#http_req{p_headers=[{Name, Default}|PHeaders]}};
  430. {Value, Req2} ->
  431. case Fun(Value) of
  432. {error, badarg} ->
  433. {error, badarg};
  434. P ->
  435. {ok, P, Req2#http_req{p_headers=[{Name, P}|PHeaders]}}
  436. end
  437. end.
  438. %% @equiv cookie(Name, Req, undefined)
  439. -spec cookie(binary(), Req)
  440. -> {binary() | true | undefined, Req} when Req::req().
  441. cookie(Name, Req) when is_binary(Name) ->
  442. cookie(Name, Req, undefined).
  443. %% @doc Return the cookie value for the given key, or a default if
  444. %% missing.
  445. -spec cookie(binary(), Req, Default)
  446. -> {binary() | true | Default, Req} when Req::req(), Default::any().
  447. cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
  448. case header(<<"cookie">>, Req) of
  449. {undefined, Req2} ->
  450. {Default, Req2#http_req{cookies=[]}};
  451. {RawCookie, Req2} ->
  452. Cookies = cowboy_cookies:parse_cookie(RawCookie),
  453. cookie(Name, Req2#http_req{cookies=Cookies}, Default)
  454. end;
  455. cookie(Name, Req, Default) ->
  456. case lists:keyfind(Name, 1, Req#http_req.cookies) of
  457. {Name, Value} -> {Value, Req};
  458. false -> {Default, Req}
  459. end.
  460. %% @doc Return the full list of cookie values.
  461. -spec cookies(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
  462. cookies(Req=#http_req{cookies=undefined}) ->
  463. case header(<<"cookie">>, Req) of
  464. {undefined, Req2} ->
  465. {[], Req2#http_req{cookies=[]}};
  466. {RawCookie, Req2} ->
  467. Cookies = cowboy_cookies:parse_cookie(RawCookie),
  468. cookies(Req2#http_req{cookies=Cookies})
  469. end;
  470. cookies(Req=#http_req{cookies=Cookies}) ->
  471. {Cookies, Req}.
  472. %% @equiv meta(Name, Req, undefined)
  473. -spec meta(atom(), Req) -> {any() | undefined, Req} when Req::req().
  474. meta(Name, Req) ->
  475. meta(Name, Req, undefined).
  476. %% @doc Return metadata information about the request.
  477. %%
  478. %% Metadata information varies from one protocol to another. Websockets
  479. %% would define the protocol version here, while REST would use it to
  480. %% indicate which media type, language and charset were retained.
  481. -spec meta(atom(), Req, any()) -> {any(), Req} when Req::req().
  482. meta(Name, Req, Default) ->
  483. case lists:keyfind(Name, 1, Req#http_req.meta) of
  484. {Name, Value} -> {Value, Req};
  485. false -> {Default, Req}
  486. end.
  487. %% @doc Set metadata information.
  488. %%
  489. %% You can use this function to attach information about the request.
  490. %%
  491. %% If the value already exists it will be overwritten.
  492. -spec set_meta(atom(), any(), Req) -> Req when Req::req().
  493. set_meta(Name, Value, Req=#http_req{meta=Meta}) ->
  494. Req#http_req{meta=[{Name, Value}|lists:keydelete(Name, 1, Meta)]}.
  495. %% Request Body API.
  496. %% @doc Return whether the request message has a body.
  497. -spec has_body(Req) -> {boolean(), Req} when Req::req().
  498. has_body(Req) ->
  499. Has = lists:keymember(<<"content-length">>, 1, Req#http_req.headers) orelse
  500. lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers),
  501. {Has, Req}.
  502. %% @doc Return the request message body length, if known.
  503. %%
  504. %% The length may not be known if Transfer-Encoding is not identity,
  505. %% and the body hasn't been read at the time of the call.
  506. -spec body_length(Req) -> {undefined | non_neg_integer(), Req} when Req::req().
  507. body_length(Req) ->
  508. case lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers) of
  509. true ->
  510. {undefined, Req};
  511. false ->
  512. {ok, Length, Req2} = parse_header(<<"content-length">>, Req, 0),
  513. {Length, Req2}
  514. end.
  515. %% @doc Initialize body streaming and set custom decoding functions.
  516. %%
  517. %% Calling this function is optional. It should only be used if you
  518. %% need to override the default behavior of Cowboy. Otherwise you
  519. %% should call stream_body/1 directly.
  520. %%
  521. %% Two decodings happen. First a decoding function is applied to the
  522. %% transferred data, and then another is applied to the actual content.
  523. %%
  524. %% Transfer encoding is generally used for chunked bodies. The decoding
  525. %% function uses a state to keep track of how much it has read, which is
  526. %% also initialized through this function.
  527. %%
  528. %% Content encoding is generally used for compression.
  529. %%
  530. %% Standard encodings can be found in cowboy_http.
  531. -spec init_stream(fun(), any(), fun(), Req) -> {ok, Req} when Req::req().
  532. init_stream(TransferDecode, TransferState, ContentDecode, Req) ->
  533. {ok, Req#http_req{body_state=
  534. {stream, TransferDecode, TransferState, ContentDecode}}}.
  535. %% @doc Stream the request's body.
  536. %%
  537. %% This is the most low level function to read the request body.
  538. %%
  539. %% In most cases, if they weren't defined before using stream_body/4,
  540. %% this function will guess which transfer and content encodings were
  541. %% used for building the request body, and configure the decoding
  542. %% functions that will be used when streaming.
  543. %%
  544. %% It then starts streaming the body, returning {ok, Data, Req}
  545. %% for each streamed part, and {done, Req} when it's finished streaming.
  546. -spec stream_body(Req) -> {ok, binary(), Req}
  547. | {done, Req} | {error, atom()} when Req::req().
  548. stream_body(Req=#http_req{body_state=waiting,
  549. version=Version, transport=Transport, socket=Socket}) ->
  550. case parse_header(<<"expect">>, Req) of
  551. {ok, [<<"100-continue">>], Req1} ->
  552. HTTPVer = cowboy_http:version_to_binary(Version),
  553. Transport:send(Socket,
  554. << HTTPVer/binary, " ", (status(100))/binary, "\r\n\r\n" >>);
  555. {ok, undefined, Req1} ->
  556. ok
  557. end,
  558. case parse_header(<<"transfer-encoding">>, Req1) of
  559. {ok, [<<"chunked">>], Req2} ->
  560. stream_body(Req2#http_req{body_state=
  561. {stream, fun cowboy_http:te_chunked/2, {0, 0},
  562. fun cowboy_http:ce_identity/1}});
  563. {ok, [<<"identity">>], Req2} ->
  564. {Length, Req3} = body_length(Req2),
  565. case Length of
  566. 0 ->
  567. {done, Req3#http_req{body_state=done}};
  568. Length ->
  569. stream_body(Req3#http_req{body_state=
  570. {stream, fun cowboy_http:te_identity/2, {0, Length},
  571. fun cowboy_http:ce_identity/1}})
  572. end
  573. end;
  574. stream_body(Req=#http_req{buffer=Buffer, body_state={stream, _, _, _}})
  575. when Buffer =/= <<>> ->
  576. transfer_decode(Buffer, Req#http_req{buffer= <<>>});
  577. stream_body(Req=#http_req{body_state={stream, _, _, _}}) ->
  578. stream_body_recv(Req);
  579. stream_body(Req=#http_req{body_state=done}) ->
  580. {done, Req}.
  581. -spec stream_body_recv(Req)
  582. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  583. stream_body_recv(Req=#http_req{
  584. transport=Transport, socket=Socket, buffer=Buffer}) ->
  585. %% @todo Allow configuring the timeout.
  586. case Transport:recv(Socket, 0, 5000) of
  587. {ok, Data} -> transfer_decode(<< Buffer/binary, Data/binary >>, Req);
  588. {error, Reason} -> {error, Reason}
  589. end.
  590. -spec transfer_decode(binary(), Req)
  591. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  592. transfer_decode(Data, Req=#http_req{
  593. body_state={stream, TransferDecode, TransferState, ContentDecode}}) ->
  594. case TransferDecode(Data, TransferState) of
  595. {ok, Data2, TransferState2} ->
  596. content_decode(ContentDecode, Data2, Req#http_req{body_state=
  597. {stream, TransferDecode, TransferState2, ContentDecode}});
  598. {ok, Data2, Rest, TransferState2} ->
  599. content_decode(ContentDecode, Data2, Req#http_req{
  600. buffer=Rest, body_state=
  601. {stream, TransferDecode, TransferState2, ContentDecode}});
  602. %% @todo {header(s) for chunked
  603. more ->
  604. stream_body_recv(Req#http_req{buffer=Data});
  605. {done, Length, Rest} ->
  606. Req2 = transfer_decode_done(Length, Rest, Req),
  607. {done, Req2};
  608. {done, Data2, Length, Rest} ->
  609. Req2 = transfer_decode_done(Length, Rest, Req),
  610. content_decode(ContentDecode, Data2, Req2);
  611. {error, Reason} ->
  612. {error, Reason}
  613. end.
  614. -spec transfer_decode_done(non_neg_integer(), binary(), Req)
  615. -> Req when Req::req().
  616. transfer_decode_done(Length, Rest, Req=#http_req{
  617. headers=Headers, p_headers=PHeaders}) ->
  618. Headers2 = lists:keystore(<<"content-length">>, 1, Headers,
  619. {<<"content-length">>, list_to_binary(integer_to_list(Length))}),
  620. %% At this point we just assume TEs were all decoded.
  621. Headers3 = lists:keydelete(<<"transfer-encoding">>, 1, Headers2),
  622. PHeaders2 = lists:keystore(<<"content-length">>, 1, PHeaders,
  623. {<<"content-length">>, Length}),
  624. PHeaders3 = lists:keydelete(<<"transfer-encoding">>, 1, PHeaders2),
  625. Req#http_req{buffer=Rest, body_state=done,
  626. headers=Headers3, p_headers=PHeaders3}.
  627. %% @todo Probably needs a Rest.
  628. -spec content_decode(fun(), binary(), Req)
  629. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  630. content_decode(ContentDecode, Data, Req) ->
  631. case ContentDecode(Data) of
  632. {ok, Data2} -> {ok, Data2, Req};
  633. {error, Reason} -> {error, Reason}
  634. end.
  635. %% @doc Return the full body sent with the request.
  636. -spec body(Req) -> {ok, binary(), Req} | {error, atom()} when Req::req().
  637. body(Req) ->
  638. read_body(infinity, Req, <<>>).
  639. %% @doc Return the full body sent with the request as long as the body
  640. %% length doesn't go over MaxLength.
  641. %%
  642. %% This is most useful to quickly be able to get the full body while
  643. %% avoiding filling your memory with huge request bodies when you're
  644. %% not expecting it.
  645. -spec body(non_neg_integer() | infinity, Req)
  646. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  647. body(MaxLength, Req) ->
  648. read_body(MaxLength, Req, <<>>).
  649. -spec read_body(non_neg_integer() | infinity, Req, binary())
  650. -> {ok, binary(), Req} | {error, atom()} when Req::req().
  651. read_body(MaxLength, Req, Acc) when MaxLength > byte_size(Acc) ->
  652. case stream_body(Req) of
  653. {ok, Data, Req2} ->
  654. read_body(MaxLength, Req2, << Acc/binary, Data/binary >>);
  655. {done, Req2} ->
  656. {ok, Acc, Req2};
  657. {error, Reason} ->
  658. {error, Reason}
  659. end.
  660. -spec skip_body(Req) -> {ok, Req} | {error, atom()} when Req::req().
  661. skip_body(Req) ->
  662. case stream_body(Req) of
  663. {ok, _, Req2} -> skip_body(Req2);
  664. {done, Req2} -> {ok, Req2};
  665. {error, Reason} -> {error, Reason}
  666. end.
  667. %% @doc Return the full body sent with the request, parsed as an
  668. %% application/x-www-form-urlencoded string. Essentially a POST query string.
  669. %% @todo We need an option to limit the size of the body for QS too.
  670. -spec body_qs(Req)
  671. -> {ok, [{binary(), binary() | true}], Req} | {error, atom()}
  672. when Req::req().
  673. body_qs(Req) ->
  674. case body(Req) of
  675. {ok, Body, Req2} ->
  676. {ok, cowboy_http:x_www_form_urlencoded(Body), Req2};
  677. {error, Reason} ->
  678. {error, Reason}
  679. end.
  680. %% Multipart Request API.
  681. %% @doc Return data from the multipart parser.
  682. %%
  683. %% Use this function for multipart streaming. For each part in the request,
  684. %% this function returns <em>{headers, Headers}</em> followed by a sequence of
  685. %% <em>{body, Data}</em> tuples and finally <em>end_of_part</em>. When there
  686. %% is no part to parse anymore, <em>eof</em> is returned.
  687. %%
  688. %% If the request Content-Type is not a multipart one, <em>{error, badarg}</em>
  689. %% is returned.
  690. -spec multipart_data(Req)
  691. -> {headers, cowboy_http:headers(), Req} | {body, binary(), Req}
  692. | {end_of_part | eof, Req} when Req::req().
  693. multipart_data(Req=#http_req{body_state=waiting}) ->
  694. {ok, {<<"multipart">>, _SubType, Params}, Req2} =
  695. parse_header(<<"content-type">>, Req),
  696. {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
  697. {ok, Length, Req3} = parse_header(<<"content-length">>, Req2),
  698. multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)});
  699. multipart_data(Req=#http_req{multipart={Length, Cont}}) ->
  700. multipart_data(Req, Length, Cont());
  701. multipart_data(Req=#http_req{body_state=done}) ->
  702. {eof, Req}.
  703. %% @todo Typespecs.
  704. multipart_data(Req, Length, {headers, Headers, Cont}) ->
  705. {headers, Headers, Req#http_req{multipart={Length, Cont}}};
  706. multipart_data(Req, Length, {body, Data, Cont}) ->
  707. {body, Data, Req#http_req{multipart={Length, Cont}}};
  708. multipart_data(Req, Length, {end_of_part, Cont}) ->
  709. {end_of_part, Req#http_req{multipart={Length, Cont}}};
  710. multipart_data(Req, 0, eof) ->
  711. {eof, Req#http_req{body_state=done, multipart=undefined}};
  712. multipart_data(Req=#http_req{socket=Socket, transport=Transport},
  713. Length, eof) ->
  714. %% We just want to skip so no need to stream data here.
  715. {ok, _Data} = Transport:recv(Socket, Length, 5000),
  716. {eof, Req#http_req{body_state=done, multipart=undefined}};
  717. multipart_data(Req, Length, {more, Parser}) when Length > 0 ->
  718. case stream_body(Req) of
  719. {ok, << Data:Length/binary, Buffer/binary >>, Req2} ->
  720. multipart_data(Req2#http_req{buffer=Buffer}, 0, Parser(Data));
  721. {ok, Data, Req2} ->
  722. multipart_data(Req2, Length - byte_size(Data), Parser(Data))
  723. end.
  724. %% @doc Skip a part returned by the multipart parser.
  725. %%
  726. %% This function repeatedly calls <em>multipart_data/1</em> until
  727. %% <em>end_of_part</em> or <em>eof</em> is parsed.
  728. -spec multipart_skip(Req) -> {ok, Req} when Req::req().
  729. multipart_skip(Req) ->
  730. case multipart_data(Req) of
  731. {end_of_part, Req2} -> {ok, Req2};
  732. {eof, Req2} -> {ok, Req2};
  733. {_, _, Req2} -> multipart_skip(Req2)
  734. end.
  735. %% Response API.
  736. %% @doc Add a cookie header to the response.
  737. -spec set_resp_cookie(binary(), binary(),
  738. [cowboy_cookies:cookie_option()], Req) -> Req when Req::req().
  739. set_resp_cookie(Name, Value, Options, Req) ->
  740. {HeaderName, HeaderValue} = cowboy_cookies:cookie(Name, Value, Options),
  741. set_resp_header(HeaderName, HeaderValue, Req).
  742. %% @doc Add a header to the response.
  743. -spec set_resp_header(binary(), iodata(), Req)
  744. -> Req when Req::req().
  745. set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) ->
  746. Req#http_req{resp_headers=[{Name, Value}|RespHeaders]}.
  747. %% @doc Add a body to the response.
  748. %%
  749. %% The body set here is ignored if the response is later sent using
  750. %% anything other than reply/2 or reply/3. The response body is expected
  751. %% to be a binary or an iolist.
  752. -spec set_resp_body(iodata(), Req) -> Req when Req::req().
  753. set_resp_body(Body, Req) ->
  754. Req#http_req{resp_body=Body}.
  755. %% @doc Add a body function to the response.
  756. %%
  757. %% The response body may also be set to a content-length - stream-function pair.
  758. %% If the response body is of this type normal response headers will be sent.
  759. %% After the response headers has been sent the body function is applied.
  760. %% The body function is expected to write the response body directly to the
  761. %% socket using the transport module.
  762. %%
  763. %% If the body function crashes while writing the response body or writes fewer
  764. %% bytes than declared the behaviour is undefined. The body set here is ignored
  765. %% if the response is later sent using anything other than `reply/2' or
  766. %% `reply/3'.
  767. %%
  768. %% @see cowboy_req:transport/1.
  769. -spec set_resp_body_fun(non_neg_integer(), resp_body_fun(), Req)
  770. -> Req when Req::req().
  771. set_resp_body_fun(StreamLen, StreamFun, Req) ->
  772. Req#http_req{resp_body={StreamLen, StreamFun}}.
  773. %% @doc Return whether the given header has been set for the response.
  774. -spec has_resp_header(binary(), req()) -> boolean().
  775. has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
  776. lists:keymember(Name, 1, RespHeaders).
  777. %% @doc Return whether a body has been set for the response.
  778. -spec has_resp_body(req()) -> boolean().
  779. has_resp_body(#http_req{resp_body={Length, _}}) ->
  780. Length > 0;
  781. has_resp_body(#http_req{resp_body=RespBody}) ->
  782. iolist_size(RespBody) > 0.
  783. %% Remove a header previously set for the response.
  784. -spec delete_resp_header(binary(), Req)
  785. -> Req when Req::req().
  786. delete_resp_header(Name, Req=#http_req{resp_headers=RespHeaders}) ->
  787. RespHeaders2 = lists:keydelete(Name, 1, RespHeaders),
  788. Req#http_req{resp_headers=RespHeaders2}.
  789. %% @equiv reply(Status, [], [], Req)
  790. -spec reply(cowboy_http:status(), Req) -> {ok, Req} when Req::req().
  791. reply(Status, Req=#http_req{resp_body=Body}) ->
  792. reply(Status, [], Body, Req).
  793. %% @equiv reply(Status, Headers, [], Req)
  794. -spec reply(cowboy_http:status(), cowboy_http:headers(), Req)
  795. -> {ok, Req} when Req::req().
  796. reply(Status, Headers, Req=#http_req{resp_body=Body}) ->
  797. reply(Status, Headers, Body, Req).
  798. %% @doc Send a reply to the client.
  799. -spec reply(cowboy_http:status(), cowboy_http:headers(),
  800. iodata() | {non_neg_integer() | resp_body_fun()}, Req)
  801. -> {ok, Req} when Req::req().
  802. reply(Status, Headers, Body, Req=#http_req{
  803. version=Version, connection=Connection,
  804. method=Method, resp_state=waiting, resp_headers=RespHeaders}) ->
  805. RespConn = response_connection(Headers, Connection),
  806. HTTP11Headers = case Version of
  807. {1, 1} -> [{<<"connection">>, atom_to_connection(Connection)}];
  808. _ -> []
  809. end,
  810. case Body of
  811. {ContentLength, BodyFun} ->
  812. {RespType, Req2} = response(Status, Headers, RespHeaders, [
  813. {<<"content-length">>, integer_to_list(ContentLength)},
  814. {<<"date">>, cowboy_clock:rfc1123()},
  815. {<<"server">>, <<"Cowboy">>}
  816. |HTTP11Headers], <<>>, Req),
  817. if RespType =/= hook, Method =/= <<"HEAD">> -> BodyFun();
  818. true -> ok
  819. end;
  820. _ ->
  821. {_, Req2} = response(Status, Headers, RespHeaders, [
  822. {<<"content-length">>, integer_to_list(iolist_size(Body))},
  823. {<<"date">>, cowboy_clock:rfc1123()},
  824. {<<"server">>, <<"Cowboy">>}
  825. |HTTP11Headers],
  826. case Method of <<"HEAD">> -> <<>>; _ -> Body end,
  827. Req)
  828. end,
  829. {ok, Req2#http_req{connection=RespConn, resp_state=done,
  830. resp_headers=[], resp_body= <<>>}}.
  831. %% @equiv chunked_reply(Status, [], Req)
  832. -spec chunked_reply(cowboy_http:status(), Req) -> {ok, Req} when Req::req().
  833. chunked_reply(Status, Req) ->
  834. chunked_reply(Status, [], Req).
  835. %% @doc Initiate the sending of a chunked reply to the client.
  836. %% @see cowboy_req:chunk/2
  837. -spec chunked_reply(cowboy_http:status(), cowboy_http:headers(), Req)
  838. -> {ok, Req} when Req::req().
  839. chunked_reply(Status, Headers, Req=#http_req{
  840. version=Version, connection=Connection,
  841. resp_state=waiting, resp_headers=RespHeaders}) ->
  842. RespConn = response_connection(Headers, Connection),
  843. HTTP11Headers = case Version of
  844. {1, 1} -> [
  845. {<<"connection">>, atom_to_connection(Connection)},
  846. {<<"transfer-encoding">>, <<"chunked">>}];
  847. _ -> []
  848. end,
  849. {_, Req2} = response(Status, Headers, RespHeaders, [
  850. {<<"date">>, cowboy_clock:rfc1123()},
  851. {<<"server">>, <<"Cowboy">>}
  852. |HTTP11Headers], <<>>, Req),
  853. {ok, Req2#http_req{connection=RespConn, resp_state=chunks,
  854. resp_headers=[], resp_body= <<>>}}.
  855. %% @doc Send a chunk of data.
  856. %%
  857. %% A chunked reply must have been initiated before calling this function.
  858. -spec chunk(iodata(), req()) -> ok | {error, atom()}.
  859. chunk(_Data, #http_req{method= <<"HEAD">>}) ->
  860. ok;
  861. chunk(Data, #http_req{socket=Socket, transport=Transport, version={1, 0}}) ->
  862. Transport:send(Socket, Data);
  863. chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) ->
  864. Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
  865. <<"\r\n">>, Data, <<"\r\n">>]).
  866. %% @doc Send an upgrade reply.
  867. %% @private
  868. -spec upgrade_reply(cowboy_http:status(), cowboy_http:headers(), Req)
  869. -> {ok, Req} when Req::req().
  870. upgrade_reply(Status, Headers, Req=#http_req{
  871. resp_state=waiting, resp_headers=RespHeaders}) ->
  872. {_, Req2} = response(Status, Headers, RespHeaders, [
  873. {<<"connection">>, <<"Upgrade">>}
  874. ], <<>>, Req),
  875. {ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
  876. %% @doc Ensure the response has been sent fully.
  877. %% @private
  878. -spec ensure_response(req(), cowboy_http:status()) -> ok.
  879. %% The response has already been fully sent to the client.
  880. ensure_response(#http_req{resp_state=done}, _) ->
  881. ok;
  882. %% No response has been sent but everything apparently went fine.
  883. %% Reply with the status code found in the second argument.
  884. ensure_response(Req=#http_req{resp_state=waiting}, Status) ->
  885. _ = reply(Status, [], [], Req),
  886. ok;
  887. %% Terminate the chunked body for HTTP/1.1 only.
  888. ensure_response(#http_req{method= <<"HEAD">>, resp_state=chunks}, _) ->
  889. ok;
  890. ensure_response(#http_req{version={1, 0}, resp_state=chunks}, _) ->
  891. ok;
  892. ensure_response(#http_req{socket=Socket, transport=Transport,
  893. resp_state=chunks}, _) ->
  894. Transport:send(Socket, <<"0\r\n\r\n">>),
  895. ok.
  896. %% Private setter/getter API.
  897. %% @private
  898. -spec get(atom(), req()) -> any(); ([atom()], req()) -> any().
  899. get(List, Req) when is_list(List) ->
  900. [g(Atom, Req) || Atom <- List];
  901. get(Atom, Req) when is_atom(Atom) ->
  902. g(Atom, Req).
  903. g(bindings, #http_req{bindings=Ret}) -> Ret;
  904. g(body_state, #http_req{body_state=Ret}) -> Ret;
  905. g(buffer, #http_req{buffer=Ret}) -> Ret;
  906. g(connection, #http_req{connection=Ret}) -> Ret;
  907. g(cookies, #http_req{cookies=Ret}) -> Ret;
  908. g(fragment, #http_req{fragment=Ret}) -> Ret;
  909. g(headers, #http_req{headers=Ret}) -> Ret;
  910. g(host, #http_req{host=Ret}) -> Ret;
  911. g(host_info, #http_req{host_info=Ret}) -> Ret;
  912. g(meta, #http_req{meta=Ret}) -> Ret;
  913. g(method, #http_req{method=Ret}) -> Ret;
  914. g(multipart, #http_req{multipart=Ret}) -> Ret;
  915. g(onresponse, #http_req{onresponse=Ret}) -> Ret;
  916. g(p_headers, #http_req{p_headers=Ret}) -> Ret;
  917. g(path, #http_req{path=Ret}) -> Ret;
  918. g(path_info, #http_req{path_info=Ret}) -> Ret;
  919. g(peer, #http_req{peer=Ret}) -> Ret;
  920. g(pid, #http_req{pid=Ret}) -> Ret;
  921. g(port, #http_req{port=Ret}) -> Ret;
  922. g(qs, #http_req{qs=Ret}) -> Ret;
  923. g(qs_vals, #http_req{qs_vals=Ret}) -> Ret;
  924. g(resp_body, #http_req{resp_body=Ret}) -> Ret;
  925. g(resp_headers, #http_req{resp_headers=Ret}) -> Ret;
  926. g(resp_state, #http_req{resp_state=Ret}) -> Ret;
  927. g(socket, #http_req{socket=Ret}) -> Ret;
  928. g(transport, #http_req{transport=Ret}) -> Ret;
  929. g(version, #http_req{version=Ret}) -> Ret.
  930. %% @private
  931. -spec set([{atom(), any()}], Req) -> Req when Req::req().
  932. set([], Req) -> Req;
  933. set([{bindings, Val}|Tail], Req) -> set(Tail, Req#http_req{bindings=Val});
  934. set([{body_state, Val}|Tail], Req) -> set(Tail, Req#http_req{body_state=Val});
  935. set([{buffer, Val}|Tail], Req) -> set(Tail, Req#http_req{buffer=Val});
  936. set([{connection, Val}|Tail], Req) -> set(Tail, Req#http_req{connection=Val});
  937. set([{cookies, Val}|Tail], Req) -> set(Tail, Req#http_req{cookies=Val});
  938. set([{fragment, Val}|Tail], Req) -> set(Tail, Req#http_req{fragment=Val});
  939. set([{headers, Val}|Tail], Req) -> set(Tail, Req#http_req{headers=Val});
  940. set([{host, Val}|Tail], Req) -> set(Tail, Req#http_req{host=Val});
  941. set([{host_info, Val}|Tail], Req) -> set(Tail, Req#http_req{host_info=Val});
  942. set([{meta, Val}|Tail], Req) -> set(Tail, Req#http_req{meta=Val});
  943. set([{method, Val}|Tail], Req) -> set(Tail, Req#http_req{method=Val});
  944. set([{multipart, Val}|Tail], Req) -> set(Tail, Req#http_req{multipart=Val});
  945. set([{onresponse, Val}|Tail], Req) -> set(Tail, Req#http_req{onresponse=Val});
  946. set([{p_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{p_headers=Val});
  947. set([{path, Val}|Tail], Req) -> set(Tail, Req#http_req{path=Val});
  948. set([{path_info, Val}|Tail], Req) -> set(Tail, Req#http_req{path_info=Val});
  949. set([{peer, Val}|Tail], Req) -> set(Tail, Req#http_req{peer=Val});
  950. set([{pid, Val}|Tail], Req) -> set(Tail, Req#http_req{pid=Val});
  951. set([{port, Val}|Tail], Req) -> set(Tail, Req#http_req{port=Val});
  952. set([{qs, Val}|Tail], Req) -> set(Tail, Req#http_req{qs=Val});
  953. set([{qs_vals, Val}|Tail], Req) -> set(Tail, Req#http_req{qs_vals=Val});
  954. set([{resp_body, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_body=Val});
  955. set([{resp_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_headers=Val});
  956. set([{resp_state, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_state=Val});
  957. set([{socket, Val}|Tail], Req) -> set(Tail, Req#http_req{socket=Val});
  958. set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val});
  959. set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}).
  960. %% @private
  961. -spec set_bindings(cowboy_dispatcher:tokens(), cowboy_dispatcher:tokens(),
  962. cowboy_dispatcher:bindings(), Req) -> Req when Req::req().
  963. set_bindings(HostInfo, PathInfo, Bindings, Req) ->
  964. Req#http_req{host_info=HostInfo, path_info=PathInfo,
  965. bindings=Bindings}.
  966. %% Misc API.
  967. %% @doc Compact the request data by removing all non-system information.
  968. %%
  969. %% This essentially removes the host and path info, query string, bindings,
  970. %% headers and cookies.
  971. %%
  972. %% Use it when you really need to save up memory, for example when having
  973. %% many concurrent long-running connections.
  974. -spec compact(Req) -> Req when Req::req().
  975. compact(Req) ->
  976. Req#http_req{host_info=undefined,
  977. path_info=undefined, qs_vals=undefined,
  978. bindings=undefined, headers=[],
  979. p_headers=[], cookies=[]}.
  980. %% @doc Prevent any further responses.
  981. %% @private
  982. -spec lock(Req) -> Req when Req::req().
  983. lock(Req) ->
  984. Req#http_req{resp_state=locked}.
  985. %% @doc Convert the Req object to a list of key/values.
  986. -spec to_list(req()) -> [{atom(), any()}].
  987. to_list(Req) ->
  988. lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))).
  989. %% @doc Return the transport module and socket associated with a request.
  990. %%
  991. %% This exposes the same socket interface used internally by the HTTP protocol
  992. %% implementation to developers that needs low level access to the socket.
  993. %%
  994. %% It is preferred to use this in conjuction with the stream function support
  995. %% in `set_resp_body_fun/3' if this is used to write a response body directly
  996. %% to the socket. This ensures that the response headers are set correctly.
  997. -spec transport(req()) -> {ok, module(), inet:socket()}.
  998. transport(#http_req{transport=Transport, socket=Socket}) ->
  999. {ok, Transport, Socket}.
  1000. %% Internal.
  1001. -spec response(cowboy_http:status(), cowboy_http:headers(),
  1002. cowboy_http:headers(), cowboy_http:headers(), iodata(), Req)
  1003. -> {normal | hook, Req} when Req::req().
  1004. response(Status, Headers, RespHeaders, DefaultHeaders, Body, Req=#http_req{
  1005. socket=Socket, transport=Transport, version=Version,
  1006. pid=ReqPid, onresponse=OnResponse}) ->
  1007. FullHeaders = response_merge_headers(Headers, RespHeaders, DefaultHeaders),
  1008. Req2 = case OnResponse of
  1009. undefined -> Req;
  1010. OnResponse -> OnResponse(Status, FullHeaders, Body,
  1011. %% Don't call 'onresponse' from the hook itself.
  1012. Req#http_req{resp_headers=[], resp_body= <<>>,
  1013. onresponse=undefined})
  1014. end,
  1015. ReplyType = case Req2#http_req.resp_state of
  1016. waiting ->
  1017. HTTPVer = cowboy_http:version_to_binary(Version),
  1018. StatusLine = << HTTPVer/binary, " ",
  1019. (status(Status))/binary, "\r\n" >>,
  1020. HeaderLines = [[Key, <<": ">>, Value, <<"\r\n">>]
  1021. || {Key, Value} <- FullHeaders],
  1022. Transport:send(Socket, [StatusLine, HeaderLines, <<"\r\n">>, Body]),
  1023. ReqPid ! {?MODULE, resp_sent},
  1024. normal;
  1025. _ ->
  1026. hook
  1027. end,
  1028. {ReplyType, Req2}.
  1029. -spec response_connection(cowboy_http:headers(), keepalive | close)
  1030. -> keepalive | close.
  1031. response_connection([], Connection) ->
  1032. Connection;
  1033. response_connection([{Name, Value}|Tail], Connection) ->
  1034. case Name of
  1035. <<"connection">> ->
  1036. Tokens = parse_connection_before(Value, []),
  1037. connection_to_atom(Tokens);
  1038. _ ->
  1039. response_connection(Tail, Connection)
  1040. end.
  1041. -spec response_merge_headers(cowboy_http:headers(), cowboy_http:headers(),
  1042. cowboy_http:headers()) -> cowboy_http:headers().
  1043. response_merge_headers(Headers, RespHeaders, DefaultHeaders) ->
  1044. Headers2 = [{Key, Value} || {Key, Value} <- Headers],
  1045. merge_headers(
  1046. merge_headers(Headers2, RespHeaders),
  1047. DefaultHeaders).
  1048. -spec merge_headers(cowboy_http:headers(), cowboy_http:headers())
  1049. -> cowboy_http:headers().
  1050. merge_headers(Headers, []) ->
  1051. Headers;
  1052. merge_headers(Headers, [{Name, Value}|Tail]) ->
  1053. Headers2 = case lists:keymember(Name, 1, Headers) of
  1054. true -> Headers;
  1055. false -> [{Name, Value}|Headers]
  1056. end,
  1057. merge_headers(Headers2, Tail).
  1058. -spec atom_to_connection(keepalive) -> <<_:80>>;
  1059. (close) -> <<_:40>>.
  1060. atom_to_connection(keepalive) ->
  1061. <<"keep-alive">>;
  1062. atom_to_connection(close) ->
  1063. <<"close">>.
  1064. %% Optimized parsing functions for the Connection header.
  1065. parse_connection_before(<<>>, Acc) ->
  1066. lists:reverse(Acc);
  1067. parse_connection_before(<< C, Rest/bits >>, Acc)
  1068. when C =:= $,; C =:= $\s; C =:= $\t ->
  1069. parse_connection_before(Rest, Acc);
  1070. parse_connection_before(Buffer, Acc) ->
  1071. parse_connection(Buffer, Acc, <<>>).
  1072. %% An evil block of code appeared!
  1073. parse_connection(<<>>, Acc, <<>>) ->
  1074. lists:reverse(Acc);
  1075. parse_connection(<<>>, Acc, Token) ->
  1076. lists:reverse([Token|Acc]);
  1077. parse_connection(<< C, Rest/bits >>, Acc, Token)
  1078. when C =:= $,; C =:= $\s; C =:= $\t ->
  1079. parse_connection_before(Rest, [Token|Acc]);
  1080. parse_connection(<< C, Rest/bits >>, Acc, Token) ->
  1081. case C of
  1082. $A -> parse_connection(Rest, Acc, << Token/binary, $a >>);
  1083. $B -> parse_connection(Rest, Acc, << Token/binary, $b >>);
  1084. $C -> parse_connection(Rest, Acc, << Token/binary, $c >>);
  1085. $D -> parse_connection(Rest, Acc, << Token/binary, $d >>);
  1086. $E -> parse_connection(Rest, Acc, << Token/binary, $e >>);
  1087. $F -> parse_connection(Rest, Acc, << Token/binary, $f >>);
  1088. $G -> parse_connection(Rest, Acc, << Token/binary, $g >>);
  1089. $H -> parse_connection(Rest, Acc, << Token/binary, $h >>);
  1090. $I -> parse_connection(Rest, Acc, << Token/binary, $i >>);
  1091. $J -> parse_connection(Rest, Acc, << Token/binary, $j >>);
  1092. $K -> parse_connection(Rest, Acc, << Token/binary, $k >>);
  1093. $L -> parse_connection(Rest, Acc, << Token/binary, $l >>);
  1094. $M -> parse_connection(Rest, Acc, << Token/binary, $m >>);
  1095. $N -> parse_connection(Rest, Acc, << Token/binary, $n >>);
  1096. $O -> parse_connection(Rest, Acc, << Token/binary, $o >>);
  1097. $P -> parse_connection(Rest, Acc, << Token/binary, $p >>);
  1098. $Q -> parse_connection(Rest, Acc, << Token/binary, $q >>);
  1099. $R -> parse_connection(Rest, Acc, << Token/binary, $r >>);
  1100. $S -> parse_connection(Rest, Acc, << Token/binary, $s >>);
  1101. $T -> parse_connection(Rest, Acc, << Token/binary, $t >>);
  1102. $U -> parse_connection(Rest, Acc, << Token/binary, $u >>);
  1103. $V -> parse_connection(Rest, Acc, << Token/binary, $v >>);
  1104. $W -> parse_connection(Rest, Acc, << Token/binary, $w >>);
  1105. $X -> parse_connection(Rest, Acc, << Token/binary, $x >>);
  1106. $Y -> parse_connection(Rest, Acc, << Token/binary, $y >>);
  1107. $Z -> parse_connection(Rest, Acc, << Token/binary, $z >>);
  1108. C -> parse_connection(Rest, Acc, << Token/binary, C >>)
  1109. end.
  1110. %% @doc Walk through a tokens list and return whether
  1111. %% the connection is keepalive or closed.
  1112. %%
  1113. %% We don't match on "keep-alive" since it is the default value.
  1114. -spec connection_to_atom([binary()]) -> keepalive | close.
  1115. connection_to_atom([]) ->
  1116. keepalive;
  1117. connection_to_atom([<<"close">>|_]) ->
  1118. close;
  1119. connection_to_atom([_|Tail]) ->
  1120. connection_to_atom(Tail).
  1121. -spec status(cowboy_http:status()) -> binary().
  1122. status(100) -> <<"100 Continue">>;
  1123. status(101) -> <<"101 Switching Protocols">>;
  1124. status(102) -> <<"102 Processing">>;
  1125. status(200) -> <<"200 OK">>;
  1126. status(201) -> <<"201 Created">>;
  1127. status(202) -> <<"202 Accepted">>;
  1128. status(203) -> <<"203 Non-Authoritative Information">>;
  1129. status(204) -> <<"204 No Content">>;
  1130. status(205) -> <<"205 Reset Content">>;
  1131. status(206) -> <<"206 Partial Content">>;
  1132. status(207) -> <<"207 Multi-Status">>;
  1133. status(226) -> <<"226 IM Used">>;
  1134. status(300) -> <<"300 Multiple Choices">>;
  1135. status(301) -> <<"301 Moved Permanently">>;
  1136. status(302) -> <<"302 Found">>;
  1137. status(303) -> <<"303 See Other">>;
  1138. status(304) -> <<"304 Not Modified">>;
  1139. status(305) -> <<"305 Use Proxy">>;
  1140. status(306) -> <<"306 Switch Proxy">>;
  1141. status(307) -> <<"307 Temporary Redirect">>;
  1142. status(400) -> <<"400 Bad Request">>;
  1143. status(401) -> <<"401 Unauthorized">>;
  1144. status(402) -> <<"402 Payment Required">>;
  1145. status(403) -> <<"403 Forbidden">>;
  1146. status(404) -> <<"404 Not Found">>;
  1147. status(405) -> <<"405 Method Not Allowed">>;
  1148. status(406) -> <<"406 Not Acceptable">>;
  1149. status(407) -> <<"407 Proxy Authentication Required">>;
  1150. status(408) -> <<"408 Request Timeout">>;
  1151. status(409) -> <<"409 Conflict">>;
  1152. status(410) -> <<"410 Gone">>;
  1153. status(411) -> <<"411 Length Required">>;
  1154. status(412) -> <<"412 Precondition Failed">>;
  1155. status(413) -> <<"413 Request Entity Too Large">>;
  1156. status(414) -> <<"414 Request-URI Too Long">>;
  1157. status(415) -> <<"415 Unsupported Media Type">>;
  1158. status(416) -> <<"416 Requested Range Not Satisfiable">>;
  1159. status(417) -> <<"417 Expectation Failed">>;
  1160. status(418) -> <<"418 I'm a teapot">>;
  1161. status(422) -> <<"422 Unprocessable Entity">>;
  1162. status(423) -> <<"423 Locked">>;
  1163. status(424) -> <<"424 Failed Dependency">>;
  1164. status(425) -> <<"425 Unordered Collection">>;
  1165. status(426) -> <<"426 Upgrade Required">>;
  1166. status(428) -> <<"428 Precondition Required">>;
  1167. status(429) -> <<"429 Too Many Requests">>;
  1168. status(431) -> <<"431 Request Header Fields Too Large">>;
  1169. status(500) -> <<"500 Internal Server Error">>;
  1170. status(501) -> <<"501 Not Implemented">>;
  1171. status(502) -> <<"502 Bad Gateway">>;
  1172. status(503) -> <<"503 Service Unavailable">>;
  1173. status(504) -> <<"504 Gateway Timeout">>;
  1174. status(505) -> <<"505 HTTP Version Not Supported">>;
  1175. status(506) -> <<"506 Variant Also Negotiates">>;
  1176. status(507) -> <<"507 Insufficient Storage">>;
  1177. status(510) -> <<"510 Not Extended">>;
  1178. status(511) -> <<"511 Network Authentication Required">>;
  1179. status(B) when is_binary(B) -> B.
  1180. %% Tests.
  1181. -ifdef(TEST).
  1182. url_test() ->
  1183. {undefined, _} =
  1184. url(#http_req{transport=ranch_tcp, host= <<>>, port= undefined,
  1185. path= <<>>, qs= <<>>, fragment= <<>>, pid=self()}),
  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. -endif.