Merge pull request #2 from pocoproject/develop

Updating develop branch
This commit is contained in:
Cristian Thiago Moecke 2014-03-10 10:29:52 -03:00
commit 6296635b72
22 changed files with 254 additions and 49 deletions

View File

@ -67,6 +67,9 @@ public:
const std::string& algorithm() const;
/// Returns the name of the digest algorithm.
int nid() const;
/// Returns the NID (OpenSSL object identifier) of the digest algorithm.
// DigestEngine
std::size_t digestLength() const;
void reset();

View File

@ -43,9 +43,7 @@
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/RSAKey.h"
#include "Poco/DigestEngine.h"
#include "Poco/MD5Engine.h"
#include "Poco/SHA1Engine.h"
#include <openssl/rsa.h>
#include "Poco/Crypto/DigestEngine.h"
#include <istream>
#include <ostream>
@ -58,10 +56,10 @@ class Crypto_API RSADigestEngine: public Poco::DigestEngine
/// This class implements a Poco::DigestEngine that can be
/// used to compute a secure digital signature.
///
/// First another Poco::DigestEngine (Poco::MD5Engine
/// or Poco::SHA1Engine) is used to compute a cryptographic
/// hash of the data to be signed. Then, the hash value is
/// encrypted, using the RSA private key.
/// First another Poco::Crypto::DigestEngine is created and
/// used to compute a cryptographic hash of the data to be
/// signed. Then, the hash value is encrypted, using
/// the RSA private key.
///
/// To verify a signature, pass it to the verify()
/// member function. It will decrypt the signature
@ -75,9 +73,19 @@ public:
DIGEST_SHA1
};
//@ deprecated
RSADigestEngine(const RSAKey& key, DigestType digestType = DIGEST_SHA1);
/// Creates the RSADigestEngine with the given RSA key,
/// using the SHA-1 hash algorithm.
/// using the MD5 or SHA-1 hash algorithm.
/// Kept for backward compatibility
RSADigestEngine(const RSAKey& key, const std::string &name);
/// Creates the RSADigestEngine with the given RSA key,
/// using the hash algorithm with the given name
/// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.).
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~RSADigestEngine();
/// Destroys the RSADigestEngine.
@ -113,12 +121,9 @@ protected:
private:
RSAKey _key;
Poco::DigestEngine& _engine;
int _type;
Poco::Crypto::DigestEngine _engine;
Poco::DigestEngine::Digest _digest;
Poco::DigestEngine::Digest _signature;
Poco::MD5Engine _md5Engine;
Poco::SHA1Engine _sha1Engine;
};

View File

@ -57,6 +57,10 @@ DigestEngine::~DigestEngine()
EVP_MD_CTX_destroy(_ctx);
}
int DigestEngine::nid() const
{
return EVP_MD_nid(_ctx->digest);
}
std::size_t DigestEngine::digestLength() const
{

View File

@ -35,7 +35,7 @@
#include "Poco/Crypto/RSADigestEngine.h"
#include <openssl/pem.h>
#include <openssl/rsa.h>
namespace Poco {
@ -44,8 +44,13 @@ namespace Crypto {
RSADigestEngine::RSADigestEngine(const RSAKey& key, DigestType digestType):
_key(key),
_engine(digestType == DIGEST_MD5 ? static_cast<Poco::DigestEngine&>(_md5Engine) : static_cast<Poco::DigestEngine&>(_sha1Engine)),
_type(digestType == DIGEST_MD5 ? NID_md5 : NID_sha1)
_engine(digestType == DIGEST_MD5 ? "MD5" : "SHA1")
{
}
RSADigestEngine::RSADigestEngine(const RSAKey& key, const std::string &name):
_key(key),
_engine(name)
{
}
@ -86,7 +91,7 @@ const DigestEngine::Digest& RSADigestEngine::signature()
digest();
_signature.resize(_key.size());
unsigned sigLen = static_cast<unsigned>(_signature.size());
RSA_sign(_type, &_digest[0], static_cast<unsigned>(_digest.size()), &_signature[0], &sigLen, _key.impl()->getRSA());
RSA_sign(_engine.nid(), &_digest[0], static_cast<unsigned>(_digest.size()), &_signature[0], &sigLen, _key.impl()->getRSA());
// truncate _sig to sigLen
if (sigLen < _signature.size())
_signature.resize(sigLen);
@ -99,7 +104,7 @@ bool RSADigestEngine::verify(const DigestEngine::Digest& sig)
{
digest();
DigestEngine::Digest sigCpy = sig; // copy becausse RSA_verify can modify sigCpy
int ret = RSA_verify(_type, &_digest[0], static_cast<unsigned>(_digest.size()), &sigCpy[0], static_cast<unsigned>(sigCpy.size()), _key.impl()->getRSA());
int ret = RSA_verify(_engine.nid(), &_digest[0], static_cast<unsigned>(_digest.size()), &sigCpy[0], static_cast<unsigned>(sigCpy.size()), _key.impl()->getRSA());
return ret != 0;
}

View File

@ -149,6 +149,27 @@ void RSATest::testSign()
}
void RSATest::testSignSha256()
{
std::string msg("Test this sign message");
RSAKey key(RSAKey::KL_2048, RSAKey::EXP_LARGE);
RSADigestEngine eng(key,"SHA256");
eng.update(msg.c_str(), static_cast<unsigned>(msg.length()));
const Poco::DigestEngine::Digest& sig = eng.signature();
std::string hexDig = Poco::DigestEngine::digestToHex(sig);
// verify
std::ostringstream strPub;
key.save(&strPub);
std::string pubKey = strPub.str();
std::istringstream iPub(pubKey);
RSAKey keyPub(&iPub);
RSADigestEngine eng2(key,"SHA256");
eng2.update(msg.c_str(), static_cast<unsigned>(msg.length()));
assert (eng2.verify(sig));
}
void RSATest::testSignManipulated()
{
std::string msg("Test this sign message");
@ -244,6 +265,7 @@ CppUnit::Test* RSATest::suite()
CppUnit_addTest(pSuite, RSATest, testNewKeys);
CppUnit_addTest(pSuite, RSATest, testSign);
CppUnit_addTest(pSuite, RSATest, testSignSha256);
CppUnit_addTest(pSuite, RSATest, testSignManipulated);
CppUnit_addTest(pSuite, RSATest, testRSACipher);
CppUnit_addTest(pSuite, RSATest, testRSACipherLarge);

View File

@ -48,6 +48,7 @@ public:
void testNewKeys();
void testSign();
void testSignSha256();
void testSignManipulated();
void testRSACipher();
void testRSACipherLarge();

View File

@ -788,7 +788,11 @@ inline bool Var::isEmpty() const
inline bool Var::isArray() const
{
return !isEmpty() && !isString();
if (isEmpty() ||
isString()) return false;
VarHolder* pHolder = content();
return pHolder ? pHolder->isArray() : false;
}

View File

@ -72,7 +72,7 @@
#elif defined(_AIX) || defined(__TOS_AIX__)
#define POCO_OS_FAMILY_UNIX 1
#define POCO_OS POCO_OS_AIX
#elif defined(hpux) || defined(_hpux)
#elif defined(hpux) || defined(_hpux) || defined(__hpux)
#define POCO_OS_FAMILY_UNIX 1
#define POCO_OS POCO_OS_HPUX
#elif defined(__digital__) || defined(__osf__)

View File

@ -1863,22 +1863,22 @@ void VarTest::testIsArray()
assert (!d0.isArray());
assert (!d1.isArray());
assert (d2.isArray());
assert (d3.isArray());
assert (d4.isArray());
assert (d5.isArray());
assert (d6.isArray());
assert (d7.isArray());
assert (d8.isArray());
assert (d9.isArray());
assert (d10.isArray());
assert (d11.isArray());
assert (d12.isArray());
assert (d13.isArray());
assert (d14.isArray());
assert (d15.isArray());
assert (!d2.isArray());
assert (!d3.isArray());
assert (!d4.isArray());
assert (!d5.isArray());
assert (!d6.isArray());
assert (!d7.isArray());
assert (!d8.isArray());
assert (!d9.isArray());
assert (!d10.isArray());
assert (!d11.isArray());
assert (!d12.isArray());
assert (!d13.isArray());
assert (!d14.isArray());
assert (!d15.isArray());
assert (d16.isArray());
assert (d17.isArray());
assert (!d17.isArray());
}

View File

@ -1,7 +1,7 @@
//
// HTTPCookie.h
//
// $Id: //poco/1.4/Net/include/Poco/Net/HTTPCookie.h#1 $
// $Id: //poco/1.4/Net/include/Poco/Net/HTTPCookie.h#2 $
//
// Library: Net
// Package: HTTP
@ -139,10 +139,16 @@ public:
void setPath(const std::string& path);
/// Sets the path for the cookie.
void setPriority(const std::string& priority);
/// Sets the priority for the cookie.
const std::string& getPath() const;
/// Returns the path for the cookie.
const std::string& getPriority() const;
/// Returns the priority for the cookie.
void setSecure(bool secure);
/// Sets the value of the secure flag for
/// the cookie.
@ -210,6 +216,7 @@ private:
std::string _comment;
std::string _domain;
std::string _path;
std::string _priority;
bool _secure;
int _maxAge;
bool _httpOnly;
@ -255,6 +262,12 @@ inline const std::string& HTTPCookie::getPath() const
}
inline const std::string& HTTPCookie::getPriority() const
{
return _priority;
}
inline bool HTTPCookie::getSecure() const
{
return _secure;

View File

@ -162,6 +162,9 @@ public:
int currentThreads() const;
/// Returns the number of currently used connection threads.
int maxThreads() const;
/// Returns the maximum number of threads available.
int totalConnections() const;
/// Returns the total number of handled connections.

View File

@ -85,6 +85,9 @@ public:
int currentThreads() const;
/// Returns the number of currently used threads.
int maxThreads() const;
/// Returns the maximum number of threads available.
int totalConnections() const;
/// Returns the total number of handled connections.

View File

@ -1,7 +1,7 @@
//
// HTTPCookie.cpp
//
// $Id: //poco/1.4/Net/src/HTTPCookie.cpp#1 $
// $Id: //poco/1.4/Net/src/HTTPCookie.cpp#3 $
//
// Library: Net
// Package: HTTP
@ -102,6 +102,10 @@ HTTPCookie::HTTPCookie(const NameValueCollection& nvc):
{
setPath(value);
}
else if (icompare(name, "priority") == 0)
{
setPriority(value);
}
else if (icompare(name, "max-age") == 0)
{
setMaxAge(NumberParser::parse(value));
@ -152,6 +156,7 @@ HTTPCookie::HTTPCookie(const HTTPCookie& cookie):
_comment(cookie._comment),
_domain(cookie._domain),
_path(cookie._path),
_priority(cookie._priority),
_secure(cookie._secure),
_maxAge(cookie._maxAge),
_httpOnly(cookie._httpOnly)
@ -174,6 +179,7 @@ HTTPCookie& HTTPCookie::operator = (const HTTPCookie& cookie)
_comment = cookie._comment;
_domain = cookie._domain;
_path = cookie._path;
_priority = cookie._priority;
_secure = cookie._secure;
_maxAge = cookie._maxAge;
_httpOnly = cookie._httpOnly;
@ -218,6 +224,12 @@ void HTTPCookie::setPath(const std::string& path)
}
void HTTPCookie::setPriority(const std::string& priority)
{
_priority = priority;
}
void HTTPCookie::setSecure(bool secure)
{
_secure = secure;
@ -256,6 +268,11 @@ std::string HTTPCookie::toString() const
result.append("; path=");
result.append(_path);
}
if (!_priority.empty())
{
result.append("; Priority=");
result.append(_priority);
}
if (_maxAge != -1)
{
Timestamp ts;
@ -296,6 +313,13 @@ std::string HTTPCookie::toString() const
result.append(_path);
result.append("\"");
}
if (!_priority.empty())
{
result.append("; Priority=\"");
result.append(_priority);
result.append("\"");
}
if (_maxAge != -1)
{
result.append("; Max-Age=\"");

View File

@ -102,15 +102,17 @@ namespace
Poco::StreamCopier::copyToString(stream, tmp);
if (_pMsg)
{
MailMessage::ContentTransferEncoding cte = MailMessage::ENCODING_7BIT;
std::string enc = header[MailMessage::HEADER_CONTENT_TRANSFER_ENCODING];
if (enc == MailMessage::CTE_8BIT)
cte = MailMessage::ENCODING_8BIT;
else if (enc == MailMessage::CTE_QUOTED_PRINTABLE)
cte = MailMessage::ENCODING_QUOTED_PRINTABLE;
else if (enc == MailMessage::CTE_BASE64)
cte = MailMessage::ENCODING_BASE64;
if (header.has(MailMessage::HEADER_CONTENT_TRANSFER_ENCODING))
{
std::string enc = header[MailMessage::HEADER_CONTENT_TRANSFER_ENCODING];
if (enc == MailMessage::CTE_8BIT)
cte = MailMessage::ENCODING_8BIT;
else if (enc == MailMessage::CTE_QUOTED_PRINTABLE)
cte = MailMessage::ENCODING_QUOTED_PRINTABLE;
else if (enc == MailMessage::CTE_BASE64)
cte = MailMessage::ENCODING_BASE64;
}
NameValueCollection::ConstIterator it = header.begin();
NameValueCollection::ConstIterator end = header.end();

View File

@ -52,19 +52,25 @@ namespace Net {
TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, Poco::UInt16 portNumber, TCPServerParams::Ptr pParams):
_socket(ServerSocket(portNumber)),
_pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),
_thread(threadName(_socket)),
_stopped(true)
{
Poco::ThreadPool& pool = Poco::ThreadPool::defaultPool();
if(pParams) pool.addCapacity(pParams->getMaxThreads() - pool.capacity());
_pDispatcher = new TCPServerDispatcher(pFactory, pool, pParams);
}
TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, const ServerSocket& socket, TCPServerParams::Ptr pParams):
_socket(socket),
_pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),
_thread(threadName(socket)),
_stopped(true)
{
Poco::ThreadPool& pool = Poco::ThreadPool::defaultPool();
if(pParams) pool.addCapacity(pParams->getMaxThreads() - pool.capacity());
_pDispatcher = new TCPServerDispatcher(pFactory, pool, pParams);
}
@ -146,6 +152,11 @@ int TCPServer::currentThreads() const
return _pDispatcher->currentThreads();
}
int TCPServer::maxThreads() const
{
return _pDispatcher->maxThreads();
}
int TCPServer::totalConnections() const
{

View File

@ -197,6 +197,13 @@ int TCPServerDispatcher::currentThreads() const
return _currentThreads;
}
int TCPServerDispatcher::maxThreads() const
{
FastMutex::ScopedLock lock(_mutex);
return _threadPool.capacity();
}
int TCPServerDispatcher::totalConnections() const
{

View File

@ -1,7 +1,7 @@
//
// HTTPCookieTest.cpp
//
// $Id: //poco/1.4/Net/testsuite/src/HTTPCookieTest.cpp#1 $
// $Id: //poco/1.4/Net/testsuite/src/HTTPCookieTest.cpp#2 $
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -44,6 +44,7 @@
#include <cstdlib>
#include <sstream>
using Poco::Timestamp;
using Poco::Timespan;
using Poco::DateTimeFormatter;
@ -80,8 +81,15 @@ void HTTPCookieTest::testCookie()
assert (cookie.toString() == "name=value; domain=appinf.com; path=/; secure");
cookie.setHttpOnly(true);
assert (cookie.toString() == "name=value; domain=appinf.com; path=/; secure; HttpOnly");
cookie.setPriority("Low");
assert (cookie.toString() == "name=value; domain=appinf.com; path=/; Priority=Low; secure; HttpOnly");
cookie.setPriority("Medium");
assert (cookie.toString() == "name=value; domain=appinf.com; path=/; Priority=Medium; secure; HttpOnly");
cookie.setPriority("High");
assert (cookie.toString() == "name=value; domain=appinf.com; path=/; Priority=High; secure; HttpOnly");
cookie.setPriority("");
cookie.setHttpOnly(false);
cookie.setVersion(1);
assert (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; secure; Version=\"1\"");
@ -91,6 +99,13 @@ void HTTPCookieTest::testCookie()
cookie.setHttpOnly(true);
assert (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"");
cookie.setPriority("Low");
assert (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Priority=\"Low\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"");
cookie.setPriority("Medium");
assert (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Priority=\"Medium\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"");
cookie.setPriority("High");
assert (cookie.toString() == "name=\"value\"; Comment=\"comment\"; Domain=\"appinf.com\"; Path=\"/\"; Priority=\"High\"; Max-Age=\"100\"; HttpOnly; Version=\"1\"");
}

View File

@ -1,7 +1,7 @@
//
// HTTPCookieTest.h
//
// $Id: //poco/1.4/Net/testsuite/src/HTTPCookieTest.h#1 $
// $Id: //poco/1.4/Net/testsuite/src/HTTPCookieTest.h#2 $
//
// Definition of the HTTPCookieTest class.
//

View File

@ -358,6 +358,31 @@ void MailMessageTest::testReadQP()
}
void MailMessageTest::testReadDefaultTransferEncoding()
{
std::istringstream istr(
"Content-Type: text/plain\r\n"
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
"From: poco@appinf.com\r\n"
"Subject: Test Message\r\n"
"To: John Doe <john.doe@no.where>\r\n"
"\r\n"
"Hello, world!\r\n"
"This is a test for the MailMessage class.\r\n"
);
MailMessage message;
message.read(istr);
assert (message.getSender() == "poco@appinf.com");
assert (message.getContentType() == "text/plain");
assert (message.getContent() ==
"Hello, world!\r\n"
"This is a test for the MailMessage class.\r\n"
);
}
void MailMessageTest::testRead8Bit()
{
std::istringstream istr(
@ -426,6 +451,47 @@ void MailMessageTest::testReadMultiPart()
}
void MailMessageTest::testReadMultiPartDefaultTransferEncoding()
{
std::istringstream istr(
"Content-Type: multipart/mixed; boundary=MIME_boundary_01234567\r\n"
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
"From: poco@appinf.com\r\n"
"Mime-Version: 1.0\r\n"
"Subject: Test Message\r\n"
"To: John Doe <john.doe@no.where>\r\n"
"\r\n"
"\r\n"
"--MIME_boundary_01234567\r\n"
"Content-Disposition: inline\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello World!\r\n"
"\r\n"
"--MIME_boundary_01234567\r\n"
"Content-Disposition: attachment; filename=sample.dat\r\n"
"Content-Transfer-Encoding: base64\r\n"
"Content-Type: application/octet-stream; name=sample\r\n"
"\r\n"
"VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
"--MIME_boundary_01234567--\r\n"
);
StringPartHandler handler;
MailMessage message;
message.read(istr, handler);
assert (handler.data().size() == 2);
assert (handler.data()[0] == "Hello World!\r\n");
assert (handler.type()[0] == "text/plain");
assert (handler.disp()[0] == "inline");
assert (handler.data()[1] == "This is some binary data. Really.");
assert (handler.type()[1] == "application/octet-stream; name=sample");
assert (handler.disp()[1] == "attachment; filename=sample.dat");
}
void MailMessageTest::testReadWriteMultiPart()
{
std::string msgin(
@ -573,8 +639,10 @@ CppUnit::Test* MailMessageTest::suite()
CppUnit_addTest(pSuite, MailMessageTest, testWriteManyRecipients);
CppUnit_addTest(pSuite, MailMessageTest, testWriteMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadQP);
CppUnit_addTest(pSuite, MailMessageTest, testReadDefaultTransferEncoding);
CppUnit_addTest(pSuite, MailMessageTest, testRead8Bit);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPartDefaultTransferEncoding);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPart);
CppUnit_addTest(pSuite, MailMessageTest, testReadWriteMultiPartStore);
CppUnit_addTest(pSuite, MailMessageTest, testEncodeWord);

View File

@ -53,9 +53,11 @@ public:
void testWriteMultiPart();
void testReadWriteMultiPart();
void testReadWriteMultiPartStore();
void testReadDefaultTransferEncoding();
void testReadQP();
void testRead8Bit();
void testReadMultiPart();
void testReadMultiPartDefaultTransferEncoding();
void testEncodeWord();
void setUp();

View File

@ -175,6 +175,7 @@ void TCPServerTest::testMultiConnections()
srv.start();
assert (srv.currentConnections() == 0);
assert (srv.currentThreads() == 0);
assert (srv.maxThreads() >= 4);
assert (srv.queuedConnections() == 0);
assert (srv.totalConnections() == 0);
@ -252,6 +253,16 @@ void TCPServerTest::testMultiConnections()
assert (srv.currentConnections() == 0);
}
void TCPServerTest::testThreadCapacity(){
ServerSocket svs(0);
TCPServerParams* pParams = new TCPServerParams;
pParams->setMaxThreads(64);
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs, pParams);
srv.start();
assert (srv.maxThreads() >= 64);
}
void TCPServerTest::setUp()
{
@ -270,6 +281,7 @@ CppUnit::Test* TCPServerTest::suite()
CppUnit_addTest(pSuite, TCPServerTest, testOneConnection);
CppUnit_addTest(pSuite, TCPServerTest, testTwoConnections);
CppUnit_addTest(pSuite, TCPServerTest, testMultiConnections);
CppUnit_addTest(pSuite, TCPServerTest, testThreadCapacity);
return pSuite;
}

View File

@ -49,6 +49,7 @@ public:
void testOneConnection();
void testTwoConnections();
void testMultiConnections();
void testThreadCapacity();
void setUp();
void tearDown();