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