-/*
-Copyright 2002 Kevin O'Donnell
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
-
-/*
- * Include the core server components.
- */
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "apr_compat.h"
#include "apr_strings.h"
-
-
/* our stuff */
#include "opensrf/transport_client.h"
#include "opensrf/osrf_message.h"
#ifdef RESTGATEWAY
#include "rest_xml.h"
-/*
- * This function is registered as a handler for HTTP methods and will
- * therefore be invoked for all GET requests (and others). Regardless
- * of the request type, this function simply sends a message to
- * STDERR (which httpd redirects to logs/error_log). A real module
- * would do *alot* more at this point.
- */
#define MODULE_NAME "ils_rest_gateway_module"
#else
#define MODULE_NAME "ils_gateway_module"
#endif
-/*
-struct session_list_struct {
- osrf_app_session* session;
- struct session_list_struct* next;
- int serve_count;
-};
-typedef struct session_list_struct session_list;
-
-static session_list* the_list = NULL;
-
-static void del_session( char* service ) {
- if(!service || ! the_list)
- return;
-
- debug_handler("In del_sesion for %s", service );
- session_list* prev = the_list;
- session_list* item = prev->next;
-
- if(!strcmp(prev->session->remote_service, service)) {
- info_handler("Removing gateway session for %s", service );
- the_list = item;
- osrf_app_session_destroy(prev->session);
- free(prev);
- return;
- }
-
- while(item) {
- if( !strcmp(item->session->remote_service, service)) {
- info_handler("Removing gateway session for %s", service );
- prev->next = item->next;
- osrf_app_session_destroy(item->session);
- free(item);
- return;
- }
- prev = item;
- item = item->next;
- }
-
- warning_handler("Attempt to remove gateway session "
- "that does not exist: %s", service );
-
-}
-
-// if(update) we add 1 to the serve_count
-static osrf_app_session* find_session( char* service, int update ) {
-
- session_list* item = the_list;
- while(item) {
-
- if(!strcmp(item->session->remote_service,service)) {
- if(update) {
- if( item->serve_count++ > 20 ) {
- debug_handler("Disconnected session on 20 requests => %s", service);
- osrf_app_session_disconnect(item->session);
- del_session(service);
- return NULL;
- //item->serve_count = 0;
- }
- }
- debug_handler("Found session for %s in gateway cache", service);
- return item->session;
- }
-
- item = item->next;
- }
- return NULL;
-}
-
-static void add_session( char* service, osrf_app_session* session ) {
-
- if(!session) return;
-
- if(find_session(service,0))
- return;
-
- debug_handler("Add session for %s to the cache", service );
-
- session_list* new_item = (session_list*) safe_malloc(sizeof(session_list));
- new_item->session = session;
- //new_item->service = service;
- new_item->serve_count = 0;
-
- if(the_list) {
- session_list* second = the_list->next;
- the_list = new_item;
- new_item->next = second;
- } else {
- the_list = new_item;
- }
-}
-*/
static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) {
- if( ! osrf_system_bootstrap_client( "/openils/conf/gateway.xml") ) {
+ if( ! osrf_system_bootstrap_client( "/openils/conf/gateway.xml") )
fatal_handler("Unable to load gateway config file...");
- }
-
- /* we don't want to waste time parsing json that we're not going to look at*/
- osrf_message_set_json_parse_result(0);
- osrf_message_set_json_parse_params(0);
fprintf(stderr, "Bootstrapping %d\n", getpid() );
fflush(stderr);
}
r->allowed |= (AP_METHOD_BIT << M_POST);
- //char* argcopy = (char*) apr_pstrdup(p, arg);
-
- //while( argcopy && (val = ap_getword(p, (const char**) &argcopy, '&'))) {
while( arg && (val = ap_getword(p, (const char**) &arg, '&'))) {
- //const char* val2 = val;
-
key = ap_getword(r->pool, (const char**) &val, '=');
if(!key || !key[0])
break;
info_handler( "param %s", string_array_get_string(sarray,k));
}
- /*
- osrf_app_session* session = find_session(service,1);
-
- if(!session) {
- debug_handler("building new session for %s", service );
- session = osrf_app_client_session_init(service);
- add_session(service, session);
- }
- */
-
osrf_app_session* session = osrf_app_client_session_init(service);
debug_handler("MOD session service: %s", session->remote_service );
-
- /* connect to the remote service */
- /*
- if(!osrf_app_session_connect(session)) {
- exception = json_object_new_object();
- json_object_object_add( exception, "is_err", json_object_new_int(1));
- json_object_object_add( exception,
- "err_msg", json_object_new_string("Unable to connect to remote service"));
-
- ap_rputs(json_object_to_json_string(exception), r );
- json_object_put(exception);
- return OK;
- }
- */
-
int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray );
string_array_destroy(sarray);
sprintf( code, "%d", omsg->status_code );
buffer_add( exc_buffer, code );
- /* build the exception object */
- /*
- exception = json_object_new_object();
- json_object_object_add( exception, "is_err", json_object_new_int(1));
- json_object_object_add( exception,
- "err_msg", json_object_new_string(exc_buffer->buf));
- */
-
exception = json_parse_string("{}");
exception->add_key(exception, "is_err", json_parse_string("1"));
exception->add_key(exception, "err_msg", new_object(exc_buffer->buf) );
/* round up our data */
if(exception) {
- //content = strdup(json_object_to_json_string(exception));
- //json_object_put(exception);
content = strdup(exception->to_json(exception));
free_object(exception);
} else {