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>
10 #include <sys/resource.h>
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
24 } osrf_json_gateway_dir_config;
27 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
29 char* osrf_json_default_locale = "en-US";
30 char* osrf_json_gateway_config_file = NULL;
34 static const char* osrf_json_gateway_set_default_locale(cmd_parms *parms,
35 void *config, const char *arg) {
37 osrf_json_default_locale = (char*) arg;
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;
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;
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"),
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;
71 static apr_status_t child_exit(void* data) {
72 osrfLogInfo(OSRF_LOG_MARK, "Disconnecting on child cleanup...");
73 osrf_system_shutdown();
77 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
79 char* cfg = osrf_json_gateway_config_file;
82 snprintf(buf, sizeof(buf), "%d", t);
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);
91 osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
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);
99 static int osrf_json_gateway_method_handler (request_rec *r) {
101 /* make sure we're needed first thing*/
102 if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
105 osrf_json_gateway_dir_config* dir_conf =
106 ap_get_module_config(r->per_dir_config, &osrf_json_gateway_module);
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;
113 if(dir_conf->legacyJSON) {
114 ap_log_rerror( APLOG_MARK, APLOG_DEBUG, 0, r, "Using legacy JSON");
117 parseJSONFunc = jsonParse;
118 jsonToStringFunc = jsonObjectToJSON;
122 osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
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 */
132 osrfLogSetAppname("osrf_json_gw");
133 osrfAppSessionSetIngress("gateway-v1");
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' */
145 r->allowed |= (AP_METHOD_BIT << M_GET);
146 r->allowed |= (AP_METHOD_BIT << M_POST);
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 */
160 format = strdup( "json" );
161 if(input_format == NULL)
162 input_format = strdup( format );
164 /* set the user defined timeout value */
166 char* tout = apacheGetFirstParamValue( params, "timeout" ); /* request timeout in seconds */
168 timeout = atoi(tout);
169 osrfLogDebug(OSRF_LOG_MARK, "Client supplied timeout of %d", timeout);
174 api_level = atoi(a_l);
178 if (!strcasecmp(format, "xml")) {
180 ap_set_content_type(r, "application/xml");
182 ap_set_content_type(r, "text/plain");
188 /* ----------------------------------------------------------------- */
189 /* Grab the requested locale using the Accept-Language header*/
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") );
202 growing_buffer* osrf_locale_buf = buffer_init(16);
203 if (index(param_locale, ',')) {
204 int ind = index(param_locale, ',') - param_locale;
206 for ( i = 0; i < ind && i < 128; i++ )
207 buffer_add_char( osrf_locale_buf, param_locale[i] );
209 buffer_add( osrf_locale_buf, param_locale );
213 osrf_locale = buffer_release( osrf_locale_buf );
215 osrf_locale = strdup( osrf_json_default_locale );
217 /* ----------------------------------------------------------------- */
220 if(!(service && method)) {
222 osrfLogError(OSRF_LOG_MARK,
223 "Service [%s] not found or not allowed", service);
224 ret = HTTP_NOT_FOUND;
228 /* This will log all heaers to the apache error log
229 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
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 );
239 osrfAppSession* session = osrfAppSessionClientInit(service);
240 osrf_app_session_set_locale(session, osrf_locale);
242 double starttime = get_timestamp_millis();
245 if(!strcasecmp(input_format, "json")) {
246 jsonObject * arr = jsonNewObject(NULL);
251 while( (str = osrfStringArrayGetString(mparams, i++)) )
252 jsonObjectPush(arr, parseJSONFunc(str));
254 req_id = osrfAppSessionSendRequest( session, arr, method, api_level );
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);
266 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
267 jsonObjectPush(jsonParams, jsonXMLToJSONObject(str));
270 req_id = osrfAppSessionSendRequest( session, jsonParams, method, api_level );
271 jsonObjectFree(jsonParams);
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 */
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 buffer_fadd(act, "[%s] [%s] [%s] %s %s", r->connection->remote_ip,
292 authtoken, osrf_locale, service, method );
294 const char* str; int i = 0;
295 int redact_params = 0;
296 while( (str = osrfStringArrayGetString(log_protect_arr, i++)) ) {
297 //osrfLogInternal(OSRF_LOG_MARK, "Checking for log protection [%s]", str);
298 if(!strncmp(method, str, strlen(str))) {
304 OSRF_BUFFER_ADD(act, " **PARAMS REDACTED**");
307 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
309 OSRF_BUFFER_ADD(act, " ");
310 OSRF_BUFFER_ADD(act, str);
312 OSRF_BUFFER_ADD(act, ", ");
313 OSRF_BUFFER_ADD(act, str);
318 osrfLogActivity( OSRF_LOG_MARK, "%s", act->buf );
320 /* ----------------------------------------------------------------- */
323 osrfMessage* omsg = NULL;
325 int statuscode = 200;
327 /* kick off the object */
329 ap_rputs( "<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>",
332 ap_rputs("{\"payload\":[", r);
335 char* statusname = NULL;
336 char* statustext = NULL;
339 while((omsg = osrfAppSessionRequestRecv( session, req_id, timeout ))) {
341 statuscode = omsg->status_code;
342 const jsonObject* res;
344 if( ( res = osrfMessageGetResult(omsg)) ) {
347 output = jsonObjectToXML( res );
349 output = jsonToStringFunc( res );
350 if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
358 if( statuscode > 299 ) { /* the request returned a low level error */
359 statusname = omsg->status_name ? strdup(omsg->status_name)
360 : strdup("Unknown Error");
361 statustext = omsg->status_text ? strdup(omsg->status_text)
362 : strdup("No Error Message");
363 osrfLogError( OSRF_LOG_MARK, "Gateway received error: %s", statustext );
367 osrfMessageFree(omsg);
368 if(statusname) break;
371 double duration = get_timestamp_millis() - starttime;
372 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %f seconds", duration);
376 ap_rputs("</payload>", r);
378 ap_rputs("]",r); /* finish off the payload array */
382 /* add a debug field if the request died */
383 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r,
384 "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
385 int l = strlen(statusname) + strlen(statustext) + 32;
389 snprintf( buf, sizeof(buf), "<debug>\"%s : %s\"</debug>", statusname, statustext );
393 snprintf(bb, sizeof(bb), "%s : %s", statusname, statustext);
394 jsonObject* tmp = jsonNewObject(bb);
395 char* j = jsonToStringFunc(tmp);
396 snprintf( buf, sizeof(buf), ",\"debug\": %s", j);
407 /* insert the status code */
411 snprintf(buf, sizeof(buf), "<status>%d</status>", statuscode );
413 snprintf(buf, sizeof(buf), ",\"status\":%d", statuscode );
418 ap_rputs("</response>", r);
420 ap_rputs( "}", r ); /* finish off the object */
422 osrfAppSessionFree(session);
425 osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
426 osrfStringArrayFree(params);
427 osrfStringArrayFree(mparams);
429 free( input_format );
433 osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
441 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
442 ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
443 ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
447 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
448 STANDARD20_MODULE_STUFF,
449 osrf_json_gateway_create_dir_config,
453 osrf_json_gateway_cmds,
454 osrf_json_gateway_register_hooks,