Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
xmlrpc_processor.cpp
1 
2 /***************************************************************************
3  * xmlrpc_processor.cpp - XML-RPC processor
4  *
5  * Created: Sun Aug 30 19:39:31 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "xmlrpc_processor.h"
24 #include <webview/page_reply.h>
25 #include <webview/error_reply.h>
26 #include <logging/logger.h>
27 
28 #include <xmlrpc-c/registry.hpp>
29 #include <cstring>
30 
31 using namespace fawkes;
32 
33 // accept up to 512KB as request
34 #define MAX_REQUEST_LENGTH (1024*512)
35 
36 /** @class XmlRpcRequestProcessor "xmlrpc_processor.h"
37  * XML-RPC web request processor.
38  * Process web requests and pass them to the XML-RPC processor.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor.
43  * @param logger logger to report problems
44  */
46  : WebRequestProcessor(/* handle session data */ true)
47 {
48  __logger = logger;
49  __xmlrpc_registry = new xmlrpc_c::registry();
50 }
51 
52 
53 /** Destructor. */
55 {
56  delete __xmlrpc_registry;
57 }
58 
59 /** Get XML-RPC registry.
60  * @return XML-RPC registry
61  */
62 xmlrpc_c::registry *
64 {
65  return __xmlrpc_registry;
66 }
67 
68 
69 WebReply *
71  const char *method,
72  const char *version,
73  const char *upload_data,
74  size_t *upload_data_size,
75  void **session_data)
76 {
77  if ( *session_data == NULL ) {
78  std::string *c = new std::string(upload_data ? upload_data : "");
79  *upload_data_size = 0;
80  *session_data = c;
81  return NULL;
82  } else {
83  if (*upload_data_size > 0) {
84  std::string *c = (std::string *)*session_data;
85  if ( (c->length() + *upload_data_size) > MAX_REQUEST_LENGTH ) {
86  delete c;
87  *session_data = NULL;
88  return new WebErrorPageReply(WebErrorPageReply::HTTP_REQUEST_ENTITY_TOO_LARGE);
89  }
90 
91  *c += upload_data;
92  *upload_data_size = 0;
93  return NULL;
94  }
95  }
96 
97  std::string *call = (std::string *)*session_data;
98  *session_data = NULL;
99 
100  if (strcmp(method, "POST") != 0) {
101  return new WebErrorPageReply(WebErrorPageReply::HTTP_METHOD_NOT_ALLOWED);
102  } else {
103  std::string response = "";
104  __xmlrpc_registry->processCall(*call, &response);
105  //__logger->log_debug("XmlRpcRequestProcessor", "Call: %s reponse: %s",
106  // call->c_str(), response.c_str());
107  delete call;
108  return new StaticWebReply(WebReply::HTTP_OK, response);
109  }
110 }