From bc7584fd7b8de83adea32e263ac238f1763fa2b5 Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 25 Apr 2005 18:22:55 +0000 Subject: [PATCH] added the ability to bypass JSON parsing for adding request parameters. use osrf_message_add_param(json_string) instead of adding a json blob. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@290 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_app_session.h | 4 +- include/opensrf/osrf_message.h | 17 ++++- src/gateway/mod_ils_gateway.c | 23 +++--- src/libstack/Makefile | 4 +- src/libstack/osrf_app_session.c | 13 +++- src/libstack/osrf_message.c | 109 ++++++++++++++++++++++------- src/srfsh/srfsh.c | 11 ++- 7 files changed, 140 insertions(+), 41 deletions(-) diff --git a/include/opensrf/osrf_app_session.h b/include/opensrf/osrf_app_session.h index 02cc2ab..608f156 100644 --- a/include/opensrf/osrf_app_session.h +++ b/include/opensrf/osrf_app_session.h @@ -3,6 +3,7 @@ #include "opensrf/generic_utils.h" #include "osrf_message.h" #include "osrf_system.h" +#include "opensrf/string_array.h" #ifndef OSRF_APP_SESSION #define OSRF_APP_SESSION @@ -86,7 +87,8 @@ osrf_app_session* osrf_app_session_find_session( char* session_id ); * requeset. */ int osrf_app_session_make_request( - osrf_app_session* session, json* params, char* method_name, int protocol ); + osrf_app_session* session, json* params, + char* method_name, int protocol, string_array* arr ); /** Sets the given request to complete state */ void osrf_app_session_set_complete( osrf_app_session* session, int request_id ); diff --git a/include/opensrf/osrf_message.h b/include/opensrf/osrf_message.h index 1f992fc..f6c4ca1 100644 --- a/include/opensrf/osrf_message.h +++ b/include/opensrf/osrf_message.h @@ -1,5 +1,6 @@ #include "libjson/json.h" #include "opensrf/generic_utils.h" +#include "opensrf/string_array.h" #ifndef osrf_message_h #define osrf_message_h @@ -29,13 +30,16 @@ enum M_TYPE { CONNECT, REQUEST, RESULT, STATUS, DISCONNECT }; +#define OSRF_MAX_PARAMS 128; + struct osrf_message_struct { enum M_TYPE m_type; int thread_trace; int protocol; - int parse_json; + int parse_json_result; + int parse_json_params; /* if we're a STATUS message */ char* status_name; @@ -60,6 +64,9 @@ struct osrf_message_struct { we won't touch this variable */ struct osrf_message_struct* next; + string_array* parray; + char* full_param_string; + }; typedef struct osrf_message_struct osrf_message; @@ -78,7 +85,13 @@ int osrf_message_from_xml( char* xml, osrf_message* msgs[] ); /* decides whether all message automatically parse incoming json data */ /* to change a single message, set msg->parse_json accordingly */ -void osrf_message_set_json_parse( int bool ); +//void osrf_message_set_json_parse( int bool ); + +void osrf_message_set_json_parse_result( int ibool ); +void osrf_message_set_json_parse_params( int ibool ); + + +void osrf_message_add_param( osrf_message*, char* param_string ); diff --git a/src/gateway/mod_ils_gateway.c b/src/gateway/mod_ils_gateway.c index 56c5140..02e96f0 100644 --- a/src/gateway/mod_ils_gateway.c +++ b/src/gateway/mod_ils_gateway.c @@ -33,6 +33,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "opensrf/generic_utils.h" #include "opensrf/osrf_message.h" #include "opensrf/osrf_app_session.h" +#include "opensrf/string_array.h" #include "md5.h" /* @@ -144,7 +145,8 @@ static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) { } /* we don't want to waste time parsing json that we're not going to look at*/ - osrf_message_set_json_parse(0); + osrf_message_set_json_parse_result(0); + osrf_message_set_json_parse_params(0); fprintf(stderr, "Bootstrapping %d\n", getpid() ); fflush(stderr); } @@ -163,8 +165,8 @@ static int mod_ils_gateway_method_handler (request_rec *r) { char* service = NULL; /* service to connect to */ char* method = NULL; /* method to perform */ - json* params = NULL; /* method parameters */ json* exception = NULL; /* returned in error conditions */ + string_array* sarray = init_string_array(12); /* method parameters */ growing_buffer* buffer = NULL; /* POST data */ growing_buffer* tmp_buf = NULL; /* temp buffer */ @@ -228,7 +230,6 @@ static int mod_ils_gateway_method_handler (request_rec *r) { char* argcopy = (char*) apr_pstrdup(p, arg); - params = json_object_new_array();; while( argcopy && (val = ap_getword(p, &argcopy, '&'))) { key = ap_getword(r->pool,&val, '='); @@ -245,11 +246,17 @@ static int mod_ils_gateway_method_handler (request_rec *r) { method = val; if(!strcmp(key,"__param")) - json_object_array_add( params, json_tokener_parse(val)); + string_array_add(sarray, val); + } - info_handler("Performing(%d): service %s | method %s | \nparams %s\n\n", - getpid(), service, method, json_object_to_json_string(params)); + info_handler("Performing(%d): service %s | method %s | \n", + getpid(), service, method ); + + int k; + for( k = 0; k!= sarray->size; k++ ) { + info_handler( "param %s", string_array_get_string(sarray,k)); + } osrf_app_session* session = find_session(service,1); @@ -274,8 +281,8 @@ static int mod_ils_gateway_method_handler (request_rec *r) { return OK; } - int req_id = osrf_app_session_make_request( session, params, method, 1 ); - json_object_put(params); + int req_id = osrf_app_session_make_request( session, NULL, method, 1, sarray ); + string_array_destroy(sarray); osrf_message* omsg = NULL; diff --git a/src/libstack/Makefile b/src/libstack/Makefile index b75b5bb..b1117c1 100644 --- a/src/libstack/Makefile +++ b/src/libstack/Makefile @@ -1,7 +1,7 @@ LIB_DIR = ../../lib CC_OPTS = -Wall -O2 -I /usr/include/libxml2 -I /usr/include/libxml2/libxml -I ../../include -fPIC -LIB_SOURCES = osrf_message.c osrf_app_session.c osrf_stack.c osrf_system.c -LIB_TARGETS = osrf_message.o osrf_app_session.o osrf_stack.o osrf_system.o +LIB_SOURCES = osrf_message.c osrf_app_session.c osrf_stack.c osrf_system.c string_array.c +LIB_TARGETS = osrf_message.o osrf_app_session.o osrf_stack.o osrf_system.o string_array.o EXE_LD_OPTS = -L $(LIB_DIR) -lxml2 -lopensrf_transport -lopensrf_stack -ljson CC = gcc diff --git a/src/libstack/osrf_app_session.c b/src/libstack/osrf_app_session.c index 27a3a04..d3bdb63 100644 --- a/src/libstack/osrf_app_session.c +++ b/src/libstack/osrf_app_session.c @@ -309,11 +309,22 @@ void _osrf_app_session_free( osrf_app_session* session ){ int osrf_app_session_make_request( - osrf_app_session* session, json* params, char* method_name, int protocol ) { + osrf_app_session* session, json* params, + char* method_name, int protocol, string_array* param_strings ) { if(session == NULL) return -1; osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol ); osrf_message_set_request_info( req_msg, method_name, params ); + + /* if we're not parsing the json, shove the strings in manually */ + if(!req_msg->parse_json_params && param_strings) { + int i; + for(i = 0; i!= param_strings->size ; i++ ) { + osrf_message_add_param(req_msg, + string_array_get_string(param_strings,i)); + } + } + osrf_app_request* req = _osrf_app_request_init( session, req_msg ); if(!_osrf_app_session_send( session, req_msg ) ) { warning_handler( "Error sending request message [%d]", session->thread_trace ); diff --git a/src/libstack/osrf_message.c b/src/libstack/osrf_message.c index 9b937a5..1993baa 100644 --- a/src/libstack/osrf_message.c +++ b/src/libstack/osrf_message.c @@ -1,40 +1,60 @@ #include "opensrf/osrf_message.h" /* default to true */ -int parse_json = 1; +int parse_json_result = 1; +int parse_json_params = 1; osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) { - osrf_message* msg = safe_malloc(sizeof(osrf_message)); - msg->m_type = type; - msg->thread_trace = thread_trace; - msg->protocol = protocol; - msg->next = NULL; - msg->is_exception = 0; - msg->parse_json = parse_json; + osrf_message* msg = (osrf_message*) safe_malloc(sizeof(osrf_message)); + msg->m_type = type; + msg->thread_trace = thread_trace; + msg->protocol = protocol; + msg->next = NULL; + msg->is_exception = 0; + msg->parse_json_result = parse_json_result; + msg->parse_json_params = parse_json_params; + msg->parray = init_string_array(16); /* start out with a slot for 16 params. can grow */ + msg->params = NULL; + msg->full_param_string = NULL; return msg; } -void osrf_message_set_json_parse( int ibool ) { - parse_json = ibool; +void osrf_message_set_json_parse_result( int ibool ) { + parse_json_result = ibool; } +void osrf_message_set_json_parse_params( int ibool ) { + parse_json_params = ibool; +} + +void osrf_message_set_request_info( + osrf_message* msg, char* method_name, json* json_params ) { -void osrf_message_set_request_info( osrf_message* msg, char* method_name, json* json_params ) { if( msg == NULL || method_name == NULL ) fatal_handler( "Bad params to osrf_message_set_request_params()" ); - if( json_params != NULL ) - msg->params = json_tokener_parse(json_object_to_json_string(json_params)); - else - msg->params = json_tokener_parse("[]"); + if(msg->parse_json_params) { + if( json_params != NULL ) + msg->params = json_tokener_parse(json_object_to_json_string(json_params)); + else + msg->params = json_tokener_parse("[]"); + } msg->method_name = strdup( method_name ); } +/* only works of parse_json_params is false */ +void osrf_message_add_param( osrf_message* msg, char* param_string ) { + if(msg == NULL || param_string == NULL) + return; + if(!msg->parse_json_params) + string_array_add(msg->parray, param_string); +} + void osrf_message_set_status_info( osrf_message* msg, char* status_name, char* status_text, int status_code ) { @@ -59,9 +79,9 @@ void osrf_message_set_result_content( osrf_message* msg, char* json_string ) { msg->result_string = strdup(json_string); debug_handler("Setting result_string to %s\n", msg->result_string ); - debug_handler( "Message Parse JSON is set to: %d", msg->parse_json ); + debug_handler( "Message Parse JSON is set to: %d", msg->parse_json_result ); - if(msg->parse_json) + if(msg->parse_json_result) msg->result_content = json_tokener_parse(msg->result_string); } @@ -86,9 +106,14 @@ void osrf_message_free( osrf_message* msg ) { if( msg->method_name != NULL ) free(msg->method_name); + if(msg->full_param_string) + free(msg->full_param_string); + if( msg->params != NULL ) json_object_put( msg->params ); + string_array_destroy(msg->parray); + free(msg); } @@ -188,9 +213,36 @@ char* osrf_message_to_xml( osrf_message* msg ) { xmlSetProp( method_name_node, BAD_CAST "name", BAD_CAST "method" ); xmlSetProp( method_name_node, BAD_CAST "value", BAD_CAST msg->method_name ); - if( msg->params != NULL ) { - params_node = xmlNewChild( method_node, NULL, - BAD_CAST "params", BAD_CAST json_object_to_json_string( msg->params ) ); + if( msg->parse_json_params ) { + if( msg->params != NULL ) { + params_node = xmlNewChild( method_node, NULL, + BAD_CAST "params", BAD_CAST json_object_to_json_string( msg->params ) ); + } + } else { + if( msg->parray != NULL ) { + /* construct the json array for the params */ + growing_buffer* buf = buffer_init(128); + buffer_add( buf, "["); + int k; + for( k=0; k!= msg->parray->size; k++) { + buffer_add( buf, string_array_get_string(msg->parray, k) ); + buffer_add( buf, "," ); + } + + /* remove trailing comma */ + if(buf->buf[buf->n_used - 1] == ',') { + buf->buf[buf->n_used - 1] = '\0'; + buf->n_used--; + } + buffer_add( buf, "]"); + msg->full_param_string = buffer_data(buf); + + + params_node = xmlNewChild( method_node, NULL, + BAD_CAST "params", BAD_CAST buf->buf ); + + buffer_free(buf); + } } } @@ -302,7 +354,7 @@ int osrf_message_from_xml( char* xml, osrf_message* msgs[] ) { xmlNodePtr cur_node = message_node->children; osrf_message* new_msg = safe_malloc(sizeof(osrf_message)); - new_msg->parse_json = parse_json; + new_msg->parse_json_result = parse_json_result; while( cur_node ) { @@ -370,10 +422,19 @@ int osrf_message_from_xml( char* xml, osrf_message* msgs[] ) { } } - if( !strcmp((char*)meth_node->name,"params" ) && meth_node->children->content ) + if( !strcmp((char*)meth_node->name,"params" ) && meth_node->children->content ) { //new_msg->params = json_object_new_string( meth_node->children->content ); - new_msg->params = json_tokener_parse(meth_node->children->content); - + if( new_msg->parse_json_params) { + new_msg->params = json_tokener_parse(meth_node->children->content); + } else { + /* XXX this will have to parse the JSON to + grab the strings for full support! This should only be + necessary for server support of + non-json-param-parsing, though. Ugh. */ + new_msg->params = json_tokener_parse(meth_node->children->content); + } + } + meth_node = meth_node->next; } } //oilsMethod diff --git a/src/srfsh/srfsh.c b/src/srfsh/srfsh.c index 8594fd1..ef09edd 100644 --- a/src/srfsh/srfsh.c +++ b/src/srfsh/srfsh.c @@ -32,7 +32,8 @@ int main( int argc, char* argv[] ) { client = osrf_system_get_transport_client(); - osrf_message_set_json_parse(1); + //osrf_message_set_json_parse_result(1); + //osrf_message_set_json_parse_result(1); /* main process loop */ @@ -437,10 +438,11 @@ int send_request( char* server, } double start = get_timestamp_millis(); - int req_id = osrf_app_session_make_request( session, params, method, 1 ); + int req_id = osrf_app_session_make_request( session, params, method, 1, NULL ); osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 12 ); + debug_handler("srfsh0"); if(!omsg) @@ -458,6 +460,7 @@ int send_request( char* server, if(omsg->result_content) { + debug_handler("srfsh1"); osrf_message_free(last_result); last_result = omsg; @@ -468,6 +471,8 @@ int send_request( char* server, else content = json_object_get_string(omsg->result_content); + debug_handler("srfsh2"); + buffer_add( resp_buffer, "\nReceived Data: " ); buffer_add( resp_buffer, content ); buffer_add( resp_buffer, "\n" ); @@ -772,7 +777,7 @@ int do_math( int count, int style ) { struct timeb t2; ftime(&t1); - int req_id = osrf_app_session_make_request( session, params, methods[j], 1 ); + int req_id = osrf_app_session_make_request( session, params, methods[j], 1, NULL ); osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 5 ); -- 2.43.2