Skip to content

Commit

Permalink
ugly indent, plus a dialyzer warning for dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
Claes Wikstrom committed Mar 3, 2011
1 parent 3d1c7df commit c90c603
Showing 1 changed file with 82 additions and 77 deletions.
159 changes: 82 additions & 77 deletions src/yaws_rpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@
-export([handler/2]).
-export([handler_session/2, handler_session/3]).

%-define(debug, 1).
%-include("../../yaws/src/yaws_debug.hrl").

%-define(debug, 1).
-include("../../yaws/src/yaws_debug.hrl").
-include("../include/yaws_api.hrl").

%%% ######################################################################
Expand Down Expand Up @@ -78,58 +77,58 @@ handler(Args, Handler) ->
%%%

%%% we should be called from yaws page or module
handler(Args, Handler, Type) when is_record(Args, arg) -> % {{{
handler(Args, Handler, Type) when is_record(Args, arg) ->
case parse_request(Args) of
ok ->
handle_payload(Args, Handler, Type);
{status, StatusCode} -> % cannot parse request
send(Args, StatusCode)
end. % }}}
ok ->
handle_payload(Args, Handler, Type);
{status, StatusCode} -> % cannot parse request
send(Args, StatusCode)
end.

-define(ERROR_LOG(Reason),
error_logger:error_report({?MODULE, ?LINE, Reason})).
error_logger:error_report({?MODULE, ?LINE, Reason})).

-define(LOG(Reason), ?ERROR_LOG(Reason)).

%%%
%%% check that request come in reasonable protocol version and reasonable method
%%%
parse_request(Args) -> % {{{
parse_request(Args) ->
Req = Args#arg.req,
case {Req#http_request.method, Req#http_request.version} of
{'POST', {1,0}} ->
% ?Debug("HTTP Version 1.0~n", []),
ok;
{'POST', {1,1}} ->
% ?Debug("HTTP Version 1.1~n", []),
ok;
{'POST', _HTTPVersion} -> {status, 505};
{_Method, {1,1}} -> {status, 501};
_ -> {status, 400}
end. % }}}
{'POST', {1,0}} ->
?Debug("HTTP Version 1.0~n", []),
ok;
{'POST', {1,1}} ->
?Debug("HTTP Version 1.1~n", []),
ok;
{'POST', _HTTPVersion} -> {status, 505};
{_Method, {1,1}} -> {status, 501};
_ -> {status, 400}
end.

handle_payload(Args, Handler, Type) -> % {{{
RpcType = recognize_rpc_type(Args),
% haXe parameters are URL encoded
%% haXe parameters are URL encoded
{Payload,DecodedStr} =
case RpcType of
T when T==haxe; T==json ->
PL = binary_to_list(Args#arg.clidata),
%% ?Debug("rpc plaintext call ~p ~n", [PL]),
?Debug("rpc plaintext call ~p ~n", [PL]),
{PL, yaws_api:url_decode(PL)};
_ ->
PL = binary_to_list(Args#arg.clidata),
{PL, PL}
%% ?Debug("rpc plaintext call ~p ~n", [PL])
?Debug("rpc plaintext call ~p ~n", [PL]),
{PL, PL}
end,
case decode_handler_payload(RpcType, DecodedStr) of
{ok, DecodedPayload, ID} ->
% ?Debug("client2erl decoded call ~p ~n", [DecodedPayload]),
?Debug("client2erl decoded call ~p ~n", [DecodedPayload]),
eval_payload(Args, Handler, DecodedPayload, Type, ID, RpcType);
{error, Reason} ->
?ERROR_LOG({html, client2erl, Payload, Reason}),
send(Args, 400, RpcType)
end. % }}}
end.

%%% Identify the RPC type. We first try recognize haXe by the
%%% "X-Haxe-Remoting" HTTP header, then the "SOAPAction" header,
Expand All @@ -144,15 +143,16 @@ recognize_rpc_hdr([{_,_,"soapaction",_,_}|_]) -> soap;
recognize_rpc_hdr([_|T]) -> recognize_rpc_hdr(T);
recognize_rpc_hdr([]) -> json.


%%%
%%% call handler/3 and provide session support
eval_payload(Args, {M, F}, Payload, {session, CookieName}, ID, RpcType) -> % {{{
eval_payload(Args, {M, F}, Payload, {session, CookieName}, ID, RpcType) ->
{SessionValue, Cookie} =
case yaws_api:find_cookie_val(CookieName, (Args#arg.headers)#headers.cookie) of
[] -> % have no session started, just call handler
case yaws_api:find_cookie_val(CookieName,
(Args#arg.headers)#headers.cookie) of
[] -> %% have no session started, just call handler
{undefined, undefined};
Cookie2 -> % get old session data
Cookie2 -> %% get old session data
case yaws_api:cookieval_to_opaque(Cookie2) of
{ok, OP} ->
{OP, Cookie2};
Expand All @@ -172,25 +172,29 @@ eval_payload(Args, {M, F}, Payload, {session, CookieName}, ID, RpcType) -> % {{{
?ERROR_LOG({M, F, Reason}),
send(Args, Rc, Reason, [], RpcType);
{false, ResponsePayload} ->
% do not have updates in session data
%% do not have updates in session data
encode_send(Args, 200, ResponsePayload, [], ID, RpcType);
{false, ResponsePayload, RespCode} ->
% do not have updates in session data
%% do not have updates in session data
encode_send(Args, RespCode, ResponsePayload, [], ID, RpcType);
false -> % soap notify
false;
{true, _NewTimeout, NewSessionValue, ResponsePayload} -> % be compatible with xmlrpc module
CO = handle_cookie(Cookie, CookieName, SessionValue, NewSessionValue, M, F),
{true, _NewTimeout, NewSessionValue, ResponsePayload} ->
%% be compatible with xmlrpc module
CO = handle_cookie(Cookie, CookieName, SessionValue,
NewSessionValue, M, F),
encode_send(Args, 200, ResponsePayload, CO, ID, RpcType);
{true, _NewTimeout, NewSessionValue, ResponsePayload, RespCode} -> % be compatible with xmlrpc module
CO = handle_cookie(Cookie, CookieName, SessionValue, NewSessionValue, M, F),
{true, _NewTimeout, NewSessionValue, ResponsePayload, RespCode} ->
%% be compatible with xmlrpc module
CO = handle_cookie(Cookie, CookieName, SessionValue,
NewSessionValue, M, F),
encode_send(Args, RespCode, ResponsePayload, CO, ID, RpcType)
end; % }}}
end;

%%%
%%% call handler/2 without session support
%%%
eval_payload(Args, {M, F}, Payload, simple, ID, RpcType) -> % {{{
eval_payload(Args, {M, F}, Payload, simple, ID, RpcType) ->
case catch M:F(Args#arg.state, Payload) of
{'EXIT', Reason} ->
?ERROR_LOG({M, F, {'EXIT', Reason}}),
Expand All @@ -204,22 +208,27 @@ eval_payload(Args, {M, F}, Payload, simple, ID, RpcType) -> % {{{
false;
{true, _NewTimeout, _NewState, ResponsePayload} ->
encode_send(Args, 200, ResponsePayload, [], ID, RpcType)
end. % }}}
end.

handle_cookie(Cookie, CookieName, SessionValue, NewSessionValue, M, F) ->
case NewSessionValue of
undefined when Cookie == undefined -> []; % nothing to do
undefined -> % rpc handler requested session delete
yaws_api:delete_cookie_session(Cookie), []; % XXX: may be return set-cookie with empty val?
_ -> % any other value will stored in session
yaws_api:delete_cookie_session(Cookie), [];
%% XXX: may be return set-cookie with empty val?
_ ->
%% any other value will stored in session
case SessionValue of
undefined -> % got session data and should start new session now
undefined ->
%% got session data and should start new session now
Cookie1 = yaws_api:new_cookie_session(NewSessionValue),
case get_expire(M, F) of
false ->
yaws_api:setcookie(CookieName, Cookie1, "/"); % return set_cookie header
yaws_api:setcookie(CookieName, Cookie1, "/");
%% return set_cookie header
Expire ->
yaws_api:setcookie(CookieName, Cookie1, "/", Expire) % return set_cookie header
yaws_api:setcookie(CookieName, Cookie1, "/",Expire)
%% return set_cookie header
end;
_ ->
yaws_api:replace_cookie_session(Cookie, NewSessionValue),
Expand All @@ -242,25 +251,21 @@ callback_fun(M, F, Args, Payload, SessionValue, _RpcType) ->

%%% XXX compatibility with XMLRPC handlers
%%% XXX - potential bug here?
encode_send(Args, StatusCode, [Payload], AddOn, ID, RpcType) -> % {{{
encode_send(Args, StatusCode, Payload, AddOn, ID, RpcType); % }}}

encode_send(Args, StatusCode, Payload, AddOn, ID, RpcType) -> % {{{
% ?Debug("rpc response ~p ~n", [Payload]),
case encode_handler_payload(Payload, ID, RpcType) of
{ok, EncodedPayload, NewRpcType} ->
% ?Debug("rpc encoded response ~p ~n", [EncodedPayload]),
send(Args, StatusCode, EncodedPayload, AddOn, NewRpcType);
{ok, EncodedPayload} ->
% ?Debug("rpc encoded response ~p ~n", [EncodedPayload]),
send(Args, StatusCode, EncodedPayload, AddOn, RpcType)
end. % }}}
encode_send(Args, StatusCode, [Payload], AddOn, ID, RpcType) ->
encode_send(Args, StatusCode, Payload, AddOn, ID, RpcType);

encode_send(_Args, _StatusCode, Payload, _AddOn, ID, RpcType) ->
?Debug("rpc response ~p ~n", [Payload]),
EncodedPayload = encode_handler_payload(Payload, ID, RpcType),
?Debug("rpc encoded response ~p ~n", [EncodedPayload]),
EncodedPayload.

send(Args, StatusCode) -> send(Args, StatusCode, json).

send(Args, StatusCode, RpcType) -> send(Args, StatusCode, "", [], RpcType). % {{{
send(Args, StatusCode, RpcType) -> send(Args, StatusCode, "", [], RpcType).

send(Args, StatusCode, Payload, AddOnData, RpcType) when not is_list(AddOnData) ->
send(Args, StatusCode, Payload, AddOnData, RpcType)
when not is_list(AddOnData) ->
send(Args, StatusCode, Payload, [AddOnData], RpcType);

send(_Args, StatusCode, Payload, AddOnData, RpcType) ->
Expand All @@ -269,19 +274,19 @@ send(_Args, StatusCode, Payload, AddOnData, RpcType) ->
content_hdr(RpcType, Payload),
{header, {content_length, lists:flatlength(Payload) }}
] ++ AddOnData,
A
. % }}}
A.

content_hdr(json, Payload) -> {content, "application/json", Payload};
content_hdr(_, Payload) -> {content, "text/xml", Payload}. % FIXME would like to add charset info here !!
content_hdr(_, Payload) -> {content, "text/xml", Payload}.
%% FIXME would like to add charset info here !!

encode_handler_payload({Xml,[]}, _ID, soap) -> % {{{
{ok, Xml};
encode_handler_payload({Xml,[]}, _ID, soap) ->
Xml;

encode_handler_payload(Xml, _ID, soap) -> % {{{
{ok, Xml};
encode_handler_payload(Xml, _ID, soap) ->
Xml;

encode_handler_payload({error, [ErlStruct]}, ID, RpcType) -> % {{{
encode_handler_payload({error, [ErlStruct]}, ID, RpcType) ->
encode_handler_payload({error, ErlStruct}, ID, RpcType);


Expand All @@ -292,9 +297,9 @@ encode_handler_payload({error, ErlStruct}, ID, RpcType) ->
{error, ErlStruct}]});
haxe -> [$h, $x, $r | haxe:encode({exception, ErlStruct})]
end,
{ok, StructStr};
StructStr;

encode_handler_payload({response, [ErlStruct]}, ID, RpcType) -> % {{{
encode_handler_payload({response, [ErlStruct]}, ID, RpcType) ->
encode_handler_payload({response, ErlStruct}, ID, RpcType);


Expand All @@ -305,10 +310,10 @@ encode_handler_payload({response, ErlStruct}, ID, RpcType) ->
{error, null}]});
haxe -> [$h, $x, $r | haxe:encode(ErlStruct)]
end,
{ok, StructStr}.
StructStr.


decode_handler_payload(json, JSonStr) -> %{{{
decode_handler_payload(json, JSonStr) ->
try
{ok, Obj} = json:decode_string(JSonStr),
Method = list_to_atom(jsonrpc:s(Obj, method)),
Expand All @@ -317,9 +322,9 @@ decode_handler_payload(json, JSonStr) -> %{{{
{ok, {call, Method, Args}, ID}
catch
error:Err ->
?ERROR_LOG({ json_decode , JSonStr , Err }),
{error, Err}
end; %}}}
?ERROR_LOG({ json_decode , JSonStr , Err }),
{error, Err}
end;

decode_handler_payload(haxe, [$_, $_, $x, $= | HaxeStr]) ->
try
Expand All @@ -338,4 +343,4 @@ decode_handler_payload(soap, Payload) ->
{ok, Payload, undefined}.


% vim: tabstop=4 ft=erlang

0 comments on commit c90c603

Please sign in to comment.