Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 224 lines (195 sloc) 8.155 kb
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
1 %% Copyright (C) 2003 Joakim Grebenö <jocke@gleipnir.com>.
2 %% All rights reserved.
3 %%
4 %% Copyright (C) 2006 Gaspar Chilingarov <nm@web.am>
5 %% Gurgen Tumanyan <barbarian@armkb.com>
6 %% All rights reserved.
7 %%
8 %%
9 %% Redistribution and use in source and binary forms, with or without
10 %% modification, are permitted provided that the following conditions
11 %% are met:
12 %%
13 %% 1. Redistributions of source code must retain the above copyright
14 %% notice, this list of conditions and the following disclaimer.
15 %% 2. Redistributions in binary form must reproduce the above
16 %% copyright notice, this list of conditions and the following
17 %% disclaimer in the documentation and/or other materials provided
18 %% with the distribution.
19 %%
20 %% THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 %% OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 %% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 %% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 %% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 %% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 %% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 %% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 %% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 %% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 -module(yaws_jsonrpc).
33 -author("Gaspar Chilingarov <nm@web.am>, Gurgen Tumanyan <barbarian@armkb.com>").
34 -export([handler/2]).
35 -export([handler_session/2, handler_session/3]).
36
37 %-define(debug, 1).
38 %-include("../../yaws/src/yaws_debug.hrl").
39
1aad2b4 Corrected reference to yaws_api.hrl so that they now compile properly.
Tobbe Tornquist authored
40 -include("../include/yaws_api.hrl").
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
41
42 %%% ######################################################################
43 %%% public interface
44 %%%
45
46 %%%
47 %%% use jsonrpc handler which can automagically start sessions if we need
48 %%%
49 handler_session(Args, Handler) ->
50 handler_session(Args, Handler, 'SID').
51
52 %%%
53 %%% allow overriding session Cookie name
54 %%%
55 handler_session(Args, Handler, SID_NAME) when is_atom(SID_NAME) ->
56 handler_session(Args, Handler, atom_to_list(SID_NAME));
57
58 handler_session(Args, Handler, SID_NAME) ->
59 handler(Args, Handler, {session, SID_NAME}). % go to generic handler
60
61 %%%
62 %%% xmlrpc:handler compatible call
63 %%% no session support will be available
64 handler(Args, Handler) ->
65 handler(Args, Handler, simple).
66
67
68 %%% ######################################################################
69 %%% private functions
70 %%%
71
72 %%% we should be called from yaws page or module
73 handler(Args, Handler, Type) when is_record(Args, arg) -> % {{{
74 case parse_request(Args) of
75 ok ->
76 handle_payload(Args, Handler, Type);
77 {status, StatusCode} -> % cannot parse request
78 send(Args, StatusCode)
79 end. % }}}
80
81 -define(ERROR_LOG(Reason),
82 error_logger:error_report({?MODULE, ?LINE, Reason})).
83
84 %%%
85 %%% check that request come in reasonable protocol version and reasonable method
86 %%%
87 parse_request(Args) -> % {{{
88 case {(Args#arg.req)#http_request.method, (Args#arg.req)#http_request.version} of
89 {'POST', {1,0}} ->
90 % ?Debug("HTTP Version 1.0~n", []),
91 ok;
92 {'POST', {1,1}} ->
93 % ?Debug("HTTP Version 1.1~n", []),
94 ok;
95 {'POST', _HTTPVersion} -> {status, 505};
96 {_Method, {1,1}} -> {status, 501};
97 _ -> {status, 400}
98 end. % }}}
99
100 handle_payload(Args, Handler, Type) -> % {{{
101 Payload = binary_to_list(Args#arg.clidata),
102 % ?Debug("jsonrpc plaintext call ~p ~n", [Payload]),
103 case decode_handler_payload(Payload) of
104 {ok, DecodedPayload, ID} ->
105 % ?Debug("json2erl decoded call ~p ~n", [DecodedPayload]),
106 eval_payload(Args, Handler, DecodedPayload, Type, ID);
107 {error, Reason} ->
108 ?ERROR_LOG({html, json2erl, Payload, Reason}),
109 send(Args, 400)
110 end. % }}}
111
112 %%%
113 %%% call handler/3 and provide session support
114 eval_payload(Args, {M, F}, Payload, {session, CookieName},ID) -> % {{{
115 {SessionValue, Cookie} = case yaws_api:find_cookie_val(CookieName, (Args#arg.headers)#headers.cookie) of
116 [] -> % have no session started, just call handler
117 {undefined, undefined};
118 Cookie2 -> % get old session data
119 case yaws_api:cookieval_to_opaque(Cookie2) of
120 {ok, OP} ->
121 yaws_api:cookieval_to_opaque(Cookie2),
122 {OP, Cookie2};
123 {error, _ErrMsg} -> % cannot get corresponding session
124 {undefined, undefined}
125 end
126 end,
127
128 case catch M:F(Args#arg.state, Payload, SessionValue) of
129 {'EXIT', Reason} ->
130 ?ERROR_LOG({M, F, {'EXIT', Reason}}),
131 send(Args, 500);
132 {error, Reason} ->
133 ?ERROR_LOG({M, F, Reason}),
134 send(Args, 500);
135 {false, ResponsePayload} ->
136 % do not have updates in session data
137 encode_send(Args, 200, ResponsePayload, [], ID);
138 {true, _NewTimeout, NewSessionValue, ResponsePayload} -> % be compatible with xmlrpc module
139 CO = case NewSessionValue of
140 undefined when Cookie == undefined -> []; % nothing to do
141 undefined -> % rpc handler requested session delete
142 yaws_api:delete_cookie_session(Cookie), []; % XXX: may be return set-cookie with empty val?
143 _ -> % any other value will stored in session
144 case SessionValue of
145 undefined -> % got session data and should start new session now
146 Cookie1 = yaws_api:new_cookie_session(NewSessionValue),
147 yaws_api:setcookie(CookieName, Cookie1, "/"); % return set_cookie header
148 _ ->
149 yaws_api:replace_cookie_session(Cookie, NewSessionValue),
150 [] % nothing to add to yaws data
151 end
152 end,
153 encode_send(Args, 200, ResponsePayload, CO, ID)
154 end; % }}}
155
156 %%%
157 %%% call handler/2 without session support
158 %%%
159 eval_payload(Args, {M, F}, Payload, simple, ID) -> % {{{
160 case catch M:F(Args#arg.state, Payload) of
161 {'EXIT', Reason} ->
162 ?ERROR_LOG({M, F, {'EXIT', Reason}}),
163 send(Args, 500);
164 {error, Reason} ->
165 ?ERROR_LOG({M, F, Reason}),
166 send(Args, 500);
167 {false, ResponsePayload} ->
168 encode_send(Args, 200, ResponsePayload, [],ID);
169 {true, _NewTimeout, _NewState, ResponsePayload} ->
170 encode_send(Args, 200, ResponsePayload, [],ID)
171 end. % }}}
172
173
174 %%% XXX compatibility with XMLRPC handlers
175 %%% XXX - potential bug here?
176 encode_send(Args, StatusCode, [Payload], AddOn, ID) -> % {{{
177 encode_send(Args, StatusCode, Payload, AddOn, ID); % }}}
178
179 encode_send(Args, StatusCode, Payload, AddOn, ID) -> % {{{
180 % ?Debug("jsonrpc response ~p ~n", [Payload]),
181 case encode_handler_payload(Payload, ID) of
182 {ok, EncodedPayload} ->
183 % ?Debug("jsonpc encoded response ~p ~n", [EncodedPayload]),
184 send(Args, StatusCode, EncodedPayload, AddOn);
185 {error, Reason} ->
186 ?ERROR_LOG({jsonrpc_encode, payload, Payload, Reason}),
187 send(Args, 500)
188 end. % }}}
189
190 send(Args, StatusCode) -> send(Args, StatusCode, "", []). % {{{
191
192 send(Args, StatusCode, Payload, AddOnData) when not is_list(AddOnData) ->
193 send(Args, StatusCode, Payload, [AddOnData]);
194
195 send(_Args, StatusCode, Payload, AddOnData) ->
196 A = [
197 {status, StatusCode},
198 {content, "text/xml", Payload},
199 {header, {content_length, lists:flatlength(Payload) }}
200 ] ++ AddOnData,
201 A
202 . % }}}
203
204 encode_handler_payload({response, [ErlStruct]},ID) -> % {{{
205 encode_handler_payload({response, ErlStruct}, ID);
206
207 encode_handler_payload({response, ErlStruct},ID) ->
208 StructStr = json:encode({struct, [ {result, ErlStruct}, {id, ID}]}),
209 {ok, StructStr}. % }}}
210
211 decode_handler_payload(JSonStr) -> %{{{
212 try
213 {ok, JSON} = json:decode_string(JSonStr),
214 Method = list_to_atom(jsonrpc:s(JSON, method)),
215 {array, Args} = jsonrpc:s(JSON, params),
216 ID = jsonrpc:s(JSON, id),
217
218 {ok, {call, Method, Args}, ID}
219 catch
220 error:Err -> {error, Err}
221 end.%}}}
222
223 % vim: tabstop=4 ft=erlang
Something went wrong with that request. Please try again.