cowboy_http_req.erl 27 KB

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