0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 20:50:24 +00:00

allow POST rest handling to specify path after accepting content

This commit is contained in:
Tom Burdick 2012-10-23 14:37:46 -05:00
parent a59c5d6e91
commit 8a798014e9
3 changed files with 69 additions and 0 deletions

View file

@ -670,6 +670,8 @@ post_is_create(Req, State) ->
%% (including the leading /).
create_path(Req, State) ->
case call(Req, State, create_path) of
no_call ->
put_resource(Req, State, fun created_path/2);
{halt, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState});
{Path, Req2, HandlerState} ->
@ -681,6 +683,23 @@ create_path(Req, State) ->
State2, 303)
end.
%% Called after content_types_accepted is called for POST methods
%% when create_path did not exist. Expects the full path to
%% be returned and MUST exist in the case that create_path
%% does not.
created_path(Req, State) ->
case call(Req, State, created_path) of
{halt, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState});
{Path, Req2, HandlerState} ->
{HostURL, Req3} = cowboy_req:host_url(Req2),
State2 = State#state{handler_state=HandlerState},
Req4 = cowboy_req:set_resp_header(
<<"Location">>, << HostURL/binary, Path/binary >>, Req3),
respond(cowboy_req:set_meta(put_path, Path, Req4),
State2, 303)
end.
%% process_post should return true when the POST body could be processed
%% and false when it hasn't, in which case a 500 error is sent.
process_post(Req, State) ->