mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add cowboy_req:get/2 and :set/2 private functions
They should not be used unless you have a very special need, which generally involves interacting with the internals of Cowboy.
This commit is contained in:
parent
0b82eebed6
commit
c326a195e0
3 changed files with 78 additions and 28 deletions
|
@ -437,7 +437,7 @@ onrequest(Req, State=#state{onrequest=undefined}, Host, Path) ->
|
||||||
dispatch(Req, State, Host, Path);
|
dispatch(Req, State, Host, Path);
|
||||||
onrequest(Req, State=#state{onrequest=OnRequest}, Host, Path) ->
|
onrequest(Req, State=#state{onrequest=OnRequest}, Host, Path) ->
|
||||||
Req2 = OnRequest(Req),
|
Req2 = OnRequest(Req),
|
||||||
case cowboy_req:get_resp_state(Req2) of
|
case cowboy_req:get(resp_state, Req2) of
|
||||||
waiting -> dispatch(Req2, State, Host, Path);
|
waiting -> dispatch(Req2, State, Host, Path);
|
||||||
_ -> next_request(Req2, State, ok)
|
_ -> next_request(Req2, State, ok)
|
||||||
end.
|
end.
|
||||||
|
@ -597,13 +597,13 @@ terminate_request(Req, State, Handler, HandlerState) ->
|
||||||
-spec next_request(cowboy_req:req(), #state{}, any()) -> ok.
|
-spec next_request(cowboy_req:req(), #state{}, any()) -> ok.
|
||||||
next_request(Req, State=#state{req_keepalive=Keepalive}, HandlerRes) ->
|
next_request(Req, State=#state{req_keepalive=Keepalive}, HandlerRes) ->
|
||||||
cowboy_req:ensure_response(Req, 204),
|
cowboy_req:ensure_response(Req, 204),
|
||||||
{BodyRes, Buffer} = case cowboy_req:skip_body(Req) of
|
{BodyRes, [Buffer, Connection]} = case cowboy_req:skip_body(Req) of
|
||||||
{ok, Req2} -> {ok, cowboy_req:get_buffer(Req2)};
|
{ok, Req2} -> {ok, cowboy_req:get([buffer, connection], Req2)};
|
||||||
{error, _} -> {close, <<>>}
|
{error, _} -> {close, [<<>>, close]}
|
||||||
end,
|
end,
|
||||||
%% Flush the resp_sent message before moving on.
|
%% Flush the resp_sent message before moving on.
|
||||||
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
|
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
|
||||||
case {HandlerRes, BodyRes, cowboy_req:get_connection(Req)} of
|
case {HandlerRes, BodyRes, Connection} of
|
||||||
{ok, ok, keepalive} ->
|
{ok, ok, keepalive} ->
|
||||||
?MODULE:parse_request(Buffer, State#state{
|
?MODULE:parse_request(Buffer, State#state{
|
||||||
req_keepalive=Keepalive + 1}, 0);
|
req_keepalive=Keepalive + 1}, 0);
|
||||||
|
|
|
@ -104,10 +104,9 @@
|
||||||
-export([ensure_response/2]).
|
-export([ensure_response/2]).
|
||||||
|
|
||||||
%% Private setter/getter API.
|
%% Private setter/getter API.
|
||||||
|
-export([get/2]).
|
||||||
|
-export([set/2]).
|
||||||
-export([set_bindings/4]).
|
-export([set_bindings/4]).
|
||||||
-export([get_resp_state/1]).
|
|
||||||
-export([get_buffer/1]).
|
|
||||||
-export([get_connection/1]).
|
|
||||||
|
|
||||||
%% Misc API.
|
%% Misc API.
|
||||||
-export([compact/1]).
|
-export([compact/1]).
|
||||||
|
@ -992,6 +991,72 @@ ensure_response(#http_req{socket=Socket, transport=Transport,
|
||||||
|
|
||||||
%% Private setter/getter API.
|
%% Private setter/getter API.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
-spec get(atom(), req()) -> any(); ([atom()], req()) -> any().
|
||||||
|
get(List, Req) when is_list(List) ->
|
||||||
|
[g(Atom, Req) || Atom <- List];
|
||||||
|
get(Atom, Req) when is_atom(Atom) ->
|
||||||
|
g(Atom, Req).
|
||||||
|
|
||||||
|
g(bindings, #http_req{bindings=Ret}) -> Ret;
|
||||||
|
g(body_state, #http_req{body_state=Ret}) -> Ret;
|
||||||
|
g(buffer, #http_req{buffer=Ret}) -> Ret;
|
||||||
|
g(connection, #http_req{connection=Ret}) -> Ret;
|
||||||
|
g(cookies, #http_req{cookies=Ret}) -> Ret;
|
||||||
|
g(fragment, #http_req{fragment=Ret}) -> Ret;
|
||||||
|
g(headers, #http_req{headers=Ret}) -> Ret;
|
||||||
|
g(host, #http_req{host=Ret}) -> Ret;
|
||||||
|
g(host_info, #http_req{host_info=Ret}) -> Ret;
|
||||||
|
g(meta, #http_req{meta=Ret}) -> Ret;
|
||||||
|
g(method, #http_req{method=Ret}) -> Ret;
|
||||||
|
g(multipart, #http_req{multipart=Ret}) -> Ret;
|
||||||
|
g(onresponse, #http_req{onresponse=Ret}) -> Ret;
|
||||||
|
g(p_headers, #http_req{p_headers=Ret}) -> Ret;
|
||||||
|
g(path, #http_req{path=Ret}) -> Ret;
|
||||||
|
g(path_info, #http_req{path_info=Ret}) -> Ret;
|
||||||
|
g(peer, #http_req{peer=Ret}) -> Ret;
|
||||||
|
g(pid, #http_req{pid=Ret}) -> Ret;
|
||||||
|
g(port, #http_req{port=Ret}) -> Ret;
|
||||||
|
g(qs, #http_req{qs=Ret}) -> Ret;
|
||||||
|
g(qs_vals, #http_req{qs_vals=Ret}) -> Ret;
|
||||||
|
g(resp_body, #http_req{resp_body=Ret}) -> Ret;
|
||||||
|
g(resp_headers, #http_req{resp_headers=Ret}) -> Ret;
|
||||||
|
g(resp_state, #http_req{resp_state=Ret}) -> Ret;
|
||||||
|
g(socket, #http_req{socket=Ret}) -> Ret;
|
||||||
|
g(transport, #http_req{transport=Ret}) -> Ret;
|
||||||
|
g(version, #http_req{version=Ret}) -> Ret.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
-spec set([{atom(), any()}], Req) -> Req when Req::req().
|
||||||
|
set([], Req) -> Req;
|
||||||
|
set([{bindings, Val}|Tail], Req) -> set(Tail, Req#http_req{bindings=Val});
|
||||||
|
set([{body_state, Val}|Tail], Req) -> set(Tail, Req#http_req{body_state=Val});
|
||||||
|
set([{buffer, Val}|Tail], Req) -> set(Tail, Req#http_req{buffer=Val});
|
||||||
|
set([{connection, Val}|Tail], Req) -> set(Tail, Req#http_req{connection=Val});
|
||||||
|
set([{cookies, Val}|Tail], Req) -> set(Tail, Req#http_req{cookies=Val});
|
||||||
|
set([{fragment, Val}|Tail], Req) -> set(Tail, Req#http_req{fragment=Val});
|
||||||
|
set([{headers, Val}|Tail], Req) -> set(Tail, Req#http_req{headers=Val});
|
||||||
|
set([{host, Val}|Tail], Req) -> set(Tail, Req#http_req{host=Val});
|
||||||
|
set([{host_info, Val}|Tail], Req) -> set(Tail, Req#http_req{host_info=Val});
|
||||||
|
set([{meta, Val}|Tail], Req) -> set(Tail, Req#http_req{meta=Val});
|
||||||
|
set([{method, Val}|Tail], Req) -> set(Tail, Req#http_req{method=Val});
|
||||||
|
set([{multipart, Val}|Tail], Req) -> set(Tail, Req#http_req{multipart=Val});
|
||||||
|
set([{onresponse, Val}|Tail], Req) -> set(Tail, Req#http_req{onresponse=Val});
|
||||||
|
set([{p_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{p_headers=Val});
|
||||||
|
set([{path, Val}|Tail], Req) -> set(Tail, Req#http_req{path=Val});
|
||||||
|
set([{path_info, Val}|Tail], Req) -> set(Tail, Req#http_req{path_info=Val});
|
||||||
|
set([{peer, Val}|Tail], Req) -> set(Tail, Req#http_req{peer=Val});
|
||||||
|
set([{pid, Val}|Tail], Req) -> set(Tail, Req#http_req{pid=Val});
|
||||||
|
set([{port, Val}|Tail], Req) -> set(Tail, Req#http_req{port=Val});
|
||||||
|
set([{qs, Val}|Tail], Req) -> set(Tail, Req#http_req{qs=Val});
|
||||||
|
set([{qs_vals, Val}|Tail], Req) -> set(Tail, Req#http_req{qs_vals=Val});
|
||||||
|
set([{resp_body, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_body=Val});
|
||||||
|
set([{resp_headers, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_headers=Val});
|
||||||
|
set([{resp_state, Val}|Tail], Req) -> set(Tail, Req#http_req{resp_state=Val});
|
||||||
|
set([{socket, Val}|Tail], Req) -> set(Tail, Req#http_req{socket=Val});
|
||||||
|
set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val});
|
||||||
|
set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}).
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
-spec set_bindings(cowboy_dispatcher:tokens(), cowboy_dispatcher:tokens(),
|
-spec set_bindings(cowboy_dispatcher:tokens(), cowboy_dispatcher:tokens(),
|
||||||
cowboy_dispatcher:bindings(), Req) -> Req when Req::req().
|
cowboy_dispatcher:bindings(), Req) -> Req when Req::req().
|
||||||
|
@ -999,21 +1064,6 @@ set_bindings(HostInfo, PathInfo, Bindings, Req) ->
|
||||||
Req#http_req{host_info=HostInfo, path_info=PathInfo,
|
Req#http_req{host_info=HostInfo, path_info=PathInfo,
|
||||||
bindings=Bindings}.
|
bindings=Bindings}.
|
||||||
|
|
||||||
%% @private
|
|
||||||
-spec get_resp_state(req()) -> locked | waiting | chunks | done.
|
|
||||||
get_resp_state(#http_req{resp_state=RespState}) ->
|
|
||||||
RespState.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
-spec get_buffer(req()) -> binary().
|
|
||||||
get_buffer(#http_req{buffer=Buffer}) ->
|
|
||||||
Buffer.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
-spec get_connection(req()) -> keepalive | close.
|
|
||||||
get_connection(#http_req{connection=Connection}) ->
|
|
||||||
Connection.
|
|
||||||
|
|
||||||
%% Misc API.
|
%% Misc API.
|
||||||
|
|
||||||
%% @doc Compact the request data by removing all non-system information.
|
%% @doc Compact the request data by removing all non-system information.
|
||||||
|
|
|
@ -58,16 +58,16 @@
|
||||||
-> {ok, Req} | close when Req::cowboy_req:req().
|
-> {ok, Req} | close when Req::cowboy_req:req().
|
||||||
upgrade(_ListenerPid, Handler, Opts, Req) ->
|
upgrade(_ListenerPid, Handler, Opts, Req) ->
|
||||||
try
|
try
|
||||||
{Method, Req1} = cowboy_req:method(Req),
|
Method = cowboy_req:get(method, Req),
|
||||||
case erlang:function_exported(Handler, rest_init, 2) of
|
case erlang:function_exported(Handler, rest_init, 2) of
|
||||||
true ->
|
true ->
|
||||||
case Handler:rest_init(Req1, Opts) of
|
case Handler:rest_init(Req, Opts) of
|
||||||
{ok, Req2, HandlerState} ->
|
{ok, Req2, HandlerState} ->
|
||||||
service_available(Req2, #state{method=Method,
|
service_available(Req2, #state{method=Method,
|
||||||
handler=Handler, handler_state=HandlerState})
|
handler=Handler, handler_state=HandlerState})
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
service_available(Req1, #state{method=Method,
|
service_available(Req, #state{method=Method,
|
||||||
handler=Handler})
|
handler=Handler})
|
||||||
end
|
end
|
||||||
catch Class:Reason ->
|
catch Class:Reason ->
|
||||||
|
@ -693,8 +693,8 @@ is_conflict(Req, State) ->
|
||||||
expect(Req, State, is_conflict, false, fun put_resource/2, 409).
|
expect(Req, State, is_conflict, false, fun put_resource/2, 409).
|
||||||
|
|
||||||
put_resource(Req, State) ->
|
put_resource(Req, State) ->
|
||||||
{Path, Req2} = cowboy_req:path(Req),
|
Path = cowboy_req:get(path, Req),
|
||||||
put_resource(cowboy_req:set_meta(put_path, Path, Req2),
|
put_resource(cowboy_req:set_meta(put_path, Path, Req),
|
||||||
State, fun is_new_resource/2).
|
State, fun is_new_resource/2).
|
||||||
|
|
||||||
%% content_types_accepted should return a list of media types and their
|
%% content_types_accepted should return a list of media types and their
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue