diff options
Diffstat (limited to 'src/cowboy_rest.erl')
-rw-r--r-- | src/cowboy_rest.erl | 117 |
1 files changed, 55 insertions, 62 deletions
diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index f779612..4ea3010 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -58,7 +58,7 @@ -spec upgrade(Req, Env, module(), any()) -> {ok, Req, Env} when Req::cowboy_req:req(), Env::cowboy_middleware:env(). upgrade(Req, Env, Handler, HandlerOpts) -> - Method = cowboy_req:get(method, Req), + Method = cowboy_req:method(Req), case erlang:function_exported(Handler, rest_init, 2) of true -> try Handler:rest_init(Req, HandlerOpts) of @@ -215,16 +215,15 @@ content_types_provided(Req, State) -> no_call -> State2 = State#state{ content_types_p=[{{<<"text">>, <<"html">>, '*'}, to_html}]}, - case cowboy_req:parse_header(<<"accept">>, Req) of - {error, badarg} -> - respond(Req, State2, 400); - {ok, undefined, Req2} -> + try cowboy_req:parse_header(<<"accept">>, Req) of + undefined -> languages_provided( - cowboy_req:set_meta(media_type, {<<"text">>, <<"html">>, []}, Req2), + cowboy_req:set_meta(media_type, {<<"text">>, <<"html">>, []}, Req), State2#state{content_type_a={{<<"text">>, <<"html">>, []}, to_html}}); - {ok, Accept, Req2} -> - Accept2 = prioritize_accept(Accept), - choose_media_type(Req2, State2, Accept2) + Accept -> + choose_media_type(Req, State2, prioritize_accept(Accept)) + catch _:_ -> + respond(Req, State2, 400) end; {halt, Req2, HandlerState} -> terminate(Req2, State#state{handler_state=HandlerState}); @@ -234,17 +233,16 @@ content_types_provided(Req, State) -> CTP2 = [normalize_content_types(P) || P <- CTP], State2 = State#state{ handler_state=HandlerState, content_types_p=CTP2}, - case cowboy_req:parse_header(<<"accept">>, Req2) of - {error, badarg} -> - respond(Req2, State2, 400); - {ok, undefined, Req3} -> + try cowboy_req:parse_header(<<"accept">>, Req2) of + undefined -> {PMT, _Fun} = HeadCTP = hd(CTP2), languages_provided( - cowboy_req:set_meta(media_type, PMT, Req3), + cowboy_req:set_meta(media_type, PMT, Req2), State2#state{content_type_a=HeadCTP}); - {ok, Accept, Req3} -> - Accept2 = prioritize_accept(Accept), - choose_media_type(Req3, State2, Accept2) + Accept -> + choose_media_type(Req2, State2, prioritize_accept(Accept)) + catch _:_ -> + respond(Req2, State2, 400) end end. @@ -335,14 +333,12 @@ languages_provided(Req, State) -> not_acceptable(Req2, State#state{handler_state=HandlerState}); {LP, Req2, HandlerState} -> State2 = State#state{handler_state=HandlerState, languages_p=LP}, - {ok, AcceptLanguage, Req3} = - cowboy_req:parse_header(<<"accept-language">>, Req2), - case AcceptLanguage of + case cowboy_req:parse_header(<<"accept-language">>, Req2) of undefined -> - set_language(Req3, State2#state{language_a=hd(LP)}); + set_language(Req2, State2#state{language_a=hd(LP)}); AcceptLanguage -> AcceptLanguage2 = prioritize_languages(AcceptLanguage), - choose_language(Req3, State2, AcceptLanguage2) + choose_language(Req2, State2, AcceptLanguage2) end end. @@ -397,14 +393,12 @@ charsets_provided(Req, State) -> not_acceptable(Req2, State#state{handler_state=HandlerState}); {CP, Req2, HandlerState} -> State2 = State#state{handler_state=HandlerState, charsets_p=CP}, - {ok, AcceptCharset, Req3} = - cowboy_req:parse_header(<<"accept-charset">>, Req2), - case AcceptCharset of + case cowboy_req:parse_header(<<"accept-charset">>, Req2) of undefined -> - set_content_type(Req3, State2#state{charset_a=hd(CP)}); + set_content_type(Req2, State2#state{charset_a=hd(CP)}); AcceptCharset -> AcceptCharset2 = prioritize_charsets(AcceptCharset), - choose_charset(Req3, State2, AcceptCharset2) + choose_charset(Req2, State2, AcceptCharset2) end end. @@ -524,12 +518,12 @@ resource_exists(Req, State) -> if_match_exists(Req, State) -> State2 = State#state{exists=true}, case cowboy_req:parse_header(<<"if-match">>, Req) of - {ok, undefined, Req2} -> - if_unmodified_since_exists(Req2, State2); - {ok, '*', Req2} -> - if_unmodified_since_exists(Req2, State2); - {ok, ETagsList, Req2} -> - if_match(Req2, State2, ETagsList) + undefined -> + if_unmodified_since_exists(Req, State2); + '*' -> + if_unmodified_since_exists(Req, State2); + ETagsList -> + if_match(Req, State2, ETagsList) end. if_match(Req, State, EtagsList) -> @@ -546,18 +540,18 @@ if_match(Req, State, EtagsList) -> if_match_must_not_exist(Req, State) -> case cowboy_req:header(<<"if-match">>, Req) of - {undefined, Req2} -> is_put_to_missing_resource(Req2, State); - {_Any, Req2} -> precondition_failed(Req2, State) + undefined -> is_put_to_missing_resource(Req, State); + _ -> precondition_failed(Req, State) end. if_unmodified_since_exists(Req, State) -> - case cowboy_req:parse_header(<<"if-unmodified-since">>, Req) of - {ok, undefined, Req2} -> - if_none_match_exists(Req2, State); - {ok, IfUnmodifiedSince, Req2} -> - if_unmodified_since(Req2, State, IfUnmodifiedSince); - {error, badarg} -> - if_none_match_exists(Req, State) + try cowboy_req:parse_header(<<"if-unmodified-since">>, Req) of + undefined -> + if_none_match_exists(Req, State); + IfUnmodifiedSince -> + if_unmodified_since(Req, State, IfUnmodifiedSince) + catch _:_ -> + if_none_match_exists(Req, State) end. %% If LastModified is the atom 'no_call', we continue. @@ -574,12 +568,12 @@ if_unmodified_since(Req, State, IfUnmodifiedSince) -> if_none_match_exists(Req, State) -> case cowboy_req:parse_header(<<"if-none-match">>, Req) of - {ok, undefined, Req2} -> - if_modified_since_exists(Req2, State); - {ok, '*', Req2} -> - precondition_is_head_get(Req2, State); - {ok, EtagsList, Req2} -> - if_none_match(Req2, State, EtagsList) + undefined -> + if_modified_since_exists(Req, State); + '*' -> + precondition_is_head_get(Req, State); + EtagsList -> + if_none_match(Req, State, EtagsList) end. if_none_match(Req, State, EtagsList) -> @@ -605,13 +599,13 @@ precondition_is_head_get(Req, State) -> precondition_failed(Req, State). if_modified_since_exists(Req, State) -> - case cowboy_req:parse_header(<<"if-modified-since">>, Req) of - {ok, undefined, Req2} -> - method(Req2, State); - {ok, IfModifiedSince, Req2} -> - if_modified_since_now(Req2, State, IfModifiedSince); - {error, badarg} -> - method(Req, State) + try cowboy_req:parse_header(<<"if-modified-since">>, Req) of + undefined -> + method(Req, State); + IfModifiedSince -> + if_modified_since_now(Req, State, IfModifiedSince) + catch _:_ -> + method(Req, State) end. if_modified_since_now(Req, State, IfModifiedSince) -> @@ -741,11 +735,11 @@ accept_resource(Req, State) -> {CTA, Req2, HandlerState} -> CTA2 = [normalize_content_types(P) || P <- CTA], State2 = State#state{handler_state=HandlerState}, - case cowboy_req:parse_header(<<"content-type">>, Req2) of - {ok, ContentType, Req3} -> - choose_content_type(Req3, State2, ContentType, CTA2); - {error, badarg} -> - respond(Req2, State2, 415) + try cowboy_req:parse_header(<<"content-type">>, Req2) of + ContentType -> + choose_content_type(Req2, State2, ContentType, CTA2) + catch _:_ -> + respond(Req2, State2, 415) end end. @@ -990,8 +984,7 @@ next(Req, State, StatusCode) when is_integer(StatusCode) -> respond(Req, State, StatusCode). respond(Req, State, StatusCode) -> - {ok, Req2} = cowboy_req:reply(StatusCode, Req), - terminate(Req2, State). + terminate(cowboy_req:reply(StatusCode, Req), State). terminate(Req, State=#state{env=Env}) -> rest_terminate(Req, State), |