📄 xmlrpc_introspection.c
字号:
/* This file is part of libXMLRPC - a C library for xml-encoded function calls. Author: Dan Libby (dan@libby.com) Epinions.com may be contacted at feedback@epinions-inc.com*//* Copyright 2001 Epinions, Inc. Subject to the following 3 conditions, Epinions, Inc. permits you, free of charge, to (a) use, copy, distribute, modify, perform and display this software and associated documentation files (the "Software"), and (b) permit others to whom the Software is furnished to do so as well. 1) The above copyright notice and this permission notice shall be included without modification in all copies or substantial portions of the Software. 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH DAMAGES. *//****h* ABOUT/xmlrpc_introspection * AUTHOR * Dan Libby, aka danda (dan@libby.com) * HISTORY * $Log: xmlrpc_introspection.c,v $ * Revision 1.5 2005/12/18 22:59:39 sniper * - Fixed bug #35723 (xmlrpc_introspection.c fails compile per C99 std) * * Revision 1.4 2003/12/16 21:00:21 sniper * Fix some compile warnings (patch by Joe Orton) * * Revision 1.3 2002/07/05 04:43:53 danda * merged in updates from SF project. bring php repository up to date with xmlrpc-epi version 0.51 * * Revision 1.9 2001/09/29 21:58:05 danda * adding cvs log to history section * * 4/10/2001 -- danda -- initial introspection support * TODO * NOTES *******/#ifdef _WIN32#include "xmlrpc_win32.h"#endif#include "queue.h"#include "xmlrpc.h"#include "xmlrpc_private.h"#include "xmlrpc_introspection_private.h"#include <string.h>#include <stdlib.h>#include <stdarg.h>/* forward declarations for static (non public, non api) funcs */static XMLRPC_VALUE xi_system_describe_methods_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData);static XMLRPC_VALUE xi_system_list_methods_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData);static XMLRPC_VALUE xi_system_method_signature_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData);static XMLRPC_VALUE xi_system_method_help_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData);/*-*********************************** Introspection Callbacks (methods) *************************************//* iterates through a list of structs and finds the one with key "name" matching * needle. slow, would benefit from a struct key hash. */static inline XMLRPC_VALUE find_named_value(XMLRPC_VALUE list, const char* needle) { XMLRPC_VALUE xIter = XMLRPC_VectorRewind(list); while(xIter) { const char* name = XMLRPC_VectorGetStringWithID(xIter, xi_token_name); if(name && !strcmp(name, needle)) { return xIter; } xIter = XMLRPC_VectorNext(list); } return NULL;}/* iterates through docs callbacks and calls any that have not yet been called */static void check_docs_loaded(XMLRPC_SERVER server, void* userData) { if(server) { q_iter qi = Q_Iter_Head_F(&server->docslist); while( qi ) { doc_method* dm = Q_Iter_Get_F(qi); if(dm && !dm->b_called) { dm->method(server, userData); dm->b_called = 1; } qi = Q_Iter_Next_F(qi); } }}/* utility function for xi_system_describe_methods_cb */static inline void describe_method(XMLRPC_SERVER server, XMLRPC_VALUE vector, const char* method) { if(method) { server_method* sm = find_method(server, method); if(sm) { XMLRPC_AddValueToVector(vector, sm->desc); } }}/* system.describeMethods() callback */static XMLRPC_VALUE xi_system_describe_methods_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData) { XMLRPC_VALUE xParams = XMLRPC_VectorRewind(XMLRPC_RequestGetData(input)); XMLRPC_VALUE xResponse = XMLRPC_CreateVector(NULL, xmlrpc_vector_struct); XMLRPC_VALUE xMethodList = XMLRPC_CreateVector("methodList", xmlrpc_vector_array); XMLRPC_VALUE xTypeList = NULL; int bAll = 1; /* lazy loading of introspection data */ check_docs_loaded(server, userData); xTypeList = XMLRPC_VectorGetValueWithID(server->xIntrospection, "typeList"); XMLRPC_AddValueToVector(xResponse, xTypeList); XMLRPC_AddValueToVector(xResponse, xMethodList); /* check if we have any param */ if(xParams) { /* check if string or vector (1 or n) */ XMLRPC_VALUE_TYPE type = XMLRPC_GetValueType(xParams); if(type == xmlrpc_string) { /* just one. spit it out. */ describe_method(server, xMethodList, XMLRPC_GetValueString(xParams)); bAll = 0; } else if(type == xmlrpc_vector) { /* multiple. spit all out */ XMLRPC_VALUE xIter = XMLRPC_VectorRewind(xParams); while(xIter) { describe_method(server, xMethodList, XMLRPC_GetValueString(xIter)); xIter = XMLRPC_VectorNext(xParams); } bAll = 0; } } /* otherwise, default to sending all methods */ if(bAll) { q_iter qi = Q_Iter_Head_F(&server->methodlist); while( qi ) { server_method* sm = Q_Iter_Get_F(qi); if(sm) { XMLRPC_AddValueToVector(xMethodList, sm->desc); } qi = Q_Iter_Next_F(qi); } } return xResponse;}/* this complies with system.listMethods as defined at http://xmlrpc.usefulinc.com/doc/reserved.html */static XMLRPC_VALUE xi_system_list_methods_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData) { XMLRPC_VALUE xResponse = XMLRPC_CreateVector(NULL, xmlrpc_vector_array); q_iter qi = Q_Iter_Head_F(&server->methodlist); while( qi ) { server_method* sm = Q_Iter_Get_F(qi); if(sm) { XMLRPC_VectorAppendString(xResponse, 0, sm->name, 0); } qi = Q_Iter_Next_F(qi); } return xResponse;}/* this complies with system.methodSignature as defined at * http://xmlrpc.usefulinc.com/doc/sysmethodsig.html */static XMLRPC_VALUE xi_system_method_signature_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData) { const char* method = XMLRPC_GetValueString(XMLRPC_VectorRewind(XMLRPC_RequestGetData(input))); XMLRPC_VALUE xResponse = NULL; /* lazy loading of introspection data */ check_docs_loaded(server, userData); if(method) { server_method* sm = find_method(server, method); if(sm && sm->desc) { XMLRPC_VALUE xTypesArray = XMLRPC_CreateVector(NULL, xmlrpc_vector_array); XMLRPC_VALUE xIter, xParams, xSig, xSigIter; const char* type; /* array of possible signatures. */ xResponse = XMLRPC_CreateVector(NULL, xmlrpc_vector_array); /* find first signature */ xSig = XMLRPC_VectorGetValueWithID(sm->desc, xi_token_signatures); xSigIter = XMLRPC_VectorRewind( xSig ); /* iterate through sigs */ while(xSigIter) { /* first type is the return value */ type = XMLRPC_VectorGetStringWithID(XMLRPC_VectorRewind( XMLRPC_VectorGetValueWithID(xSigIter, xi_token_returns)), xi_token_type); XMLRPC_AddValueToVector(xTypesArray, XMLRPC_CreateValueString(NULL, type ? type : type_to_str(xmlrpc_none, 0), 0)); /* the rest are parameters */ xParams = XMLRPC_VectorGetValueWithID(xSigIter, xi_token_params); xIter = XMLRPC_VectorRewind(xParams); /* iter through params, adding to types array */ while(xIter) { XMLRPC_AddValueToVector(xTypesArray, XMLRPC_CreateValueString(NULL, XMLRPC_VectorGetStringWithID(xIter, xi_token_type), 0)); xIter = XMLRPC_VectorNext(xParams); } /* add types for this signature */ XMLRPC_AddValueToVector(xResponse, xTypesArray); xSigIter = XMLRPC_VectorNext( xSig ); } } } return xResponse;}/* this complies with system.methodHelp as defined at * http://xmlrpc.usefulinc.com/doc/sysmethhelp.html */static XMLRPC_VALUE xi_system_method_help_cb(XMLRPC_SERVER server, XMLRPC_REQUEST input, void* userData) { const char* method = XMLRPC_GetValueString(XMLRPC_VectorRewind(XMLRPC_RequestGetData(input))); XMLRPC_VALUE xResponse = NULL; /* lazy loading of introspection data */ check_docs_loaded(server, userData); if(method) { server_method* sm = find_method(server, method); if(sm && sm->desc) { const char* help = XMLRPC_VectorGetStringWithID(sm->desc, xi_token_purpose); /* returns a documentation string, or empty string */ xResponse = XMLRPC_CreateValueString(NULL, help ? help : xi_token_empty, 0); } } return xResponse;}/*-*************************************** End Introspection Callbacks (methods) *****************************************//*-************************* Introspection Utilities ***************************//* performs registration of introspection methods */void xi_register_system_methods(XMLRPC_SERVER server) { XMLRPC_ServerRegisterMethod(server, xi_token_system_list_methods, xi_system_list_methods_cb); XMLRPC_ServerRegisterMethod(server, xi_token_system_method_help, xi_system_method_help_cb); XMLRPC_ServerRegisterMethod(server, xi_token_system_method_signature, xi_system_method_signature_cb); XMLRPC_ServerRegisterMethod(server, xi_token_system_describe_methods, xi_system_describe_methods_cb);}/* describe a value (param, return, type) */static XMLRPC_VALUE describeValue_worker(const char* type, const char* id, const char* desc, int optional, const char* default_val, XMLRPC_VALUE sub_params) { XMLRPC_VALUE xParam = NULL; if(id || desc) { xParam = XMLRPC_CreateVector(NULL, xmlrpc_vector_struct); XMLRPC_VectorAppendString(xParam, xi_token_name, id, 0); XMLRPC_VectorAppendString(xParam, xi_token_type, type, 0); XMLRPC_VectorAppendString(xParam, xi_token_description, desc, 0); if(optional != 2) { XMLRPC_VectorAppendInt(xParam, xi_token_optional, optional); } if(optional == 1 && default_val) { XMLRPC_VectorAppendString(xParam, xi_token_default, default_val, 0); } XMLRPC_AddValueToVector(xParam, sub_params); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -