]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/gateway/mod_ils_gateway.c
re-arranged the log code so that VA_LIST_TO_ARGS (which runs vsnprintf)
[Evergreen.git] / OpenSRF / src / gateway / mod_ils_gateway.c
1 #include "mod_ils_gateway.h"
2 #include "http_log.h"
3
4 char* ils_gateway_config_file = NULL;
5 char* ils_rest_gateway_config_file = NULL;
6
7 static const char* ils_gateway_set_config(cmd_parms *parms, void *config, const char *arg) {
8         ils_gateway_config  *cfg;
9         cfg = ap_get_module_config(parms->server->module_config, &ils_gateway_module);
10         cfg->configfile = (char*) arg;
11         ils_gateway_config_file = (char*) arg;
12         return NULL;
13 }
14
15 /* tell apache about our commands */
16 static const command_rec ils_gateway_cmds[] = {
17         AP_INIT_TAKE1( GATEWAY_CONFIG, ils_gateway_set_config, NULL, RSRC_CONF, "gateway config file"),
18         {NULL}
19 };
20
21 /* build the config object */
22 static void* ils_gateway_create_config( apr_pool_t* p, server_rec* s) {
23         ils_gateway_config* cfg = (ils_gateway_config*) apr_palloc(p, sizeof(ils_gateway_config));
24         cfg->configfile = GATEWAY_DEFAULT_CONFIG;
25         return (void*) cfg;
26 }
27
28
29 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
30
31         char* cfg = ils_gateway_config_file;
32         if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) ) {
33                 osrfLogError("Unable to bootstrap client in gateway...");
34                 return;
35         }
36         fprintf(stderr, "Bootstrapping %d\n", getpid() );
37         fflush(stderr);
38 }
39
40 static int mod_ils_gateway_method_handler (request_rec *r) {
41
42         /* make sure we're needed first thing*/
43         if (strcmp(r->handler, MODULE_NAME )) 
44                 return DECLINED;
45         
46         osrfLogSetAppname("osrf_json_gw");
47
48         apr_pool_t *p = r->pool;        /* memory pool */
49         char* arg = r->args;                    /* url query string */
50
51         char* service                                   = NULL; /* service to connect to */
52         char* method                                    = NULL; /* method to perform */
53
54         string_array* sarray                    = init_string_array(12); /* method parameters */
55
56         growing_buffer* buffer          = NULL; /* POST data */
57         growing_buffer* tmp_buf         = NULL; /* temp buffer */
58
59         char* key                                               = NULL; /* query item name */
60         char* val                                               = NULL; /* query item value */
61
62         jsonObject* response                    = jsonParseString("{ }");
63         jsonObject* payload                     = jsonParseString("[ ]");
64         jsonObjectSetKey(response, "payload", payload );
65
66         /* verify we are connected */
67         if(!osrf_system_get_transport_client()) {
68                 osrfLogError("Bootstrap Failed, no transport client");
69                 return HTTP_INTERNAL_SERVER_ERROR;
70         }
71
72         ap_set_content_type(r, "text/plain");
73         osrfLogDebug("Apache request method: %s", r->method );
74
75         /* gather the post args and append them to the url query string */
76         if( !strcmp(r->method,"POST") ) {
77
78                 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
79
80                 if(! ap_should_client_block(r)) {
81                         osrfLogWarning("No Post Body");
82                         ap_rputs("null", r);
83                         return OK;
84                 }
85
86                 int BUFL = 1024;
87                 char body[BUFL];
88                 bzero(body, BUFL);
89                 buffer = buffer_init(BUFL);
90
91                 while(ap_get_client_block(r, body, BUFL - 1 )) {
92                         buffer_add( buffer, body );
93                         bzero(body, BUFL);
94                 }
95
96                 if( buffer->n_used < 1 ) {
97                         osrfLogWarning("No Post Body");
98                         ap_rputs("null", r);
99                         return OK;
100                 }
101
102                 if(arg && strlen(arg) > 0 ) {
103                         tmp_buf = buffer_init(BUFL);
104                         buffer_add(tmp_buf,arg);
105                         buffer_add(tmp_buf,buffer->buf);
106                         arg = (char*) apr_pstrdup(p, tmp_buf->buf);
107                         buffer_free(tmp_buf);
108                 } else {
109                         arg = (char*) apr_pstrdup(p, buffer->buf);
110                 }
111                 buffer_free(buffer);
112
113         } 
114
115         if( ! arg || strlen(arg) == 0 ) { /* we received no request */
116                 osrfLogWarning("No Args");
117                 ap_rputs("null", r);
118                 return OK;
119         }
120
121         osrfLogDebug("URL: %s", arg );
122         ap_log_rerror( APLOG_MARK, APLOG_DEBUG, 0, r, "URL: %s", arg );
123
124         r->allowed |= (AP_METHOD_BIT << M_GET);
125         r->allowed |= (AP_METHOD_BIT << M_POST);
126         
127         while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
128
129                 key = ap_getword(r->pool, (const char**) &val, '=');
130                 if(!key || !key[0])
131                         break;
132
133                 ap_unescape_url((char*)key);
134                 ap_unescape_url((char*)val);
135
136                 if(!strcmp(key,"service")) 
137                         service = val;
138
139                 if(!strcmp(key,"method"))
140                         method = val;
141
142                 if(!strcmp(key,"param"))
143                         string_array_add(sarray, val);
144
145         }
146
147         osrfLogInfo("\r\nPerforming(%d):  service %s "
148                         "| method %s |", getpid(), service, method );
149
150         int k;
151         for( k = 0; k!= sarray->size; k++ ) {
152                 osrfLogInfo( "param %s", string_array_get_string(sarray,k));
153         }
154
155         osrf_app_session* session = osrf_app_client_session_init(service);
156
157         osrfLogDebug("session service: %s", session->remote_service );
158
159         int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
160         string_array_destroy(sarray);
161
162         osrf_message* omsg = NULL;
163
164         while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
165
166                 jsonObjectSetKey(response, "status", jsonNewNumberObject(omsg->status_code));
167
168                 if( omsg->_result_content ) {
169                         jsonObjectPush( payload, jsonObjectClone(omsg->_result_content));
170         
171                 } else {
172
173                         if( omsg->status_code > 299 ) {
174                                 char* s = omsg->status_name ? omsg->status_name : "Unknown Error";
175                                 char* t = omsg->status_text ? omsg->status_text : "No Error Message";
176                                 jsonObjectSetKey(response, "debug", jsonNewObject("\n\n%s:\n%s\n", s, t));
177                                 osrfLogError( "Gateway received error: %s", 
178                                                 jsonObjectGetString(jsonObjectGetKey(response, "debug")));
179                                 break;
180                         }
181                 }
182
183                 osrf_message_free(omsg);
184                 omsg = NULL;
185         }
186
187         char* content = jsonObjectToJSON(response);
188         if(content) {
189                 osrfLogInfo( "Gateway responding with:  %s", content );
190                 ap_rputs(content,r);
191                 free(content);
192         } 
193         jsonObjectFree(response);
194
195         osrf_app_session_request_finish( session, req_id );
196         osrfLogDebug("gateway processed message successfully");
197
198         osrf_app_session_destroy(session);
199         return OK;
200 }
201
202 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
203         ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
204         ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
205 }
206
207
208 module AP_MODULE_DECLARE_DATA ils_gateway_module = {
209         STANDARD20_MODULE_STUFF,
210         NULL,
211         NULL,
212         ils_gateway_create_config,
213         NULL,
214         ils_gateway_cmds,
215         mod_ils_gateway_register_hooks,
216 };
217
218
219
220