printf family format fixup found (har har) and suggested by Scott McKellar -- %lf...
[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 "objson/object.h"
6 #include "objson/json2xml.h"
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <unistd.h>
10
11
12 #define MODULE_NAME "osrf_json_gateway_module"
13 #define GATEWAY_CONFIG "OSRFGatewayConfig"
14 #define CONFIG_CONTEXT "gateway"
15
16 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
17
18
19 /* our config structure */
20 typedef struct { 
21         char* configfile;  /* our bootstrap config file */
22 } osrf_json_gateway_config;
23
24 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
25
26 char* osrf_json_gateway_config_file = NULL;
27 int bootstrapped = 0;
28 int numserved = 0;
29 osrfStringArray* allowedServices = NULL;
30
31 static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
32         osrf_json_gateway_config  *cfg;
33         cfg = ap_get_module_config(parms->server->module_config, &osrf_json_gateway_module);
34         cfg->configfile = (char*) arg;
35         osrf_json_gateway_config_file = (char*) arg;
36         return NULL;
37 }
38
39 /* tell apache about our commands */
40 static const command_rec osrf_json_gateway_cmds[] = {
41         AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config, 
42                         NULL, RSRC_CONF, "osrf json gateway config file"),
43         {NULL}
44 };
45
46 /* build the config object */
47 static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) {
48         osrf_json_gateway_config* cfg = (osrf_json_gateway_config*) 
49                         apr_palloc(p, sizeof(osrf_json_gateway_config));
50         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
51         return (void*) cfg;
52 }
53
54
55 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
56
57         char* cfg = osrf_json_gateway_config_file;
58         char buf[32];
59         memset(buf, 0x0, 32);
60         int t = time(NULL);
61         snprintf(buf, 32, "%d", t);
62
63         if( ! osrfSystemBootstrapClientResc( cfg, CONFIG_CONTEXT, buf ) ) {
64                 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s, 
65                         "Unable to Bootstrap OpenSRF Client with config %s..", cfg);
66                 return;
67         }
68
69         bootstrapped = 1;
70         allowedServices = osrfNewStringArray(8);
71         osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
72         osrfConfigGetValueList( NULL, allowedServices, "/services/service" );
73
74         int i;
75         for( i = 0; i < allowedServices->size; i++ ) {
76                 ap_log_error( APLOG_MARK, APLOG_DEBUG, 0, s, 
77                         "allowed service: %s\n", osrfStringArrayGetString(allowedServices, i));
78         }
79 }
80
81 static int osrf_json_gateway_method_handler (request_rec *r) {
82
83         /* make sure we're needed first thing*/
84         if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
85
86         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
87
88         /* verify we are connected */
89         if( !bootstrapped || !osrf_system_get_transport_client()) {
90                 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
91                                 "because the OpenSRF JSON gateway has not been bootstrapped...");
92                 usleep( 100000 ); /* 100 milliseconds */
93                 exit(1);
94         }
95
96         osrfLogSetAppname("osrf_json_gw");
97
98         char* service           = NULL; /* service to connect to */
99         char* method            = NULL; /* method to perform */
100         char* format            = NULL; /* method to perform */
101         char* a_l                       = NULL; /* request api level */
102         int   isXML                     = 0;
103         int   api_level = 1;
104
105         r->allowed |= (AP_METHOD_BIT << M_GET);
106         r->allowed |= (AP_METHOD_BIT << M_POST);
107
108         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: parsing URL params");
109         string_array* mparams   = NULL;
110         string_array* params            = apacheParseParms(r); /* free me */
111         service         = apacheGetFirstParamValue( params, "service" );
112         method          = apacheGetFirstParamValue( params, "method" ); 
113         format          = apacheGetFirstParamValue( params, "format" ); 
114         a_l                     = apacheGetFirstParamValue( params, "api_level" ); 
115         mparams         = apacheGetParamValues( params, "param" ); /* free me */
116
117    /* set the user defined timeout value */
118    int timeout = 60;
119    char* tout = apacheGetFirstParamValue( params, "timeout" ); /* request timeout in seconds */
120    if( tout ) {
121       timeout = atoi(tout);
122       osrfLogDebug(OSRF_LOG_MARK, "Client supplied timeout of %d", timeout);
123    }
124
125
126         if (a_l)
127                 api_level = atoi(a_l);
128
129         if (format && !strcasecmp(format, "xml" )) {
130                 isXML = 1;
131                 ap_set_content_type(r, "application/xml");
132         } else {
133                 ap_set_content_type(r, "text/plain");
134         }
135
136         int ret = OK;
137
138         if(!(service && method) || 
139                 !osrfStringArrayContains(allowedServices, service)) {
140
141                 osrfLogError(OSRF_LOG_MARK, 
142                         "Service [%s] not found or not allowed", service);
143                 ret = HTTP_NOT_FOUND;
144
145         } else {
146
147                 /* This will log all heaers to the apache error log 
148                 const apr_array_header_t* arr = apr_table_elts(r->headers_in);
149                 const void* ptr;
150
151                 while( (ptr = apr_array_pop(arr)) ) {
152                         apr_table_entry_t* e = (apr_table_entry_t*) ptr;
153                         fprintf(stderr, "Table entry: %s : %s\n", e->key, e->val );
154                 }
155                 fflush(stderr);
156                 */
157
158                 osrfAppSession* session = osrf_app_client_session_init(service);
159
160                 double starttime = get_timestamp_millis();
161                 int req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams );
162
163
164                 if( req_id == -1 ) {
165                         osrfLogError(OSRF_LOG_MARK, "I am unable to communcate with opensrf..going away...");
166                         /* we don't want to spawn an intense re-forking storm 
167                          * if there is no jabber server.. so give it some time before we die */
168                         usleep( 100000 ); /* 100 milliseconds */
169                         exit(1);
170                 }
171
172
173                 /* ----------------------------------------------------------------- */
174                 /* log all requests to the activity log */
175                 const char* authtoken = apr_table_get(r->headers_in, "X-OILS-Authtoken");
176                 if(!authtoken) authtoken = "";
177                 growing_buffer* act = buffer_init(128); 
178                 buffer_fadd(act, "[%s] [%s] %s %s", r->connection->remote_ip, authtoken, service, method );
179                 char* str; int i = 0;
180                 while( (str = osrfStringArrayGetString(mparams, i++)) ) {
181                         if( i == 1 ) {
182             OSRF_BUFFER_ADD(act, " ");
183                                 OSRF_BUFFER_ADD(act, str);
184                         } else {
185                                 OSRF_BUFFER_ADD(act, ", ");
186                                 OSRF_BUFFER_ADD(act, str);
187                         }
188                 }
189
190                 osrfLogActivity( OSRF_LOG_MARK, act->buf );
191                 buffer_free(act);
192                 /* ----------------------------------------------------------------- */
193
194
195                 osrf_message* omsg = NULL;
196
197                 int statuscode = 200;
198
199                 /* kick off the object */
200                 if (isXML)
201                         ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
202                 else
203                         ap_rputs("{\"payload\":[", r);
204
205                 int morethan1           = 0;
206                 char* statusname        = NULL;
207                 char* statustext        = NULL;
208                 char* output            = NULL;
209
210                 while((omsg = osrfAppSessionRequestRecv( session, req_id, timeout ))) {
211         
212                         statuscode = omsg->status_code;
213                         jsonObject* res;        
214
215                         if( ( res = osrfMessageGetResult(omsg)) ) {
216
217                                 if (isXML) {
218                                         output = jsonObjectToXML( res );
219                                 } else {
220                                         output = jsonObjectToJSON( res );
221                                         if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
222                                 }
223                                 ap_rputs(output, r);
224                                 free(output);
225                                 morethan1 = 1;
226                 
227                         } else {
228         
229                                 if( statuscode > 299 ) { /* the request returned a low level error */
230                                         statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
231                                         statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
232                                         osrfLogError( OSRF_LOG_MARK,  "Gateway received error: %s", statustext );
233                                 }
234                         }
235         
236                         osrf_message_free(omsg);
237                         if(statusname) break;
238                 }
239
240                 double duration = get_timestamp_millis() - starttime;
241                 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %f seconds", duration);
242
243
244                 if (isXML)
245                         ap_rputs("</payload>", r);
246                 else
247                         ap_rputs("]",r); /* finish off the payload array */
248
249                 if(statusname) {
250
251                         /* add a debug field if the request died */
252                         ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, 
253                                         "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
254                         int l = strlen(statusname) + strlen(statustext) + 32;
255                         char buf[l];
256                         bzero(buf,l);
257
258                         if (isXML)
259                                 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
260
261                         else {
262                                 char bb[l];
263                                 bzero(bb, l);
264                                 snprintf(bb, l,  "%s : %s", statusname, statustext);
265                                 jsonObject* tmp = jsonNewObject(bb);
266                                 char* j = jsonObjectToJSON(tmp);
267                                 snprintf( buf, l, ",\"debug\": %s", j);
268                                 free(j);
269                                 jsonObjectFree(tmp);
270                         }
271
272                         ap_rputs(buf, r);
273
274                         free(statusname);
275                         free(statustext);
276                 }
277
278                 /* insert the status code */
279                 char buf[32];
280                 bzero(buf,32);
281
282                 if (isXML)
283                         snprintf(buf, 32, "<status>%d</status>", statuscode );
284                 else
285                         snprintf(buf, 32, ",\"status\":%d", statuscode );
286
287                 ap_rputs( buf, r );
288
289                 if (isXML)
290                         ap_rputs("</response>", r);
291                 else
292                         ap_rputs( "}", r ); /* finish off the object */
293
294                 osrf_app_session_destroy(session);
295         }
296
297         osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
298         string_array_destroy(params);
299         string_array_destroy(mparams);
300
301         osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
302    osrfLogClearXid();
303
304         return ret;
305 }
306
307
308
309 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
310         ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
311         ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
312 }
313
314
315 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
316         STANDARD20_MODULE_STUFF,
317         NULL,
318         NULL,
319         osrf_json_gateway_create_config,
320         NULL,
321         osrf_json_gateway_cmds,
322         osrf_json_gateway_register_hooks,
323 };
324
325
326
327