Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * xmlrpc_processor.cpp - XML-RPC processor 00004 * 00005 * Created: Sun Aug 30 19:39:31 2009 00006 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include "xmlrpc_processor.h" 00024 #include <webview/page_reply.h> 00025 #include <webview/error_reply.h> 00026 #include <logging/logger.h> 00027 00028 #include <xmlrpc-c/registry.hpp> 00029 #include <cstring> 00030 00031 using namespace fawkes; 00032 00033 // accept up to 512KB as request 00034 #define MAX_REQUEST_LENGTH (1024*512) 00035 00036 /** @class XmlRpcRequestProcessor "xmlrpc_processor.h" 00037 * XML-RPC web request processor. 00038 * Process web requests and pass them to the XML-RPC processor. 00039 * @author Tim Niemueller 00040 */ 00041 00042 /** Constructor. 00043 * @param logger logger to report problems 00044 */ 00045 XmlRpcRequestProcessor::XmlRpcRequestProcessor(fawkes::Logger *logger) 00046 : WebRequestProcessor(/* handle session data */ true) 00047 { 00048 __logger = logger; 00049 __xmlrpc_registry = new xmlrpc_c::registry(); 00050 } 00051 00052 00053 /** Destructor. */ 00054 XmlRpcRequestProcessor::~XmlRpcRequestProcessor() 00055 { 00056 delete __xmlrpc_registry; 00057 } 00058 00059 /** Get XML-RPC registry. 00060 * @return XML-RPC registry 00061 */ 00062 xmlrpc_c::registry * 00063 XmlRpcRequestProcessor::registry() 00064 { 00065 return __xmlrpc_registry; 00066 } 00067 00068 00069 WebReply * 00070 XmlRpcRequestProcessor::process_request(const char *url, 00071 const char *method, 00072 const char *version, 00073 const char *upload_data, 00074 size_t *upload_data_size, 00075 void **session_data) 00076 { 00077 if ( *session_data == NULL ) { 00078 std::string *c = new std::string(upload_data ? upload_data : ""); 00079 *upload_data_size = 0; 00080 *session_data = c; 00081 return NULL; 00082 } else { 00083 if (*upload_data_size > 0) { 00084 std::string *c = (std::string *)*session_data; 00085 if ( (c->length() + *upload_data_size) > MAX_REQUEST_LENGTH ) { 00086 delete c; 00087 *session_data = NULL; 00088 return new WebErrorPageReply(WebErrorPageReply::HTTP_REQUEST_ENTITY_TOO_LARGE); 00089 } 00090 00091 *c += upload_data; 00092 *upload_data_size = 0; 00093 return NULL; 00094 } 00095 } 00096 00097 std::string *call = (std::string *)*session_data; 00098 *session_data = NULL; 00099 00100 if (strcmp(method, "POST") != 0) { 00101 return new WebErrorPageReply(WebErrorPageReply::HTTP_METHOD_NOT_ALLOWED); 00102 } else { 00103 std::string response = ""; 00104 __xmlrpc_registry->processCall(*call, &response); 00105 //__logger->log_debug("XmlRpcRequestProcessor", "Call: %s reponse: %s", 00106 // call->c_str(), response.c_str()); 00107 delete call; 00108 return new StaticWebReply(WebReply::HTTP_OK, response); 00109 } 00110 }