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