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