Selaa lähdekoodia

2009-05-15 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added ExpatXmlRpcRequestProcessor
	* src/ExpatXmlRpcRequestProcessor.cc
	* src/ExpatXmlRpcRequestProcessor.h
	* src/Makefile.am
Tatsuhiro Tsujikawa 16 vuotta sitten
vanhempi
commit
549864bc1c
5 muutettua tiedostoa jossa 196 lisäystä ja 7 poistoa
  1. 7 0
      ChangeLog
  2. 121 0
      src/ExpatXmlRpcRequestProcessor.cc
  3. 62 0
      src/ExpatXmlRpcRequestProcessor.h
  4. 2 1
      src/Makefile.am
  5. 4 6
      src/Makefile.in

+ 7 - 0
ChangeLog

@@ -1,3 +1,10 @@
+2009-05-15  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Added ExpatXmlRpcRequestProcessor
+	* src/ExpatXmlRpcRequestProcessor.cc
+	* src/ExpatXmlRpcRequestProcessor.h
+	* src/Makefile.am
+
 2009-05-15  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Added tests for addTorrent and addMetalink command.

+ 121 - 0
src/ExpatXmlRpcRequestProcessor.cc

@@ -0,0 +1,121 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2009 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "ExpatXmlRpcRequestProcessor.h"
+
+#include <stack>
+
+#include <expat.h>
+
+#include "XmlRpcRequestParserStateMachine.h"
+#include "Util.h"
+#include "DlAbortEx.h"
+
+namespace aria2 {
+
+namespace xmlrpc {
+
+struct SessionData {
+  XmlRpcRequestParserStateMachine* _stm;
+
+  std::stack<std::string> _charactersStack;
+
+  SessionData(XmlRpcRequestParserStateMachine* stm):_stm(stm) {}
+};
+
+static void mlStartElement(void* userData, const char* name, const char** attrs)
+{
+  SessionData* sd = reinterpret_cast<SessionData*>(userData);
+
+  std::map<std::string, std::string> attrmap;
+  if(attrs) {
+    const char** p = attrs;
+    while(*p != 0) {
+      std::string name = *p++;
+      if(*p == 0) {
+	break;
+      }
+      std::string value = Util::trim(*p++);
+      attrmap[name] = value;
+    }
+  }
+  sd->_stm->beginElement(name, attrmap);
+  if(sd->_stm->needsCharactersBuffering()) {
+    sd->_charactersStack.push(std::string());
+  }
+}
+
+static void mlEndElement(void* userData, const char* name)
+{
+  SessionData* sd = reinterpret_cast<SessionData*>(userData);
+  std::string characters;
+  if(sd->_stm->needsCharactersBuffering()) {
+    characters = Util::trim(sd->_charactersStack.top());
+    sd->_charactersStack.pop();
+  }
+  sd->_stm->endElement(name, characters);
+}
+
+static void mlCharacters(void* userData, const char* ch, int len)
+{
+  SessionData* sd = reinterpret_cast<SessionData*>(userData);
+  if(sd->_stm->needsCharactersBuffering()) {
+    sd->_charactersStack.top() += std::string(&ch[0], &ch[len]);
+  }
+}
+
+XmlRpcRequest
+XmlRpcRequestProcessor::parseMemory(const std::string& xml)
+{
+  _stm.reset(new XmlRpcRequestParserStateMachine());
+  SharedHandle<SessionData> sessionData(new SessionData(_stm.get()));
+
+  XML_Parser parser = XML_ParserCreate(0);
+
+  XML_SetUserData(parser, sessionData.get());
+  XML_SetElementHandler(parser, &mlStartElement, &mlEndElement);
+  XML_SetCharacterDataHandler(parser, &mlCharacters);
+
+  int r = XML_Parse(parser, xml.data(), xml.size(), 1);
+  XML_ParserFree(parser);
+
+  if(r == XML_STATUS_ERROR) {
+    throw DlAbortEx("Failed to parse xml-rpc request.");
+  }
+  return XmlRpcRequest(_stm->getMethodName(), _stm->getCurrentFrameValue());
+}
+
+} // namespace xmlrpc
+
+} // namespace aria2

+ 62 - 0
src/ExpatXmlRpcRequestProcessor.h

@@ -0,0 +1,62 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2009 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_EXPAT_XML_RPC_REQUEST_PROCESSOR_H_
+#define _D_EXPAT_XML_RPC_REQUEST_PROCESSOR_H_
+
+#include "common.h"
+
+#include <string>
+
+#include "SharedHandle.h"
+#include "XmlRpcRequest.h"
+
+namespace aria2 {
+
+namespace xmlrpc {
+
+class XmlRpcRequestParserStateMachine;
+
+class XmlRpcRequestProcessor {
+private:
+  SharedHandle<XmlRpcRequestParserStateMachine> _stm;
+public:
+  XmlRpcRequest parseMemory(const std::string& xml);
+};
+
+} // namespace xmlrpc
+
+} // namespace aria2
+
+#endif // _D_EXPAT_XML_RPC_REQUEST_PROCESSOR_H_

+ 2 - 1
src/Makefile.am

@@ -222,7 +222,6 @@ SRCS += XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\
 	HttpServerCommand.cc HttpServerCommand.h\
 	HttpServerResponseCommand.cc HttpServerResponseCommand.h\
 	HttpServer.cc HttpServer.h
-endif # ENABLE_XML_RPC
 
 if HAVE_LIBXML2
 SRCS += Xml2XmlRpcRequestProcessor.cc Xml2XmlRpcRequestProcessor.h
@@ -232,6 +231,8 @@ if HAVE_LIBEXPAT
 SRCS += ExpatXmlRpcRequestProcessor.cc ExpatXmlRpcRequestProcessor.h
 endif # HAVE_LIBEXPAT
 
+endif # ENABLE_XML_RPC
+
 if HAVE_POSIX_FALLOCATE
 SRCS += FallocFileAllocationIterator.cc FallocFileAllocationIterator.h
 endif # HAVE_POSIX_FALLOCATE

+ 4 - 6
src/Makefile.in

@@ -52,8 +52,8 @@ bin_PROGRAMS = aria2c$(EXEEXT)
 @ENABLE_XML_RPC_TRUE@	HttpServerResponseCommand.cc HttpServerResponseCommand.h\
 @ENABLE_XML_RPC_TRUE@	HttpServer.cc HttpServer.h
 
-@HAVE_LIBXML2_TRUE@am__append_2 = Xml2XmlRpcRequestProcessor.cc Xml2XmlRpcRequestProcessor.h
-@HAVE_LIBEXPAT_TRUE@am__append_3 = ExpatXmlRpcRequestProcessor.cc ExpatXmlRpcRequestProcessor.h
+@ENABLE_XML_RPC_TRUE@@HAVE_LIBXML2_TRUE@am__append_2 = Xml2XmlRpcRequestProcessor.cc Xml2XmlRpcRequestProcessor.h
+@ENABLE_XML_RPC_TRUE@@HAVE_LIBEXPAT_TRUE@am__append_3 = ExpatXmlRpcRequestProcessor.cc ExpatXmlRpcRequestProcessor.h
 @HAVE_POSIX_FALLOCATE_TRUE@am__append_4 = FallocFileAllocationIterator.cc FallocFileAllocationIterator.h
 @HAVE_EPOLL_TRUE@am__append_5 = EpollEventPoll.cc EpollEventPoll.h
 @ENABLE_SSL_TRUE@am__append_6 = TLSContext.h
@@ -608,10 +608,8 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 @ENABLE_XML_RPC_TRUE@	HttpServerCommand.$(OBJEXT) \
 @ENABLE_XML_RPC_TRUE@	HttpServerResponseCommand.$(OBJEXT) \
 @ENABLE_XML_RPC_TRUE@	HttpServer.$(OBJEXT)
-@HAVE_LIBXML2_TRUE@am__objects_2 =  \
-@HAVE_LIBXML2_TRUE@	Xml2XmlRpcRequestProcessor.$(OBJEXT)
-@HAVE_LIBEXPAT_TRUE@am__objects_3 =  \
-@HAVE_LIBEXPAT_TRUE@	ExpatXmlRpcRequestProcessor.$(OBJEXT)
+@ENABLE_XML_RPC_TRUE@@HAVE_LIBXML2_TRUE@am__objects_2 = Xml2XmlRpcRequestProcessor.$(OBJEXT)
+@ENABLE_XML_RPC_TRUE@@HAVE_LIBEXPAT_TRUE@am__objects_3 = ExpatXmlRpcRequestProcessor.$(OBJEXT)
 @HAVE_POSIX_FALLOCATE_TRUE@am__objects_4 = FallocFileAllocationIterator.$(OBJEXT)
 @HAVE_EPOLL_TRUE@am__objects_5 = EpollEventPoll.$(OBJEXT)
 am__objects_6 =