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