]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/gateway/mod_ils_gateway.c
removed unused variable, added log line
[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 static void del_session( char* service ) {
59         if(!service || ! the_list)
60                 return;
61         
62         debug_handler("In del_sesion for %s", service );
63         session_list* prev = the_list;
64         session_list* item = prev->next;
65
66         if(!strcmp(prev->session->remote_service, service)) {
67                 info_handler("Removing gateway session for %s", service );
68                 the_list = item;
69                 osrf_app_session_destroy(prev->session);
70                 free(prev);
71                 return;
72         }
73
74         while(item) {
75                 if( !strcmp(item->session->remote_service, service)) {
76                         info_handler("Removing gateway session for %s", service );
77                         prev->next = item->next;
78                         osrf_app_session_destroy(item->session);
79                         free(item);
80                         return;
81                 }
82                 prev = item;
83                 item = item->next;
84         }
85
86         warning_handler("Attempt to remove gateway session "
87                         "that does not exist: %s", service );
88
89 }
90
91 /* find a session in the list */
92 /* if(update) we add 1 to the serve_count */
93 static osrf_app_session* find_session( char* service, int update ) {
94
95         session_list* item = the_list;
96         while(item) {
97
98                 if(!strcmp(item->session->remote_service,service)) {
99                         if(update) { 
100                                 if( item->serve_count++ > 20 ) {
101                                         debug_handler("Disconnected session on 20 requests => %s", service);
102                                         osrf_app_session_disconnect(item->session);
103                                         del_session(service);
104                                         return NULL;
105                                         //item->serve_count = 0;
106                                 }
107                         }
108                         debug_handler("Found session for %s in gateway cache", service);
109                         return item->session;
110                 }
111
112                 item = item->next;
113         }
114         return NULL;
115 }
116
117 /* add a session to the list */
118 static void add_session( char* service, osrf_app_session* session ) {
119
120         if(!session) return;
121
122         if(find_session(service,0))
123                 return;
124
125         debug_handler("Add session for %s to the cache", service );
126
127         session_list* new_item = (session_list*) safe_malloc(sizeof(session_list));
128         new_item->session = session;
129         //new_item->service = service;
130         new_item->serve_count = 0;
131
132         if(the_list) {
133                 session_list* second = the_list->next;
134                 the_list = new_item;
135                 new_item->next = second;
136         } else {
137                 the_list = new_item;
138         }
139 }
140
141 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
142         if( ! osrf_system_bootstrap_client( 
143                 "/pines/cvs/ILS/OpenSRF/src/gateway/gateway.xml") ) { /* config option */
144         }
145
146         /* we don't want to waste time parsing json that we're not going to look at*/
147         osrf_message_set_json_parse(0);
148         fprintf(stderr, "Bootstrapping %d\n", getpid() );
149         fflush(stderr);
150 }
151
152 static int mod_ils_gateway_method_handler (request_rec *r) {
153
154
155         /* make sure we're needed first thing*/
156         if (strcmp(r->handler, MODULE_NAME )) 
157                 return DECLINED;
158
159
160         apr_pool_t *p = r->pool;        /* memory pool */
161         char* arg = r->args;                    /* url query string */
162
163         char* service                                   = NULL; /* service to connect to */
164         char* method                                    = NULL; /* method to perform */
165
166         json* params                                    = NULL; /* method parameters */ 
167         json* exception                         = NULL; /* returned in error conditions */
168
169         growing_buffer* buffer          = NULL; /* POST data */
170         growing_buffer* tmp_buf         = NULL; /* temp buffer */
171
172         char* key                                               = NULL; /* query item name */
173         char* val                                               = NULL; /* query item value */
174
175
176
177         /* verify we are connected */
178         if(!osrf_system_get_transport_client()) {
179                 fatal_handler("Bootstrap Failed, no transport client");
180                 return HTTP_INTERNAL_SERVER_ERROR;
181         }
182
183         /* set content type to text/plain for passing around JSON objects */
184         ap_set_content_type(r, "text/plain");
185
186
187
188         /* gather the post args and append them to the url query string */
189         if( !strcmp(r->method,"POST") ) {
190
191                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
192
193                 if(! ap_should_client_block(r)) {
194                         warning_handler("No Post Body");
195                 }
196
197                 char body[1024];
198                 memset(body,0,1024);
199                 buffer = buffer_init(1024);
200
201                 while(ap_get_client_block(r, body, 1024)) {
202                         buffer_add( buffer, body );
203                         memset(body,0,1024);
204                 }
205
206                 if(arg && arg[0]) {
207                         tmp_buf = buffer_init(1024);
208                         buffer_add(tmp_buf,arg);
209                         buffer_add(tmp_buf,buffer->buf);
210                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
211                         buffer_free(tmp_buf);
212                 } else {
213                         arg = (char*) apr_pstrdup(p, buffer->buf);
214                 }
215                 buffer_free(buffer);
216
217         } 
218
219
220         if( ! arg || !arg[0] ) { /* we received no request */
221                 warning_handler("No Args");
222                 return OK;
223         }
224
225         r->allowed |= (AP_METHOD_BIT << M_GET);
226         r->allowed |= (AP_METHOD_BIT << M_POST);
227
228         
229         char* argcopy = (char*) apr_pstrdup(p, arg);
230
231         params = json_object_new_array();;
232         while( argcopy && (val = ap_getword(p, &argcopy, '&'))) {
233
234                 key = ap_getword(r->pool,&val, '=');
235                 if(!key || !key[0])
236                         break;
237
238                 ap_unescape_url((char*)key);
239                 ap_unescape_url((char*)val);
240
241                 if(!strcmp(key,"service")) 
242                         service = val;
243
244                 if(!strcmp(key,"method"))
245                         method = val;
246
247                 if(!strcmp(key,"__param"))
248                         json_object_array_add( params, json_tokener_parse(val));
249         }
250
251         info_handler("Performing(%d):  service %s | method %s | \nparams %s\n\n",
252                         getpid(), service, method, json_object_to_json_string(params));
253
254         osrf_app_session* session = find_session(service,1);
255
256         if(!session) {
257                 debug_handler("building new session for %s", service );
258                 session = osrf_app_client_session_init(service);
259                 add_session(service, session);
260         }
261
262         debug_handler("MOD session service: %s", session->remote_service );
263
264
265         /* connect to the remote service */
266         if(!osrf_app_session_connect(session)) {
267                 exception = json_object_new_object();
268                 json_object_object_add( exception, "is_err", json_object_new_int(1));
269                 json_object_object_add( exception, 
270                                 "err_msg", json_object_new_string("Unable to connect to remote service"));
271
272                 ap_rputs(json_object_to_json_string(exception), r );
273                 json_object_put(exception);
274                 return OK;
275         }
276
277         int req_id = osrf_app_session_make_request( session, params, method, 1 );
278         json_object_put(params);
279
280         osrf_message* omsg = NULL;
281
282         growing_buffer* result_data = buffer_init(256);
283         buffer_add(result_data, "[");
284
285         /* gather result data */
286         while((omsg = osrf_app_session_request_recv( session, req_id, 30 ))) {
287
288                 if( omsg->result_string ) {
289                         buffer_add(result_data, omsg->result_string);
290                         buffer_add( result_data, ",");
291
292                 } else {
293
294
295                         /* build the exception information */
296                         growing_buffer* exc_buffer = buffer_init(256);
297                         buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
298                         buffer_add( exc_buffer, omsg->status_name );
299                         buffer_add( exc_buffer, "\nStatus: " );
300                         buffer_add( exc_buffer, omsg->status_text );
301                         buffer_add( exc_buffer, "\nStatus: " );
302                         char code[16];
303                         memset(code, 0, 16);
304                         sprintf( code, "%d", omsg->status_code );
305                         buffer_add( exc_buffer, code );
306
307                         /* build the exception object */
308                         exception = json_object_new_object();
309                         json_object_object_add( exception, "is_err", json_object_new_int(1));
310                         json_object_object_add( exception, 
311                                         "err_msg", json_object_new_string(exc_buffer->buf));
312
313                         warning_handler("*** Looks like we got a "
314                                         "server exception\n%s", exc_buffer->buf );
315
316                         buffer_free(exc_buffer);
317                         osrf_message_free(omsg);
318                         break;
319                 }
320
321                 osrf_message_free(omsg);
322                 omsg = NULL;
323         }
324
325         /* remove trailing comma */
326         if( result_data->buf[strlen(result_data->buf)-1] == ',') {
327                 result_data->buf[strlen(result_data->buf)-1] = '\0';
328                 result_data->n_used--;
329         }
330
331         buffer_add(result_data,"]");
332
333         char* content = NULL;
334
335         /* round up our data */
336         if(exception) {
337                 content = strdup(json_object_to_json_string(exception));
338                 json_object_put(exception);
339         } else 
340                 content = buffer_data(result_data); 
341         
342
343         buffer_free(result_data);
344
345         if(content) {
346                 ap_rputs(content,r);
347                 free(content);
348         } 
349
350         osrf_app_session_request_finish( session, req_id );
351
352         return OK;
353
354 }
355
356 /*
357  * This function is a callback and it declares what other functions
358   * should be called for request processing and configuration requests.
359    * This callback function declares the Handlers for other events.  */
360 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
361 // I think this is the call to make to register a handler for method calls (GET PUT et. al.).
362 // We will ask to be last so that the comment has a higher tendency to
363 // go at the end.
364         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
365         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
366 }
367
368 /*
369  * Declare and populate the module's data structure.  The
370   * name of this structure ('tut1_module') is important - it
371    * must match the name of the module.  This structure is the
372  * only "glue" between the httpd core and the module.
373   */
374
375 module AP_MODULE_DECLARE_DATA ils_gateway_module =
376 {
377 STANDARD20_MODULE_STUFF,
378 NULL,
379 NULL,
380 NULL,
381 NULL,
382 NULL,
383 mod_ils_gateway_register_hooks,
384 };
385