Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 217 lines (188 sloc) 7.894 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
%% Copyright (C) 2003 Joakim Grebenö <jocke@gleipnir.com>.
%% All rights reserved.
%%
%% Copyright (C) 2006 Gaspar Chilingarov <nm@web.am>
%% Gurgen Tumanyan <barbarian@armkb.com>
%% All rights reserved.
%%
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% 1. Redistributions of source code must retain the above copyright
%% notice, this list of conditions and the following disclaimer.
%% 2. Redistributions in binary form must reproduce the above
%% copyright notice, this list of conditions and the following
%% disclaimer in the documentation and/or other materials provided
%% with the distribution.
%%
%% THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
%% OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
%% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
%% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
%% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
%% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
%% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-module(yaws_jsonrpc).
-author("Gaspar Chilingarov <nm@web.am>, Gurgen Tumanyan <barbarian@armkb.com>").
-export([handler/2]).
-export([handler_session/2, handler_session/3]).

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

-include("../include/yaws_api.hrl").

%%% ######################################################################
%%% public interface
%%%

%%%
%%% use jsonrpc handler which can automagically start sessions if we need
%%%
handler_session(Args, Handler) ->
    handler_session(Args, Handler, 'SID').

%%%
%%% allow overriding session Cookie name
%%%
handler_session(Args, Handler, SID_NAME) when is_atom(SID_NAME) ->
    handler_session(Args, Handler, atom_to_list(SID_NAME));

handler_session(Args, Handler, SID_NAME) ->
    handler(Args, Handler, {session, SID_NAME}). % go to generic handler

%%%
%%% xmlrpc:handler compatible call
%%% no session support will be available
handler(Args, Handler) ->
    handler(Args, Handler, simple).


%%% ######################################################################
%%% private functions
%%%

%%% we should be called from yaws page or module
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. % }}}

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

%%%
%%% check that request come in reasonable protocol version and reasonable method
%%%
parse_request(Args) -> % {{{
    case {(Args#arg.req)#http_request.method, (Args#arg.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. % }}}
    
handle_payload(Args, Handler, Type) -> % {{{
    Payload = binary_to_list(Args#arg.clidata),
% ?Debug("jsonrpc plaintext call ~p ~n", [Payload]),
    case decode_handler_payload(Payload) of
    {ok, DecodedPayload, ID} ->
% ?Debug("json2erl decoded call ~p ~n", [DecodedPayload]),
        eval_payload(Args, Handler, DecodedPayload, Type, ID);
    {error, Reason} ->
        ?ERROR_LOG({html, json2erl, Payload, Reason}),
        send(Args, 400)
    end. % }}}
    
%%%
%%% call handler/3 and provide session support
eval_payload(Args, {M, F}, Payload, {session, CookieName},ID) -> % {{{
    {SessionValue, Cookie} = 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
        case yaws_api:cookieval_to_opaque(Cookie2) of
        {ok, OP} ->
            yaws_api:cookieval_to_opaque(Cookie2),
            {OP, Cookie2};
        {error, _ErrMsg} -> % cannot get corresponding session
            {undefined, undefined}
        end
    end,

    case catch M:F(Args#arg.state, Payload, SessionValue) of
    {'EXIT', Reason} ->
        ?ERROR_LOG({M, F, {'EXIT', Reason}}),
        send(Args, 500);
    {error, Reason} ->
        ?ERROR_LOG({M, F, Reason}),
        send(Args, 500);
    {false, ResponsePayload} ->
        % do not have updates in session data
        encode_send(Args, 200, ResponsePayload, [], ID);
    {true, _NewTimeout, NewSessionValue, ResponsePayload} -> % be compatible with xmlrpc module
        CO = 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
            case SessionValue of
            undefined -> % got session data and should start new session now
                Cookie1 = yaws_api:new_cookie_session(NewSessionValue),
                yaws_api:setcookie(CookieName, Cookie1, "/"); % return set_cookie header
            _ ->
                yaws_api:replace_cookie_session(Cookie, NewSessionValue),
                [] % nothing to add to yaws data
            end
        end,
        encode_send(Args, 200, ResponsePayload, CO, ID)
     end; % }}}

%%%
%%% call handler/2 without session support
%%%
eval_payload(Args, {M, F}, Payload, simple, ID) -> % {{{
    case catch M:F(Args#arg.state, Payload) of
    {'EXIT', Reason} ->
        ?ERROR_LOG({M, F, {'EXIT', Reason}}),
        send(Args, 500);
    {error, Reason} ->
        ?ERROR_LOG({M, F, Reason}),
        send(Args, 500);
    {false, ResponsePayload} ->
        encode_send(Args, 200, ResponsePayload, [],ID);
    {true, _NewTimeout, _NewState, ResponsePayload} ->
        encode_send(Args, 200, ResponsePayload, [],ID)
    end. % }}}


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

encode_send(Args, StatusCode, Payload, AddOn, ID) -> % {{{
    {ok, EncodedPayload} = encode_handler_payload(Payload, ID),
    send(Args, StatusCode, EncodedPayload, AddOn).

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

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

send(_Args, StatusCode, Payload, AddOnData) ->
    A = [
    {status, StatusCode},
    {content, "text/xml", Payload},
    {header, {content_length, lists:flatlength(Payload) }}
    ] ++ AddOnData,
    A
     . % }}}

encode_handler_payload({response, [ErlStruct]},ID) -> % {{{
    encode_handler_payload({response, ErlStruct}, ID);
    
encode_handler_payload({response, ErlStruct},ID) ->
    StructStr = json:encode({struct, [ {result, ErlStruct}, {id, ID}]}),
    {ok, StructStr}. % }}}
      
decode_handler_payload(JSonStr) -> %{{{
    try
        {ok, JSON} = json:decode_string(JSonStr),
        Method = list_to_atom(jsonrpc:s(JSON, method)),
        {array, Args} = jsonrpc:s(JSON, params),
        ID = jsonrpc:s(JSON, id),

        {ok, {call, Method, Args}, ID}
    catch
        error:Err -> {error, Err}
    end.%}}}

% vim: tabstop=4 ft=erlang
Something went wrong with that request. Please try again.