cowboy_http_req.erl 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.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. %% Almost all functions in this module return a new <em>Req</em> variable.
  18. %% It should always be used instead of the one used in your function call
  19. %% because it keeps the state of the request. It also allows Cowboy to do
  20. %% some lazy evaluation and cache results where possible.
  21. -module(cowboy_http_req).
  22. -export([
  23. method/1, version/1, peer/1, peer_addr/1,
  24. host/1, host_info/1, raw_host/1, port/1,
  25. path/1, path_info/1, raw_path/1,
  26. qs_val/2, qs_val/3, qs_vals/1, raw_qs/1,
  27. binding/2, binding/3, bindings/1,
  28. header/2, header/3, headers/1,
  29. parse_header/2, parse_header/3,
  30. cookie/2, cookie/3, cookies/1,
  31. meta/2, meta/3
  32. ]). %% Request API.
  33. -export([
  34. body/1, body/2, body_qs/1,
  35. multipart_data/1, multipart_skip/1
  36. ]). %% Request Body API.
  37. -export([
  38. set_resp_cookie/4, set_resp_header/3, set_resp_body/2,
  39. set_resp_body_fun/3, has_resp_header/2, has_resp_body/1,
  40. reply/2, reply/3, reply/4,
  41. chunked_reply/2, chunked_reply/3, chunk/2,
  42. upgrade_reply/3
  43. ]). %% Response API.
  44. -export([
  45. compact/1, transport/1
  46. ]). %% Misc API.
  47. -include("http.hrl").
  48. %% Request API.
  49. %% @doc Return the HTTP method of the request.
  50. -spec method(#http_req{}) -> {cowboy_http:method(), #http_req{}}.
  51. method(Req) ->
  52. {Req#http_req.method, Req}.
  53. %% @doc Return the HTTP version used for the request.
  54. -spec version(#http_req{}) -> {cowboy_http:version(), #http_req{}}.
  55. version(Req) ->
  56. {Req#http_req.version, Req}.
  57. %% @doc Return the peer address and port number of the remote host.
  58. -spec peer(#http_req{})
  59. -> {{inet:ip_address(), inet:port_number()}, #http_req{}}.
  60. peer(Req=#http_req{socket=Socket, transport=Transport, peer=undefined}) ->
  61. {ok, Peer} = Transport:peername(Socket),
  62. {Peer, Req#http_req{peer=Peer}};
  63. peer(Req) ->
  64. {Req#http_req.peer, Req}.
  65. %% @doc Returns the peer address calculated from headers.
  66. -spec peer_addr(#http_req{}) -> {inet:ip_address(), #http_req{}}.
  67. peer_addr(Req = #http_req{}) ->
  68. {RealIp, Req1} = header(<<"X-Real-Ip">>, Req),
  69. {ForwardedForRaw, Req2} = header(<<"X-Forwarded-For">>, Req1),
  70. {{PeerIp, _PeerPort}, Req3} = peer(Req2),
  71. ForwardedFor = case ForwardedForRaw of
  72. undefined ->
  73. undefined;
  74. ForwardedForRaw ->
  75. case re:run(ForwardedForRaw, "^(?<first_ip>[^\\,]+)",
  76. [{capture, [first_ip], binary}]) of
  77. {match, [FirstIp]} -> FirstIp;
  78. _Any -> undefined
  79. end
  80. end,
  81. {ok, PeerAddr} = if
  82. is_binary(RealIp) -> inet_parse:address(binary_to_list(RealIp));
  83. is_binary(ForwardedFor) -> inet_parse:address(binary_to_list(ForwardedFor));
  84. true -> {ok, PeerIp}
  85. end,
  86. {PeerAddr, Req3}.
  87. %% @doc Return the tokens for the hostname requested.
  88. -spec host(#http_req{}) -> {cowboy_dispatcher:tokens(), #http_req{}}.
  89. host(Req) ->
  90. {Req#http_req.host, Req}.
  91. %% @doc Return the extra host information obtained from partially matching
  92. %% the hostname using <em>'...'</em>.
  93. -spec host_info(#http_req{})
  94. -> {cowboy_dispatcher:tokens() | undefined, #http_req{}}.
  95. host_info(Req) ->
  96. {Req#http_req.host_info, Req}.
  97. %% @doc Return the raw host directly taken from the request.
  98. -spec raw_host(#http_req{}) -> {binary(), #http_req{}}.
  99. raw_host(Req) ->
  100. {Req#http_req.raw_host, Req}.
  101. %% @doc Return the port used for this request.
  102. -spec port(#http_req{}) -> {inet:port_number(), #http_req{}}.
  103. port(Req) ->
  104. {Req#http_req.port, Req}.
  105. %% @doc Return the path segments for the path requested.
  106. %%
  107. %% Following RFC2396, this function may return path segments containing any
  108. %% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
  109. %% and part of a path segment in the path requested.
  110. -spec path(#http_req{}) -> {cowboy_dispatcher:tokens(), #http_req{}}.
  111. path(Req) ->
  112. {Req#http_req.path, Req}.
  113. %% @doc Return the extra path information obtained from partially matching
  114. %% the patch using <em>'...'</em>.
  115. -spec path_info(#http_req{})
  116. -> {cowboy_dispatcher:tokens() | undefined, #http_req{}}.
  117. path_info(Req) ->
  118. {Req#http_req.path_info, Req}.
  119. %% @doc Return the raw path directly taken from the request.
  120. -spec raw_path(#http_req{}) -> {binary(), #http_req{}}.
  121. raw_path(Req) ->
  122. {Req#http_req.raw_path, Req}.
  123. %% @equiv qs_val(Name, Req, undefined)
  124. -spec qs_val(binary(), #http_req{})
  125. -> {binary() | true | undefined, #http_req{}}.
  126. qs_val(Name, Req) when is_binary(Name) ->
  127. qs_val(Name, Req, undefined).
  128. %% @doc Return the query string value for the given key, or a default if
  129. %% missing.
  130. -spec qs_val(binary(), #http_req{}, Default)
  131. -> {binary() | true | Default, #http_req{}} when Default::any().
  132. qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined,
  133. urldecode={URLDecFun, URLDecArg}}, Default) when is_binary(Name) ->
  134. QsVals = cowboy_http:x_www_form_urlencoded(
  135. RawQs, fun(Bin) -> URLDecFun(Bin, URLDecArg) end),
  136. qs_val(Name, Req#http_req{qs_vals=QsVals}, Default);
  137. qs_val(Name, Req, Default) ->
  138. case lists:keyfind(Name, 1, Req#http_req.qs_vals) of
  139. {Name, Value} -> {Value, Req};
  140. false -> {Default, Req}
  141. end.
  142. %% @doc Return the full list of query string values.
  143. -spec qs_vals(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}.
  144. qs_vals(Req=#http_req{raw_qs=RawQs, qs_vals=undefined,
  145. urldecode={URLDecFun, URLDecArg}}) ->
  146. QsVals = cowboy_http:x_www_form_urlencoded(
  147. RawQs, fun(Bin) -> URLDecFun(Bin, URLDecArg) end),
  148. qs_vals(Req#http_req{qs_vals=QsVals});
  149. qs_vals(Req=#http_req{qs_vals=QsVals}) ->
  150. {QsVals, Req}.
  151. %% @doc Return the raw query string directly taken from the request.
  152. -spec raw_qs(#http_req{}) -> {binary(), #http_req{}}.
  153. raw_qs(Req) ->
  154. {Req#http_req.raw_qs, Req}.
  155. %% @equiv binding(Name, Req, undefined)
  156. -spec binding(atom(), #http_req{}) -> {binary() | undefined, #http_req{}}.
  157. binding(Name, Req) when is_atom(Name) ->
  158. binding(Name, Req, undefined).
  159. %% @doc Return the binding value for the given key obtained when matching
  160. %% the host and path against the dispatch list, or a default if missing.
  161. -spec binding(atom(), #http_req{}, Default)
  162. -> {binary() | Default, #http_req{}} when Default::any().
  163. binding(Name, Req, Default) when is_atom(Name) ->
  164. case lists:keyfind(Name, 1, Req#http_req.bindings) of
  165. {Name, Value} -> {Value, Req};
  166. false -> {Default, Req}
  167. end.
  168. %% @doc Return the full list of binding values.
  169. -spec bindings(#http_req{}) -> {list({atom(), binary()}), #http_req{}}.
  170. bindings(Req) ->
  171. {Req#http_req.bindings, Req}.
  172. %% @equiv header(Name, Req, undefined)
  173. -spec header(atom() | binary(), #http_req{})
  174. -> {binary() | undefined, #http_req{}}.
  175. header(Name, Req) when is_atom(Name) orelse is_binary(Name) ->
  176. header(Name, Req, undefined).
  177. %% @doc Return the header value for the given key, or a default if missing.
  178. -spec header(atom() | binary(), #http_req{}, Default)
  179. -> {binary() | Default, #http_req{}} when Default::any().
  180. header(Name, Req, Default) when is_atom(Name) orelse is_binary(Name) ->
  181. case lists:keyfind(Name, 1, Req#http_req.headers) of
  182. {Name, Value} -> {Value, Req};
  183. false -> {Default, Req}
  184. end.
  185. %% @doc Return the full list of headers.
  186. -spec headers(#http_req{}) -> {cowboy_http:headers(), #http_req{}}.
  187. headers(Req) ->
  188. {Req#http_req.headers, Req}.
  189. %% @doc Semantically parse headers.
  190. %%
  191. %% When the value isn't found, a proper default value for the type
  192. %% returned is used as a return value.
  193. %% @see parse_header/3
  194. -spec parse_header(cowboy_http:header(), #http_req{})
  195. -> {any(), #http_req{}} | {error, badarg}.
  196. parse_header(Name, Req=#http_req{p_headers=PHeaders}) ->
  197. case lists:keyfind(Name, 1, PHeaders) of
  198. false -> parse_header(Name, Req, parse_header_default(Name));
  199. {Name, Value} -> {Value, Req}
  200. end.
  201. %% @doc Default values for semantic header parsing.
  202. -spec parse_header_default(cowboy_http:header()) -> any().
  203. parse_header_default('Connection') -> [];
  204. parse_header_default(_Name) -> undefined.
  205. %% @doc Semantically parse headers.
  206. %%
  207. %% When the header is unknown, the value is returned directly without parsing.
  208. -spec parse_header(cowboy_http:header(), #http_req{}, any())
  209. -> {any(), #http_req{}} | {error, badarg}.
  210. parse_header(Name, Req, Default) when Name =:= 'Accept' ->
  211. parse_header(Name, Req, Default,
  212. fun (Value) ->
  213. cowboy_http:list(Value, fun cowboy_http:media_range/2)
  214. end);
  215. parse_header(Name, Req, Default) when Name =:= 'Accept-Charset' ->
  216. parse_header(Name, Req, Default,
  217. fun (Value) ->
  218. cowboy_http:nonempty_list(Value, fun cowboy_http:conneg/2)
  219. end);
  220. parse_header(Name, Req, Default) when Name =:= 'Accept-Encoding' ->
  221. parse_header(Name, Req, Default,
  222. fun (Value) ->
  223. cowboy_http:list(Value, fun cowboy_http:conneg/2)
  224. end);
  225. parse_header(Name, Req, Default) when Name =:= 'Accept-Language' ->
  226. parse_header(Name, Req, Default,
  227. fun (Value) ->
  228. cowboy_http:nonempty_list(Value, fun cowboy_http:language_range/2)
  229. end);
  230. parse_header(Name, Req, Default) when Name =:= 'Connection' ->
  231. parse_header(Name, Req, Default,
  232. fun (Value) ->
  233. cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
  234. end);
  235. parse_header(Name, Req, Default) when Name =:= 'Content-Length' ->
  236. parse_header(Name, Req, Default,
  237. fun (Value) ->
  238. cowboy_http:digits(Value)
  239. end);
  240. parse_header(Name, Req, Default) when Name =:= 'Content-Type' ->
  241. parse_header(Name, Req, Default,
  242. fun (Value) ->
  243. cowboy_http:content_type(Value)
  244. end);
  245. parse_header(Name, Req, Default) when Name =:= 'Expect' ->
  246. parse_header(Name, Req, Default,
  247. fun (Value) ->
  248. cowboy_http:nonempty_list(Value, fun cowboy_http:expectation/2)
  249. end);
  250. parse_header(Name, Req, Default)
  251. when Name =:= 'If-Match'; Name =:= 'If-None-Match' ->
  252. parse_header(Name, Req, Default,
  253. fun (Value) ->
  254. cowboy_http:entity_tag_match(Value)
  255. end);
  256. parse_header(Name, Req, Default)
  257. when Name =:= 'If-Modified-Since'; Name =:= 'If-Unmodified-Since' ->
  258. parse_header(Name, Req, Default,
  259. fun (Value) ->
  260. cowboy_http:http_date(Value)
  261. end);
  262. parse_header(Name, Req, Default) when Name =:= 'Upgrade' ->
  263. parse_header(Name, Req, Default,
  264. fun (Value) ->
  265. cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
  266. end);
  267. parse_header(Name, Req, Default) ->
  268. {Value, Req2} = header(Name, Req, Default),
  269. {undefined, Value, Req2}.
  270. parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) ->
  271. case header(Name, Req) of
  272. {undefined, Req2} ->
  273. {Default, Req2#http_req{p_headers=[{Name, Default}|PHeaders]}};
  274. {Value, Req2} ->
  275. case Fun(Value) of
  276. {error, badarg} ->
  277. {error, badarg};
  278. P ->
  279. {P, Req2#http_req{p_headers=[{Name, P}|PHeaders]}}
  280. end
  281. end.
  282. %% @equiv cookie(Name, Req, undefined)
  283. -spec cookie(binary(), #http_req{})
  284. -> {binary() | true | undefined, #http_req{}}.
  285. cookie(Name, Req) when is_binary(Name) ->
  286. cookie(Name, Req, undefined).
  287. %% @doc Return the cookie value for the given key, or a default if
  288. %% missing.
  289. -spec cookie(binary(), #http_req{}, Default)
  290. -> {binary() | true | Default, #http_req{}} when Default::any().
  291. cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
  292. case header('Cookie', Req) of
  293. {undefined, Req2} ->
  294. {Default, Req2#http_req{cookies=[]}};
  295. {RawCookie, Req2} ->
  296. Cookies = cowboy_cookies:parse_cookie(RawCookie),
  297. cookie(Name, Req2#http_req{cookies=Cookies}, Default)
  298. end;
  299. cookie(Name, Req, Default) ->
  300. case lists:keyfind(Name, 1, Req#http_req.cookies) of
  301. {Name, Value} -> {Value, Req};
  302. false -> {Default, Req}
  303. end.
  304. %% @doc Return the full list of cookie values.
  305. -spec cookies(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}.
  306. cookies(Req=#http_req{cookies=undefined}) ->
  307. case header('Cookie', Req) of
  308. {undefined, Req2} ->
  309. {[], Req2#http_req{cookies=[]}};
  310. {RawCookie, Req2} ->
  311. Cookies = cowboy_cookies:parse_cookie(RawCookie),
  312. cookies(Req2#http_req{cookies=Cookies})
  313. end;
  314. cookies(Req=#http_req{cookies=Cookies}) ->
  315. {Cookies, Req}.
  316. %% @equiv meta(Name, Req, undefined)
  317. -spec meta(atom(), #http_req{}) -> {any() | undefined, #http_req{}}.
  318. meta(Name, Req) ->
  319. meta(Name, Req, undefined).
  320. %% @doc Return metadata information about the request.
  321. %%
  322. %% Metadata information varies from one protocol to another. Websockets
  323. %% would define the protocol version here, while REST would use it to
  324. %% indicate which media type, language and charset were retained.
  325. -spec meta(atom(), #http_req{}, any()) -> {any(), #http_req{}}.
  326. meta(Name, Req, Default) ->
  327. case lists:keyfind(Name, 1, Req#http_req.meta) of
  328. {Name, Value} -> {Value, Req};
  329. false -> {Default, Req}
  330. end.
  331. %% Request Body API.
  332. %% @doc Return the full body sent with the request, or <em>{error, badarg}</em>
  333. %% if no <em>Content-Length</em> is available.
  334. %% @todo We probably want to allow a max length.
  335. %% @todo Add multipart support to this function.
  336. -spec body(#http_req{}) -> {ok, binary(), #http_req{}} | {error, atom()}.
  337. body(Req) ->
  338. {Length, Req2} = cowboy_http_req:parse_header('Content-Length', Req),
  339. case Length of
  340. undefined -> {error, badarg};
  341. {error, badarg} -> {error, badarg};
  342. _Any ->
  343. body(Length, Req2)
  344. end.
  345. %% @doc Return <em>Length</em> bytes of the request body.
  346. %%
  347. %% You probably shouldn't be calling this function directly, as it expects the
  348. %% <em>Length</em> argument to be the full size of the body, and will consider
  349. %% the body to be fully read from the socket.
  350. %% @todo We probably want to configure the timeout.
  351. -spec body(non_neg_integer(), #http_req{})
  352. -> {ok, binary(), #http_req{}} | {error, atom()}.
  353. body(Length, Req=#http_req{body_state=waiting, buffer=Buffer})
  354. when is_integer(Length) andalso Length =< byte_size(Buffer) ->
  355. << Body:Length/binary, Rest/bits >> = Buffer,
  356. {ok, Body, Req#http_req{body_state=done, buffer=Rest}};
  357. body(Length, Req=#http_req{socket=Socket, transport=Transport,
  358. body_state=waiting, buffer=Buffer}) ->
  359. case Transport:recv(Socket, Length - byte_size(Buffer), 5000) of
  360. {ok, Body} -> {ok, << Buffer/binary, Body/binary >>,
  361. Req#http_req{body_state=done, buffer= <<>>}};
  362. {error, Reason} -> {error, Reason}
  363. end.
  364. %% @doc Return the full body sent with the reqest, parsed as an
  365. %% application/x-www-form-urlencoded string. Essentially a POST query string.
  366. -spec body_qs(#http_req{}) -> {list({binary(), binary() | true}), #http_req{}}.
  367. body_qs(Req=#http_req{urldecode={URLDecFun, URLDecArg}}) ->
  368. {ok, Body, Req2} = body(Req),
  369. {cowboy_http:x_www_form_urlencoded(
  370. Body, fun(Bin) -> URLDecFun(Bin, URLDecArg) end), Req2}.
  371. %% Multipart Request API.
  372. %% @doc Return data from the multipart parser.
  373. %%
  374. %% Use this function for multipart streaming. For each part in the request,
  375. %% this function returns <em>{headers, Headers}</em> followed by a sequence of
  376. %% <em>{data, Data}</em> tuples and finally <em>end_of_part</em>. When there
  377. %% is no part to parse anymore, <em>eof</em> is returned.
  378. %%
  379. %% If the request Content-Type is not a multipart one, <em>{error, badarg}</em>
  380. %% is returned.
  381. -spec multipart_data(#http_req{})
  382. -> {{headers, cowboy_http:headers()}
  383. | {data, binary()} | end_of_part | eof,
  384. #http_req{}}.
  385. multipart_data(Req=#http_req{body_state=waiting}) ->
  386. {{<<"multipart">>, _SubType, Params}, Req2} =
  387. parse_header('Content-Type', Req),
  388. {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
  389. {Length, Req3=#http_req{buffer=Buffer}} =
  390. parse_header('Content-Length', Req2),
  391. multipart_data(Req3, Length, cowboy_multipart:parser(Boundary), Buffer);
  392. multipart_data(Req=#http_req{body_state={multipart, Length, Cont}}) ->
  393. multipart_data(Req, Length, Cont());
  394. multipart_data(Req=#http_req{body_state=done}) ->
  395. {eof, Req}.
  396. multipart_data(Req, Length, Parser, Buffer) when byte_size(Buffer) >= Length ->
  397. << Data:Length/binary, Rest/binary >> = Buffer,
  398. multipart_data(Req#http_req{buffer=Rest}, 0, Parser(Data));
  399. multipart_data(Req, Length, Parser, Buffer) ->
  400. NewLength = Length - byte_size(Buffer),
  401. multipart_data(Req#http_req{buffer= <<>>}, NewLength, Parser(Buffer)).
  402. multipart_data(Req, Length, {headers, Headers, Cont}) ->
  403. {{headers, Headers}, Req#http_req{body_state={multipart, Length, Cont}}};
  404. multipart_data(Req, Length, {body, Data, Cont}) ->
  405. {{body, Data}, Req#http_req{body_state={multipart, Length, Cont}}};
  406. multipart_data(Req, Length, {end_of_part, Cont}) ->
  407. {end_of_part, Req#http_req{body_state={multipart, Length, Cont}}};
  408. multipart_data(Req, 0, eof) ->
  409. {eof, Req#http_req{body_state=done}};
  410. multipart_data(Req=#http_req{socket=Socket, transport=Transport},
  411. Length, eof) ->
  412. {ok, _Data} = Transport:recv(Socket, Length, 5000),
  413. {eof, Req#http_req{body_state=done}};
  414. multipart_data(Req=#http_req{socket=Socket, transport=Transport},
  415. Length, {more, Parser}) when Length > 0 ->
  416. case Transport:recv(Socket, 0, 5000) of
  417. {ok, << Data:Length/binary, Buffer/binary >>} ->
  418. multipart_data(Req#http_req{buffer=Buffer}, 0, Parser(Data));
  419. {ok, Data} ->
  420. multipart_data(Req, Length - byte_size(Data), Parser(Data))
  421. end.
  422. %% @doc Skip a part returned by the multipart parser.
  423. %%
  424. %% This function repeatedly calls <em>multipart_data/1</em> until
  425. %% <em>end_of_part</em> or <em>eof</em> is parsed.
  426. multipart_skip(Req) ->
  427. case multipart_data(Req) of
  428. {end_of_part, Req2} -> {ok, Req2};
  429. {eof, Req2} -> {ok, Req2};
  430. {_Other, Req2} -> multipart_skip(Req2)
  431. end.
  432. %% Response API.
  433. %% @doc Add a cookie header to the response.
  434. -spec set_resp_cookie(binary(), binary(), [cowboy_cookies:cookie_option()],
  435. #http_req{}) -> {ok, #http_req{}}.
  436. set_resp_cookie(Name, Value, Options, Req) ->
  437. {HeaderName, HeaderValue} = cowboy_cookies:cookie(Name, Value, Options),
  438. set_resp_header(HeaderName, HeaderValue, Req).
  439. %% @doc Add a header to the response.
  440. -spec set_resp_header(cowboy_http:header(), iodata(), #http_req{})
  441. -> {ok, #http_req{}}.
  442. set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) ->
  443. NameBin = header_to_binary(Name),
  444. {ok, Req#http_req{resp_headers=[{NameBin, Value}|RespHeaders]}}.
  445. %% @doc Add a body to the response.
  446. %%
  447. %% The body set here is ignored if the response is later sent using
  448. %% anything other than reply/2 or reply/3. The response body is expected
  449. %% to be a binary or an iolist.
  450. -spec set_resp_body(iodata(), #http_req{}) -> {ok, #http_req{}}.
  451. set_resp_body(Body, Req) ->
  452. {ok, Req#http_req{resp_body=Body}}.
  453. %% @doc Add a body function to the response.
  454. %%
  455. %% The response body may also be set to a content-length - stream-function pair.
  456. %% If the response body is of this type normal response headers will be sent.
  457. %% After the response headers has been sent the body function is applied.
  458. %% The body function is expected to write the response body directly to the
  459. %% socket using the transport module.
  460. %%
  461. %% If the body function crashes while writing the response body or writes fewer
  462. %% bytes than declared the behaviour is undefined. The body set here is ignored
  463. %% if the response is later sent using anything other than `reply/2' or
  464. %% `reply/3'.
  465. %%
  466. %% @see cowboy_http_req:transport/1.
  467. -spec set_resp_body_fun(non_neg_integer(), fun(() -> {sent, non_neg_integer()}),
  468. #http_req{}) -> {ok, #http_req{}}.
  469. set_resp_body_fun(StreamLen, StreamFun, Req) ->
  470. {ok, Req#http_req{resp_body={StreamLen, StreamFun}}}.
  471. %% @doc Return whether the given header has been set for the response.
  472. -spec has_resp_header(cowboy_http:header(), #http_req{}) -> boolean().
  473. has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
  474. NameBin = header_to_binary(Name),
  475. lists:keymember(NameBin, 1, RespHeaders).
  476. %% @doc Return whether a body has been set for the response.
  477. -spec has_resp_body(#http_req{}) -> boolean().
  478. has_resp_body(#http_req{resp_body={Length, _}}) ->
  479. Length > 0;
  480. has_resp_body(#http_req{resp_body=RespBody}) ->
  481. iolist_size(RespBody) > 0.
  482. %% @equiv reply(Status, [], [], Req)
  483. -spec reply(cowboy_http:status(), #http_req{}) -> {ok, #http_req{}}.
  484. reply(Status, Req=#http_req{resp_body=Body}) ->
  485. reply(Status, [], Body, Req).
  486. %% @equiv reply(Status, Headers, [], Req)
  487. -spec reply(cowboy_http:status(), cowboy_http:headers(), #http_req{})
  488. -> {ok, #http_req{}}.
  489. reply(Status, Headers, Req=#http_req{resp_body=Body}) ->
  490. reply(Status, Headers, Body, Req).
  491. %% @doc Send a reply to the client.
  492. -spec reply(cowboy_http:status(), cowboy_http:headers(), iodata(), #http_req{})
  493. -> {ok, #http_req{}}.
  494. reply(Status, Headers, Body, Req=#http_req{socket=Socket,
  495. transport=Transport, connection=Connection, pid=ReqPid,
  496. method=Method, resp_state=waiting, resp_headers=RespHeaders}) ->
  497. RespConn = response_connection(Headers, Connection),
  498. ContentLen = case Body of {CL, _} -> CL; _ -> iolist_size(Body) end,
  499. Head = response_head(Status, Headers, RespHeaders, [
  500. {<<"Connection">>, atom_to_connection(Connection)},
  501. {<<"Content-Length">>, integer_to_list(ContentLen)},
  502. {<<"Date">>, cowboy_clock:rfc1123()},
  503. {<<"Server">>, <<"Cowboy">>}
  504. ]),
  505. case {Method, Body} of
  506. {'HEAD', _} -> Transport:send(Socket, Head);
  507. {_, {_, StreamFun}} -> Transport:send(Socket, Head), StreamFun();
  508. {_, _} -> Transport:send(Socket, [Head, Body])
  509. end,
  510. ReqPid ! {?MODULE, resp_sent},
  511. {ok, Req#http_req{connection=RespConn, resp_state=done,
  512. resp_headers=[], resp_body= <<>>}}.
  513. %% @equiv chunked_reply(Status, [], Req)
  514. -spec chunked_reply(cowboy_http:status(), #http_req{}) -> {ok, #http_req{}}.
  515. chunked_reply(Status, Req) ->
  516. chunked_reply(Status, [], Req).
  517. %% @doc Initiate the sending of a chunked reply to the client.
  518. %% @see cowboy_http_req:chunk/2
  519. -spec chunked_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{})
  520. -> {ok, #http_req{}}.
  521. chunked_reply(Status, Headers, Req=#http_req{socket=Socket,
  522. transport=Transport, version=Version, connection=Connection,
  523. pid=ReqPid, resp_state=waiting, resp_headers=RespHeaders}) ->
  524. RespConn = response_connection(Headers, Connection),
  525. DefaultHeaders = [
  526. {<<"Date">>, cowboy_clock:rfc1123()},
  527. {<<"Server">>, <<"Cowboy">>}
  528. ],
  529. DefaultHeaders2 = case Version of
  530. {1, 1} -> [
  531. {<<"Connection">>, atom_to_connection(Connection)},
  532. {<<"Transfer-Encoding">>, <<"chunked">>}
  533. ] ++ DefaultHeaders;
  534. _ -> DefaultHeaders
  535. end,
  536. Head = response_head(Status, Headers, RespHeaders, DefaultHeaders2),
  537. Transport:send(Socket, Head),
  538. ReqPid ! {?MODULE, resp_sent},
  539. {ok, Req#http_req{connection=RespConn, resp_state=chunks,
  540. resp_headers=[], resp_body= <<>>}}.
  541. %% @doc Send a chunk of data.
  542. %%
  543. %% A chunked reply must have been initiated before calling this function.
  544. -spec chunk(iodata(), #http_req{}) -> ok | {error, atom()}.
  545. chunk(_Data, #http_req{socket=_Socket, transport=_Transport, method='HEAD'}) ->
  546. ok;
  547. chunk(Data, #http_req{socket=Socket, transport=Transport, version={1, 0}}) ->
  548. Transport:send(Socket, Data);
  549. chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) ->
  550. Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
  551. <<"\r\n">>, Data, <<"\r\n">>]).
  552. %% @doc Send an upgrade reply.
  553. %% @private
  554. -spec upgrade_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{})
  555. -> {ok, #http_req{}}.
  556. upgrade_reply(Status, Headers, Req=#http_req{socket=Socket, transport=Transport,
  557. pid=ReqPid, resp_state=waiting, resp_headers=RespHeaders}) ->
  558. Head = response_head(Status, Headers, RespHeaders, [
  559. {<<"Connection">>, <<"Upgrade">>}
  560. ]),
  561. Transport:send(Socket, Head),
  562. ReqPid ! {?MODULE, resp_sent},
  563. {ok, Req#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
  564. %% Misc API.
  565. %% @doc Compact the request data by removing all non-system information.
  566. %%
  567. %% This essentially removes the host, path, query string, bindings and headers.
  568. %% Use it when you really need to save up memory, for example when having
  569. %% many concurrent long-running connections.
  570. -spec compact(#http_req{}) -> #http_req{}.
  571. compact(Req) ->
  572. Req#http_req{host=undefined, host_info=undefined, path=undefined,
  573. path_info=undefined, qs_vals=undefined,
  574. bindings=undefined, headers=[],
  575. p_headers=[], cookies=[]}.
  576. %% @doc Return the transport module and socket associated with a request.
  577. %%
  578. %% This exposes the same socket interface used internally by the HTTP protocol
  579. %% implementation to developers that needs low level access to the socket.
  580. %%
  581. %% It is preferred to use this in conjuction with the stream function support
  582. %% in `set_resp_body_fun/3' if this is used to write a response body directly
  583. %% to the socket. This ensures that the response headers are set correctly.
  584. -spec transport(#http_req{}) -> {ok, module(), inet:socket()}.
  585. transport(#http_req{transport=Transport, socket=Socket}) ->
  586. {ok, Transport, Socket}.
  587. %% Internal.
  588. -spec response_connection(cowboy_http:headers(), keepalive | close)
  589. -> keepalive | close.
  590. response_connection([], Connection) ->
  591. Connection;
  592. response_connection([{Name, Value}|Tail], Connection) ->
  593. case Name of
  594. 'Connection' -> response_connection_parse(Value);
  595. Name when is_atom(Name) -> response_connection(Tail, Connection);
  596. Name ->
  597. Name2 = cowboy_bstr:to_lower(Name),
  598. case Name2 of
  599. <<"connection">> -> response_connection_parse(Value);
  600. _Any -> response_connection(Tail, Connection)
  601. end
  602. end.
  603. -spec response_connection_parse(binary()) -> keepalive | close.
  604. response_connection_parse(ReplyConn) ->
  605. Tokens = cowboy_http:nonempty_list(ReplyConn, fun cowboy_http:token/2),
  606. cowboy_http:connection_to_atom(Tokens).
  607. -spec response_head(cowboy_http:status(), cowboy_http:headers(),
  608. cowboy_http:headers(), cowboy_http:headers()) -> iolist().
  609. response_head(Status, Headers, RespHeaders, DefaultHeaders) ->
  610. StatusLine = <<"HTTP/1.1 ", (status(Status))/binary, "\r\n">>,
  611. Headers2 = [{header_to_binary(Key), Value} || {Key, Value} <- Headers],
  612. Headers3 = merge_headers(
  613. merge_headers(Headers2, RespHeaders),
  614. DefaultHeaders),
  615. Headers4 = [[Key, <<": ">>, Value, <<"\r\n">>]
  616. || {Key, Value} <- Headers3],
  617. [StatusLine, Headers4, <<"\r\n">>].
  618. -spec merge_headers(cowboy_http:headers(), cowboy_http:headers())
  619. -> cowboy_http:headers().
  620. merge_headers(Headers, []) ->
  621. Headers;
  622. merge_headers(Headers, [{Name, Value}|Tail]) ->
  623. Headers2 = case lists:keymember(Name, 1, Headers) of
  624. true -> Headers;
  625. false -> Headers ++ [{Name, Value}]
  626. end,
  627. merge_headers(Headers2, Tail).
  628. -spec atom_to_connection(keepalive) -> <<_:80>>;
  629. (close) -> <<_:40>>.
  630. atom_to_connection(keepalive) ->
  631. <<"keep-alive">>;
  632. atom_to_connection(close) ->
  633. <<"close">>.
  634. -spec status(cowboy_http:status()) -> binary().
  635. status(100) -> <<"100 Continue">>;
  636. status(101) -> <<"101 Switching Protocols">>;
  637. status(102) -> <<"102 Processing">>;
  638. status(200) -> <<"200 OK">>;
  639. status(201) -> <<"201 Created">>;
  640. status(202) -> <<"202 Accepted">>;
  641. status(203) -> <<"203 Non-Authoritative Information">>;
  642. status(204) -> <<"204 No Content">>;
  643. status(205) -> <<"205 Reset Content">>;
  644. status(206) -> <<"206 Partial Content">>;
  645. status(207) -> <<"207 Multi-Status">>;
  646. status(226) -> <<"226 IM Used">>;
  647. status(300) -> <<"300 Multiple Choices">>;
  648. status(301) -> <<"301 Moved Permanently">>;
  649. status(302) -> <<"302 Found">>;
  650. status(303) -> <<"303 See Other">>;
  651. status(304) -> <<"304 Not Modified">>;
  652. status(305) -> <<"305 Use Proxy">>;
  653. status(306) -> <<"306 Switch Proxy">>;
  654. status(307) -> <<"307 Temporary Redirect">>;
  655. status(400) -> <<"400 Bad Request">>;
  656. status(401) -> <<"401 Unauthorized">>;
  657. status(402) -> <<"402 Payment Required">>;
  658. status(403) -> <<"403 Forbidden">>;
  659. status(404) -> <<"404 Not Found">>;
  660. status(405) -> <<"405 Method Not Allowed">>;
  661. status(406) -> <<"406 Not Acceptable">>;
  662. status(407) -> <<"407 Proxy Authentication Required">>;
  663. status(408) -> <<"408 Request Timeout">>;
  664. status(409) -> <<"409 Conflict">>;
  665. status(410) -> <<"410 Gone">>;
  666. status(411) -> <<"411 Length Required">>;
  667. status(412) -> <<"412 Precondition Failed">>;
  668. status(413) -> <<"413 Request Entity Too Large">>;
  669. status(414) -> <<"414 Request-URI Too Long">>;
  670. status(415) -> <<"415 Unsupported Media Type">>;
  671. status(416) -> <<"416 Requested Range Not Satisfiable">>;
  672. status(417) -> <<"417 Expectation Failed">>;
  673. status(418) -> <<"418 I'm a teapot">>;
  674. status(422) -> <<"422 Unprocessable Entity">>;
  675. status(423) -> <<"423 Locked">>;
  676. status(424) -> <<"424 Failed Dependency">>;
  677. status(425) -> <<"425 Unordered Collection">>;
  678. status(426) -> <<"426 Upgrade Required">>;
  679. status(500) -> <<"500 Internal Server Error">>;
  680. status(501) -> <<"501 Not Implemented">>;
  681. status(502) -> <<"502 Bad Gateway">>;
  682. status(503) -> <<"503 Service Unavailable">>;
  683. status(504) -> <<"504 Gateway Timeout">>;
  684. status(505) -> <<"505 HTTP Version Not Supported">>;
  685. status(506) -> <<"506 Variant Also Negotiates">>;
  686. status(507) -> <<"507 Insufficient Storage">>;
  687. status(510) -> <<"510 Not Extended">>;
  688. status(B) when is_binary(B) -> B.
  689. -spec header_to_binary(cowboy_http:header()) -> binary().
  690. header_to_binary('Cache-Control') -> <<"Cache-Control">>;
  691. header_to_binary('Connection') -> <<"Connection">>;
  692. header_to_binary('Date') -> <<"Date">>;
  693. header_to_binary('Pragma') -> <<"Pragma">>;
  694. header_to_binary('Transfer-Encoding') -> <<"Transfer-Encoding">>;
  695. header_to_binary('Upgrade') -> <<"Upgrade">>;
  696. header_to_binary('Via') -> <<"Via">>;
  697. header_to_binary('Accept') -> <<"Accept">>;
  698. header_to_binary('Accept-Charset') -> <<"Accept-Charset">>;
  699. header_to_binary('Accept-Encoding') -> <<"Accept-Encoding">>;
  700. header_to_binary('Accept-Language') -> <<"Accept-Language">>;
  701. header_to_binary('Authorization') -> <<"Authorization">>;
  702. header_to_binary('From') -> <<"From">>;
  703. header_to_binary('Host') -> <<"Host">>;
  704. header_to_binary('If-Modified-Since') -> <<"If-Modified-Since">>;
  705. header_to_binary('If-Match') -> <<"If-Match">>;
  706. header_to_binary('If-None-Match') -> <<"If-None-Match">>;
  707. header_to_binary('If-Range') -> <<"If-Range">>;
  708. header_to_binary('If-Unmodified-Since') -> <<"If-Unmodified-Since">>;
  709. header_to_binary('Max-Forwards') -> <<"Max-Forwards">>;
  710. header_to_binary('Proxy-Authorization') -> <<"Proxy-Authorization">>;
  711. header_to_binary('Range') -> <<"Range">>;
  712. header_to_binary('Referer') -> <<"Referer">>;
  713. header_to_binary('User-Agent') -> <<"User-Agent">>;
  714. header_to_binary('Age') -> <<"Age">>;
  715. header_to_binary('Location') -> <<"Location">>;
  716. header_to_binary('Proxy-Authenticate') -> <<"Proxy-Authenticate">>;
  717. header_to_binary('Public') -> <<"Public">>;
  718. header_to_binary('Retry-After') -> <<"Retry-After">>;
  719. header_to_binary('Server') -> <<"Server">>;
  720. header_to_binary('Vary') -> <<"Vary">>;
  721. header_to_binary('Warning') -> <<"Warning">>;
  722. header_to_binary('Www-Authenticate') -> <<"Www-Authenticate">>;
  723. header_to_binary('Allow') -> <<"Allow">>;
  724. header_to_binary('Content-Base') -> <<"Content-Base">>;
  725. header_to_binary('Content-Encoding') -> <<"Content-Encoding">>;
  726. header_to_binary('Content-Language') -> <<"Content-Language">>;
  727. header_to_binary('Content-Length') -> <<"Content-Length">>;
  728. header_to_binary('Content-Location') -> <<"Content-Location">>;
  729. header_to_binary('Content-Md5') -> <<"Content-Md5">>;
  730. header_to_binary('Content-Range') -> <<"Content-Range">>;
  731. header_to_binary('Content-Type') -> <<"Content-Type">>;
  732. header_to_binary('Etag') -> <<"Etag">>;
  733. header_to_binary('Expires') -> <<"Expires">>;
  734. header_to_binary('Last-Modified') -> <<"Last-Modified">>;
  735. header_to_binary('Accept-Ranges') -> <<"Accept-Ranges">>;
  736. header_to_binary('Set-Cookie') -> <<"Set-Cookie">>;
  737. header_to_binary('Set-Cookie2') -> <<"Set-Cookie2">>;
  738. header_to_binary('X-Forwarded-For') -> <<"X-Forwarded-For">>;
  739. header_to_binary('Cookie') -> <<"Cookie">>;
  740. header_to_binary('Keep-Alive') -> <<"Keep-Alive">>;
  741. header_to_binary('Proxy-Connection') -> <<"Proxy-Connection">>;
  742. header_to_binary(B) when is_binary(B) -> B.