]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/gateway/osrf_json_gateway.c
added IP addresses to log line
[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
10
11 #define MODULE_NAME "osrf_json_gateway_module"
12 #define GATEWAY_CONFIG "OSRFGatewayConfig"
13 #define CONFIG_CONTEXT "gateway"
14
15 #define GATEWAY_DEFAULT_CONFIG "/openils/conf/opensrf_core.xml"
16
17
18 /* our config structure */
19 typedef struct { 
20         char* configfile;  /* our bootstrap config file */
21 } osrf_json_gateway_config;
22
23 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module;
24
25 char* osrf_json_gateway_config_file = NULL;
26 int bootstrapped = 0;
27 int numserved = 0;
28 osrfStringArray* allowedServices = NULL;
29
30 static const char* osrf_json_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
31         osrf_json_gateway_config  *cfg;
32         cfg = ap_get_module_config(parms->server->module_config, &osrf_json_gateway_module);
33         cfg->configfile = (char*) arg;
34         osrf_json_gateway_config_file = (char*) arg;
35         return NULL;
36 }
37
38 /* tell apache about our commands */
39 static const command_rec osrf_json_gateway_cmds[] = {
40         AP_INIT_TAKE1( GATEWAY_CONFIG, osrf_json_gateway_set_config, 
41                         NULL, RSRC_CONF, "osrf json gateway config file"),
42         {NULL}
43 };
44
45 /* build the config object */
46 static void* osrf_json_gateway_create_config( apr_pool_t* p, server_rec* s) {
47         osrf_json_gateway_config* cfg = (osrf_json_gateway_config*) 
48                         apr_palloc(p, sizeof(osrf_json_gateway_config));
49         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
50         return (void*) cfg;
51 }
52
53
54 static void osrf_json_gateway_child_init(apr_pool_t *p, server_rec *s) {
55
56         char* cfg = osrf_json_gateway_config_file;
57         if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT ) ) {
58                 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s, 
59                         "Unable to Bootstrap OpenSRF Client with config %s..", cfg);
60                 return;
61         }
62
63         bootstrapped = 1;
64         allowedServices = osrfNewStringArray(8);
65         osrfLogInfo(OSRF_LOG_MARK, "Bootstrapping gateway child for requests");
66         osrfConfigGetValueList( NULL, allowedServices, "/services/service" );
67
68         int i;
69         for( i = 0; i < allowedServices->size; i++ ) {
70                 fprintf(stderr, "allowed service: %s\n", 
71                         osrfStringArrayGetString(allowedServices, i));
72         }
73
74
75 #ifdef OSRF_GATEWAY_ENABLE_RLIMIT /* emergency test measures only */
76         struct rlimit lim = { 536870912, 536870912 }; /* 512MB */
77         setrlimit(RLIMIT_AS, &lim);
78         setrlimit(RLIMIT_MEMLOCK,&lim);
79         setrlimit(RLIMIT_STACK, &lim);
80         setrlimit(RLIMIT_DATA,&lim);
81
82         struct rlimit lim2;
83         getrlimit(RLIMIT_DATA, &lim2);
84         osrfLogDebug(OSRF_LOG_MARK, "rlimit for data set to %d", lim2.rlim_cur);
85 #endif
86
87 }
88
89 static int osrf_json_gateway_method_handler (request_rec *r) {
90
91         /* make sure we're needed first thing*/
92         if (strcmp(r->handler, MODULE_NAME )) return DECLINED;
93
94         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: entered request handler");
95
96         /* verify we are connected */
97         if( !bootstrapped || !osrf_system_get_transport_client()) {
98                 ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, r, "Cannot process request "
99                                 "because the OpenSRF JSON gateway has not been bootstrapped...");
100                 return HTTP_INTERNAL_SERVER_ERROR;
101         }
102
103         osrfLogSetAppname("osrf_json_gw");
104
105         char* service           = NULL; /* service to connect to */
106         char* method            = NULL; /* method to perform */
107         char* format            = NULL; /* method to perform */
108         char* a_l                       = NULL; /* request api level */
109         int   isXML                     = 0;
110         int   api_level = 1;
111         apr_table_t* cookies = NULL;
112
113         r->allowed |= (AP_METHOD_BIT << M_GET);
114         r->allowed |= (AP_METHOD_BIT << M_POST);
115
116         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: parsing URL params");
117         string_array* mparams   = NULL;
118         string_array* params            = apacheParseParms(r); /* free me */
119         service         = apacheGetFirstParamValue( params, "service" );
120         method          = apacheGetFirstParamValue( params, "method" ); 
121         format          = apacheGetFirstParamValue( params, "format" ); 
122         a_l                     = apacheGetFirstParamValue( params, "api_level" ); 
123         mparams         = apacheGetParamValues( params, "param" ); /* free me */
124
125         if (a_l)
126                 api_level = atoi(a_l);
127
128         if (format && !strcasecmp(format, "xml" )) {
129                 isXML = 1;
130                 ap_set_content_type(r, "application/xml");
131         } else {
132                 ap_set_content_type(r, "text/plain");
133         }
134
135         int ret = OK;
136
137         if(!(service && method) || 
138                 !osrfStringArrayContains(allowedServices, service)) {
139
140                 osrfLogError(OSRF_LOG_MARK, 
141                         "Service [%s] not found or not allowed", service);
142                 ret = HTTP_NOT_FOUND;
143
144         } else {
145
146                 if( 0 && (cookies = apacheParseCookies(r)) ) {
147                         const char* authtoken = apr_table_get(cookies, "ses");
148                         osrfLogInfo(OSRF_LOG_MARK, "SESSION = %s", authtoken);
149                 }
150
151
152                 osrfLogInfo( OSRF_LOG_MARK,  "[%s] service=%s, method=%s", 
153                                 r->connection->remote_ip, service, method );
154
155                 osrfAppSession* session = osrf_app_client_session_init(service);
156
157                 double starttime = get_timestamp_millis();
158                 int req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams );
159                 osrf_message* omsg = NULL;
160
161                 int statuscode = 200;
162
163                 /* kick off the object */
164                 if (isXML)
165                         ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
166                 else
167                         ap_rputs("{\"payload\":[", r);
168
169                 int morethan1           = 0;
170                 char* statusname        = NULL;
171                 char* statustext        = NULL;
172                 char* output            = NULL;
173
174                 while((omsg = osrfAppSessionRequestRecv( session, req_id, 60 ))) {
175         
176                         statuscode = omsg->status_code;
177                         jsonObject* res;        
178
179                         if( ( res = osrfMessageGetResult(omsg)) ) {
180
181                                 if (isXML) {
182                                         output = jsonObjectToXML( res );
183                                 } else {
184                                         output = jsonObjectToJSON( res );
185                                         if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
186                                 }
187                                 ap_rputs(output, r);
188                                 free(output);
189                                 morethan1 = 1;
190                 
191                         } else {
192         
193                                 if( statuscode > 299 ) { /* the request returned a low level error */
194                                         statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
195                                         statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
196                                         osrfLogError( OSRF_LOG_MARK,  "Gateway received error: %s", statustext );
197                                 }
198                         }
199         
200                         osrf_message_free(omsg);
201                         if(statusname) break;
202                 }
203
204                 double duration = get_timestamp_millis() - starttime;
205                 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %lf seconds", duration);
206
207
208                 if (isXML)
209                         ap_rputs("</payload>", r);
210                 else
211                         ap_rputs("]",r); /* finish off the payload array */
212
213                 if(statusname) {
214
215                         /* add a debug field if the request died */
216                         ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, 
217                                         "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
218                         int l = strlen(statusname) + strlen(statustext) + 32;
219                         char buf[l];
220                         bzero(buf,l);
221
222                         if (isXML)
223                                 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
224
225                         else {
226                                 char bb[l];
227                                 bzero(bb, l);
228                                 snprintf(bb, l,  "%s : %s", statusname, statustext);
229                                 jsonObject* tmp = jsonNewObject(bb);
230                                 char* j = jsonObjectToJSON(tmp);
231                                 snprintf( buf, l, ",\"debug\": %s", j);
232                                 free(j);
233                                 jsonObjectFree(tmp);
234                         }
235
236                         ap_rputs(buf, r);
237
238                         free(statusname);
239                         free(statustext);
240                 }
241
242                 /* insert the status code */
243                 char buf[32];
244                 bzero(buf,32);
245
246                 if (isXML)
247                         snprintf(buf, 32, "<status>%d</status>", statuscode );
248                 else
249                         snprintf(buf, 32, ",\"status\":%d", statuscode );
250
251                 ap_rputs( buf, r );
252
253                 if (isXML)
254                         ap_rputs("</response>", r);
255                 else
256                         ap_rputs( "}", r ); /* finish off the object */
257
258                 osrf_app_session_destroy(session);
259         }
260
261         osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
262         string_array_destroy(params);
263         string_array_destroy(mparams);
264
265         osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
266
267         return ret;
268 }
269
270
271 #ifdef OSRF_GATEWAY_NASTY_DEBUG
272 /* --------------------------------------------------------------------- */
273 /* DEBUG HOOKS */
274 static int osrf_dbg_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp){
275         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_pre_config hook");
276         return OK;
277 }
278 static int osrf_dbg_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
279         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_post_config hook");
280         return OK;
281 }
282 static int osrf_dbg_open_logs(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
283         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_open_logs hook");
284         return OK;
285 }
286 static int osrf_dbg_quick_handler(request_rec *r, int lookup_uri) {
287         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_quick_handler hook");
288         return DECLINED;
289 }
290 static int osrf_dbg_pre_connection(conn_rec *c, void *csd) {
291         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_pre_connection hook");
292         return OK;
293 }
294 static int osrf_dbg_process_connection(conn_rec *c) {
295         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_process_connection hook");
296         return DECLINED;
297 }
298 static int osrf_dbg_post_read_request(request_rec *r) {
299         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_read_request hook");
300         return DECLINED;
301 }
302 static int osrf_dbg_logger(request_rec *r) {
303         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_dbg_logger hook");
304         return DECLINED;
305 }
306 static int osrf_dbg_translate_handler(request_rec *r) {
307         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_translate_handler hook");
308         return DECLINED;
309 }
310 static int osrf_dbg_header_parser_handler(request_rec *r) {
311         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_header_parser_handler hook");
312         return DECLINED;
313 }
314 static int osrf_dbg_check_user_id(request_rec *r) {
315         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_check_user_id hook");
316         return DECLINED;
317 }
318 static int osrf_dbg_fixer_upper(request_rec *r) {
319         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_fixer_upper hook");
320         return OK;
321 }
322 static int osrf_dbg_type_checker(request_rec *r) {
323         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_type_checker hook");
324         return DECLINED;
325 }
326 static int osrf_dbg_access_checker(request_rec *r) {
327         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_access_checker hook");
328         return DECLINED;
329 }
330 static int osrf_dbg_auth_checker(request_rec *r) {
331         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_auth_checker hook");
332         return DECLINED;
333 }
334 static void osrf_dbg_insert_filter(request_rec *r) {
335         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_insert_filter hook");
336 }
337 /* --------------------------------------------------------------------- */
338 #endif
339
340
341 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
342         ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
343         ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
344
345 #ifdef OSRF_GATEWAY_NASTY_DEBUG
346         ap_hook_pre_config(osrf_dbg_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
347         ap_hook_post_config(osrf_dbg_post_config, NULL, NULL, APR_HOOK_MIDDLE);
348         ap_hook_open_logs(osrf_dbg_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
349         ap_hook_quick_handler(osrf_dbg_quick_handler, NULL, NULL, APR_HOOK_MIDDLE);
350         ap_hook_pre_connection(osrf_dbg_pre_connection, NULL, NULL, APR_HOOK_MIDDLE);
351         ap_hook_process_connection(osrf_dbg_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
352         ap_hook_post_read_request(osrf_dbg_post_read_request, NULL, NULL, APR_HOOK_MIDDLE);
353         ap_hook_log_transaction(osrf_dbg_logger, NULL, NULL, APR_HOOK_MIDDLE);
354         ap_hook_translate_name(osrf_dbg_translate_handler, NULL, NULL, APR_HOOK_MIDDLE);
355         ap_hook_header_parser(osrf_dbg_header_parser_handler, NULL, NULL, APR_HOOK_MIDDLE);
356         ap_hook_check_user_id(osrf_dbg_check_user_id, NULL, NULL, APR_HOOK_MIDDLE);
357         ap_hook_fixups(osrf_dbg_fixer_upper, NULL, NULL, APR_HOOK_MIDDLE);
358         ap_hook_type_checker(osrf_dbg_type_checker, NULL, NULL, APR_HOOK_MIDDLE);
359         ap_hook_access_checker(osrf_dbg_access_checker, NULL, NULL, APR_HOOK_MIDDLE);
360         ap_hook_auth_checker(osrf_dbg_auth_checker, NULL, NULL, APR_HOOK_MIDDLE);
361         ap_hook_insert_filter(osrf_dbg_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
362 #endif
363 }
364
365
366 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
367         STANDARD20_MODULE_STUFF,
368         NULL,
369         NULL,
370         osrf_json_gateway_create_config,
371         NULL,
372         osrf_json_gateway_cmds,
373         osrf_json_gateway_register_hooks,
374 };
375
376
377
378