Skip to content

Commit

Permalink
WHISTLE-1154: updated the response for invalid urls
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson committed Apr 24, 2012
1 parent 095a125 commit a855b46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
43 changes: 23 additions & 20 deletions whistle_apps/apps/crossbar/src/v1_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ rest_init(Req0, Opts) ->
,raw_path=Path
,raw_qs=QS
,method=Method
,resp_status=fatal
,resp_error_msg = <<"unspecified">>
,resp_error_code=500
},

{Context1, _} = crossbar_bindings:fold(<<"v1_resource.init">>, {Context0, Opts}),
Expand Down Expand Up @@ -105,11 +108,8 @@ allowed_methods(Req0, #cb_context{allowed_methods=Methods}=Context) ->
%% HTTP method with the tunneled version
case v1_util:get_req_data(Context, Req1) of
{halt, Context1, Req2} ->
Context2 = crossbar_util:response_invalid_data(
wh_json:set_value(<<"error">>, <<"failed to parse request body">>, wh_json:new())
,Context1),

v1_util:halt(Req2, Context2);
E = wh_json:set_value([<<"data">>, <<"parse">>], <<"failed to parse request body">>, wh_json:new()),
v1_util:halt(Req2, crossbar_util:response_invalid_data(E, Context1));
{Context1, Req2} ->
check_preflight(Req2, Context1#cb_context{req_nouns=Nouns})
end;
Expand All @@ -124,20 +124,23 @@ check_preflight(Req0, #cb_context{allowed_methods=Methods, req_nouns=[{Mod, Para
{Method, Req1} = cowboy_http_req:method(Req0),
Verb = v1_util:get_http_verb(Method, Context),

Methods1 = v1_util:allow_methods(Responses, Methods, Verb, Method),
case v1_util:is_cors_preflight(Req1) of
{true, Req2} ->
lager:debug("allowing OPTIONS request for CORS preflight"),
{ok, Req3} = v1_util:add_cors_headers(Req2, Context),
{['OPTIONS'], Req3, Context#cb_context{allow_methods=Methods1
,req_verb=Verb
}};
{false, Req2} ->
lager:debug("not CORS preflight"),
{ok, Req3} = v1_util:add_cors_headers(Req2, Context),
{Methods1, Req3, Context#cb_context{allow_methods=Methods1
,req_verb=Verb
}}
case v1_util:allow_methods(Responses, Methods, Verb, Method) of
[] -> {Methods, Req1, Context#cb_context{req_nouns=[{<<"404">>, []}]}};
Methods1 ->
case v1_util:is_cors_preflight(Req1) of
{true, Req2} ->
lager:debug("allowing OPTIONS request for CORS preflight"),
{ok, Req3} = v1_util:add_cors_headers(Req2, Context),
{['OPTIONS'], Req3, Context#cb_context{allow_methods=Methods1
,req_verb=Verb
}};
{false, Req2} ->
lager:debug("not CORS preflight"),
{ok, Req3} = v1_util:add_cors_headers(Req2, Context),
{Methods1, Req3, Context#cb_context{allow_methods=Methods1
,req_verb=Verb
}}
end
end.

malformed_request(Req, #cb_context{req_json={malformed, _}}=Context) ->
Expand All @@ -152,7 +155,7 @@ malformed_request(Req, #cb_context{req_nouns=Nouns}=Context) ->
#cb_context{resp_status=success}=Context1 ->
{false, Req, Context1};
Context1 ->
{true, Req, Context1}
v1_util:halt(Req, Context1)
end;
_Other ->
lager:debug("other: ~p", [Nouns]),
Expand Down
18 changes: 9 additions & 9 deletions whistle_apps/apps/crossbar/src/v1_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ extract_file(Context, ContentType, Req0) ->
end.

-spec decode_base64/3 :: (#cb_context{}, ne_binary(), #http_req{}) -> {#cb_context{}, #http_req{}}.
decode_base64(Context, CT, Req0) ->
decode_base64(Context, CT, Req0) ->
case cowboy_http_req:body(Req0) of
{error, badarg} -> {Context, Req0};
{ok, Base64Data, Req1} ->
Expand All @@ -221,28 +221,28 @@ decode_base64(Context, CT, Req0) ->
lager:debug("request is a base64 file upload of type: ~s", [ContentType]),
{Context#cb_context{req_files=[{<<"uploaded_file">>, FileJObj}]}, Req1}
end.

-spec decode_base64/1 :: (ne_binary()) -> {undefined | ne_binary(), ne_binary()}.
decode_base64(Base64) ->
case binary:split(Base64, <<",">>) of
%% http://tools.ietf.org/html/rfc4648
[Bin] -> {undefined, corrected_base64_decode(Bin)};
%% http://tools.ietf.org/rfc/rfc2397.txt
[<<"data:", CT/binary>>, Bin] ->
[<<"data:", CT/binary>>, Bin] ->
{ContentType, _Opts} = mochiweb_util:parse_header(wh_util:to_list(CT)),
{wh_util:to_binary(ContentType), corrected_base64_decode(Bin)}
end.

-spec corrected_base64_decode/1 :: (ne_binary()) -> ne_binary().
corrected_base64_decode(Base64) when byte_size(Base64) rem 4 == 3 ->
base64:mime_decode(<<Base64/bytes, "=">>);
corrected_base64_decode(Base64) when byte_size(Base64) rem 4 == 2 ->
corrected_base64_decode(Base64) when byte_size(Base64) rem 4 == 2 ->
base64:mime_decode(<<Base64/bytes, "==">>);
corrected_base64_decode(Base64) ->
base64:mime_decode(Base64).

-spec get_json_body/1 :: (#http_req{}) -> {wh_json:json_object(), #http_req{}} |
{{'malformed', ne_binary()}, #http_req{}}.
{{'malformed', ne_binary()}, #http_req{}}.
get_json_body(Req0) ->
case cowboy_http_req:body(Req0) of
{error, _E} ->
Expand Down Expand Up @@ -534,9 +534,9 @@ validate(#cb_context{req_nouns=Nouns}=Context0) ->
Context1 = lists:foldr(fun({Mod, Params}, ContextAcc) ->
Event = <<"v1_resource.validate.", Mod/binary>>,
lager:debug("validating against params ~p", [Params]),
Payload = [ContextAcc | Params],
Payload = [ContextAcc#cb_context{resp_status=fatal} | Params],
crossbar_bindings:fold(Event, Payload)
end, Context0, Nouns),
end, Context0#cb_context{resp_status=fatal}, Nouns),
case succeeded(Context1) of
true -> process_billing(Context1);
false -> Context1
Expand Down Expand Up @@ -689,7 +689,7 @@ create_pull_response(Req0, Context) ->
%% This function extracts the reponse fields and puts them in a proplist
%% @end
%%--------------------------------------------------------------------
-spec create_resp_envelope/1 :: (#cb_context{}) -> wh_json:json_proplist(<<_:32,_:_*8>>).
-spec create_resp_envelope/1 :: (#cb_context{}) -> wh_json:json_proplist(<<_:32,_:_*8>>).
create_resp_envelope(#cb_context{resp_data=RespData, resp_status=success, auth_token=AuthToken, resp_etag=undefined}) ->
[{<<"auth_token">>, AuthToken}
,{<<"status">>, <<"success">>}
Expand Down Expand Up @@ -760,7 +760,7 @@ fix_header(H, V, _) ->
-spec halt/2 :: (#http_req{}, #cb_context{}) -> {'halt', #http_req{}, #cb_context{}}.
halt(Req0, #cb_context{resp_error_code=StatusCode}=Context) ->
lager:debug("halting execution here"),
{Content, Req1} = create_resp_content(Req0, Context),
{Content, Req1} = create_resp_content(Req0, Context),
lager:debug("setting resp body: ~s", [Content]),
{ok, Req2} = cowboy_http_req:set_resp_body(Content, Req1),
lager:debug("setting status code: ~p", [StatusCode]),
Expand Down

0 comments on commit a855b46

Please sign in to comment.