From 38fe1459f8f8205245d5c599aa0e0f74b424139e Mon Sep 17 00:00:00 2001 From: scottmk Date: Wed, 14 Jan 2009 14:36:14 +0000 Subject: [PATCH] Add a function osrfMethodVerifyContext() to do what the existing OSRF_METHOD_VERIFY_CONTEXT macro does. Use it in _osrfAppRunSystemMethod() and osrfAppEcho(). git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1626 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_application.h | 12 ++++++-- src/libopensrf/osrf_application.c | 61 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/include/opensrf/osrf_application.h b/include/opensrf/osrf_application.h index 8b1e247..d9bca62 100644 --- a/include/opensrf/osrf_application.h +++ b/include/opensrf/osrf_application.h @@ -31,10 +31,10 @@ extern "C" { This macro verifies methods receive the correct parameters */ #define _OSRF_METHOD_VERIFY_CONTEXT(d) \ if(!d) return -1; \ - if(!d->session) { osrfLogError( OSRF_LOG_MARK, "Session is NULL in app reqeust" ); return -1; }\ - if(!d->method) { osrfLogError( OSRF_LOG_MARK, "Method is NULL in app reqeust" ); return -1; }\ + if(!d->session) { osrfLogError( OSRF_LOG_MARK, "Session is NULL in app request" ); return -1; }\ + if(!d->method) { osrfLogError( OSRF_LOG_MARK, "Method is NULL in app request" ); return -1; }\ if(d->method->argc) {\ - if(!d->params) { osrfLogError( OSRF_LOG_MARK, "Params is NULL in app reqeust %s", d->method->name ); return -1; }\ + if(!d->params) { osrfLogError( OSRF_LOG_MARK, "Params is NULL in app request %s", d->method->name ); return -1; }\ if( d->params->type != JSON_ARRAY ) { \ osrfLogError( OSRF_LOG_MARK, "'params' is not a JSON array for method %s", d->method->name);\ return -1; }\ @@ -187,6 +187,12 @@ int osrfAppRespondComplete( osrfMethodContext* context, const jsonObject* data ) int osrfAppRunChildInit(const char* appname); void osrfAppRunExitCode(); +/** + Determine whether the context looks healthy. + Return 0 if it does, or -1 if it doesn't. + */ +int osrfMethodVerifyContext( osrfMethodContext* ctx ); + #ifdef __cplusplus } #endif diff --git a/src/libopensrf/osrf_application.c b/src/libopensrf/osrf_application.c index 5737787..d8d1a18 100644 --- a/src/libopensrf/osrf_application.c +++ b/src/libopensrf/osrf_application.c @@ -419,7 +419,10 @@ static void _osrfAppSetIntrospectMethod( osrfMethodContext* ctx, const osrfMetho message */ static int _osrfAppRunSystemMethod(osrfMethodContext* ctx) { - OSRF_METHOD_VERIFY_CONTEXT(ctx); + if( osrfMethodVerifyContext( ctx ) < 0 ) { + osrfLogError( OSRF_LOG_MARK, "_osrfAppRunSystemMethod: Received invalid method context" ); + return -1; + } if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) || !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) { @@ -502,7 +505,11 @@ static int osrfAppIntrospectAll( osrfMethodContext* ctx ) { } static int osrfAppEcho( osrfMethodContext* ctx ) { - OSRF_METHOD_VERIFY_CONTEXT(ctx); + if( osrfMethodVerifyContext( ctx ) < 0 ) { + osrfLogError( OSRF_LOG_MARK, "osrfAppEcho: Received invalid method context" ); + return -1; + } + int i; for( i = 0; i < ctx->params->size; i++ ) { const jsonObject* str = jsonObjectGetIndex(ctx->params,i); @@ -511,3 +518,53 @@ static int osrfAppEcho( osrfMethodContext* ctx ) { return 1; } +/** + Determine whether the context looks healthy. + Return 0 if it does, or -1 if it doesn't. + */ +int osrfMethodVerifyContext( osrfMethodContext* ctx ) +{ + if( !ctx ) { + osrfLogError( OSRF_LOG_MARK, "Context is NULL in app request" ); + return -1; + } + + if( !ctx->session ) { + osrfLogError( OSRF_LOG_MARK, "Session is NULL in app request" ); + return -1; + } + + if( !ctx->method ) + { + osrfLogError( OSRF_LOG_MARK, "Method is NULL in app request" ); + return -1; + } + + if( ctx->method->argc ) { + if( !ctx->params ) { + osrfLogError( OSRF_LOG_MARK, + "Params is NULL in app request %s", ctx->method->name ); + return -1; + } + if( ctx->params->type != JSON_ARRAY ) { + osrfLogError( OSRF_LOG_MARK, + "'params' is not a JSON array for method %s", ctx->method->name ); + return -1; + } + } + + if( !ctx->method->name ) { + osrfLogError( OSRF_LOG_MARK, "Method name is NULL" ); + return -1; + } + + // Log the call, with the method and parameters + char* params_str = jsonObjectToJSON( ctx->params ); + if( params_str ) { + osrfLogInfo( OSRF_LOG_MARK, "CALL: %s %s - %s", + ctx->session->remote_service, ctx->method->name, params_str ); + free( params_str ); + } + return 0; +} + -- 2.11.0