added some cleanup code on child exit to ensure friendly shutdown of sockets
[OpenSRF.git] / src / gateway / osrf_json_gateway.c
1 #include "apachetools.h"
2 #include "opensrf/osrf_app_session.h"
3 #include "opensrf/osrf_system.h"
4 #include "opensrf/osrfConfig.h"
5 #include <opensrf/osrf_json.h>
6 #include <opensrf/osrf_json_xml.h>
7 #include <opensrf/osrf_legacy_json.h>
8 #include <sys/time.h>
9 #include <sys/resource.h>
10 #include <unistd.h>
11 #include <strings.h>
12
13
14 #define MODULE_NAME "osrf_json_gateway_module"
15 #define GATEWAY_CONFIG "OSRFGatewayConfig"
16 #define DEFAULT_LOCALE "OSRFDefaultLocale"
17 #define CONFIG_CONTEXT "gateway"
18 #define JSON_PROTOCOL "OSRFGatewayLegacyJSON"
19 #define GATEWAY_USE_LEGACY_JSON 1
20
21 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
22
23
24 /* our config structure */
25 typedef struct { 
26         char* configfile;  /* our bootstrap config file */
27 } osrf_json_gateway_config;
28
29 typedef struct { 
30         int legacyJSON;
31 } osrf_json_gateway_dir_config;
32
33
34 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
35
36 char* osrf_json_default_locale = "en-US";
37 char* osrf_json_gateway_config_file = NULL;
38 int bootstrapped = 0;
39 int numserved = 0;
40 osrfStringArray* allowedServices = NULL;
41
42 static const char* osrf_json_gateway_set_default_locale(cmd_parms *parms, void *config, const char *arg) {
43         if (arg)
44                 osrf_json_default_locale = (char*) arg;
45         return NULL;
46 }
47
48 static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
49         osrf_json_gateway_config  *cfg;
50         cfg = ap_get_module_config(parms->server->module_config, &osrf_json_gateway_module);
51         cfg->configfile = (char*) arg;
52         osrf_json_gateway_config_file = (char*) arg;
53         return NULL;
54 }
55
56 static const char* osrf_json_gateway_set_json_proto(cmd_parms *parms, void *config, const char *arg) {
57         osrf_json_gateway_dir_config* cfg = (osrf_json_gateway_dir_config*) config;
58         cfg->legacyJSON = (!strcasecmp((char*) arg, "false")) ? 0 : 1;
59         return NULL;
60 }
61
62 /* tell apache about our commands */
63 static const command_rec osrf_json_gateway_cmds[] = {
64         AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config, 
65                         NULL, RSRC_CONF, "osrf json gateway config file"),
66         AP_INIT_TAKE1( DEFAULT_LOCALE, osrf_json_gateway_set_default_locale, 
67                         NULL, RSRC_CONF, "osrf json gateway default locale"),
68         AP_INIT_TAKE1( JSON_PROTOCOL, osrf_json_gateway_set_json_proto,
69                         NULL, ACCESS_CONF, "osrf json gateway config file"),
70         {NULL}
71 };
72
73 /* build the config object */
74 static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) {
75         osrf_json_gateway_config* cfg = (osrf_json_gateway_config*) 
76                         apr_palloc(p, sizeof(osrf_json_gateway_config));
77         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
78         return (void*) cfg;
79 }
80
81 static void* osrf_json_gateway_create_dir_config( apr_pool_t* p, char* dir) {
82         osrf_json_gateway_dir_config* cfg = (osrf_json_gateway_dir_config*) 
83                         apr_palloc(p, sizeof(osrf_json_gateway_dir_config));
84         cfg->legacyJSON = GATEWAY_USE_LEGACY_JSON;
85         return (void*) cfg;
86 }
87
88 static apr_status_t child_exit(void* data) {
89     osrfLogInfo(OSRF_LOG_MARK, "Disconnecting on child cleanup...");
90     osrf_system_shutdown();
91     return OK;
92 }
93
94 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
95
96         char* cfg = osrf_json_gateway_config_file;
97         char buf[32];
98         int t = time(NULL);
99         snprintf(buf, sizeof(buf), "%d", t);
100
101         if( ! osrfSystemBootstrapClientResc( cfg, CONFIG_CONTEXT, buf ) ) {
102                 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s, 
103                         "Unable to Bootstrap OpenSRF Client with config %s..", cfg);
104                 return;
105         }
106
107         bootstrapped = 1;
108         allowedServices = osrfNewStringArray(8);
109         osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
110         osrfConfigGetValueList( NULL, allowedServices, "/services/service" );
111
112         int i;
113         for( i = 0; i < allowedServices->size; i++ ) {
114                 ap_log_error( APLOG_MARK, APLOG_DEBUG, 0, s, 
115                         "allowed service: %s\n", osrfStringArrayGetString(allowedServices, i));
116         }
117
118     // when this pool is cleaned up, it means the child 
119     // process is going away.  register some cleanup code
120     apr_pool_cleanup_register(p, NULL, child_exit, NULL);
121 }
122
123 static int osrf_json_gateway_method_handler (request_rec *r) {
124
125         /* make sure we're needed first thing*/
126         if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
127
128
129         osrf_json_gateway_dir_config* dir_conf =  
130                 ap_get_module_config(r->per_dir_config, &osrf_json_gateway_module);
131
132
133         /* provide 2 different JSON parsers and serializers to support legacy JSON */
134         jsonObject* (*parseJSONFunc) (const char*) = legacy_jsonParseString;
135         char* (*jsonToStringFunc) (const jsonObject*) = legacy_jsonObjectToJSON;
136
137         if(dir_conf->legacyJSON) {
138                 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, "Using legacy JSON");
139
140         } else {
141                 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, "Not using legacy JSON");
142                 parseJSONFunc = jsonParseString;
143                 jsonToStringFunc = jsonObjectToJSON;
144         }
145
146
147         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
148
149         /* verify we are connected */
150         if( !bootstrapped || !osrfSystemGetTransportClient()) {
151                 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
152                                 "because the OpenSRF JSON gateway has not been bootstrapped...");
153                 usleep( 100000 ); /* 100 milliseconds */
154                 exit(1);
155         }
156
157         osrfLogSetAppname("osrf_json_gw");
158
159         char* osrf_locale       = NULL;
160         char* param_locale      = NULL; /* locale for this call */
161         char* service           = NULL; /* service to connect to */
162         char* method            = NULL; /* method to perform */
163         char* format            = NULL; /* method to perform */
164         char* a_l               = NULL; /* request api level */
165         char* input_format      = NULL; /* POST data format, defaults to 'format' */
166         int   isXML             = 0;
167         int   api_level         = 1;
168
169         r->allowed |= (AP_METHOD_BIT << M_GET);
170         r->allowed |= (AP_METHOD_BIT << M_POST);
171
172         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: parsing URL params");
173         osrfStringArray* mparams        = NULL;
174         osrfStringArray* params = apacheParseParms(r); /* free me */
175         param_locale            = apacheGetFirstParamValue( params, "locale" );
176         service                 = apacheGetFirstParamValue( params, "service" );
177         method                  = apacheGetFirstParamValue( params, "method" ); 
178         format                  = apacheGetFirstParamValue( params, "format" ); 
179         input_format            = apacheGetFirstParamValue( params, "input_format" ); 
180         a_l                     = apacheGetFirstParamValue( params, "api_level" ); 
181         mparams                 = apacheGetParamValues( params, "param" ); /* free me */
182
183         if(format == NULL)
184                 format = strdup( "json" );
185         if(input_format == NULL)
186                 input_format = strdup( format );
187
188         /* set the user defined timeout value */
189         int timeout = 60;
190         char* tout = apacheGetFirstParamValue( params, "timeout" ); /* request timeout in seconds */
191         if( tout ) {
192                 timeout = atoi(tout);
193                 osrfLogDebug(OSRF_LOG_MARK, "Client supplied timeout of %d", timeout);
194                 free( tout );
195         }
196
197         if (a_l) {
198                 api_level = atoi(a_l);
199                 free( a_l );
200         }
201
202         if (!strcasecmp(format, "xml")) {
203                 isXML = 1;
204                 ap_set_content_type(r, "application/xml");
205         } else {
206                 ap_set_content_type(r, "text/plain");
207         }
208
209         free( format );
210         int ret = OK;
211
212         /* ----------------------------------------------------------------- */
213         /* Grab the requested locale using the Accept-Language header*/
214
215
216         if ( !param_locale ) {
217                 if ( apr_table_get(r->headers_in, "X-OpenSRF-Language") ) {
218                         param_locale = strdup( apr_table_get(r->headers_in, "X-OpenSRF-Language") );
219                 } else if ( apr_table_get(r->headers_in, "Accept-Language") ) {
220                         param_locale = strdup( apr_table_get(r->headers_in, "Accept-Language") );
221                 }
222         }
223
224
225         if (param_locale) {
226                 growing_buffer* osrf_locale_buf = buffer_init(16);      
227                 if (index(param_locale, ',')) {
228                         int ind = index(param_locale, ',') - param_locale;
229                         int i;
230                         for ( i = 0; i < ind && i < 128; i++ )
231                                 buffer_add_char( osrf_locale_buf, param_locale[i] );
232                 } else {
233                         buffer_add( osrf_locale_buf, param_locale );
234                 }
235
236                 free(param_locale);
237                 osrf_locale = buffer_release( osrf_locale_buf );
238         } else {
239                 osrf_locale = strdup( osrf_json_default_locale );
240         }
241         /* ----------------------------------------------------------------- */
242
243
244         if(!(service && method) || 
245                 !osrfStringArrayContains(allowedServices, service)) {
246
247                 osrfLogError(OSRF_LOG_MARK, 
248                         "Service [%s] not found or not allowed", service);
249                 ret = HTTP_NOT_FOUND;
250
251         } else {
252
253                 /* This will log all heaers to the apache error log 
254                 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
255                 const void* ptr;
256
257                 while( (ptr = apr_array_pop(arr)) ) {
258                         apr_table_entry_t* e = (apr_table_entry_t*) ptr;
259                         fprintf(stderr, "Table entry: %s : %s\n", e->key, e->val );
260                 }
261                 fflush(stderr);
262                 */
263
264                 osrfAppSession* session = osrfAppSessionClientInit(service);
265                 osrf_app_session_set_locale(session, osrf_locale);
266
267                 double starttime = get_timestamp_millis();
268                 int req_id = -1;
269
270                 if(!strcasecmp(input_format, "json")) {
271                         jsonObject * arr = jsonNewObject(NULL);
272
273                         char* str;
274                         int i = 0;
275
276                         while( (str = osrfStringArrayGetString(mparams, i++)) ) 
277                                 jsonObjectPush(arr, parseJSONFunc(str));
278
279                         req_id = osrfAppSessionMakeRequest( session, arr, method, api_level, NULL );
280                         jsonObjectFree(arr);
281                 } else {
282
283                         /**
284                         * If we receive XML method params, convert each param to a JSON object
285                         * and pass the array of JSON object params to the method */
286                         if(!strcasecmp(input_format, "xml")) {
287                                 jsonObject* jsonParams = jsonNewObject(NULL);
288
289                                 char* str;
290                                 int i = 0;
291                                 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
292                                         jsonObjectPush(jsonParams, jsonXMLToJSONObject(str));
293                                 }
294
295                                 req_id = osrfAppSessionMakeRequest( session, jsonParams, method, api_level, NULL );
296                                 jsonObjectFree(jsonParams);
297                         }
298                 }
299
300
301                 if( req_id == -1 ) {
302                         osrfLogError(OSRF_LOG_MARK, "I am unable to communicate with opensrf..going away...");
303                         osrfAppSessionFree(session);
304                         /* we don't want to spawn an intense re-forking storm 
305                          * if there is no jabber server.. so give it some time before we die */
306                         usleep( 100000 ); /* 100 milliseconds */
307                         exit(1);
308                 }
309
310
311                 /* ----------------------------------------------------------------- */
312                 /* log all requests to the activity log */
313                 const char* authtoken = apr_table_get(r->headers_in, "X-OILS-Authtoken");
314                 if(!authtoken) authtoken = "";
315                 growing_buffer* act = buffer_init(128); 
316                 buffer_fadd(act, "[%s] [%s] %s %s", r->connection->remote_ip, authtoken, service, method );
317                 char* str; int i = 0;
318                 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
319                         if( i == 1 ) {
320                                 OSRF_BUFFER_ADD(act, " ");
321                                 OSRF_BUFFER_ADD(act, str);
322                         } else {
323                                 OSRF_BUFFER_ADD(act, ", ");
324                                 OSRF_BUFFER_ADD(act, str);
325                         }
326                 }
327
328                 osrfLogActivity( OSRF_LOG_MARK, act->buf );
329                 buffer_free(act);
330                 /* ----------------------------------------------------------------- */
331
332
333                 osrfMessage* omsg = NULL;
334
335                 int statuscode = 200;
336
337                 /* kick off the object */
338                 if (isXML)
339                         ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
340                 else
341                         ap_rputs("{\"payload\":[", r);
342
343                 int morethan1           = 0;
344                 char* statusname        = NULL;
345                 char* statustext        = NULL;
346                 char* output            = NULL;
347
348                 while((omsg = osrfAppSessionRequestRecv( session, req_id, timeout ))) {
349         
350                         statuscode = omsg->status_code;
351                         jsonObject* res;        
352
353                         if( ( res = osrfMessageGetResult(omsg)) ) {
354
355                                 if (isXML) {
356                                         output = jsonObjectToXML( res );
357                                 } else {
358                                         output = jsonToStringFunc( res );
359                                         if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
360                                 }
361                                 ap_rputs(output, r);
362                                 free(output);
363                                 morethan1 = 1;
364                 
365                         } else {
366         
367                                 if( statuscode > 299 ) { /* the request returned a low level error */
368                                         statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
369                                         statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
370                                         osrfLogError( OSRF_LOG_MARK,  "Gateway received error: %s", statustext );
371                                 }
372                         }
373         
374                         osrfMessageFree(omsg);
375                         if(statusname) break;
376                 }
377
378                 double duration = get_timestamp_millis() - starttime;
379                 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %f seconds", duration);
380
381
382                 if (isXML)
383                         ap_rputs("</payload>", r);
384                 else
385                         ap_rputs("]",r); /* finish off the payload array */
386
387                 if(statusname) {
388
389                         /* add a debug field if the request died */
390                         ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, 
391                                         "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
392                         int l = strlen(statusname) + strlen(statustext) + 32;
393                         char buf[l];
394
395                         if (isXML)
396                                 snprintf( buf, sizeof(buf), "<debug>\"%s : %s\"</debug>", statusname, statustext );
397
398                         else {
399                                 char bb[l];
400                                 snprintf(bb, sizeof(bb),  "%s : %s", statusname, statustext);
401                                 jsonObject* tmp = jsonNewObject(bb);
402                                 char* j = jsonToStringFunc(tmp);
403                                 snprintf( buf, sizeof(buf), ",\"debug\": %s", j);
404                                 free(j);
405                                 jsonObjectFree(tmp);
406                         }
407
408                         ap_rputs(buf, r);
409
410                         free(statusname);
411                         free(statustext);
412                 }
413
414                 /* insert the status code */
415                 char buf[32];
416
417                 if (isXML)
418                         snprintf(buf, sizeof(buf), "<status>%d</status>", statuscode );
419                 else
420                         snprintf(buf, sizeof(buf), ",\"status\":%d", statuscode );
421
422                 ap_rputs( buf, r );
423
424                 if (isXML)
425                         ap_rputs("</response>", r);
426                 else
427                         ap_rputs( "}", r ); /* finish off the object */
428
429                 osrfAppSessionFree(session);
430         }
431
432         osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
433         osrfStringArrayFree(params);
434         osrfStringArrayFree(mparams);
435         free( osrf_locale );
436         free( input_format );
437         free( method );
438         free( service );
439
440         osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
441         osrfLogClearXid();
442
443         return ret;
444 }
445
446
447
448 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
449         ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
450         ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
451 }
452
453
454 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
455         STANDARD20_MODULE_STUFF,
456         osrf_json_gateway_create_dir_config,
457         NULL,
458         osrf_json_gateway_create_config,
459         NULL,
460         osrf_json_gateway_cmds,
461         osrf_json_gateway_register_hooks,
462 };
463
464
465
466