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>
9 #include <sys/resource.h>
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
23 } osrf_json_gateway_dir_config;
26 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
28 char* osrf_json_default_locale = "en-US";
29 char* osrf_json_gateway_config_file = NULL;
33 static const char* osrf_json_gateway_set_default_locale(cmd_parms *parms,
34 void *config, const char *arg) {
36 osrf_json_default_locale = (char*) arg;
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;
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;
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"),
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;
70 static apr_status_t child_exit(void* data) {
71 osrfLogInfo(OSRF_LOG_MARK, "Disconnecting on child cleanup...");
72 osrf_system_shutdown();
76 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
78 char* cfg = osrf_json_gateway_config_file;
81 snprintf(buf, sizeof(buf), "%d", t);
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);
90 osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
92 // when this pool is cleaned up, it means the child
93 // process is going away. register some cleanup code
94 // XXX causes us to disconnect even for clone()'d process cleanup (as in mod_cgi)
95 //apr_pool_cleanup_register(p, NULL, child_exit, apr_pool_cleanup_null);
98 static int osrf_json_gateway_method_handler (request_rec *r) {
100 /* make sure we're needed first thing*/
101 if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
104 osrf_json_gateway_dir_config* dir_conf =
105 ap_get_module_config(r->per_dir_config, &osrf_json_gateway_module);
108 /* provide 2 different JSON parsers and serializers to support legacy JSON */
109 jsonObject* (*parseJSONFunc) (const char*) = legacy_jsonParseString;
110 char* (*jsonToStringFunc) (const jsonObject*) = legacy_jsonObjectToJSON;
112 if(dir_conf->legacyJSON) {
113 ap_log_rerror( APLOG_MARK, APLOG_DEBUG, 0, r, "Using legacy JSON");
116 parseJSONFunc = jsonParse;
117 jsonToStringFunc = jsonObjectToJSON;
121 osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
123 /* verify we are connected */
124 if( !bootstrapped || !osrfSystemGetTransportClient()) {
125 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
126 "because the OpenSRF JSON gateway has not been bootstrapped...");
127 usleep( 100000 ); /* 100 milliseconds */
131 osrfLogSetAppname("osrf_json_gw");
133 char* osrf_locale = NULL;
134 char* param_locale = NULL; /* locale for this call */
135 char* service = NULL; /* service to connect to */
136 char* method = NULL; /* method to perform */
137 char* format = NULL; /* method to perform */
138 char* a_l = NULL; /* request api level */
139 char* input_format = NULL; /* POST data format, defaults to 'format' */
143 r->allowed |= (AP_METHOD_BIT << M_GET);
144 r->allowed |= (AP_METHOD_BIT << M_POST);
146 osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: parsing URL params");
147 osrfStringArray* mparams = NULL;
148 osrfStringArray* params = apacheParseParms(r); /* free me */
149 param_locale = apacheGetFirstParamValue( params, "locale" );
150 service = apacheGetFirstParamValue( params, "service" );
151 method = apacheGetFirstParamValue( params, "method" );
152 format = apacheGetFirstParamValue( params, "format" );
153 input_format = apacheGetFirstParamValue( params, "input_format" );
154 a_l = apacheGetFirstParamValue( params, "api_level" );
155 mparams = apacheGetParamValues( params, "param" ); /* free me */
158 format = strdup( "json" );
159 if(input_format == NULL)
160 input_format = strdup( format );
162 /* set the user defined timeout value */
164 char* tout = apacheGetFirstParamValue( params, "timeout" ); /* request timeout in seconds */
166 timeout = atoi(tout);
167 osrfLogDebug(OSRF_LOG_MARK, "Client supplied timeout of %d", timeout);
172 api_level = atoi(a_l);
176 if (!strcasecmp(format, "xml")) {
178 ap_set_content_type(r, "application/xml");
180 ap_set_content_type(r, "text/plain");
186 /* ----------------------------------------------------------------- */
187 /* Grab the requested locale using the Accept-Language header*/
190 if ( !param_locale ) {
191 if ( apr_table_get(r->headers_in, "X-OpenSRF-Language") ) {
192 param_locale = strdup( apr_table_get(r->headers_in, "X-OpenSRF-Language") );
193 } else if ( apr_table_get(r->headers_in, "Accept-Language") ) {
194 param_locale = strdup( apr_table_get(r->headers_in, "Accept-Language") );
200 growing_buffer* osrf_locale_buf = buffer_init(16);
201 if (index(param_locale, ',')) {
202 int ind = index(param_locale, ',') - param_locale;
204 for ( i = 0; i < ind && i < 128; i++ )
205 buffer_add_char( osrf_locale_buf, param_locale[i] );
207 buffer_add( osrf_locale_buf, param_locale );
211 osrf_locale = buffer_release( osrf_locale_buf );
213 osrf_locale = strdup( osrf_json_default_locale );
215 /* ----------------------------------------------------------------- */
218 if(!(service && method)) {
220 osrfLogError(OSRF_LOG_MARK,
221 "Service [%s] not found or not allowed", service);
222 ret = HTTP_NOT_FOUND;
226 /* This will log all heaers to the apache error log
227 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
230 while( (ptr = apr_array_pop(arr)) ) {
231 apr_table_entry_t* e = (apr_table_entry_t*) ptr;
232 fprintf(stderr, "Table entry: %s : %s\n", e->key, e->val );
237 osrfAppSession* session = osrfAppSessionClientInit(service);
238 osrf_app_session_set_locale(session, osrf_locale);
240 double starttime = get_timestamp_millis();
243 if(!strcasecmp(input_format, "json")) {
244 jsonObject * arr = jsonNewObject(NULL);
249 while( (str = osrfStringArrayGetString(mparams, i++)) )
250 jsonObjectPush(arr, parseJSONFunc(str));
252 req_id = osrfAppSessionSendRequest( session, arr, method, api_level );
257 * If we receive XML method params, convert each param to a JSON object
258 * and pass the array of JSON object params to the method */
259 if(!strcasecmp(input_format, "xml")) {
260 jsonObject* jsonParams = jsonNewObject(NULL);
264 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
265 jsonObjectPush(jsonParams, jsonXMLToJSONObject(str));
268 req_id = osrfAppSessionSendRequest( session, jsonParams, method, api_level );
269 jsonObjectFree(jsonParams);
275 osrfLogError(OSRF_LOG_MARK, "I am unable to communicate with opensrf..going away...");
276 osrfAppSessionFree(session);
277 /* we don't want to spawn an intense re-forking storm
278 * if there is no jabber server.. so give it some time before we die */
279 usleep( 100000 ); /* 100 milliseconds */
284 /* ----------------------------------------------------------------- */
285 /* log all requests to the activity log */
286 const char* authtoken = apr_table_get(r->headers_in, "X-OILS-Authtoken");
287 if(!authtoken) authtoken = "";
288 growing_buffer* act = buffer_init(128);
289 buffer_fadd(act, "[%s] [%s] [%s] %s %s", r->connection->remote_ip,
290 authtoken, osrf_locale, service, method );
291 const char* str; int i = 0;
292 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
294 OSRF_BUFFER_ADD(act, " ");
295 OSRF_BUFFER_ADD(act, str);
297 OSRF_BUFFER_ADD(act, ", ");
298 OSRF_BUFFER_ADD(act, str);
302 osrfLogActivity( OSRF_LOG_MARK, act->buf );
304 /* ----------------------------------------------------------------- */
307 osrfMessage* omsg = NULL;
309 int statuscode = 200;
311 /* kick off the object */
313 ap_rputs( "<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>",
316 ap_rputs("{\"payload\":[", r);
319 char* statusname = NULL;
320 char* statustext = NULL;
323 while((omsg = osrfAppSessionRequestRecv( session, req_id, timeout ))) {
325 statuscode = omsg->status_code;
326 const jsonObject* res;
328 if( ( res = osrfMessageGetResult(omsg)) ) {
331 output = jsonObjectToXML( res );
333 output = jsonToStringFunc( res );
334 if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
342 if( statuscode > 299 ) { /* the request returned a low level error */
343 statusname = omsg->status_name ? strdup(omsg->status_name)
344 : strdup("Unknown Error");
345 statustext = omsg->status_text ? strdup(omsg->status_text)
346 : strdup("No Error Message");
347 osrfLogError( OSRF_LOG_MARK, "Gateway received error: %s", statustext );
351 osrfMessageFree(omsg);
352 if(statusname) break;
355 double duration = get_timestamp_millis() - starttime;
356 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %f seconds", duration);
360 ap_rputs("</payload>", r);
362 ap_rputs("]",r); /* finish off the payload array */
366 /* add a debug field if the request died */
367 ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r,
368 "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
369 int l = strlen(statusname) + strlen(statustext) + 32;
373 snprintf( buf, sizeof(buf), "<debug>\"%s : %s\"</debug>", statusname, statustext );
377 snprintf(bb, sizeof(bb), "%s : %s", statusname, statustext);
378 jsonObject* tmp = jsonNewObject(bb);
379 char* j = jsonToStringFunc(tmp);
380 snprintf( buf, sizeof(buf), ",\"debug\": %s", j);
391 /* insert the status code */
395 snprintf(buf, sizeof(buf), "<status>%d</status>", statuscode );
397 snprintf(buf, sizeof(buf), ",\"status\":%d", statuscode );
402 ap_rputs("</response>", r);
404 ap_rputs( "}", r ); /* finish off the object */
406 osrfAppSessionFree(session);
409 osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
410 osrfStringArrayFree(params);
411 osrfStringArrayFree(mparams);
413 free( input_format );
417 osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
425 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
426 ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
427 ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
431 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
432 STANDARD20_MODULE_STUFF,
433 osrf_json_gateway_create_dir_config,
437 osrf_json_gateway_cmds,
438 osrf_json_gateway_register_hooks,