]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/gateway/osrf_json_gateway.c
6de37c909996d6da2829e85d0af41f7a88edd3e5
[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                 /*
147                 if( 0 && (cookies = apacheParseCookies(r)) ) {
148                         const char* authtoken = apr_table_get(cookies, "ses");
149                         osrfLogInfo(OSRF_LOG_MARK, "SESSION = %s", authtoken);
150                 }
151                 */
152
153
154                 osrfLogInfo( OSRF_LOG_MARK,  "[%s] service=%s, method=%s", 
155                                 r->connection->remote_ip, service, method );
156
157                 osrfAppSession* session = osrf_app_client_session_init(service);
158
159                 double starttime = get_timestamp_millis();
160                 int req_id = osrf_app_session_make_req( session, NULL, method, api_level, mparams );
161                 osrf_message* omsg = NULL;
162
163                 int statuscode = 200;
164
165                 /* kick off the object */
166                 if (isXML)
167                         ap_rputs("<response xmlns=\"http://opensrf.org/-/namespaces/gateway/v1\"><payload>", r);
168                 else
169                         ap_rputs("{\"payload\":[", r);
170
171                 int morethan1           = 0;
172                 char* statusname        = NULL;
173                 char* statustext        = NULL;
174                 char* output            = NULL;
175
176                 while((omsg = osrfAppSessionRequestRecv( session, req_id, 60 ))) {
177         
178                         statuscode = omsg->status_code;
179                         jsonObject* res;        
180
181                         if( ( res = osrfMessageGetResult(omsg)) ) {
182
183                                 if (isXML) {
184                                         output = jsonObjectToXML( res );
185                                 } else {
186                                         output = jsonObjectToJSON( res );
187                                         if( morethan1 ) ap_rputs(",", r); /* comma between JSON array items */
188                                 }
189                                 ap_rputs(output, r);
190                                 free(output);
191                                 morethan1 = 1;
192                 
193                         } else {
194         
195                                 if( statuscode > 299 ) { /* the request returned a low level error */
196                                         statusname = omsg->status_name ? strdup(omsg->status_name) : strdup("Unknown Error");
197                                         statustext = omsg->status_text ? strdup(omsg->status_text) : strdup("No Error Message");
198                                         osrfLogError( OSRF_LOG_MARK,  "Gateway received error: %s", statustext );
199                                 }
200                         }
201         
202                         osrf_message_free(omsg);
203                         if(statusname) break;
204                 }
205
206                 double duration = get_timestamp_millis() - starttime;
207                 osrfLogDebug(OSRF_LOG_MARK, "gateway request took %lf seconds", duration);
208
209
210                 if (isXML)
211                         ap_rputs("</payload>", r);
212                 else
213                         ap_rputs("]",r); /* finish off the payload array */
214
215                 if(statusname) {
216
217                         /* add a debug field if the request died */
218                         ap_log_rerror( APLOG_MARK, APLOG_INFO, 0, r, 
219                                         "OpenSRF JSON Request returned error: %s -> %s", statusname, statustext );
220                         int l = strlen(statusname) + strlen(statustext) + 32;
221                         char buf[l];
222                         bzero(buf,l);
223
224                         if (isXML)
225                                 snprintf( buf, l, "<debug>\"%s : %s\"</debug>", statusname, statustext );
226
227                         else {
228                                 char bb[l];
229                                 bzero(bb, l);
230                                 snprintf(bb, l,  "%s : %s", statusname, statustext);
231                                 jsonObject* tmp = jsonNewObject(bb);
232                                 char* j = jsonObjectToJSON(tmp);
233                                 snprintf( buf, l, ",\"debug\": %s", j);
234                                 free(j);
235                                 jsonObjectFree(tmp);
236                         }
237
238                         ap_rputs(buf, r);
239
240                         free(statusname);
241                         free(statustext);
242                 }
243
244                 /* insert the status code */
245                 char buf[32];
246                 bzero(buf,32);
247
248                 if (isXML)
249                         snprintf(buf, 32, "<status>%d</status>", statuscode );
250                 else
251                         snprintf(buf, 32, ",\"status\":%d", statuscode );
252
253                 ap_rputs( buf, r );
254
255                 if (isXML)
256                         ap_rputs("</response>", r);
257                 else
258                         ap_rputs( "}", r ); /* finish off the object */
259
260                 osrf_app_session_destroy(session);
261         }
262
263         osrfLogInfo(OSRF_LOG_MARK, "Completed processing service=%s, method=%s", service, method);
264         string_array_destroy(params);
265         string_array_destroy(mparams);
266
267         osrfLogDebug(OSRF_LOG_MARK, "Gateway served %d requests", ++numserved);
268
269         return ret;
270 }
271
272
273 #ifdef OSRF_GATEWAY_NASTY_DEBUG
274 /* --------------------------------------------------------------------- */
275 /* DEBUG HOOKS */
276 static int osrf_dbg_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp){
277         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_pre_config hook");
278         return OK;
279 }
280 static int osrf_dbg_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
281         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_post_config hook");
282         return OK;
283 }
284 static int osrf_dbg_open_logs(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
285         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_open_logs hook");
286         return OK;
287 }
288 static int osrf_dbg_quick_handler(request_rec *r, int lookup_uri) {
289         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_quick_handler hook");
290         return DECLINED;
291 }
292 static int osrf_dbg_pre_connection(conn_rec *c, void *csd) {
293         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_pre_connection hook");
294         return OK;
295 }
296 static int osrf_dbg_process_connection(conn_rec *c) {
297         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_process_connection hook");
298         return DECLINED;
299 }
300 static int osrf_dbg_post_read_request(request_rec *r) {
301         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_read_request hook");
302         return DECLINED;
303 }
304 static int osrf_dbg_logger(request_rec *r) {
305         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_dbg_logger hook");
306         return DECLINED;
307 }
308 static int osrf_dbg_translate_handler(request_rec *r) {
309         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_translate_handler hook");
310         return DECLINED;
311 }
312 static int osrf_dbg_header_parser_handler(request_rec *r) {
313         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_header_parser_handler hook");
314         return DECLINED;
315 }
316 static int osrf_dbg_check_user_id(request_rec *r) {
317         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_check_user_id hook");
318         return DECLINED;
319 }
320 static int osrf_dbg_fixer_upper(request_rec *r) {
321         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_fixer_upper hook");
322         return OK;
323 }
324 static int osrf_dbg_type_checker(request_rec *r) {
325         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_type_checker hook");
326         return DECLINED;
327 }
328 static int osrf_dbg_access_checker(request_rec *r) {
329         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_access_checker hook");
330         return DECLINED;
331 }
332 static int osrf_dbg_auth_checker(request_rec *r) {
333         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_auth_checker hook");
334         return DECLINED;
335 }
336 static void osrf_dbg_insert_filter(request_rec *r) {
337         osrfLogDebug(OSRF_LOG_MARK, "osrf gateway: osrf_dbg_insert_filter hook");
338 }
339 /* --------------------------------------------------------------------- */
340 #endif
341
342
343 static void osrf_json_gateway_register_hooks (apr_pool_t *p) {
344         ap_hook_handler(osrf_json_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
345         ap_hook_child_init(osrf_json_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
346
347 #ifdef OSRF_GATEWAY_NASTY_DEBUG
348         ap_hook_pre_config(osrf_dbg_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
349         ap_hook_post_config(osrf_dbg_post_config, NULL, NULL, APR_HOOK_MIDDLE);
350         ap_hook_open_logs(osrf_dbg_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
351         ap_hook_quick_handler(osrf_dbg_quick_handler, NULL, NULL, APR_HOOK_MIDDLE);
352         ap_hook_pre_connection(osrf_dbg_pre_connection, NULL, NULL, APR_HOOK_MIDDLE);
353         ap_hook_process_connection(osrf_dbg_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
354         ap_hook_post_read_request(osrf_dbg_post_read_request, NULL, NULL, APR_HOOK_MIDDLE);
355         ap_hook_log_transaction(osrf_dbg_logger, NULL, NULL, APR_HOOK_MIDDLE);
356         ap_hook_translate_name(osrf_dbg_translate_handler, NULL, NULL, APR_HOOK_MIDDLE);
357         ap_hook_header_parser(osrf_dbg_header_parser_handler, NULL, NULL, APR_HOOK_MIDDLE);
358         ap_hook_check_user_id(osrf_dbg_check_user_id, NULL, NULL, APR_HOOK_MIDDLE);
359         ap_hook_fixups(osrf_dbg_fixer_upper, NULL, NULL, APR_HOOK_MIDDLE);
360         ap_hook_type_checker(osrf_dbg_type_checker, NULL, NULL, APR_HOOK_MIDDLE);
361         ap_hook_access_checker(osrf_dbg_access_checker, NULL, NULL, APR_HOOK_MIDDLE);
362         ap_hook_auth_checker(osrf_dbg_auth_checker, NULL, NULL, APR_HOOK_MIDDLE);
363         ap_hook_insert_filter(osrf_dbg_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
364 #endif
365 }
366
367
368 module AP_MODULE_DECLARE_DATA osrf_json_gateway_module = {
369         STANDARD20_MODULE_STUFF,
370         NULL,
371         NULL,
372         osrf_json_gateway_create_config,
373         NULL,
374         osrf_json_gateway_cmds,
375         osrf_json_gateway_register_hooks,
376 };
377
378
379
380