2 Copyright 2002 Kevin O'Donnell
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Include the core server components.
23 #include "http_config.h"
24 #include "http_core.h"
25 #include "http_protocol.h"
26 #include "apr_compat.h"
27 #include "apr_strings.h"
32 #include "opensrf/transport_client.h"
33 #include "opensrf/osrf_message.h"
34 #include "opensrf/osrf_app_session.h"
35 #include "opensrf/string_array.h"
39 * This function is registered as a handler for HTTP methods and will
40 * therefore be invoked for all GET requests (and others). Regardless
41 * of the request type, this function simply sends a message to
42 * STDERR (which httpd redirects to logs/error_log). A real module
43 * would do *alot* more at this point.
45 #define MODULE_NAME "ils_gateway_module"
48 struct session_list_struct {
49 osrf_app_session* session;
50 struct session_list_struct* next;
53 typedef struct session_list_struct session_list;
55 static session_list* the_list = NULL;
57 static void del_session( char* service ) {
58 if(!service || ! the_list)
61 debug_handler("In del_sesion for %s", service );
62 session_list* prev = the_list;
63 session_list* item = prev->next;
65 if(!strcmp(prev->session->remote_service, service)) {
66 info_handler("Removing gateway session for %s", service );
68 osrf_app_session_destroy(prev->session);
74 if( !strcmp(item->session->remote_service, service)) {
75 info_handler("Removing gateway session for %s", service );
76 prev->next = item->next;
77 osrf_app_session_destroy(item->session);
85 warning_handler("Attempt to remove gateway session "
86 "that does not exist: %s", service );
90 // if(update) we add 1 to the serve_count
91 static osrf_app_session* find_session( char* service, int update ) {
93 session_list* item = the_list;
96 if(!strcmp(item->session->remote_service,service)) {
98 if( item->serve_count++ > 20 ) {
99 debug_handler("Disconnected session on 20 requests => %s", service);
100 osrf_app_session_disconnect(item->session);
101 del_session(service);
103 //item->serve_count = 0;
106 debug_handler("Found session for %s in gateway cache", service);
107 return item->session;
115 static void add_session( char* service, osrf_app_session* session ) {
119 if(find_session(service,0))
122 debug_handler("Add session for %s to the cache", service );
124 session_list* new_item = (session_list*) safe_malloc(sizeof(session_list));
125 new_item->session = session;
126 //new_item->service = service;
127 new_item->serve_count = 0;
130 session_list* second = the_list->next;
132 new_item->next = second;
139 static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
140 if( ! osrf_system_bootstrap_client(
141 "/pines/cvs/ILS/OpenSRF/src/gateway/gateway.xml") ) { /* config option */
144 /* we don't want to waste time parsing json that we're not going to look at*/
145 osrf_message_set_json_parse_result(0);
146 osrf_message_set_json_parse_params(0);
147 fprintf(stderr, "Bootstrapping %d\n", getpid() );
151 static int mod_ils_gateway_method_handler (request_rec *r) {
154 /* make sure we're needed first thing*/
155 if (strcmp(r->handler, MODULE_NAME ))
159 apr_pool_t *p = r->pool; /* memory pool */
160 char* arg = r->args; /* url query string */
162 char* service = NULL; /* service to connect to */
163 char* method = NULL; /* method to perform */
165 json* exception = NULL; /* returned in error conditions */
166 string_array* sarray = init_string_array(12); /* method parameters */
168 growing_buffer* buffer = NULL; /* POST data */
169 growing_buffer* tmp_buf = NULL; /* temp buffer */
171 char* key = NULL; /* query item name */
172 char* val = NULL; /* query item value */
176 /* verify we are connected */
177 if(!osrf_system_get_transport_client()) {
178 fatal_handler("Bootstrap Failed, no transport client");
179 return HTTP_INTERNAL_SERVER_ERROR;
182 /* set content type to text/plain for passing around JSON objects */
183 ap_set_content_type(r, "text/plain");
187 /* gather the post args and append them to the url query string */
188 if( !strcmp(r->method,"POST") ) {
190 ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK);
192 if(! ap_should_client_block(r)) {
193 warning_handler("No Post Body");
198 buffer = buffer_init(1025);
200 while(ap_get_client_block(r, body, 1024)) {
201 debug_handler("Apache read POST block data: %s\n", body);
202 buffer_add( buffer, body );
207 tmp_buf = buffer_init(1024);
208 buffer_add(tmp_buf,arg);
209 buffer_add(tmp_buf,buffer->buf);
210 arg = (char*) apr_pstrdup(p, tmp_buf->buf);
211 buffer_free(tmp_buf);
213 arg = (char*) apr_pstrdup(p, buffer->buf);
219 debug_handler("params args are %s", arg);
222 if( ! arg || !arg[0] ) { /* we received no request */
223 warning_handler("No Args");
227 r->allowed |= (AP_METHOD_BIT << M_GET);
228 r->allowed |= (AP_METHOD_BIT << M_POST);
231 //char* argcopy = (char*) apr_pstrdup(p, arg);
233 //while( argcopy && (val = ap_getword(p, (const char**) &argcopy, '&'))) {
234 while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
236 //const char* val2 = val;
238 key = ap_getword(r->pool, (const char**) &val, '=');
242 ap_unescape_url((char*)key);
243 ap_unescape_url((char*)val);
245 if(!strcmp(key,"service"))
248 if(!strcmp(key,"method"))
251 if(!strcmp(key,"__param"))
252 string_array_add(sarray, val);
256 info_handler("Performing(%d): service %s | method %s | \n",
257 getpid(), service, method );
260 for( k = 0; k!= sarray->size; k++ ) {
261 info_handler( "param %s", string_array_get_string(sarray,k));
265 osrf_app_session* session = find_session(service,1);
268 debug_handler("building new session for %s", service );
269 session = osrf_app_client_session_init(service);
270 add_session(service, session);
274 osrf_app_session* session = osrf_app_client_session_init(service);
276 debug_handler("MOD session service: %s", session->remote_service );
279 /* connect to the remote service */
281 if(!osrf_app_session_connect(session)) {
282 exception = json_object_new_object();
283 json_object_object_add( exception, "is_err", json_object_new_int(1));
284 json_object_object_add( exception,
285 "err_msg", json_object_new_string("Unable to connect to remote service"));
287 ap_rputs(json_object_to_json_string(exception), r );
288 json_object_put(exception);
293 int req_id = osrf_app_session_make_request( session, NULL, method, 1, sarray );
294 string_array_destroy(sarray);
296 osrf_message* omsg = NULL;
298 growing_buffer* result_data = buffer_init(256);
299 buffer_add(result_data, "[");
301 /* gather result data */
302 while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) {
304 if( omsg->result_string ) {
305 buffer_add(result_data, omsg->result_string);
306 buffer_add( result_data, ",");
311 /* build the exception information */
312 growing_buffer* exc_buffer = buffer_init(256);
313 buffer_add( exc_buffer, "\nReceived Exception:\nName: " );
314 buffer_add( exc_buffer, omsg->status_name );
315 buffer_add( exc_buffer, "\nStatus: " );
316 buffer_add( exc_buffer, omsg->status_text );
317 buffer_add( exc_buffer, "\nStatus: " );
320 sprintf( code, "%d", omsg->status_code );
321 buffer_add( exc_buffer, code );
323 /* build the exception object */
324 exception = json_object_new_object();
325 json_object_object_add( exception, "is_err", json_object_new_int(1));
326 json_object_object_add( exception,
327 "err_msg", json_object_new_string(exc_buffer->buf));
329 warning_handler("*** Looks like we got a "
330 "server exception\n%s", exc_buffer->buf );
332 buffer_free(exc_buffer);
333 osrf_message_free(omsg);
337 osrf_message_free(omsg);
341 /* remove trailing comma */
342 if( result_data->buf[strlen(result_data->buf)-1] == ',') {
343 result_data->buf[strlen(result_data->buf)-1] = '\0';
344 result_data->n_used--;
347 buffer_add(result_data,"]");
349 char* content = NULL;
351 /* round up our data */
353 content = strdup(json_object_to_json_string(exception));
354 json_object_put(exception);
356 content = buffer_data(result_data);
359 buffer_free(result_data);
362 debug_handler( "APACHE writing data to web client: %s", content );
367 osrf_app_session_request_finish( session, req_id );
368 debug_handler("gateway process message successfully");
371 osrf_app_session_destroy(session);
377 * This function is a callback and it declares what other functions
378 * should be called for request processing and configuration requests.
379 * This callback function declares the Handlers for other events. */
380 static void mod_ils_gateway_register_hooks (apr_pool_t *p) {
381 // I think this is the call to make to register a handler for method calls (GET PUT et. al.).
382 // We will ask to be last so that the comment has a higher tendency to
384 ap_hook_handler(mod_ils_gateway_method_handler, NULL, NULL, APR_HOOK_MIDDLE);
385 ap_hook_child_init(mod_ils_gateway_child_init,NULL,NULL,APR_HOOK_MIDDLE);
389 * Declare and populate the module's data structure. The
390 * name of this structure ('tut1_module') is important - it
391 * must match the name of the module. This structure is the
392 * only "glue" between the httpd core and the module.
395 module AP_MODULE_DECLARE_DATA ils_gateway_module =
397 STANDARD20_MODULE_STUFF,
403 mod_ils_gateway_register_hooks,