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