]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/gateway/mod_ils_gateway.c
we now have session caching.
[Evergreen.git] / OpenSRF / src / gateway / mod_ils_gateway.c
1 /*
2 Copyright 2002 Kevin O'Donnell
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 /*
20  * Include the core server components.
21   */
22 #include "httpd.h"
23 #include "http_config.h"
24 #include "http_core.h"
25 #include "http_protocol.h"
26 #include "apr_compat.h"
27 #include "apr_strings.h"
28
29
30
31 /* our stuff */
32 #include "opensrf/transport_client.h"
33 #include "opensrf/generic_utils.h"
34 #include "opensrf/osrf_message.h"
35 #include "opensrf/osrf_app_session.h"
36 #include "md5.h"
37
38 /*
39  * This function is registered as a handler for HTTP methods and will
40   * therefore be invoked for all GET requests (and others).  Regardless
41    * of the request type, this function simply sends a message to
42  * STDERR (which httpd redirects to logs/error_log).  A real module
43   * would do *alot* more at this point.
44    */
45 #define MODULE_NAME "ils_gateway_module"
46
47 struct session_list_struct {
48         char* service;
49         osrf_app_session* session;
50         struct session_list_struct* next;
51         int serve_count;
52 };
53 typedef struct session_list_struct session_list;
54
55 /* the global session cache */
56 static session_list* the_list = NULL;
57
58 /* find a session in the list */
59 /* if(update) we add 1 to the serve_count */
60 static osrf_app_session* find_session( char* service, int update ) {
61
62         session_list* item = the_list;
63         while(item) {
64
65                 if(!strcmp(item->service,service)) {
66                         if(update) { 
67                                 if( item->serve_count++ > 20 ) {
68                                         debug_handler("Disconnected session on 20 requests => %s", service);
69                                         osrf_app_session_disconnect(item->session);
70                                         item->serve_count = 0;
71                                 }
72                         }
73                         return item->session;
74                 }
75
76                 item = item->next;
77         }
78         return NULL;
79 }
80
81 /* add a session to the list */
82 static void add_session( char* service, osrf_app_session* session ) {
83
84         if(!session) return;
85
86         if(find_session(service,0))
87                 return;
88
89         debug_handler("Add session for %s to the cache", service );
90
91         session_list* new_item = (session_list*) safe_malloc(sizeof(session_list));
92         new_item->session = session;
93         new_item->service = service;
94         new_item->serve_count = 0;
95
96         if(the_list) {
97                 session_list* second = the_list->next;
98                 the_list = new_item;
99                 new_item->next = second;
100         } else {
101                 the_list = new_item;
102         }
103 }
104
105 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
106         if( ! osrf_system_bootstrap_client( 
107                 "/pines/cvs/ILS/OpenSRF/src/gateway/gateway.xml") ) { /* config option */
108         }
109         fprintf(stderr, "Bootstrapping %d\n", getpid() );
110         fflush(stderr);
111 }
112
113 static int mod_ils_gateway_method_handler (request_rec *r) {
114
115
116         /* make sure we're needed first thing*/
117         if (strcmp(r->handler, MODULE_NAME )) 
118                 return DECLINED;
119
120
121         apr_pool_t *p = r->pool;        /* memory pool */
122         char* arg = r->args;                    /* url query string */
123
124         char* service                                   = NULL; /* service to connect to */
125         char* method                                    = NULL; /* method to perform */
126
127         json* params                                    = NULL; /* method parameters */ 
128         json* exception                         = NULL; /* returned in error conditions */
129
130         growing_buffer* buffer          = NULL; /* POST data */
131         growing_buffer* tmp_buf         = NULL; /* temp buffer */
132
133         char* key                                               = NULL; /* query item name */
134         char* val                                               = NULL; /* query item value */
135
136
137
138         /* verify we are connected */
139         if(!osrf_system_get_transport_client()) {
140                 fatal_handler("Bootstrap Failed, no transport client");
141                 return HTTP_INTERNAL_SERVER_ERROR;
142         }
143
144         /* set content type to text/plain for passing around JSON objects */
145         ap_set_content_type(r, "text/plain");
146
147
148
149         /* gather the post args and append them to the url query string */
150         if( !strcmp(r->method,"POST") ) {
151
152                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
153
154                 if(! ap_should_client_block(r)) {
155                         warning_handler("No Post Body");
156                 }
157
158                 char body[1024];
159                 memset(body,0,1024);
160                 buffer = buffer_init(1024);
161
162                 while(ap_get_client_block(r, body, 1024)) {
163                         buffer_add( buffer, body );
164                         memset(body,0,1024);
165                 }
166
167                 if(arg && arg[0]) {
168                         tmp_buf = buffer_init(1024);
169                         buffer_add(tmp_buf,arg);
170                         buffer_add(tmp_buf,buffer->buf);
171                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
172                         buffer_free(tmp_buf);
173                 } else {
174                         arg = (char*) apr_pstrdup(p, buffer->buf);
175                 }
176                 buffer_free(buffer);
177
178         } 
179
180
181         if( ! arg || !arg[0] ) { /* we received no request */
182                 warning_handler("No Args");
183                 return OK;
184         }
185
186         r->allowed |= (AP_METHOD_BIT << M_GET);
187         r->allowed |= (AP_METHOD_BIT << M_POST);
188
189         
190         char* argcopy = (char*) apr_pstrdup(p, arg);
191
192         params = json_object_new_array();;
193         while( argcopy && (val = ap_getword(p, &argcopy, '&'))) {
194
195                 key = ap_getword(r->pool,&val, '=');
196                 if(!key || !key[0])
197                         break;
198
199                 ap_unescape_url((char*)key);
200                 ap_unescape_url((char*)val);
201
202                 if(!strcmp(key,"service")) 
203                         service = val;
204
205                 if(!strcmp(key,"method"))
206                         method = val;
207
208                 if(!strcmp(key,"__param"))
209                         json_object_array_add( params, json_tokener_parse(val));
210         }
211
212         debug_handler("Performing(%d):  service %s | method %s | \nparams %s\n\n",
213                         getpid(), service, method, json_object_to_json_string(params));
214
215         osrf_app_session* session = find_session(service,1);
216
217         if(!session) {
218                 debug_handler("building new session for %s", service );
219                 session = osrf_app_client_session_init(service);
220                 add_session(service, session);
221         }
222
223
224         /* connect to the remote service */
225         if(!osrf_app_session_connect(session)) {
226                 exception = json_object_new_object();
227                 json_object_object_add( exception, "is_err", json_object_new_int(1));
228                 json_object_object_add( exception, 
229                                 "err_msg", json_object_new_string("Unable to connect to remote service"));
230
231                 ap_rputs(json_object_to_json_string(exception), r );
232                 json_object_put(exception);
233                 return OK;
234         }
235
236         int req_id = osrf_app_session_make_request( session, params, method, 1 );
237         json_object_put(params);
238
239         osrf_message* omsg = NULL;
240
241         growing_buffer* result_data = buffer_init(256);
242         buffer_add(result_data, "[");
243
244         /* gather result data */
245         while((omsg = osrf_app_session_request_recv( session, req_id, 30 ))) {
246
247                 if( omsg->result_string ) {
248                         buffer_add(result_data, omsg->result_string);
249                         buffer_add( result_data, ",");
250
251                 } else {
252
253                         warning_handler("*** Looks like we got an exception\n" );
254
255                         /* build the exception information */
256                         growing_buffer* exc_buffer = buffer_init(256);
257                         buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
258                         buffer_add( exc_buffer, omsg->status_name );
259                         buffer_add( exc_buffer, "\nStatus: " );
260                         buffer_add( exc_buffer, omsg->status_text );
261                         buffer_add( exc_buffer, "\nStatus: " );
262                         char code[16];
263                         memset(code, 0, 16);
264                         sprintf( code, "%d", omsg->status_code );
265                         buffer_add( exc_buffer, code );
266
267                         /* build the exception object */
268                         exception = json_object_new_object();
269                         json_object_object_add( exception, "is_err", json_object_new_int(1));
270                         json_object_object_add( exception, 
271                                         "err_msg", json_object_new_string(exc_buffer->buf));
272                         buffer_free(exc_buffer);
273                         osrf_message_free(omsg);
274                         break;
275                 }
276
277                 osrf_message_free(omsg);
278                 omsg = NULL;
279         }
280
281         /* remove trailing comma */
282         if( result_data->buf[strlen(result_data->buf)-1] == ',') {
283                 result_data->buf[strlen(result_data->buf)-1] = '\0';
284                 result_data->n_used--;
285         }
286
287         buffer_add(result_data,"]");
288
289         char* content = NULL;
290
291         /* round up our data */
292         if(exception) {
293                 content = strdup(json_object_to_json_string(exception));
294                 json_object_put(exception);
295         } else 
296                 content = buffer_data(result_data); 
297         
298
299         buffer_free(result_data);
300
301         if(content) {
302                 ap_rputs(content,r);
303                 free(content);
304         } 
305
306         osrf_app_session_request_finish( session, req_id );
307         //osrf_app_session_disconnect( session );
308         //osrf_app_session_destroy(session); //need to test removing this
309
310         return OK;
311
312 }
313
314 /*
315  * This function is a callback and it declares what other functions
316   * should be called for request processing and configuration requests.
317    * This callback function declares the Handlers for other events.  */
318 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
319 // I think this is the call to make to register a handler for method calls (GET PUT et. al.).
320 // We will ask to be last so that the comment has a higher tendency to
321 // go at the end.
322         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
323         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
324 }
325
326 /*
327  * Declare and populate the module's data structure.  The
328   * name of this structure ('tut1_module') is important - it
329    * must match the name of the module.  This structure is the
330  * only "glue" between the httpd core and the module.
331   */
332
333 module AP_MODULE_DECLARE_DATA ils_gateway_module =
334 {
335 STANDARD20_MODULE_STUFF,
336 NULL,
337 NULL,
338 NULL,
339 NULL,
340 NULL,
341 mod_ils_gateway_register_hooks,
342 };
343