diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cowboy_http.erl | 37 | ||||
-rw-r--r-- | src/cowboy_rest.erl | 19 |
2 files changed, 37 insertions, 19 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 67d3f70..a78e090 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -802,6 +802,23 @@ qvalue(<< C, Rest/binary >>, Fun, Q, M) qvalue(Data, Fun, Q, _M) -> Fun(Data, Q). +%% @doc Parse authorization value according rfc 2617. +%% Only Basic authorization is supported so far. +-spec authorization(binary(), binary()) -> {binary(), any()} | {error, badarg}. +authorization(UserPass, Type = <<"basic">>) -> + cowboy_http:whitespace(UserPass, + fun(D) -> + authorization_basic_userid(base64:mime_decode(D), + fun(Rest, Userid) -> + authorization_basic_password(Rest, + fun(Password) -> + {Type, {Userid, Password}} + end) + end) + end); +authorization(String, Type) -> + {Type, String}. + %% @doc Parse user credentials. -spec authorization_basic_userid(binary(), fun()) -> any(). authorization_basic_userid(Data, Fun) -> @@ -831,23 +848,6 @@ authorization_basic_password(<<>>, Fun, Acc) -> authorization_basic_password(<<C, Rest/binary>>, Fun, Acc) -> authorization_basic_password(Rest, Fun, <<Acc/binary, C>>). -%% @doc Parse authorization value according rfc 2617. -%% Only Basic authorization is supported so far. --spec authorization(binary(), binary()) -> {binary(), any()} | {error, badarg}. -authorization(UserPass, Type = <<"basic">>) -> - cowboy_http:whitespace(UserPass, - fun(D) -> - authorization_basic_userid(base64:mime_decode(D), - fun(Rest, Userid) -> - authorization_basic_password(Rest, - fun(Password) -> - {Type, {Userid, Password}} - end) - end) - end); -authorization(String, Type) -> - {Type, String}. - %% Decoding. %% @doc Decode a stream of chunks. @@ -1350,7 +1350,6 @@ http_authorization_test_() -> authorization(<<"_[]@#$%^&*()-AA==">>, <<"basic">>)), ?_assertEqual({error, badarg}, authorization(<<"dXNlcjpwYXNzCA==">>, <<"basic">>)) %% user:pass\010 - ]. - + ]. -endif. diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index fb7f2e1..8967b1d 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -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) -> |