2010-01-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

aria2 now returns gzip compressed XML-RPC response if XML-RPC
	client accepts gzip content encoding.
	* src/A2STR.cc
	* src/A2STR.h
	* src/GZipEncoder.cc
	* src/GZipEncoder.h
	* src/HttpHeader.cc
	* src/HttpHeader.h
	* src/HttpServer.cc
	* src/HttpServer.h
	* src/HttpServerBodyCommand.cc
	* src/HttpServerCommand.cc
	* src/Makefile.am
	* src/XmlRpcResponse.cc
	* src/XmlRpcResponse.h
	* test/GZipEncoderTest.cc
	* test/Makefile.am
pull/1/head
Tatsuhiro Tsujikawa 2010-01-22 14:09:39 +00:00
parent 5d8864caed
commit 06a52cad02
18 changed files with 418 additions and 41 deletions

View File

@ -1,3 +1,23 @@
2010-01-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
aria2 now returns gzip compressed XML-RPC response if XML-RPC
client accepts gzip content encoding.
* src/A2STR.cc
* src/A2STR.h
* src/GZipEncoder.cc
* src/GZipEncoder.h
* src/HttpHeader.cc
* src/HttpHeader.h
* src/HttpServer.cc
* src/HttpServer.h
* src/HttpServerBodyCommand.cc
* src/HttpServerCommand.cc
* src/Makefile.am
* src/XmlRpcResponse.cc
* src/XmlRpcResponse.h
* test/GZipEncoderTest.cc
* test/Makefile.am
2010-01-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2010-01-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added XML-RPC client sample code in Python. Added XML-RPC client sample code in Python.

View File

@ -60,4 +60,6 @@ const std::string A2STR::UNDERSCORE_C("_");
const std::string A2STR::BACK_SLASH_C("\\"); const std::string A2STR::BACK_SLASH_C("\\");
const std::string A2STR::COMMA_C(",");
} // namespace aria2 } // namespace aria2

View File

@ -66,6 +66,8 @@ public:
static const std::string UNDERSCORE_C; static const std::string UNDERSCORE_C;
static const std::string BACK_SLASH_C; static const std::string BACK_SLASH_C;
static const std::string COMMA_C;
}; };
} // namespace aria2 } // namespace aria2

145
src/GZipEncoder.cc Normal file
View File

@ -0,0 +1,145 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2010 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 "GZipEncoder.h"
#include <cstring>
#include "StringFormat.h"
#include "DlAbortEx.h"
#include "util.h"
namespace aria2 {
namespace {
const int OUTBUF_LENGTH = 4096;
}
GZipEncoder::GZipEncoder():_strm(0), _finished(false) {}
GZipEncoder::~GZipEncoder()
{
release();
}
void GZipEncoder::init()
{
_finished = false;
release();
_strm = new z_stream();
_strm->zalloc = Z_NULL;
_strm->zfree = Z_NULL;
_strm->opaque = Z_NULL;
_strm->avail_in = 0;
_strm->next_in = Z_NULL;
if(Z_OK != deflateInit2(_strm, 9, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY)) {
throw DL_ABORT_EX("Initializing z_stream failed.");
}
}
void GZipEncoder::release()
{
if(_strm) {
deflateEnd(_strm);
delete _strm;
_strm = 0;
}
}
std::string GZipEncoder::encode
(const unsigned char* in, size_t length, int flush)
{
std::string out;
_strm->avail_in = length;
_strm->next_in = const_cast<unsigned char*>(in);
unsigned char outbuf[OUTBUF_LENGTH];
while(1) {
_strm->avail_out = OUTBUF_LENGTH;
_strm->next_out = outbuf;
int ret = ::deflate(_strm, flush);
if(ret == Z_STREAM_END) {
_finished = true;
} else if(ret != Z_OK) {
throw DL_ABORT_EX(StringFormat("libz::deflate() failed. cause:%s",
_strm->msg).str());
}
size_t produced = OUTBUF_LENGTH-_strm->avail_out;
out.append(&outbuf[0], &outbuf[produced]);
if(_strm->avail_out > 0) {
break;
}
}
return out;
}
bool GZipEncoder::finished()
{
return _finished;
}
std::string GZipEncoder::str()
{
_internalBuf += encode(0, 0, Z_FINISH);
return _internalBuf;
}
GZipEncoder& GZipEncoder::operator<<(const char* s)
{
_internalBuf += encode(reinterpret_cast<const unsigned char*>(s), strlen(s));
return *this;
}
GZipEncoder& GZipEncoder::operator<<(const std::string& s)
{
_internalBuf += encode
(reinterpret_cast<const unsigned char*>(s.data()), s.size());
return *this;
}
GZipEncoder& GZipEncoder::operator<<(int64_t i)
{
std::string s = util::itos(i);
(*this) << s;
return *this;
}
} // namespace aria2

96
src/GZipEncoder.h Normal file
View File

@ -0,0 +1,96 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2010 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_GZIP_ENCODER_H_
#define _D_GZIP_ENCODER_H_
#include <string>
#include <zlib.h>
namespace aria2 {
class GZipEncoder {
private:
z_stream* _strm;
bool _finished;
// Internal buffer for deflated data.
std::string _internalBuf;
std::string encode(const unsigned char* in, size_t length, int flush);
public:
GZipEncoder();
~GZipEncoder();
// Initializes deflator.
void init();
// Feeds NULL-terminated c-string s to deflater. The deflated
// result is kept in this class.
GZipEncoder& operator<<(const char* s);
// Feeds binary data in s to deflater. The deflated result is kept
// in this class.
GZipEncoder& operator<<(const std::string& s);
// Feeds integer to deflator. Before passed to deflator, i is
// converted to std::string using util::itos(). The deflated result
// is kept in this class.
GZipEncoder& operator<<(int64_t i);
// Feeds binary data pointed by in with size length to deflator and
// returns compressed output available so far. Don't use this
// method with operator<< methods.
std::string encode(const unsigned char* in, size_t length)
{
return encode(in, length, Z_NO_FLUSH);
}
// Returns true if deflator finished.
bool finished();
// Releases allocated resources.
void release();
// Returns deflated result kept internally. After this function
// call, further calls to operator<<() and encode() are not allowed.
std::string str();
};
} // namespace aria2
#endif // _D_GZIP_ENCODER_H_

View File

@ -72,6 +72,8 @@ const std::string HttpHeader::CONTENT_RANGE("Content-Range");
const std::string HttpHeader::LAST_MODIFIED("Last-Modified"); const std::string HttpHeader::LAST_MODIFIED("Last-Modified");
const std::string HttpHeader::ACCEPT_ENCODING("Accept-Encoding");
const std::string HttpHeader::HTTP_1_1("HTTP/1.1"); const std::string HttpHeader::HTTP_1_1("HTTP/1.1");
const std::string HttpHeader::S200("200"); const std::string HttpHeader::S200("200");

View File

@ -140,6 +140,8 @@ public:
static const std::string LAST_MODIFIED; static const std::string LAST_MODIFIED;
static const std::string ACCEPT_ENCODING;
static const std::string HTTP_1_1; static const std::string HTTP_1_1;
static const std::string S200; static const std::string S200;

View File

@ -56,7 +56,10 @@ HttpServer::HttpServer(const SharedHandle<SocketCore>& socket,
_e(e), _e(e),
_headerProcessor(new HttpHeaderProcessor()), _headerProcessor(new HttpHeaderProcessor()),
_logger(LogFactory::getInstance()), _logger(LogFactory::getInstance()),
_keepAlive(true) _keepAlive(true),
_gzip(false),
_acceptsPersistentConnection(true),
_acceptsGZip(false)
{} {}
HttpServer::~HttpServer() {} HttpServer::~HttpServer() {}
@ -88,8 +91,21 @@ SharedHandle<HttpHeader> HttpServer::receiveRequest()
_lastContentLength = _lastContentLength =
_lastRequestHeader->getFirstAsUInt(HttpHeader::CONTENT_LENGTH); _lastRequestHeader->getFirstAsUInt(HttpHeader::CONTENT_LENGTH);
_headerProcessor->clear(); _headerProcessor->clear();
}
std::string connection =
util::toLower(_lastRequestHeader->getFirst(HttpHeader::CONNECTION));
_acceptsPersistentConnection =
connection.find(HttpHeader::CLOSE) == std::string::npos &&
(_lastRequestHeader->getVersion() == HttpHeader::HTTP_1_1 ||
connection.find("keep-alive") != std::string::npos);
std::vector<std::string> acceptEncodings;
util::split(_lastRequestHeader->getFirst(HttpHeader::ACCEPT_ENCODING),
std::back_inserter(acceptEncodings), A2STR::COMMA_C, true);
_acceptsGZip =
std::find(acceptEncodings.begin(), acceptEncodings.end(), "gzip")
!= acceptEncodings.end();
}
return header; return header;
} }
@ -121,20 +137,6 @@ const std::string& HttpServer::getRequestPath() const
return _lastRequestHeader->getRequestPath(); return _lastRequestHeader->getRequestPath();
} }
bool HttpServer::supportsPersistentConnection() const
{
if(!_keepAlive) {
return false;
}
std::string connection =
util::toLower(_lastRequestHeader->getFirst(HttpHeader::CONNECTION));
return connection.find(HttpHeader::CLOSE) == std::string::npos &&
(_lastRequestHeader->getVersion() == HttpHeader::HTTP_1_1 ||
connection.find("keep-alive") != std::string::npos);
}
void HttpServer::feedResponse(const std::string& text, const std::string& contentType) void HttpServer::feedResponse(const std::string& text, const std::string& contentType)
{ {
feedResponse("200 OK", "", text, contentType); feedResponse("200 OK", "", text, contentType);
@ -149,7 +151,9 @@ void HttpServer::feedResponse(const std::string& status,
strappend(header, status, "\r\n", strappend(header, status, "\r\n",
"Content-Type: ", contentType, "\r\n", "Content-Type: ", contentType, "\r\n",
"Content-Length: ", util::uitos(text.size()), "\r\n"); "Content-Length: ", util::uitos(text.size()), "\r\n");
if(supportsGZip()) {
header += "Content-Encoding: gzip\r\n";
}
if(!supportsPersistentConnection()) { if(!supportsPersistentConnection()) {
header += "Connection: close\r\n"; header += "Connection: close\r\n";
} }

View File

@ -39,6 +39,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector>
#include "SharedHandle.h" #include "SharedHandle.h"
#include "SocketBuffer.h" #include "SocketBuffer.h"
@ -62,8 +63,11 @@ private:
uint64_t _lastContentLength; uint64_t _lastContentLength;
std::stringstream _lastBody; std::stringstream _lastBody;
bool _keepAlive; bool _keepAlive;
bool _gzip;
std::string _username; std::string _username;
std::string _password; std::string _password;
bool _acceptsPersistentConnection;
bool _acceptsGZip;
public: public:
HttpServer(const SharedHandle<SocketCore>& socket, DownloadEngine* e); HttpServer(const SharedHandle<SocketCore>& socket, DownloadEngine* e);
@ -97,12 +101,24 @@ public:
bool sendBufferIsEmpty() const; bool sendBufferIsEmpty() const;
bool supportsPersistentConnection() const; bool supportsPersistentConnection() const
{
return _keepAlive && _acceptsPersistentConnection;
}
bool supportsGZip() const
{
return _gzip && _acceptsGZip;
}
void enableKeepAlive() { _keepAlive = true; } void enableKeepAlive() { _keepAlive = true; }
void disableKeepAlive() { _keepAlive = false; } void disableKeepAlive() { _keepAlive = false; }
void enableGZip() { _gzip = true; }
void disableGZip() { _gzip = false; }
uint64_t getContentLength() const { return _lastContentLength; } uint64_t getContentLength() const { return _lastContentLength; }
}; };

View File

@ -90,7 +90,9 @@ bool HttpServerBodyCommand::execute()
SharedHandle<xmlrpc::XmlRpcMethod> method = SharedHandle<xmlrpc::XmlRpcMethod> method =
xmlrpc::XmlRpcMethodFactory::create(req._methodName); xmlrpc::XmlRpcMethodFactory::create(req._methodName);
xmlrpc::XmlRpcResponse res = method->execute(req, _e); xmlrpc::XmlRpcResponse res = method->execute(req, _e);
_httpServer->feedResponse(res.toXml(), "text/xml"); bool gzip = _httpServer->supportsGZip();
std::string responseData = res.toXml(gzip);
_httpServer->feedResponse(responseData, "text/xml");
Command* command = Command* command =
new HttpServerResponseCommand(cuid, _httpServer, _e, _socket); new HttpServerResponseCommand(cuid, _httpServer, _e, _socket);
_e->commands.push_back(command); _e->commands.push_back(command);

View File

@ -61,6 +61,11 @@ HttpServerCommand::HttpServerCommand(int32_t cuid, DownloadEngine* e,
_e->addSocketForReadCheck(_socket, this); _e->addSocketForReadCheck(_socket, this);
_httpServer->setUsernamePassword(_e->option->get(PREF_XML_RPC_USER), _httpServer->setUsernamePassword(_e->option->get(PREF_XML_RPC_USER),
_e->option->get(PREF_XML_RPC_PASSWD)); _e->option->get(PREF_XML_RPC_PASSWD));
#ifdef HAVE_LIBZ
_httpServer->enableGZip();
#else // !HAVE_LIBZ
_httpServer->disableGZip();
#endif // !HAVE_LIBZ
} }
HttpServerCommand::HttpServerCommand(int32_t cuid, HttpServerCommand::HttpServerCommand(int32_t cuid,

View File

@ -252,7 +252,8 @@ SRCS += LibsslTLSContext.cc LibsslTLSContext.h
endif # HAVE_LIBSSL endif # HAVE_LIBSSL
if HAVE_LIBZ if HAVE_LIBZ
SRCS += GZipDecoder.cc GZipDecoder.h SRCS += GZipDecoder.cc GZipDecoder.h\
GZipEncoder.cc GZipEncoder.h
endif # HAVE_LIBZ endif # HAVE_LIBZ
if HAVE_SQLITE3 if HAVE_SQLITE3

View File

@ -61,7 +61,9 @@ bin_PROGRAMS = aria2c$(EXEEXT)
@ENABLE_SSL_TRUE@am__append_6 = TLSContext.h @ENABLE_SSL_TRUE@am__append_6 = TLSContext.h
@HAVE_LIBGNUTLS_TRUE@am__append_7 = LibgnutlsTLSContext.cc LibgnutlsTLSContext.h @HAVE_LIBGNUTLS_TRUE@am__append_7 = LibgnutlsTLSContext.cc LibgnutlsTLSContext.h
@HAVE_LIBSSL_TRUE@am__append_8 = LibsslTLSContext.cc LibsslTLSContext.h @HAVE_LIBSSL_TRUE@am__append_8 = LibsslTLSContext.cc LibsslTLSContext.h
@HAVE_LIBZ_TRUE@am__append_9 = GZipDecoder.cc GZipDecoder.h @HAVE_LIBZ_TRUE@am__append_9 = GZipDecoder.cc GZipDecoder.h\
@HAVE_LIBZ_TRUE@ GZipEncoder.cc GZipEncoder.h
@HAVE_SQLITE3_TRUE@am__append_10 = Sqlite3MozCookieParser.cc Sqlite3MozCookieParser.h @HAVE_SQLITE3_TRUE@am__append_10 = Sqlite3MozCookieParser.cc Sqlite3MozCookieParser.h
@ENABLE_ASYNC_DNS_TRUE@am__append_11 = AsyncNameResolver.cc AsyncNameResolver.h @ENABLE_ASYNC_DNS_TRUE@am__append_11 = AsyncNameResolver.cc AsyncNameResolver.h
@ENABLE_MESSAGE_DIGEST_TRUE@am__append_12 = IteratableChunkChecksumValidator.cc IteratableChunkChecksumValidator.h\ @ENABLE_MESSAGE_DIGEST_TRUE@am__append_12 = IteratableChunkChecksumValidator.cc IteratableChunkChecksumValidator.h\
@ -441,9 +443,10 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
EpollEventPoll.cc EpollEventPoll.h TLSContext.h \ EpollEventPoll.cc EpollEventPoll.h TLSContext.h \
LibgnutlsTLSContext.cc LibgnutlsTLSContext.h \ LibgnutlsTLSContext.cc LibgnutlsTLSContext.h \
LibsslTLSContext.cc LibsslTLSContext.h GZipDecoder.cc \ LibsslTLSContext.cc LibsslTLSContext.h GZipDecoder.cc \
GZipDecoder.h Sqlite3MozCookieParser.cc \ GZipDecoder.h GZipEncoder.cc GZipEncoder.h \
Sqlite3MozCookieParser.h AsyncNameResolver.cc \ Sqlite3MozCookieParser.cc Sqlite3MozCookieParser.h \
AsyncNameResolver.h IteratableChunkChecksumValidator.cc \ AsyncNameResolver.cc AsyncNameResolver.h \
IteratableChunkChecksumValidator.cc \
IteratableChunkChecksumValidator.h \ IteratableChunkChecksumValidator.h \
IteratableChecksumValidator.cc IteratableChecksumValidator.h \ IteratableChecksumValidator.cc IteratableChecksumValidator.h \
CheckIntegrityDispatcherCommand.cc \ CheckIntegrityDispatcherCommand.cc \
@ -607,7 +610,8 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
am__objects_6 = am__objects_6 =
@HAVE_LIBGNUTLS_TRUE@am__objects_7 = LibgnutlsTLSContext.$(OBJEXT) @HAVE_LIBGNUTLS_TRUE@am__objects_7 = LibgnutlsTLSContext.$(OBJEXT)
@HAVE_LIBSSL_TRUE@am__objects_8 = LibsslTLSContext.$(OBJEXT) @HAVE_LIBSSL_TRUE@am__objects_8 = LibsslTLSContext.$(OBJEXT)
@HAVE_LIBZ_TRUE@am__objects_9 = GZipDecoder.$(OBJEXT) @HAVE_LIBZ_TRUE@am__objects_9 = GZipDecoder.$(OBJEXT) \
@HAVE_LIBZ_TRUE@ GZipEncoder.$(OBJEXT)
@HAVE_SQLITE3_TRUE@am__objects_10 = Sqlite3MozCookieParser.$(OBJEXT) @HAVE_SQLITE3_TRUE@am__objects_10 = Sqlite3MozCookieParser.$(OBJEXT)
@ENABLE_ASYNC_DNS_TRUE@am__objects_11 = AsyncNameResolver.$(OBJEXT) @ENABLE_ASYNC_DNS_TRUE@am__objects_11 = AsyncNameResolver.$(OBJEXT)
@ENABLE_MESSAGE_DIGEST_TRUE@am__objects_12 = IteratableChunkChecksumValidator.$(OBJEXT) \ @ENABLE_MESSAGE_DIGEST_TRUE@am__objects_12 = IteratableChunkChecksumValidator.$(OBJEXT) \
@ -1437,6 +1441,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FtpTunnelRequestCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FtpTunnelRequestCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FtpTunnelResponseCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FtpTunnelResponseCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GZipDecoder.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GZipDecoder.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GZipEncoder.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GrowSegment.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GrowSegment.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HandshakeExtensionMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HandshakeExtensionMessage.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HaveEraseCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HaveEraseCommand.Po@am__quote@

View File

@ -38,16 +38,20 @@
#include <sstream> #include <sstream>
#include "util.h" #include "util.h"
#ifdef HAVE_LIBZ
# include "GZipEncoder.h"
#endif // HAVE_LIBZ
namespace aria2 { namespace aria2 {
namespace xmlrpc { namespace xmlrpc {
static void encodeValue(const BDE& value, std::ostream& o); template<typename OutputStream>
static void encodeValue(const BDE& value, OutputStream& o);
template<typename InputIterator> template<typename InputIterator, typename OutputStream>
static void encodeArray static void encodeArray
(InputIterator first, InputIterator last, std::ostream& o) (InputIterator first, InputIterator last, OutputStream& o)
{ {
o << "<array>" << "<data>"; o << "<array>" << "<data>";
for(; first != last; ++first) { for(; first != last; ++first) {
@ -56,9 +60,9 @@ static void encodeArray
o << "</data>" << "</array>"; o << "</data>" << "</array>";
} }
template<typename InputIterator> template<typename InputIterator, typename OutputStream>
static void encodeStruct static void encodeStruct
(InputIterator first, InputIterator last, std::ostream& o) (InputIterator first, InputIterator last, OutputStream& o)
{ {
o << "<struct>"; o << "<struct>";
for(; first != last; ++first) { for(; first != last; ++first) {
@ -70,7 +74,8 @@ static void encodeStruct
o << "</struct>"; o << "</struct>";
} }
static void encodeValue(const BDE& value, std::ostream& o) template<typename OutputStream>
static void encodeValue(const BDE& value, OutputStream& o)
{ {
o << "<value>"; o << "<value>";
if(value.isString()) { if(value.isString()) {
@ -85,23 +90,39 @@ static void encodeValue(const BDE& value, std::ostream& o)
o << "</value>"; o << "</value>";
} }
std::string XmlRpcResponse::toXml() const template<typename OutputStream>
std::string encodeAll(OutputStream& o, int code, const BDE& param)
{ {
std::stringstream o;
o << "<?xml version=\"1.0\"?>" << "<methodResponse>"; o << "<?xml version=\"1.0\"?>" << "<methodResponse>";
if(_code == 0) { if(code == 0) {
o << "<params>" << "<param>"; o << "<params>" << "<param>";
encodeValue(_param, o); encodeValue(param, o);
o << "</param>" << "</params>"; o << "</param>" << "</params>";
} else { } else {
o << "<fault>"; o << "<fault>";
encodeValue(_param, o); encodeValue(param, o);
o << "</fault>"; o << "</fault>";
} }
o << "</methodResponse>"; o << "</methodResponse>";
return o.str(); return o.str();
} }
std::string XmlRpcResponse::toXml(bool gzip) const
{
if(gzip) {
#ifdef HAVE_LIBZ
GZipEncoder o;
o.init();
return encodeAll(o, _code, _param);
#else // !HAVE_LIBZ
abort();
#endif // !HAVE_LIBZ
} else {
std::stringstream o;
return encodeAll(o, _code, _param);
}
}
} // namespace xmlrpc } // namespace xmlrpc
} // namespace aria2 } // namespace aria2

View File

@ -53,7 +53,7 @@ struct XmlRpcResponse {
XmlRpcResponse(int code, const BDE& param):_code(code), _param(param) {} XmlRpcResponse(int code, const BDE& param):_code(code), _param(param) {}
std::string toXml() const; std::string toXml(bool gzip = false) const;
}; };
} // namespace xmlrpc } // namespace xmlrpc

48
test/GZipEncoderTest.cc Normal file
View File

@ -0,0 +1,48 @@
#include "GZipEncoder.h"
#include <cppunit/extensions/HelperMacros.h>
#include "GZipDecoder.h"
#include "util.h"
namespace aria2 {
class GZipEncoderTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(GZipEncoderTest);
CPPUNIT_TEST(testEncode);
CPPUNIT_TEST_SUITE_END();
public:
void testEncode();
};
CPPUNIT_TEST_SUITE_REGISTRATION(GZipEncoderTest);
void GZipEncoderTest::testEncode()
{
GZipEncoder encoder;
encoder.init();
std::vector<std::string> inputs;
inputs.push_back("Hello World");
inputs.push_back("9223372036854775807");
inputs.push_back("Fox");
encoder << inputs[0];
encoder << util::parseLLInt(inputs[1]);
encoder << inputs[2].c_str();
std::string gzippedData = encoder.str();
GZipDecoder decoder;
decoder.init();
std::string gunzippedData =
decoder.decode(reinterpret_cast<const unsigned char*>(gzippedData.data()),
gzippedData.size());
CPPUNIT_ASSERT(decoder.finished());
CPPUNIT_ASSERT_EQUAL(strjoin(inputs.begin(), inputs.end(), ""),
gunzippedData);
}
} // namespace aria2

View File

@ -84,7 +84,8 @@ aria2c_SOURCES += FallocFileAllocationIteratorTest.cc
endif # HAVE_POSIX_FALLOCATE endif # HAVE_POSIX_FALLOCATE
if HAVE_LIBZ if HAVE_LIBZ
aria2c_SOURCES += GZipDecoderTest.cc aria2c_SOURCES += GZipDecoderTest.cc\
GZipEncoderTest.cc
endif # HAVE_LIBZ endif # HAVE_LIBZ
if HAVE_SQLITE3 if HAVE_SQLITE3

View File

@ -41,7 +41,9 @@ check_PROGRAMS = $(am__EXEEXT_1)
@ENABLE_XML_RPC_TRUE@ XmlRpcMethodTest.cc @ENABLE_XML_RPC_TRUE@ XmlRpcMethodTest.cc
@HAVE_POSIX_FALLOCATE_TRUE@am__append_2 = FallocFileAllocationIteratorTest.cc @HAVE_POSIX_FALLOCATE_TRUE@am__append_2 = FallocFileAllocationIteratorTest.cc
@HAVE_LIBZ_TRUE@am__append_3 = GZipDecoderTest.cc @HAVE_LIBZ_TRUE@am__append_3 = GZipDecoderTest.cc\
@HAVE_LIBZ_TRUE@ GZipEncoderTest.cc
@HAVE_SQLITE3_TRUE@am__append_4 = Sqlite3MozCookieParserTest.cc @HAVE_SQLITE3_TRUE@am__append_4 = Sqlite3MozCookieParserTest.cc
@ENABLE_MESSAGE_DIGEST_TRUE@am__append_5 = MessageDigestHelperTest.cc\ @ENABLE_MESSAGE_DIGEST_TRUE@am__append_5 = MessageDigestHelperTest.cc\
@ENABLE_MESSAGE_DIGEST_TRUE@ IteratableChunkChecksumValidatorTest.cc\ @ENABLE_MESSAGE_DIGEST_TRUE@ IteratableChunkChecksumValidatorTest.cc\
@ -212,7 +214,8 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
DownloadContextTest.cc XmlRpcRequestParserControllerTest.cc \ DownloadContextTest.cc XmlRpcRequestParserControllerTest.cc \
XmlRpcRequestProcessorTest.cc XmlRpcMethodTest.cc \ XmlRpcRequestProcessorTest.cc XmlRpcMethodTest.cc \
FallocFileAllocationIteratorTest.cc GZipDecoderTest.cc \ FallocFileAllocationIteratorTest.cc GZipDecoderTest.cc \
Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \ GZipEncoderTest.cc Sqlite3MozCookieParserTest.cc \
MessageDigestHelperTest.cc \
IteratableChunkChecksumValidatorTest.cc \ IteratableChunkChecksumValidatorTest.cc \
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \ IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
BtBitfieldMessageTest.cc BtCancelMessageTest.cc \ BtBitfieldMessageTest.cc BtCancelMessageTest.cc \
@ -271,7 +274,8 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
@ENABLE_XML_RPC_TRUE@ XmlRpcRequestProcessorTest.$(OBJEXT) \ @ENABLE_XML_RPC_TRUE@ XmlRpcRequestProcessorTest.$(OBJEXT) \
@ENABLE_XML_RPC_TRUE@ XmlRpcMethodTest.$(OBJEXT) @ENABLE_XML_RPC_TRUE@ XmlRpcMethodTest.$(OBJEXT)
@HAVE_POSIX_FALLOCATE_TRUE@am__objects_2 = FallocFileAllocationIteratorTest.$(OBJEXT) @HAVE_POSIX_FALLOCATE_TRUE@am__objects_2 = FallocFileAllocationIteratorTest.$(OBJEXT)
@HAVE_LIBZ_TRUE@am__objects_3 = GZipDecoderTest.$(OBJEXT) @HAVE_LIBZ_TRUE@am__objects_3 = GZipDecoderTest.$(OBJEXT) \
@HAVE_LIBZ_TRUE@ GZipEncoderTest.$(OBJEXT)
@HAVE_SQLITE3_TRUE@am__objects_4 = \ @HAVE_SQLITE3_TRUE@am__objects_4 = \
@HAVE_SQLITE3_TRUE@ Sqlite3MozCookieParserTest.$(OBJEXT) @HAVE_SQLITE3_TRUE@ Sqlite3MozCookieParserTest.$(OBJEXT)
@ENABLE_MESSAGE_DIGEST_TRUE@am__objects_5 = \ @ENABLE_MESSAGE_DIGEST_TRUE@am__objects_5 = \
@ -810,6 +814,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FileTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FileTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FtpConnectionTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FtpConnectionTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GZipDecoderTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GZipDecoderTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GZipEncoderTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GrowSegmentTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GrowSegmentTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HandshakeExtensionMessageTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HandshakeExtensionMessageTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpHeaderProcessorTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpHeaderProcessorTest.Po@am__quote@