cowboy_req.erl 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. %% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(cowboy_req).
  16. %% Request API.
  17. -export([new/14]).
  18. -export([method/1]).
  19. -export([version/1]).
  20. -export([peer/1]).
  21. -export([host/1]).
  22. -export([host_info/1]).
  23. -export([port/1]).
  24. -export([path/1]).
  25. -export([path_info/1]).
  26. -export([qs/1]).
  27. -export([qs_val/2]).
  28. -export([qs_val/3]).
  29. -export([qs_vals/1]).
  30. -export([host_url/1]).
  31. -export([url/1]).
  32. -export([binding/2]).
  33. -export([binding/3]).
  34. -export([bindings/1]).
  35. -export([header/2]).
  36. -export([header/3]).
  37. -export([headers/1]).
  38. -export([parse_header/2]).
  39. -export([parse_header/3]).
  40. -export([cookie/2]).
  41. -export([cookie/3]).
  42. -export([cookies/1]).
  43. -export([meta/2]).
  44. -export([meta/3]).
  45. -export([set_meta/3]).
  46. %% Request body API.
  47. -export([has_body/1]).
  48. -export([body_length/1]).
  49. -export([body/1]).
  50. -export([body/2]).
  51. -export([body_qs/1]).
  52. -export([body_qs/2]).
  53. %% Multipart API.
  54. -export([part/1]).
  55. -export([part/2]).
  56. -export([part_body/1]).
  57. -export([part_body/2]).
  58. %% Response API.
  59. -export([set_resp_cookie/4]).
  60. -export([set_resp_header/3]).
  61. -export([set_resp_body/2]).
  62. -export([set_resp_body_fun/2]).
  63. -export([set_resp_body_fun/3]).
  64. -export([has_resp_header/2]).
  65. -export([has_resp_body/1]).
  66. -export([delete_resp_header/2]).
  67. -export([reply/2]).
  68. -export([reply/3]).
  69. -export([reply/4]).
  70. -export([chunked_reply/2]).
  71. -export([chunked_reply/3]).
  72. -export([chunk/2]).
  73. -export([upgrade_reply/3]).
  74. -export([continue/1]).
  75. -export([maybe_reply/2]).
  76. -export([ensure_response/2]).
  77. %% Private setter/getter API.
  78. -export([append_buffer/2]).
  79. -export([get/2]).
  80. -export([set/2]).
  81. -export([set_bindings/4]).
  82. %% Misc API.
  83. -export([compact/1]).
  84. -export([lock/1]).
  85. -export([to_list/1]).
  86. -type cookie_opts() :: cow_cookie:cookie_opts().
  87. -export_type([cookie_opts/0]).
  88. -type content_decode_fun() :: fun((binary())
  89. -> {ok, binary()}
  90. | {error, atom()}).
  91. -type transfer_decode_fun() :: fun((binary(), any())
  92. -> cow_http_te:decode_ret()).
  93. -type body_opts() :: [{continue, boolean()}
  94. | {length, non_neg_integer()}
  95. | {read_length, non_neg_integer()}
  96. | {read_timeout, timeout()}
  97. | {transfer_decode, transfer_decode_fun(), any()}
  98. | {content_decode, content_decode_fun()}].
  99. -export_type([body_opts/0]).
  100. -type resp_body_fun() :: fun((any(), module()) -> ok).
  101. -type send_chunk_fun() :: fun((iodata()) -> ok | {error, atom()}).
  102. -type resp_chunked_fun() :: fun((send_chunk_fun()) -> ok).
  103. -record(http_req, {
  104. %% Transport.
  105. socket = undefined :: any(),
  106. transport = undefined :: undefined | module(),
  107. connection = keepalive :: keepalive | close,
  108. %% Request.
  109. pid = undefined :: pid(),
  110. method = <<"GET">> :: binary(),
  111. version = 'HTTP/1.1' :: cowboy:http_version(),
  112. peer = undefined :: undefined | {inet:ip_address(), inet:port_number()},
  113. host = undefined :: undefined | binary(),
  114. host_info = undefined :: undefined | cowboy_router:tokens(),
  115. port = undefined :: undefined | inet:port_number(),
  116. path = undefined :: binary(),
  117. path_info = undefined :: undefined | cowboy_router:tokens(),
  118. qs = undefined :: binary(),
  119. qs_vals = undefined :: undefined | list({binary(), binary() | true}),
  120. bindings = undefined :: undefined | cowboy_router:bindings(),
  121. headers = [] :: cowboy:http_headers(),
  122. p_headers = [] :: [any()],
  123. cookies = undefined :: undefined | [{binary(), binary()}],
  124. meta = [] :: [{atom(), any()}],
  125. %% Request body.
  126. body_state = waiting :: waiting | done | {stream, non_neg_integer(),
  127. transfer_decode_fun(), any(), content_decode_fun()},
  128. buffer = <<>> :: binary(),
  129. multipart = undefined :: undefined | {binary(), binary()},
  130. %% Response.
  131. resp_compress = false :: boolean(),
  132. resp_state = waiting :: locked | waiting | waiting_stream
  133. | chunks | stream | done,
  134. resp_headers = [] :: cowboy:http_headers(),
  135. resp_body = <<>> :: iodata() | resp_body_fun()
  136. | {non_neg_integer(), resp_body_fun()}
  137. | {chunked, resp_chunked_fun()},
  138. %% Functions.
  139. onresponse = undefined :: undefined | already_called
  140. | cowboy:onresponse_fun()
  141. }).
  142. -opaque req() :: #http_req{}.
  143. -export_type([req/0]).
  144. %% Request API.
  145. -spec new(any(), module(),
  146. undefined | {inet:ip_address(), inet:port_number()},
  147. binary(), binary(), binary(),
  148. cowboy:http_version(), cowboy:http_headers(), binary(),
  149. inet:port_number() | undefined, binary(), boolean(), boolean(),
  150. undefined | cowboy:onresponse_fun())
  151. -> req().
  152. new(Socket, Transport, Peer, Method, Path, Query,
  153. Version, Headers, Host, Port, Buffer, CanKeepalive,
  154. Compress, OnResponse) ->
  155. Req = #http_req{socket=Socket, transport=Transport, pid=self(), peer=Peer,
  156. method=Method, path=Path, qs=Query, version=Version,
  157. headers=Headers, host=Host, port=Port, buffer=Buffer,
  158. resp_compress=Compress, onresponse=OnResponse},
  159. case CanKeepalive of
  160. false ->
  161. Req#http_req{connection=close};
  162. true ->
  163. case lists:keyfind(<<"connection">>, 1, Headers) of
  164. false ->
  165. case Version of
  166. 'HTTP/1.1' -> Req; %% keepalive
  167. 'HTTP/1.0' -> Req#http_req{connection=close}
  168. end;
  169. {_, ConnectionHeader} ->
  170. Tokens = cow_http_hd:parse_connection(ConnectionHeader),
  171. Connection = connection_to_atom(Tokens),
  172. Req#http_req{connection=Connection,
  173. p_headers=[{<<"connection">>, Tokens}]}
  174. end
  175. end.
  176. -spec method(Req) -> {binary(), Req} when Req::req().
  177. method(Req) ->
  178. {Req#http_req.method, Req}.
  179. -spec version(Req) -> {cowboy:http_version(), Req} when Req::req().
  180. version(Req) ->
  181. {Req#http_req.version, Req}.
  182. -spec peer(Req)
  183. -> {{inet:ip_address(), inet:port_number()}, Req}
  184. when Req::req().
  185. peer(Req) ->
  186. {Req#http_req.peer, Req}.
  187. -spec host(Req) -> {binary(), Req} when Req::req().
  188. host(Req) ->
  189. {Req#http_req.host, Req}.
  190. -spec host_info(Req)
  191. -> {cowboy_router:tokens() | undefined, Req} when Req::req().
  192. host_info(Req) ->
  193. {Req#http_req.host_info, Req}.
  194. -spec port(Req) -> {inet:port_number(), Req} when Req::req().
  195. port(Req) ->
  196. {Req#http_req.port, Req}.
  197. -spec path(Req) -> {binary(), Req} when Req::req().
  198. path(Req) ->
  199. {Req#http_req.path, Req}.
  200. -spec path_info(Req)
  201. -> {cowboy_router:tokens() | undefined, Req} when Req::req().
  202. path_info(Req) ->
  203. {Req#http_req.path_info, Req}.
  204. -spec qs(Req) -> {binary(), Req} when Req::req().
  205. qs(Req) ->
  206. {Req#http_req.qs, Req}.
  207. -spec qs_val(binary(), Req)
  208. -> {binary() | true | undefined, Req} when Req::req().
  209. qs_val(Name, Req) when is_binary(Name) ->
  210. qs_val(Name, Req, undefined).
  211. -spec qs_val(binary(), Req, Default)
  212. -> {binary() | true | Default, Req} when Req::req(), Default::any().
  213. qs_val(Name, Req=#http_req{qs=RawQs, qs_vals=undefined}, Default)
  214. when is_binary(Name) ->
  215. QsVals = cow_qs:parse_qs(RawQs),
  216. qs_val(Name, Req#http_req{qs_vals=QsVals}, Default);
  217. qs_val(Name, Req, Default) ->
  218. case lists:keyfind(Name, 1, Req#http_req.qs_vals) of
  219. {Name, Value} -> {Value, Req};
  220. false -> {Default, Req}
  221. end.
  222. -spec qs_vals(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
  223. qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) ->
  224. QsVals = cow_qs:parse_qs(RawQs),
  225. qs_vals(Req#http_req{qs_vals=QsVals});
  226. qs_vals(Req=#http_req{qs_vals=QsVals}) ->
  227. {QsVals, Req}.
  228. %% The URL includes the scheme, host and port only.
  229. -spec host_url(Req) -> {undefined | binary(), Req} when Req::req().
  230. host_url(Req=#http_req{port=undefined}) ->
  231. {undefined, Req};
  232. host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
  233. TransportName = Transport:name(),
  234. Secure = case TransportName of
  235. ssl -> <<"s">>;
  236. _ -> <<>>
  237. end,
  238. PortBin = case {TransportName, Port} of
  239. {ssl, 443} -> <<>>;
  240. {tcp, 80} -> <<>>;
  241. _ -> << ":", (integer_to_binary(Port))/binary >>
  242. end,
  243. {<< "http", Secure/binary, "://", Host/binary, PortBin/binary >>, Req}.
  244. %% The URL includes the scheme, host, port, path and query string.
  245. -spec url(Req) -> {undefined | binary(), Req} when Req::req().
  246. url(Req=#http_req{}) ->
  247. {HostURL, Req2} = host_url(Req),
  248. url(HostURL, Req2).
  249. url(undefined, Req=#http_req{}) ->
  250. {undefined, Req};
  251. url(HostURL, Req=#http_req{path=Path, qs=QS}) ->
  252. QS2 = case QS of
  253. <<>> -> <<>>;
  254. _ -> << "?", QS/binary >>
  255. end,
  256. {<< HostURL/binary, Path/binary, QS2/binary >>, Req}.
  257. -spec binding(atom(), Req) -> {any() | undefined, Req} when Req::req().
  258. binding(Name, Req) when is_atom(Name) ->
  259. binding(Name, Req, undefined).
  260. -spec binding(atom(), Req, Default)
  261. -> {any() | Default, Req} when Req::req(), Default::any().
  262. binding(Name, Req, Default) when is_atom(Name) ->
  263. case lists:keyfind(Name, 1, Req#http_req.bindings) of
  264. {Name, Value} -> {Value, Req};
  265. false -> {Default, Req}
  266. end.
  267. -spec bindings(Req) -> {[{atom(), any()}], Req} when Req::req().
  268. bindings(Req) ->
  269. {Req#http_req.bindings, Req}.
  270. -spec header(binary(), Req)
  271. -> {binary() | undefined, Req} when Req::req().
  272. header(Name, Req) ->
  273. header(Name, Req, undefined).
  274. -spec header(binary(), Req, Default)
  275. -> {binary() | Default, Req} when Req::req(), Default::any().
  276. header(Name, Req, Default) ->
  277. case lists:keyfind(Name, 1, Req#http_req.headers) of
  278. {Name, Value} -> {Value, Req};
  279. false -> {Default, Req}
  280. end.
  281. -spec headers(Req) -> {cowboy:http_headers(), Req} when Req::req().
  282. headers(Req) ->
  283. {Req#http_req.headers, Req}.
  284. -spec parse_header(binary(), Req)
  285. -> {ok, any(), Req} | {undefined, binary(), Req}
  286. | {error, badarg} when Req::req().
  287. parse_header(Name, Req=#http_req{p_headers=PHeaders}) ->
  288. case lists:keyfind(Name, 1, PHeaders) of
  289. false -> parse_header(Name, Req, parse_header_default(Name));
  290. {Name, Value} -> {ok, Value, Req}
  291. end.
  292. -spec parse_header_default(binary()) -> any().
  293. parse_header_default(<<"transfer-encoding">>) -> [<<"identity">>];
  294. parse_header_default(_Name) -> undefined.
  295. -spec parse_header(binary(), Req, any())
  296. -> {ok, any(), Req} | {undefined, binary(), Req}
  297. | {error, badarg} when Req::req().
  298. parse_header(Name = <<"accept">>, Req, Default) ->
  299. parse_header(Name, Req, Default,
  300. fun (Value) ->
  301. cowboy_http:list(Value, fun cowboy_http:media_range/2)
  302. end);
  303. parse_header(Name = <<"accept-charset">>, Req, Default) ->
  304. parse_header(Name, Req, Default,
  305. fun (Value) ->
  306. cowboy_http:nonempty_list(Value, fun cowboy_http:conneg/2)
  307. end);
  308. parse_header(Name = <<"accept-encoding">>, Req, Default) ->
  309. parse_header(Name, Req, Default,
  310. fun (Value) ->
  311. cowboy_http:list(Value, fun cowboy_http:conneg/2)
  312. end);
  313. parse_header(Name = <<"accept-language">>, Req, Default) ->
  314. parse_header(Name, Req, Default,
  315. fun (Value) ->
  316. cowboy_http:nonempty_list(Value, fun cowboy_http:language_range/2)
  317. end);
  318. parse_header(Name = <<"authorization">>, Req, Default) ->
  319. parse_header(Name, Req, Default,
  320. fun (Value) ->
  321. cowboy_http:token_ci(Value, fun cowboy_http:authorization/2)
  322. end);
  323. parse_header(Name = <<"content-length">>, Req, Default) ->
  324. parse_header(Name, Req, Default, fun cow_http_hd:parse_content_length/1);
  325. parse_header(Name = <<"content-type">>, Req, Default) ->
  326. parse_header(Name, Req, Default, fun cowboy_http:content_type/1);
  327. parse_header(Name = <<"cookie">>, Req, Default) ->
  328. parse_header(Name, Req, Default, fun cow_cookie:parse_cookie/1);
  329. parse_header(Name = <<"expect">>, Req, Default) ->
  330. parse_header(Name, Req, Default,
  331. fun (Value) ->
  332. cowboy_http:nonempty_list(Value, fun cowboy_http:expectation/2)
  333. end);
  334. parse_header(Name, Req, Default)
  335. when Name =:= <<"if-match">>;
  336. Name =:= <<"if-none-match">> ->
  337. parse_header(Name, Req, Default, fun cowboy_http:entity_tag_match/1);
  338. parse_header(Name, Req, Default)
  339. when Name =:= <<"if-modified-since">>;
  340. Name =:= <<"if-unmodified-since">> ->
  341. parse_header(Name, Req, Default, fun cowboy_http:http_date/1);
  342. parse_header(Name = <<"range">>, Req, Default) ->
  343. parse_header(Name, Req, Default, fun cowboy_http:range/1);
  344. parse_header(Name, Req, Default)
  345. when Name =:= <<"sec-websocket-protocol">>;
  346. Name =:= <<"x-forwarded-for">> ->
  347. parse_header(Name, Req, Default,
  348. fun (Value) ->
  349. cowboy_http:nonempty_list(Value, fun cowboy_http:token/2)
  350. end);
  351. parse_header(Name = <<"transfer-encoding">>, Req, Default) ->
  352. parse_header(Name, Req, Default, fun cow_http_hd:parse_transfer_encoding/1);
  353. %% @todo Product version.
  354. parse_header(Name = <<"upgrade">>, Req, Default) ->
  355. parse_header(Name, Req, Default,
  356. fun (Value) ->
  357. cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
  358. end);
  359. parse_header(Name = <<"sec-websocket-extensions">>, Req, Default) ->
  360. parse_header(Name, Req, Default, fun cowboy_http:parameterized_tokens/1);
  361. parse_header(Name, Req, Default) ->
  362. {Value, Req2} = header(Name, Req, Default),
  363. {undefined, Value, Req2}.
  364. parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) ->
  365. case header(Name, Req) of
  366. {undefined, Req2} ->
  367. {ok, Default, Req2#http_req{p_headers=[{Name, Default}|PHeaders]}};
  368. {Value, Req2} ->
  369. case Fun(Value) of
  370. {error, badarg} ->
  371. {error, badarg};
  372. P ->
  373. {ok, P, Req2#http_req{p_headers=[{Name, P}|PHeaders]}}
  374. end
  375. end.
  376. -spec cookie(binary(), Req)
  377. -> {binary() | undefined, Req} when Req::req().
  378. cookie(Name, Req) when is_binary(Name) ->
  379. cookie(Name, Req, undefined).
  380. -spec cookie(binary(), Req, Default)
  381. -> {binary() | Default, Req} when Req::req(), Default::any().
  382. cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
  383. case parse_header(<<"cookie">>, Req) of
  384. {ok, undefined, Req2} ->
  385. {Default, Req2#http_req{cookies=[]}};
  386. {ok, Cookies, Req2} ->
  387. cookie(Name, Req2#http_req{cookies=Cookies}, Default)
  388. end;
  389. cookie(Name, Req, Default) ->
  390. case lists:keyfind(Name, 1, Req#http_req.cookies) of
  391. {Name, Value} -> {Value, Req};
  392. false -> {Default, Req}
  393. end.
  394. -spec cookies(Req) -> {list({binary(), binary()}), Req} when Req::req().
  395. cookies(Req=#http_req{cookies=undefined}) ->
  396. case parse_header(<<"cookie">>, Req) of
  397. {ok, undefined, Req2} ->
  398. {[], Req2#http_req{cookies=[]}};
  399. {ok, Cookies, Req2} ->
  400. cookies(Req2#http_req{cookies=Cookies});
  401. %% Flash player incorrectly sends an empty Cookie header.
  402. {error, badarg} ->
  403. {[], Req#http_req{cookies=[]}}
  404. end;
  405. cookies(Req=#http_req{cookies=Cookies}) ->
  406. {Cookies, Req}.
  407. -spec meta(atom(), Req) -> {any() | undefined, Req} when Req::req().
  408. meta(Name, Req) ->
  409. meta(Name, Req, undefined).
  410. -spec meta(atom(), Req, any()) -> {any(), Req} when Req::req().
  411. meta(Name, Req, Default) ->
  412. case lists:keyfind(Name, 1, Req#http_req.meta) of
  413. {Name, Value} -> {Value, Req};
  414. false -> {Default, Req}
  415. end.
  416. -spec set_meta(atom(), any(), Req) -> Req when Req::req().
  417. set_meta(Name, Value, Req=#http_req{meta=Meta}) ->
  418. Req#http_req{meta=lists:keystore(Name, 1, Meta, {Name, Value})}.
  419. %% Request Body API.
  420. -spec has_body(req()) -> boolean().
  421. has_body(Req) ->
  422. case lists:keyfind(<<"content-length">>, 1, Req#http_req.headers) of
  423. {_, <<"0">>} ->
  424. false;
  425. {_, _} ->
  426. true;
  427. _ ->
  428. lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers)
  429. end.
  430. %% The length may not be known if Transfer-Encoding is not identity,
  431. %% and the body hasn't been read at the time of the call.
  432. -spec body_length(Req) -> {undefined | non_neg_integer(), Req} when Req::req().
  433. body_length(Req) ->
  434. case parse_header(<<"transfer-encoding">>, Req) of
  435. {ok, [<<"identity">>], Req2} ->
  436. {ok, Length, Req3} = parse_header(<<"content-length">>, Req2, 0),
  437. {Length, Req3};
  438. {ok, _, Req2} ->
  439. {undefined, Req2}
  440. end.
  441. -spec body(Req)
  442. -> {ok, binary(), Req} | {more, binary(), Req}
  443. | {error, atom()} when Req::req().
  444. body(Req) ->
  445. body(Req, []).
  446. -spec body(Req, body_opts())
  447. -> {ok, binary(), Req} | {more, binary(), Req}
  448. | {error, atom()} when Req::req().
  449. body(Req=#http_req{body_state=waiting}, Opts) ->
  450. %% Send a 100 continue if needed (enabled by default).
  451. Req1 = case lists:keyfind(continue, 1, Opts) of
  452. {_, false} ->
  453. Req;
  454. _ ->
  455. {ok, ExpectHeader, Req0} = parse_header(<<"expect">>, Req),
  456. ok = case ExpectHeader of
  457. [<<"100-continue">>] -> continue(Req0);
  458. _ -> ok
  459. end,
  460. Req0
  461. end,
  462. %% Initialize body streaming state.
  463. CFun = case lists:keyfind(content_decode, 1, Opts) of
  464. false ->
  465. fun cowboy_http:ce_identity/1;
  466. {_, CFun0} ->
  467. CFun0
  468. end,
  469. case lists:keyfind(transfer_decode, 1, Opts) of
  470. false ->
  471. case parse_header(<<"transfer-encoding">>, Req1) of
  472. {ok, [<<"chunked">>], Req2} ->
  473. body(Req2#http_req{body_state={stream, 0,
  474. fun cow_http_te:stream_chunked/2, {0, 0}, CFun}}, Opts);
  475. {ok, [<<"identity">>], Req2} ->
  476. {Len, Req3} = body_length(Req2),
  477. case Len of
  478. 0 ->
  479. {ok, <<>>, Req3#http_req{body_state=done}};
  480. _ ->
  481. body(Req3#http_req{body_state={stream, Len,
  482. fun cow_http_te:stream_identity/2, {0, Len},
  483. CFun}}, Opts)
  484. end
  485. end;
  486. {_, TFun, TState} ->
  487. body(Req1#http_req{body_state={stream, 0,
  488. TFun, TState, CFun}}, Opts)
  489. end;
  490. body(Req=#http_req{body_state=done}, _) ->
  491. {ok, <<>>, Req};
  492. body(Req, Opts) ->
  493. ChunkLen = case lists:keyfind(length, 1, Opts) of
  494. false -> 8000000;
  495. {_, ChunkLen0} -> ChunkLen0
  496. end,
  497. ReadLen = case lists:keyfind(read_length, 1, Opts) of
  498. false -> 1000000;
  499. {_, ReadLen0} -> ReadLen0
  500. end,
  501. ReadTimeout = case lists:keyfind(read_timeout, 1, Opts) of
  502. false -> 15000;
  503. {_, ReadTimeout0} -> ReadTimeout0
  504. end,
  505. body_loop(Req, ReadTimeout, ReadLen, ChunkLen, <<>>).
  506. body_loop(Req=#http_req{buffer=Buffer, body_state={stream, Length, _, _, _}},
  507. ReadTimeout, ReadLength, ChunkLength, Acc) ->
  508. {Tag, Res, Req2} = case Buffer of
  509. <<>> ->
  510. body_recv(Req, ReadTimeout, min(Length, ReadLength));
  511. _ ->
  512. body_decode(Req, ReadTimeout)
  513. end,
  514. case {Tag, Res} of
  515. {ok, {ok, Data}} ->
  516. {ok, << Acc/binary, Data/binary >>, Req2};
  517. {more, {ok, Data}} ->
  518. Acc2 = << Acc/binary, Data/binary >>,
  519. case byte_size(Acc2) >= ChunkLength of
  520. true -> {more, Acc2, Req2};
  521. false -> body_loop(Req2, ReadTimeout, ReadLength, ChunkLength, Acc2)
  522. end;
  523. _ -> %% Error.
  524. Res
  525. end.
  526. body_recv(Req=#http_req{transport=Transport, socket=Socket, buffer=Buffer},
  527. ReadTimeout, ReadLength) ->
  528. case Transport:recv(Socket, ReadLength, ReadTimeout) of
  529. {ok, Data} ->
  530. body_decode(Req#http_req{buffer= << Buffer/binary, Data/binary >>},
  531. ReadTimeout);
  532. Error = {error, _} ->
  533. {error, Error, Req}
  534. end.
  535. %% Two decodings happen. First a decoding function is applied to the
  536. %% transferred data, and then another is applied to the actual content.
  537. %%
  538. %% Transfer encoding is generally used for chunked bodies. The decoding
  539. %% function uses a state to keep track of how much it has read, which is
  540. %% also initialized through this function.
  541. %%
  542. %% Content encoding is generally used for compression.
  543. %%
  544. %% @todo Handle chunked after-the-facts headers.
  545. %% @todo Depending on the length returned we might want to 0 or +5 it.
  546. body_decode(Req=#http_req{buffer=Data, body_state={stream, _,
  547. TDecode, TState, CDecode}}, ReadTimeout) ->
  548. case TDecode(Data, TState) of
  549. more ->
  550. body_recv(Req#http_req{body_state={stream, 0,
  551. TDecode, TState, CDecode}}, ReadTimeout, 0);
  552. {more, Data2, TState2} ->
  553. {more, CDecode(Data2), Req#http_req{body_state={stream, 0,
  554. TDecode, TState2, CDecode}, buffer= <<>>}};
  555. {more, Data2, Length, TState2} when is_integer(Length) ->
  556. {more, CDecode(Data2), Req#http_req{body_state={stream, Length,
  557. TDecode, TState2, CDecode}, buffer= <<>>}};
  558. {more, Data2, Rest, TState2} ->
  559. {more, CDecode(Data2), Req#http_req{body_state={stream, 0,
  560. TDecode, TState2, CDecode}, buffer=Rest}};
  561. {done, TotalLength, Rest} ->
  562. {ok, {ok, <<>>}, body_decode_end(Req, TotalLength, Rest)};
  563. {done, Data2, TotalLength, Rest} ->
  564. {ok, CDecode(Data2), body_decode_end(Req, TotalLength, Rest)}
  565. end.
  566. body_decode_end(Req=#http_req{headers=Headers, p_headers=PHeaders},
  567. TotalLength, Rest) ->
  568. Headers2 = lists:keystore(<<"content-length">>, 1, Headers,
  569. {<<"content-length">>, integer_to_binary(TotalLength)}),
  570. %% At this point we just assume TEs were all decoded.
  571. Headers3 = lists:keydelete(<<"transfer-encoding">>, 1, Headers2),
  572. PHeaders2 = lists:keystore(<<"content-length">>, 1, PHeaders,
  573. {<<"content-length">>, TotalLength}),
  574. PHeaders3 = lists:keydelete(<<"transfer-encoding">>, 1, PHeaders2),
  575. Req#http_req{buffer=Rest, body_state=done,
  576. headers=Headers3, p_headers=PHeaders3}.
  577. -spec body_qs(Req)
  578. -> {ok, [{binary(), binary() | true}], Req} | {error, atom()}
  579. when Req::req().
  580. body_qs(Req) ->
  581. body_qs(Req, [
  582. {length, 64000},
  583. {read_length, 64000},
  584. {read_timeout, 5000}]).
  585. -spec body_qs(Req, body_opts()) -> {ok, [{binary(), binary() | true}], Req}
  586. | {badlength, Req} | {error, atom()} when Req::req().
  587. body_qs(Req, Opts) ->
  588. case body(Req, Opts) of
  589. {ok, Body, Req2} ->
  590. {ok, cow_qs:parse_qs(Body), Req2};
  591. {more, _, Req2} ->
  592. {badlength, Req2};
  593. {error, Reason} ->
  594. {error, Reason}
  595. end.
  596. %% Multipart API.
  597. -spec part(Req)
  598. -> {ok, cow_multipart:headers(), Req} | {done, Req}
  599. when Req::req().
  600. part(Req) ->
  601. part(Req, [
  602. {length, 64000},
  603. {read_length, 64000},
  604. {read_timeout, 5000}]).
  605. -spec part(Req, body_opts())
  606. -> {ok, cow_multipart:headers(), Req} | {done, Req}
  607. when Req::req().
  608. part(Req=#http_req{multipart=undefined}, Opts) ->
  609. part(init_multipart(Req), Opts);
  610. part(Req, Opts) ->
  611. {Data, Req2} = stream_multipart(Req, Opts),
  612. part(Data, Opts, Req2).
  613. part(Buffer, Opts, Req=#http_req{multipart={Boundary, _}}) ->
  614. case cow_multipart:parse_headers(Buffer, Boundary) of
  615. more ->
  616. {Data, Req2} = stream_multipart(Req, Opts),
  617. part(<< Buffer/binary, Data/binary >>, Opts, Req2);
  618. {more, Buffer2} ->
  619. {Data, Req2} = stream_multipart(Req, Opts),
  620. part(<< Buffer2/binary, Data/binary >>, Opts, Req2);
  621. {ok, Headers, Rest} ->
  622. {ok, Headers, Req#http_req{multipart={Boundary, Rest}}};
  623. %% Ignore epilogue.
  624. {done, _} ->
  625. {done, Req#http_req{multipart=undefined}}
  626. end.
  627. -spec part_body(Req)
  628. -> {ok, binary(), Req} | {more, binary(), Req}
  629. when Req::req().
  630. part_body(Req) ->
  631. part_body(Req, []).
  632. -spec part_body(Req, body_opts())
  633. -> {ok, binary(), Req} | {more, binary(), Req}
  634. when Req::req().
  635. part_body(Req=#http_req{multipart=undefined}, Opts) ->
  636. part_body(init_multipart(Req), Opts);
  637. part_body(Req, Opts) ->
  638. part_body(<<>>, Opts, Req, <<>>).
  639. part_body(Buffer, Opts, Req=#http_req{multipart={Boundary, _}}, Acc) ->
  640. ChunkLen = case lists:keyfind(length, 1, Opts) of
  641. false -> 8000000;
  642. {_, ChunkLen0} -> ChunkLen0
  643. end,
  644. case byte_size(Acc) > ChunkLen of
  645. true ->
  646. {more, Acc, Req#http_req{multipart={Boundary, Buffer}}};
  647. false ->
  648. {Data, Req2} = stream_multipart(Req, Opts),
  649. case cow_multipart:parse_body(<< Buffer/binary, Data/binary >>, Boundary) of
  650. {ok, Body} ->
  651. part_body(<<>>, Opts, Req2, << Acc/binary, Body/binary >>);
  652. {ok, Body, Rest} ->
  653. part_body(Rest, Opts, Req2, << Acc/binary, Body/binary >>);
  654. done ->
  655. {ok, Acc, Req2};
  656. {done, Body} ->
  657. {ok, << Acc/binary, Body/binary >>, Req2};
  658. {done, Body, Rest} ->
  659. {ok, << Acc/binary, Body/binary >>,
  660. Req2#http_req{multipart={Boundary, Rest}}}
  661. end
  662. end.
  663. init_multipart(Req) ->
  664. {ok, {<<"multipart">>, _, Params}, Req2}
  665. = parse_header(<<"content-type">>, Req),
  666. {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
  667. Req2#http_req{multipart={Boundary, <<>>}}.
  668. stream_multipart(Req=#http_req{body_state=BodyState, multipart={_, <<>>}}, Opts) ->
  669. true = BodyState =/= done,
  670. {_, Data, Req2} = body(Req, Opts),
  671. {Data, Req2};
  672. stream_multipart(Req=#http_req{multipart={Boundary, Buffer}}, _) ->
  673. {Buffer, Req#http_req{multipart={Boundary, <<>>}}}.
  674. %% Response API.
  675. %% The cookie name cannot contain any of the following characters:
  676. %% =,;\s\t\r\n\013\014
  677. %%
  678. %% The cookie value cannot contain any of the following characters:
  679. %% ,; \t\r\n\013\014
  680. -spec set_resp_cookie(iodata(), iodata(), cookie_opts(), Req)
  681. -> Req when Req::req().
  682. set_resp_cookie(Name, Value, Opts, Req) ->
  683. Cookie = cow_cookie:setcookie(Name, Value, Opts),
  684. set_resp_header(<<"set-cookie">>, Cookie, Req).
  685. -spec set_resp_header(binary(), iodata(), Req)
  686. -> Req when Req::req().
  687. set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) ->
  688. Req#http_req{resp_headers=[{Name, Value}|RespHeaders]}.
  689. -spec set_resp_body(iodata(), Req) -> Req when Req::req().
  690. set_resp_body(Body, Req) ->
  691. Req#http_req{resp_body=Body}.
  692. -spec set_resp_body_fun(resp_body_fun(), Req) -> Req when Req::req().
  693. set_resp_body_fun(StreamFun, Req) when is_function(StreamFun) ->
  694. Req#http_req{resp_body=StreamFun}.
  695. %% If the body function crashes while writing the response body or writes
  696. %% fewer bytes than declared the behaviour is undefined.
  697. -spec set_resp_body_fun(non_neg_integer(), resp_body_fun(), Req)
  698. -> Req when Req::req();
  699. (chunked, resp_chunked_fun(), Req)
  700. -> Req when Req::req().
  701. set_resp_body_fun(StreamLen, StreamFun, Req)
  702. when is_integer(StreamLen), is_function(StreamFun) ->
  703. Req#http_req{resp_body={StreamLen, StreamFun}};
  704. set_resp_body_fun(chunked, StreamFun, Req)
  705. when is_function(StreamFun) ->
  706. Req#http_req{resp_body={chunked, StreamFun}}.
  707. -spec has_resp_header(binary(), req()) -> boolean().
  708. has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
  709. lists:keymember(Name, 1, RespHeaders).
  710. -spec has_resp_body(req()) -> boolean().
  711. has_resp_body(#http_req{resp_body=RespBody}) when is_function(RespBody) ->
  712. true;
  713. has_resp_body(#http_req{resp_body={chunked, _}}) ->
  714. true;
  715. has_resp_body(#http_req{resp_body={Length, _}}) ->
  716. Length > 0;
  717. has_resp_body(#http_req{resp_body=RespBody}) ->
  718. iolist_size(RespBody) > 0.
  719. -spec delete_resp_header(binary(), Req)
  720. -> Req when Req::req().
  721. delete_resp_header(Name, Req=#http_req{resp_headers=RespHeaders}) ->
  722. RespHeaders2 = lists:keydelete(Name, 1, RespHeaders),
  723. Req#http_req{resp_headers=RespHeaders2}.
  724. -spec reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req().
  725. reply(Status, Req=#http_req{resp_body=Body}) ->
  726. reply(Status, [], Body, Req).
  727. -spec reply(cowboy:http_status(), cowboy:http_headers(), Req)
  728. -> {ok, Req} when Req::req().
  729. reply(Status, Headers, Req=#http_req{resp_body=Body}) ->
  730. reply(Status, Headers, Body, Req).
  731. -spec reply(cowboy:http_status(), cowboy:http_headers(),
  732. iodata() | {non_neg_integer() | resp_body_fun()}, Req)
  733. -> {ok, Req} when Req::req().
  734. reply(Status, Headers, Body, Req=#http_req{
  735. socket=Socket, transport=Transport,
  736. version=Version, connection=Connection,
  737. method=Method, resp_compress=Compress,
  738. resp_state=RespState, resp_headers=RespHeaders})
  739. when RespState =:= waiting; RespState =:= waiting_stream ->
  740. HTTP11Headers = if
  741. Transport =/= cowboy_spdy, Version =:= 'HTTP/1.0', Connection =:= keepalive ->
  742. [{<<"connection">>, atom_to_connection(Connection)}];
  743. Transport =/= cowboy_spdy, Version =:= 'HTTP/1.1', Connection =:= close ->
  744. [{<<"connection">>, atom_to_connection(Connection)}];
  745. true ->
  746. []
  747. end,
  748. Req3 = case Body of
  749. BodyFun when is_function(BodyFun) ->
  750. %% We stream the response body until we close the connection.
  751. RespConn = close,
  752. {RespType, Req2} = if
  753. Transport =:= cowboy_spdy ->
  754. response(Status, Headers, RespHeaders, [
  755. {<<"date">>, cowboy_clock:rfc1123()},
  756. {<<"server">>, <<"Cowboy">>}
  757. ], stream, Req);
  758. true ->
  759. response(Status, Headers, RespHeaders, [
  760. {<<"connection">>, <<"close">>},
  761. {<<"date">>, cowboy_clock:rfc1123()},
  762. {<<"server">>, <<"Cowboy">>},
  763. {<<"transfer-encoding">>, <<"identity">>}
  764. ], <<>>, Req)
  765. end,
  766. if RespType =/= hook, Method =/= <<"HEAD">> ->
  767. BodyFun(Socket, Transport);
  768. true -> ok
  769. end,
  770. Req2#http_req{connection=RespConn};
  771. {chunked, BodyFun} ->
  772. %% We stream the response body in chunks.
  773. {RespType, Req2} = chunked_response(Status, Headers, Req),
  774. if RespType =/= hook, Method =/= <<"HEAD">> ->
  775. ChunkFun = fun(IoData) -> chunk(IoData, Req2) end,
  776. BodyFun(ChunkFun),
  777. %% Send the last chunk if chunked encoding was used.
  778. if
  779. Version =:= 'HTTP/1.0'; RespState =:= waiting_stream ->
  780. Req2;
  781. true ->
  782. last_chunk(Req2)
  783. end;
  784. true -> Req2
  785. end;
  786. {ContentLength, BodyFun} ->
  787. %% We stream the response body for ContentLength bytes.
  788. RespConn = response_connection(Headers, Connection),
  789. {RespType, Req2} = response(Status, Headers, RespHeaders, [
  790. {<<"content-length">>, integer_to_list(ContentLength)},
  791. {<<"date">>, cowboy_clock:rfc1123()},
  792. {<<"server">>, <<"Cowboy">>}
  793. |HTTP11Headers], stream, Req),
  794. if RespType =/= hook, Method =/= <<"HEAD">> ->
  795. BodyFun(Socket, Transport);
  796. true -> ok
  797. end,
  798. Req2#http_req{connection=RespConn};
  799. _ when Compress ->
  800. RespConn = response_connection(Headers, Connection),
  801. Req2 = reply_may_compress(Status, Headers, Body, Req,
  802. RespHeaders, HTTP11Headers, Method),
  803. Req2#http_req{connection=RespConn};
  804. _ ->
  805. RespConn = response_connection(Headers, Connection),
  806. Req2 = reply_no_compress(Status, Headers, Body, Req,
  807. RespHeaders, HTTP11Headers, Method, iolist_size(Body)),
  808. Req2#http_req{connection=RespConn}
  809. end,
  810. {ok, Req3#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
  811. reply_may_compress(Status, Headers, Body, Req,
  812. RespHeaders, HTTP11Headers, Method) ->
  813. BodySize = iolist_size(Body),
  814. case parse_header(<<"accept-encoding">>, Req) of
  815. {ok, Encodings, Req2} ->
  816. CanGzip = (BodySize > 300)
  817. andalso (false =:= lists:keyfind(<<"content-encoding">>,
  818. 1, Headers))
  819. andalso (false =:= lists:keyfind(<<"content-encoding">>,
  820. 1, RespHeaders))
  821. andalso (false =:= lists:keyfind(<<"transfer-encoding">>,
  822. 1, Headers))
  823. andalso (false =:= lists:keyfind(<<"transfer-encoding">>,
  824. 1, RespHeaders))
  825. andalso (Encodings =/= undefined)
  826. andalso (false =/= lists:keyfind(<<"gzip">>, 1, Encodings)),
  827. case CanGzip of
  828. true ->
  829. GzBody = zlib:gzip(Body),
  830. {_, Req3} = response(Status, Headers, RespHeaders, [
  831. {<<"content-length">>, integer_to_list(byte_size(GzBody))},
  832. {<<"content-encoding">>, <<"gzip">>},
  833. {<<"date">>, cowboy_clock:rfc1123()},
  834. {<<"server">>, <<"Cowboy">>}
  835. |HTTP11Headers],
  836. case Method of <<"HEAD">> -> <<>>; _ -> GzBody end,
  837. Req2),
  838. Req3;
  839. false ->
  840. reply_no_compress(Status, Headers, Body, Req,
  841. RespHeaders, HTTP11Headers, Method, BodySize)
  842. end;
  843. {error, badarg} ->
  844. reply_no_compress(Status, Headers, Body, Req,
  845. RespHeaders, HTTP11Headers, Method, BodySize)
  846. end.
  847. reply_no_compress(Status, Headers, Body, Req,
  848. RespHeaders, HTTP11Headers, Method, BodySize) ->
  849. {_, Req2} = response(Status, Headers, RespHeaders, [
  850. {<<"content-length">>, integer_to_list(BodySize)},
  851. {<<"date">>, cowboy_clock:rfc1123()},
  852. {<<"server">>, <<"Cowboy">>}
  853. |HTTP11Headers],
  854. case Method of <<"HEAD">> -> <<>>; _ -> Body end,
  855. Req),
  856. Req2.
  857. -spec chunked_reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req().
  858. chunked_reply(Status, Req) ->
  859. chunked_reply(Status, [], Req).
  860. -spec chunked_reply(cowboy:http_status(), cowboy:http_headers(), Req)
  861. -> {ok, Req} when Req::req().
  862. chunked_reply(Status, Headers, Req) ->
  863. {_, Req2} = chunked_response(Status, Headers, Req),
  864. {ok, Req2}.
  865. -spec chunk(iodata(), req()) -> ok | {error, atom()}.
  866. chunk(_Data, #http_req{method= <<"HEAD">>}) ->
  867. ok;
  868. chunk(Data, #http_req{socket=Socket, transport=cowboy_spdy,
  869. resp_state=chunks}) ->
  870. cowboy_spdy:stream_data(Socket, Data);
  871. chunk(Data, #http_req{socket=Socket, transport=Transport,
  872. resp_state=stream}) ->
  873. Transport:send(Socket, Data);
  874. chunk(Data, #http_req{socket=Socket, transport=Transport,
  875. resp_state=chunks}) ->
  876. Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
  877. <<"\r\n">>, Data, <<"\r\n">>]).
  878. %% If ever made public, need to send nothing if HEAD.
  879. -spec last_chunk(Req) -> Req when Req::req().
  880. last_chunk(Req=#http_req{socket=Socket, transport=cowboy_spdy}) ->
  881. _ = cowboy_spdy:stream_close(Socket),
  882. Req#http_req{resp_state=done};
  883. last_chunk(Req=#http_req{socket=Socket, transport=Transport}) ->
  884. _ = Transport:send(Socket, <<"0\r\n\r\n">>),
  885. Req#http_req{resp_state=done}.
  886. -spec upgrade_reply(cowboy:http_status(), cowboy:http_headers(), Req)
  887. -> {ok, Req} when Req::req().
  888. upgrade_reply(Status, Headers, Req=#http_req{transport=Transport,
  889. resp_state=waiting, resp_headers=RespHeaders})
  890. when Transport =/= cowboy_spdy ->
  891. {_, Req2} = response(Status, Headers, RespHeaders, [
  892. {<<"connection">>, <<"Upgrade">>}
  893. ], <<>>, Req),
  894. {ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
  895. -spec continue(req()) -> ok | {error, atom()}.
  896. continue(#http_req{socket=Socket, transport=Transport,
  897. version=Version}) ->
  898. HTTPVer = atom_to_binary(Version, latin1),
  899. Transport:send(Socket,
  900. << HTTPVer/binary, " ", (status(100))/binary, "\r\n\r\n" >>).
  901. %% Meant to be used internally for sending errors after crashes.
  902. -spec maybe_reply([{module(), atom(), arity() | [term()], _}], req()) -> ok.
  903. maybe_reply(Stacktrace, Req) ->
  904. receive
  905. {cowboy_req, resp_sent} -> ok
  906. after 0 ->
  907. _ = do_maybe_reply(Stacktrace, Req),
  908. ok
  909. end.
  910. do_maybe_reply([
  911. {cow_http_hd, _, _, _},
  912. {cowboy_req, parse_header, _, _}|_], Req) ->
  913. cowboy_req:reply(400, Req);
  914. do_maybe_reply(_, Req) ->
  915. cowboy_req:reply(500, Req).
  916. -spec ensure_response(req(), cowboy:http_status()) -> ok.
  917. %% The response has already been fully sent to the client.
  918. ensure_response(#http_req{resp_state=done}, _) ->
  919. ok;
  920. %% No response has been sent but everything apparently went fine.
  921. %% Reply with the status code found in the second argument.
  922. ensure_response(Req=#http_req{resp_state=RespState}, Status)
  923. when RespState =:= waiting; RespState =:= waiting_stream ->
  924. _ = reply(Status, [], [], Req),
  925. ok;
  926. %% Terminate the chunked body for HTTP/1.1 only.
  927. ensure_response(#http_req{method= <<"HEAD">>}, _) ->
  928. ok;
  929. ensure_response(Req=#http_req{resp_state=chunks}, _) ->
  930. _ = last_chunk(Req),
  931. ok;
  932. ensure_response(#http_req{}, _) ->
  933. ok.
  934. %% Private setter/getter API.
  935. -spec append_buffer(binary(), Req) -> Req when Req::req().
  936. append_buffer(Suffix, Req=#http_req{buffer=Buffer}) ->
  937. Req#http_req{buffer= << Buffer/binary, Suffix/binary >>}.
  938. -spec get(atom(), req()) -> any(); ([atom()], req()) -> any().
  939. get(List, Req) when is_list(List) ->
  940. [g(Atom, Req) || Atom <- List];
  941. get(Atom, Req) when is_atom(Atom) ->
  942. g(Atom, Req).
  943. g(bindings, #http_req{bindings=Ret}) -> Ret;
  944. g(body_state, #http_req{body_state=Ret}) -> Ret;
  945. g(buffer, #http_req{buffer=Ret}) -> Ret;
  946. g(connection, #http_req{connection=Ret}) -> Ret;
  947. g(cookies, #http_req{cookies=Ret}) -> Ret;
  948. g(headers, #http_req{headers=Ret}) -> Ret;
  949. g(host, #http_req{host=Ret}) -> Ret;
  950. g(host_info, #http_req{host_info=Ret}) -> Ret;
  951. g(meta, #http_req{meta=Ret}) -> Ret;
  952. g(method, #http_req{method=Ret}) -> Ret;
  953. g(multipart, #http_req{multipart=Ret}) -> Ret;
  954. g(onresponse, #http_req{onresponse=Ret}) -> Ret;
  955. g(p_headers, #http_req{p_headers=Ret}) -> Ret;
  956. g(path, #http_req{path=Ret}) -> Ret;
  957. g(path_info, #http_req{path_info=Ret}) -> Ret;
  958. g(peer, #http_req{peer=Ret}) -> Ret;
  959. g(pid, #http_req{pid=Ret}) -> Ret;
  960. g(port, #http_req{port=Ret}) -> Ret;
  961. g(qs, #http_req{qs=Ret}) -> Ret;
  962. g(qs_vals, #http_req{qs_vals=Ret}) -> Ret;
  963. g(resp_body, #http_req{resp_body=Ret}) -> Ret;
  964. g(resp_compress, #http_req{resp_compress=Ret}) -> Ret;
  965. g(resp_headers, #http_req{resp_headers=Ret}) -> Ret;
  966. g(resp_state, #http_req{resp_state=Ret}) -> Ret;
  967. g(socket, #http_req{socket=Ret}) -> Ret;
  968. g(transport, #http_req{transport=Ret}) -> Ret;
  969. g(version, #http_req{version=Ret}) -> Ret.
  970. -spec set([{atom(), any()}], Req) -> Req when Req::req().
  971. set([], Req) -> Req;
  972. set([{bindings, Val}|Tail], Req) -> set(Tail, Req#http_req{bindings=Val});
  973. set([{body_state, Val}|Tail], Req) -> set(Tail, Req#http_req{body_state=Val});
  974. set([{buffer, Val}|Tail], Req) -> set(Tail, Req#http_req{buffer=Val});
  975. set([{connection, Val}|Tail], Req) -> set(Tail, Req#http_req{connection=Val});
  976. set([{cookies, Val}|Tail], Req) -> set(Tail, Req#http_req{cookies=Val});
  977. set([{headers, Val}|Tail], Req) -> set(Tail, Req#http_req{headers=Val});
  978. set([{host, Val}|Tail], Req) -> set(Tail, Req#http_req{host=Val});
  979. set([{host_info, Val}|Tail], Req) -> set(Tail, Req#http_req{host_info=Val});
  980. set([{meta, Val}|Tail], Req) -> set(Tail, Req#http_req{meta=Val});
  981. set([{method, Val}|Tail], Req) -> set(Tail, Req#http_req{method=Val});
  982. set([{multipart, Val}|Tail], Req) -> set(Tail, Req#http_req{multipart=Val});
  983. set([{onresponse, Val}|Tail], Req) -> set(Tail, Req#http_req{onresponse=Val});
  984. set([{p_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{p_headers=Val});
  985. set([{path, Val}|Tail], Req) -> set(Tail, Req#http_req{path=Val});
  986. set([{path_info, Val}|Tail], Req) -> set(Tail, Req#http_req{path_info=Val});
  987. set([{peer, Val}|Tail], Req) -> set(Tail, Req#http_req{peer=Val});
  988. set([{pid, Val}|Tail], Req) -> set(Tail, Req#http_req{pid=Val});
  989. set([{port, Val}|Tail], Req) -> set(Tail, Req#http_req{port=Val});
  990. set([{qs, Val}|Tail], Req) -> set(Tail, Req#http_req{qs=Val});
  991. set([{qs_vals, Val}|Tail], Req) -> set(Tail, Req#http_req{qs_vals=Val});
  992. set([{resp_body, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_body=Val});
  993. set([{resp_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_headers=Val});
  994. set([{resp_state, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_state=Val});
  995. set([{socket, Val}|Tail], Req) -> set(Tail, Req#http_req{socket=Val});
  996. set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val});
  997. set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}).
  998. -spec set_bindings(cowboy_router:tokens(), cowboy_router:tokens(),
  999. cowboy_router:bindings(), Req) -> Req when Req::req().
  1000. set_bindings(HostInfo, PathInfo, Bindings, Req) ->
  1001. Req#http_req{host_info=HostInfo, path_info=PathInfo,
  1002. bindings=Bindings}.
  1003. %% Misc API.
  1004. -spec compact(Req) -> Req when Req::req().
  1005. compact(Req) ->
  1006. Req#http_req{host_info=undefined,
  1007. path_info=undefined, qs_vals=undefined,
  1008. bindings=undefined, headers=[],
  1009. p_headers=[], cookies=[]}.
  1010. -spec lock(Req) -> Req when Req::req().
  1011. lock(Req) ->
  1012. Req#http_req{resp_state=locked}.
  1013. -spec to_list(req()) -> [{atom(), any()}].
  1014. to_list(Req) ->
  1015. lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))).
  1016. %% Internal.
  1017. -spec chunked_response(cowboy:http_status(), cowboy:http_headers(), Req) ->
  1018. {normal | hook, Req} when Req::req().
  1019. chunked_response(Status, Headers, Req=#http_req{
  1020. transport=cowboy_spdy, resp_state=waiting,
  1021. resp_headers=RespHeaders}) ->
  1022. {RespType, Req2} = response(Status, Headers, RespHeaders, [
  1023. {<<"date">>, cowboy_clock:rfc1123()},
  1024. {<<"server">>, <<"Cowboy">>}
  1025. ], stream, Req),
  1026. {RespType, Req2#http_req{resp_state=chunks,
  1027. resp_headers=[], resp_body= <<>>}};
  1028. chunked_response(Status, Headers, Req=#http_req{
  1029. version=Version, connection=Connection,
  1030. resp_state=RespState, resp_headers=RespHeaders})
  1031. when RespState =:= waiting; RespState =:= waiting_stream ->
  1032. RespConn = response_connection(Headers, Connection),
  1033. HTTP11Headers = if
  1034. Version =:= 'HTTP/1.0', Connection =:= keepalive ->
  1035. [{<<"connection">>, atom_to_connection(Connection)}];
  1036. Version =:= 'HTTP/1.0' -> [];
  1037. true ->
  1038. MaybeTE = if
  1039. RespState =:= waiting_stream -> [];
  1040. true -> [{<<"transfer-encoding">>, <<"chunked">>}]
  1041. end,
  1042. if
  1043. Connection =:= close ->
  1044. [{<<"connection">>, atom_to_connection(Connection)}|MaybeTE];
  1045. true ->
  1046. MaybeTE
  1047. end
  1048. end,
  1049. RespState2 = if
  1050. Version =:= 'HTTP/1.1', RespState =:= 'waiting' -> chunks;
  1051. true -> stream
  1052. end,
  1053. {RespType, Req2} = response(Status, Headers, RespHeaders, [
  1054. {<<"date">>, cowboy_clock:rfc1123()},
  1055. {<<"server">>, <<"Cowboy">>}
  1056. |HTTP11Headers], <<>>, Req),
  1057. {RespType, Req2#http_req{connection=RespConn, resp_state=RespState2,
  1058. resp_headers=[], resp_body= <<>>}}.
  1059. -spec response(cowboy:http_status(), cowboy:http_headers(),
  1060. cowboy:http_headers(), cowboy:http_headers(), stream | iodata(), Req)
  1061. -> {normal | hook, Req} when Req::req().
  1062. response(Status, Headers, RespHeaders, DefaultHeaders, Body, Req=#http_req{
  1063. socket=Socket, transport=Transport, version=Version,
  1064. pid=ReqPid, onresponse=OnResponse}) ->
  1065. FullHeaders = case OnResponse of
  1066. already_called -> Headers;
  1067. _ -> response_merge_headers(Headers, RespHeaders, DefaultHeaders)
  1068. end,
  1069. Body2 = case Body of stream -> <<>>; _ -> Body end,
  1070. {Status2, FullHeaders2, Req2} = case OnResponse of
  1071. already_called -> {Status, FullHeaders, Req};
  1072. undefined -> {Status, FullHeaders, Req};
  1073. OnResponse ->
  1074. case OnResponse(Status, FullHeaders, Body2,
  1075. %% Don't call 'onresponse' from the hook itself.
  1076. Req#http_req{resp_headers=[], resp_body= <<>>,
  1077. onresponse=already_called}) of
  1078. StHdReq = {_, _, _} ->
  1079. StHdReq;
  1080. Req1 ->
  1081. {Status, FullHeaders, Req1}
  1082. end
  1083. end,
  1084. ReplyType = case Req2#http_req.resp_state of
  1085. waiting when Transport =:= cowboy_spdy, Body =:= stream ->
  1086. cowboy_spdy:stream_reply(Socket, status(Status2), FullHeaders2),
  1087. ReqPid ! {?MODULE, resp_sent},
  1088. normal;
  1089. waiting when Transport =:= cowboy_spdy ->
  1090. cowboy_spdy:reply(Socket, status(Status2), FullHeaders2, Body),
  1091. ReqPid ! {?MODULE, resp_sent},
  1092. normal;
  1093. RespState when RespState =:= waiting; RespState =:= waiting_stream ->
  1094. HTTPVer = atom_to_binary(Version, latin1),
  1095. StatusLine = << HTTPVer/binary, " ",
  1096. (status(Status2))/binary, "\r\n" >>,
  1097. HeaderLines = [[Key, <<": ">>, Value, <<"\r\n">>]
  1098. || {Key, Value} <- FullHeaders2],
  1099. Transport:send(Socket, [StatusLine, HeaderLines, <<"\r\n">>, Body2]),
  1100. ReqPid ! {?MODULE, resp_sent},
  1101. normal;
  1102. _ ->
  1103. hook
  1104. end,
  1105. {ReplyType, Req2}.
  1106. -spec response_connection(cowboy:http_headers(), keepalive | close)
  1107. -> keepalive | close.
  1108. response_connection([], Connection) ->
  1109. Connection;
  1110. response_connection([{Name, Value}|Tail], Connection) ->
  1111. case Name of
  1112. <<"connection">> ->
  1113. Tokens = cow_http_hd:parse_connection(Value),
  1114. connection_to_atom(Tokens);
  1115. _ ->
  1116. response_connection(Tail, Connection)
  1117. end.
  1118. -spec response_merge_headers(cowboy:http_headers(), cowboy:http_headers(),
  1119. cowboy:http_headers()) -> cowboy:http_headers().
  1120. response_merge_headers(Headers, RespHeaders, DefaultHeaders) ->
  1121. Headers2 = [{Key, Value} || {Key, Value} <- Headers],
  1122. merge_headers(
  1123. merge_headers(Headers2, RespHeaders),
  1124. DefaultHeaders).
  1125. -spec merge_headers(cowboy:http_headers(), cowboy:http_headers())
  1126. -> cowboy:http_headers().
  1127. %% Merge headers by prepending the tuples in the second list to the
  1128. %% first list. It also handles Set-Cookie properly, which supports
  1129. %% duplicated entries. Notice that, while the RFC2109 does allow more
  1130. %% than one cookie to be set per Set-Cookie header, we are following
  1131. %% the implementation of common web servers and applications which
  1132. %% return many distinct headers per each Set-Cookie entry to avoid
  1133. %% issues with clients/browser which may not support it.
  1134. merge_headers(Headers, []) ->
  1135. Headers;
  1136. merge_headers(Headers, [{<<"set-cookie">>, Value}|Tail]) ->
  1137. merge_headers([{<<"set-cookie">>, Value}|Headers], Tail);
  1138. merge_headers(Headers, [{Name, Value}|Tail]) ->
  1139. Headers2 = case lists:keymember(Name, 1, Headers) of
  1140. true -> Headers;
  1141. false -> [{Name, Value}|Headers]
  1142. end,
  1143. merge_headers(Headers2, Tail).
  1144. -spec atom_to_connection(keepalive) -> <<_:80>>;
  1145. (close) -> <<_:40>>.
  1146. atom_to_connection(keepalive) ->
  1147. <<"keep-alive">>;
  1148. atom_to_connection(close) ->
  1149. <<"close">>.
  1150. %% We don't match on "keep-alive" since it is the default value.
  1151. -spec connection_to_atom([binary()]) -> keepalive | close.
  1152. connection_to_atom([]) ->
  1153. keepalive;
  1154. connection_to_atom([<<"close">>|_]) ->
  1155. close;
  1156. connection_to_atom([_|Tail]) ->
  1157. connection_to_atom(Tail).
  1158. -spec status(cowboy:http_status()) -> binary().
  1159. status(100) -> <<"100 Continue">>;
  1160. status(101) -> <<"101 Switching Protocols">>;
  1161. status(102) -> <<"102 Processing">>;
  1162. status(200) -> <<"200 OK">>;
  1163. status(201) -> <<"201 Created">>;
  1164. status(202) -> <<"202 Accepted">>;
  1165. status(203) -> <<"203 Non-Authoritative Information">>;
  1166. status(204) -> <<"204 No Content">>;
  1167. status(205) -> <<"205 Reset Content">>;
  1168. status(206) -> <<"206 Partial Content">>;
  1169. status(207) -> <<"207 Multi-Status">>;
  1170. status(226) -> <<"226 IM Used">>;
  1171. status(300) -> <<"300 Multiple Choices">>;
  1172. status(301) -> <<"301 Moved Permanently">>;
  1173. status(302) -> <<"302 Found">>;
  1174. status(303) -> <<"303 See Other">>;
  1175. status(304) -> <<"304 Not Modified">>;
  1176. status(305) -> <<"305 Use Proxy">>;
  1177. status(306) -> <<"306 Switch Proxy">>;
  1178. status(307) -> <<"307 Temporary Redirect">>;
  1179. status(400) -> <<"400 Bad Request">>;
  1180. status(401) -> <<"401 Unauthorized">>;
  1181. status(402) -> <<"402 Payment Required">>;
  1182. status(403) -> <<"403 Forbidden">>;
  1183. status(404) -> <<"404 Not Found">>;
  1184. status(405) -> <<"405 Method Not Allowed">>;
  1185. status(406) -> <<"406 Not Acceptable">>;
  1186. status(407) -> <<"407 Proxy Authentication Required">>;
  1187. status(408) -> <<"408 Request Timeout">>;
  1188. status(409) -> <<"409 Conflict">>;
  1189. status(410) -> <<"410 Gone">>;
  1190. status(411) -> <<"411 Length Required">>;
  1191. status(412) -> <<"412 Precondition Failed">>;
  1192. status(413) -> <<"413 Request Entity Too Large">>;
  1193. status(414) -> <<"414 Request-URI Too Long">>;
  1194. status(415) -> <<"415 Unsupported Media Type">>;
  1195. status(416) -> <<"416 Requested Range Not Satisfiable">>;
  1196. status(417) -> <<"417 Expectation Failed">>;
  1197. status(418) -> <<"418 I'm a teapot">>;
  1198. status(422) -> <<"422 Unprocessable Entity">>;
  1199. status(423) -> <<"423 Locked">>;
  1200. status(424) -> <<"424 Failed Dependency">>;
  1201. status(425) -> <<"425 Unordered Collection">>;
  1202. status(426) -> <<"426 Upgrade Required">>;
  1203. status(428) -> <<"428 Precondition Required">>;
  1204. status(429) -> <<"429 Too Many Requests">>;
  1205. status(431) -> <<"431 Request Header Fields Too Large">>;
  1206. status(500) -> <<"500 Internal Server Error">>;
  1207. status(501) -> <<"501 Not Implemented">>;
  1208. status(502) -> <<"502 Bad Gateway">>;
  1209. status(503) -> <<"503 Service Unavailable">>;
  1210. status(504) -> <<"504 Gateway Timeout">>;
  1211. status(505) -> <<"505 HTTP Version Not Supported">>;
  1212. status(506) -> <<"506 Variant Also Negotiates">>;
  1213. status(507) -> <<"507 Insufficient Storage">>;
  1214. status(510) -> <<"510 Not Extended">>;
  1215. status(511) -> <<"511 Network Authentication Required">>;
  1216. status(B) when is_binary(B) -> B.
  1217. %% Tests.
  1218. -ifdef(TEST).
  1219. url_test() ->
  1220. {undefined, _} =
  1221. url(#http_req{transport=ranch_tcp, host= <<>>, port= undefined,
  1222. path= <<>>, qs= <<>>, pid=self()}),
  1223. {<<"http://localhost/path">>, _ } =
  1224. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=80,
  1225. path= <<"/path">>, qs= <<>>, pid=self()}),
  1226. {<<"http://localhost:443/path">>, _} =
  1227. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=443,
  1228. path= <<"/path">>, qs= <<>>, pid=self()}),
  1229. {<<"http://localhost:8080/path">>, _} =
  1230. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=8080,
  1231. path= <<"/path">>, qs= <<>>, pid=self()}),
  1232. {<<"http://localhost:8080/path?dummy=2785">>, _} =
  1233. url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=8080,
  1234. path= <<"/path">>, qs= <<"dummy=2785">>, pid=self()}),
  1235. {<<"https://localhost/path">>, _} =
  1236. url(#http_req{transport=ranch_ssl, host= <<"localhost">>, port=443,
  1237. path= <<"/path">>, qs= <<>>, pid=self()}),
  1238. {<<"https://localhost:8443/path">>, _} =
  1239. url(#http_req{transport=ranch_ssl, host= <<"localhost">>, port=8443,
  1240. path= <<"/path">>, qs= <<>>, pid=self()}),
  1241. {<<"https://localhost:8443/path?dummy=2785">>, _} =
  1242. url(#http_req{transport=ranch_ssl, host= <<"localhost">>, port=8443,
  1243. path= <<"/path">>, qs= <<"dummy=2785">>, pid=self()}),
  1244. ok.
  1245. connection_to_atom_test_() ->
  1246. Tests = [
  1247. {[<<"close">>], close},
  1248. {[<<"keep-alive">>], keepalive},
  1249. {[<<"keep-alive">>, <<"upgrade">>], keepalive}
  1250. ],
  1251. [{lists:flatten(io_lib:format("~p", [T])),
  1252. fun() -> R = connection_to_atom(T) end} || {T, R} <- Tests].
  1253. merge_headers_test_() ->
  1254. Tests = [
  1255. {[{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
  1256. [{<<"set-cookie">>,<<"foo=bar">>},{<<"content-length">>,<<"11">>}],
  1257. [{<<"set-cookie">>,<<"foo=bar">>},
  1258. {<<"content-length">>,<<"13">>},
  1259. {<<"server">>,<<"Cowboy">>}]},
  1260. {[{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
  1261. [{<<"set-cookie">>,<<"foo=bar">>},{<<"set-cookie">>,<<"bar=baz">>}],
  1262. [{<<"set-cookie">>,<<"bar=baz">>},
  1263. {<<"set-cookie">>,<<"foo=bar">>},
  1264. {<<"content-length">>,<<"13">>},
  1265. {<<"server">>,<<"Cowboy">>}]}
  1266. ],
  1267. [fun() -> Res = merge_headers(L,R) end || {L, R, Res} <- Tests].
  1268. -endif.