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