mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-poco.git
synced 2025-10-30 18:17:50 +00:00
HTTPSClient (incomplete testsuite)
This commit is contained in:
150
NetSSL_OpenSSL/include/Poco/Net/HTTPSClient.h
Normal file
150
NetSSL_OpenSSL/include/Poco/Net/HTTPSClient.h
Normal file
@@ -0,0 +1,150 @@
|
||||
//
|
||||
// HTTPSClient.h
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/include/Poco/Net/HTTPSClient.h#2 $
|
||||
//
|
||||
// Library: NetSSL_OpenSSL
|
||||
// Package: HTTPSClient
|
||||
// Module: HTTPSClient
|
||||
//
|
||||
// Definition of the HTTPSClient class.
|
||||
//
|
||||
// Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef NetSSL_HTTPSClient_INCLUDED
|
||||
#define NetSSL_HTTPSClient_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Net/NetSSL.h"
|
||||
#include "Poco/Net/Utility.h"
|
||||
#include "Poco/Net/HTTPClient.h"
|
||||
#include "Poco/Net/Context.h"
|
||||
#include "Poco/Net/Session.h"
|
||||
#include "Poco/Net/HTTPSSessionInstantiator.h"
|
||||
#include "Poco/Net/X509Certificate.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
class SecureStreamSocket;
|
||||
class HTTPRequest;
|
||||
class HTTPResponse;
|
||||
|
||||
|
||||
class NetSSL_API HTTPSClient: public HTTPClient
|
||||
/// This class implements the client-side of
|
||||
/// a HTTPS session.
|
||||
///
|
||||
/// To send a HTTPS request to a HTTPS server, first
|
||||
/// instantiate a HTTPSClient object and
|
||||
/// specify the server's host name and port number.
|
||||
///
|
||||
/// Then create a HTTPRequest object, fill it accordingly,
|
||||
/// and pass it as argument to the sendRequst() method.
|
||||
///
|
||||
/// sendRequest() will return an output stream that can
|
||||
/// be used to send the request body, if there is any.
|
||||
///
|
||||
/// After you are done sending the request body, create
|
||||
/// a HTTPResponse object and pass it to receiveResponse().
|
||||
///
|
||||
/// This will return an input stream that can be used to
|
||||
/// read the response body.
|
||||
///
|
||||
/// See RFC 2616 <http://www.faqs.org/rfcs/rfc2616.html> for more
|
||||
/// information about the HTTP protocol.
|
||||
///
|
||||
/// Note that sending requests that neither contain a content length
|
||||
/// field in the header nor are using chunked transfer encoding will
|
||||
/// result in a SSL protocol violation, as the framework shuts down
|
||||
/// the socket after sending the message body. No orderly SSL shutdown
|
||||
/// will be performed in this case.
|
||||
///
|
||||
/// If session caching has been enabled for the Context object passed
|
||||
/// to the HTTPSClient, the HTTPSClient class will
|
||||
/// attempt to reuse a previously obtained Session object in
|
||||
/// case of a reconnect.
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
HTTPS_PORT = 443
|
||||
};
|
||||
|
||||
explicit HTTPSClient(const URI& uri,
|
||||
InstantiatorPtr pInstantiator = new HTTPSSessionInstantiator,
|
||||
bool redirect = true);
|
||||
/// Creates a HTTPSClient using the given URI.
|
||||
|
||||
explicit HTTPSClient(const SocketAddress& address,
|
||||
const std::string& scheme = "https",
|
||||
InstantiatorPtr pInstantiator = new HTTPSSessionInstantiator,
|
||||
bool redirect = true);
|
||||
/// Creates a HTTPSClient using the given address.
|
||||
|
||||
HTTPSClient(const std::string& host,
|
||||
Poco::UInt16 port = HTTPS_PORT,
|
||||
const std::string& scheme = "https",
|
||||
InstantiatorPtr pInstantiator = new HTTPSSessionInstantiator,
|
||||
bool redirect = true);
|
||||
/// Creates a HTTPSClient using the given host and port.
|
||||
|
||||
HTTPSClient(const std::string& host,
|
||||
Poco::UInt16 port,
|
||||
const HTTPClientSession::ProxyConfig& proxyConfig,
|
||||
const std::string& scheme = "https",
|
||||
InstantiatorPtr pInstantiator = new HTTPSSessionInstantiator,
|
||||
bool redirect = true);
|
||||
/// Creates a HTTPClient using the given host, port and proxy configuration.
|
||||
|
||||
~HTTPSClient();
|
||||
/// Destroys the HTTPSClient and closes
|
||||
/// the underlying socket.
|
||||
|
||||
bool secure() const;
|
||||
/// Return true iff the session uses SSL or TLS,
|
||||
/// or false otherwise.
|
||||
|
||||
//X509Certificate serverCertificate();
|
||||
/// Returns the server's certificate.
|
||||
///
|
||||
/// The certificate is available after the first request has been sent.
|
||||
|
||||
Session::Ptr sslSession();
|
||||
/// Returns the SSL Session object for the current
|
||||
/// connection, if session caching has been enabled for
|
||||
/// the HTTPSClient's Context. A null pointer is
|
||||
/// returned otherwise.
|
||||
///
|
||||
/// The Session object can be obtained after the first request has
|
||||
/// been sent.
|
||||
/*
|
||||
// HTTPSession
|
||||
void abort();
|
||||
|
||||
protected:
|
||||
void connect(const SocketAddress& address);
|
||||
std::string proxyRequestPrefix() const;
|
||||
void proxyAuthenticate(HTTPRequest& request);
|
||||
int read(char* buffer, std::streamsize length);
|
||||
*/
|
||||
private:
|
||||
HTTPSClient(const HTTPSClient&);
|
||||
HTTPSClient& operator = (const HTTPSClient&);
|
||||
|
||||
Context::Ptr _pContext;
|
||||
Session::Ptr _pSession;
|
||||
};
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
||||
|
||||
|
||||
#endif // Net_HTTPSClient_INCLUDED
|
||||
157
NetSSL_OpenSSL/src/HTTPSClient.cpp
Normal file
157
NetSSL_OpenSSL/src/HTTPSClient.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
//
|
||||
// HTTPSClient.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/src/HTTPSClient.cpp#4 $
|
||||
//
|
||||
// Library: NetSSL_OpenSSL
|
||||
// Package: HTTPSClient
|
||||
// Module: HTTPSClient
|
||||
//
|
||||
// Copyright (c) 2006-2010, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Net/HTTPSClient.h"
|
||||
#include "Poco/Net/SecureStreamSocket.h"
|
||||
#include "Poco/Net/SecureStreamSocketImpl.h"
|
||||
#include "Poco/Net/SSLManager.h"
|
||||
#include "Poco/Net/SSLException.h"
|
||||
#include "Poco/Net/HTTPRequest.h"
|
||||
#include "Poco/Net/HTTPResponse.h"
|
||||
#include "Poco/Net/NetException.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
|
||||
|
||||
using Poco::NumberFormatter;
|
||||
using Poco::IllegalStateException;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
HTTPSClient::HTTPSClient(const URI& uri,
|
||||
InstantiatorPtr pInstantiator,
|
||||
bool redirect):
|
||||
HTTPClient(uri, pInstantiator, redirect)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
HTTPSClient::HTTPSClient(const SocketAddress& address,
|
||||
const std::string& scheme,
|
||||
InstantiatorPtr pInstantiator,
|
||||
bool redirect):
|
||||
HTTPClient(address, scheme, pInstantiator, redirect)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
HTTPSClient::HTTPSClient(const std::string& host,
|
||||
Poco::UInt16 port,
|
||||
const std::string& scheme,
|
||||
InstantiatorPtr pInstantiator,
|
||||
bool redirect):
|
||||
HTTPClient(host, port, scheme, pInstantiator, redirect)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
HTTPSClient::HTTPSClient(const std::string& host,
|
||||
Poco::UInt16 port,
|
||||
const HTTPClientSession::ProxyConfig& proxyConfig,
|
||||
const std::string& scheme,
|
||||
InstantiatorPtr pInstantiator,
|
||||
bool redirect):
|
||||
HTTPClient(host, port, proxyConfig, scheme, pInstantiator, redirect)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
HTTPSClient::~HTTPSClient()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool HTTPSClient::secure() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
void HTTPSClient::abort()
|
||||
{
|
||||
SecureStreamSocket sss(socket());
|
||||
sss.abort();
|
||||
}
|
||||
|
||||
|
||||
X509Certificate HTTPSClient::serverCertificate()
|
||||
{
|
||||
SecureStreamSocket sss(socket());
|
||||
return sss.peerCertificate();
|
||||
}
|
||||
|
||||
|
||||
std::string HTTPSClient::proxyRequestPrefix() const
|
||||
{
|
||||
return std::string();
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClient::proxyAuthenticate(HTTPRequest& request)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClient::connect(const SocketAddress& address)
|
||||
{
|
||||
if (getProxyHost().empty() || bypassProxy())
|
||||
{
|
||||
SecureStreamSocket sss(socket());
|
||||
if (_pContext->sessionCacheEnabled())
|
||||
{
|
||||
sss.useSession(_pSession);
|
||||
}
|
||||
connect(address);
|
||||
if (_pContext->sessionCacheEnabled())
|
||||
{
|
||||
_pSession = sss.currentSession();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
StreamSocket proxySocket(proxyConnect());
|
||||
SecureStreamSocket secureSocket = SecureStreamSocket::attach(proxySocket, getHost(), _pContext, _pSession);
|
||||
attachSocket(secureSocket);
|
||||
if (_pContext->sessionCacheEnabled())
|
||||
{
|
||||
_pSession = secureSocket.currentSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int HTTPSClient::read(char* buffer, std::streamsize length)
|
||||
{
|
||||
try
|
||||
{
|
||||
return read(buffer, length);
|
||||
}
|
||||
catch(SSLConnectionUnexpectedlyClosedException&)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
Session::Ptr HTTPSClient::sslSession()
|
||||
{
|
||||
return _pSession;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
||||
499
NetSSL_OpenSSL/testsuite/src/HTTPSClientTest.cpp
Normal file
499
NetSSL_OpenSSL/testsuite/src/HTTPSClientTest.cpp
Normal file
@@ -0,0 +1,499 @@
|
||||
//
|
||||
// HTTPSClientTest.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/testsuite/src/HTTPSClientTest.cpp#1 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "HTTPSClientTest.h"
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Net/HTTPSClient.h"
|
||||
#include "Poco/Net/HTTPRequest.h"
|
||||
#include "Poco/Net/HTTPRequestHandler.h"
|
||||
#include "Poco/Net/HTTPRequestHandlerFactory.h"
|
||||
#include "Poco/Net/HTTPResponse.h"
|
||||
#include "Poco/Net/HTTPServer.h"
|
||||
#include "Poco/Net/HTTPServerResponse.h"
|
||||
#include "Poco/Net/HTTPServerRequest.h"
|
||||
#include "Poco/Net/HTTPServerParams.h"
|
||||
#include "Poco/Net/SecureStreamSocket.h"
|
||||
#include "Poco/Net/Context.h"
|
||||
#include "Poco/Net/Session.h"
|
||||
#include "Poco/Net/SSLManager.h"
|
||||
#include "Poco/Net/SSLException.h"
|
||||
#include "Poco/Util/Application.h"
|
||||
#include "Poco/Util/AbstractConfiguration.h"
|
||||
#include "Poco/StreamCopier.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/DateTimeFormatter.h"
|
||||
#include "Poco/DateTimeFormat.h"
|
||||
#include "Poco/Delegate.h"
|
||||
#include "Poco/Thread.h"
|
||||
#include "Poco/SharedPtr.h"
|
||||
#include "HTTPSTestServer.h"
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
using namespace Poco::Net;
|
||||
using Poco::Util::Application;
|
||||
using Poco::StreamCopier;
|
||||
using Poco::Delegate;
|
||||
using Poco::Thread;
|
||||
using Poco::SharedPtr;
|
||||
|
||||
|
||||
class TestRequestHandler: public HTTPRequestHandler
|
||||
/// Return a HTML document with the current date and time.
|
||||
{
|
||||
public:
|
||||
TestRequestHandler()
|
||||
{
|
||||
}
|
||||
|
||||
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
|
||||
{
|
||||
response.setChunkedTransferEncoding(true);
|
||||
response.setContentType(request.getContentType());
|
||||
std::ostream& ostr = response.send();
|
||||
Poco::StreamCopier::copyStream(request.stream(), ostr);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class TestRequestHandlerFactory: public HTTPRequestHandlerFactory
|
||||
{
|
||||
public:
|
||||
TestRequestHandlerFactory()
|
||||
{
|
||||
}
|
||||
|
||||
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
|
||||
{
|
||||
return new TestRequestHandler();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
HTTPSClientTest::HTTPSClientTest(const std::string& name): CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
HTTPSClientTest::~HTTPSClientTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::onResponse(const void* pSender, HTTPEventArgs& args)
|
||||
{
|
||||
_contentType = args.response().getContentType();
|
||||
_contentLength = static_cast<int>(args.response().getContentLength());
|
||||
_chunked = args.response().getChunkedTransferEncoding();
|
||||
_keepAlive = args.response().getKeepAlive();
|
||||
_body = args.body();
|
||||
_done = true;
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::onError(const void* pSender, HTTPEventArgs& args)
|
||||
{
|
||||
_error = args.response().getStatus();
|
||||
_done = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void HTTPSClientTest::testGetSmall()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.httpResponse += Delegate<HTTPSClientTest, HTTPEventArgs>(this, &HTTPSClientTest::onResponse);
|
||||
s.sendGet("/small");
|
||||
while (!_done) Thread::sleep(10);
|
||||
assert(_contentLength == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert(_contentType == "text/plain");
|
||||
assert(!_chunked);
|
||||
assert(_body == HTTPSTestServer::SMALL_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testGetLarge()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.httpResponse += Delegate<HTTPSClientTest, HTTPEventArgs>(this, &HTTPSClientTest::onResponse);
|
||||
s.sendGet("/large");
|
||||
while (!_done) Thread::sleep(10);
|
||||
assert(_contentLength == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert(_contentType == "text/plain");
|
||||
assert(!_chunked);
|
||||
assert(_body == HTTPSTestServer::LARGE_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testHead()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.httpResponse += Delegate<HTTPSClientTest, HTTPEventArgs>(this, &HTTPSClientTest::onResponse);
|
||||
s.sendHead("/large");
|
||||
while (!_done) Thread::sleep(10);
|
||||
assert(_contentLength == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert(_contentType == "text/plain");
|
||||
assert(!_chunked);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testPostSmallIdentity()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.httpResponse += Delegate<HTTPSClientTest, HTTPEventArgs>(this, &HTTPSClientTest::onResponse);
|
||||
std::string body("this is a random request body\r\n0\r\n");
|
||||
s.sendPost("/echo", body);
|
||||
while (!_done) Thread::sleep(10);
|
||||
assert(_contentLength == body.length());
|
||||
assert(!_chunked);
|
||||
assert(_body == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testPostLargeIdentity()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.httpResponse += Delegate<HTTPSClientTest, HTTPEventArgs>(this, &HTTPSClientTest::onResponse);
|
||||
std::string body(8000, 'x');
|
||||
body.append("\r\n0\r\n");
|
||||
s.sendPost("/echo", body);
|
||||
while (!_done) Thread::sleep(10);
|
||||
assert(_contentLength == body.length());
|
||||
assert(!_chunked);
|
||||
assert(_body == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testPostSmallChunked()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.httpResponse += Delegate<HTTPSClientTest, HTTPEventArgs>(this, &HTTPSClientTest::onResponse);
|
||||
SharedPtr<HTTPRequest> request = new HTTPRequest(HTTPRequest::HTTP_POST, "/echo");
|
||||
std::string body("this is a random request body");
|
||||
request->setChunkedTransferEncoding(true);
|
||||
s.sendRequest(request, body);
|
||||
while (!_done) Thread::sleep(10);
|
||||
assert(_contentLength == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert(_chunked);
|
||||
assert(_body == body);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testPostLargeChunked()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.httpResponse += Delegate<HTTPSClientTest, HTTPEventArgs>(this, &HTTPSClientTest::onResponse);
|
||||
SharedPtr<HTTPRequest> request = new HTTPRequest(HTTPRequest::HTTP_POST, "/echo");
|
||||
std::string body(16000, 'x');
|
||||
request->setChunkedTransferEncoding(true);
|
||||
s.sendRequest(request, body);
|
||||
while (!_done) Thread::sleep(10);
|
||||
assert(_contentLength == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert(_chunked);
|
||||
assert(_body == body);
|
||||
}
|
||||
|
||||
/*
|
||||
void HTTPSClientTest::testPostLargeChunkedKeepAlive()
|
||||
{
|
||||
SecureServerSocket svs(32322);
|
||||
HTTPServer srv(new TestRequestHandlerFactory(), svs, new HTTPServerParams());
|
||||
srv.start();
|
||||
try
|
||||
{
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.setKeepAlive(true);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
HTTPRequest request(HTTPRequest::HTTP_POST, "/keepAlive", HTTPMessage::HTTP_1_1);
|
||||
std::string body(16000, 'x');
|
||||
request.setChunkedTransferEncoding(true);
|
||||
s.sendRequest(request) << body;
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == body);
|
||||
}
|
||||
srv.stop();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
srv.stop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testKeepAlive()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
s.setKeepAlive(true);
|
||||
HTTPRequest request(HTTPRequest::HTTP_HEAD, "/keepAlive", HTTPMessage::HTTP_1_1);
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs1 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr1;
|
||||
assert (StreamCopier::copyStream(rs1, ostr1) == 0);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_GET);
|
||||
request.setURI("/small");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs2 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr2;
|
||||
StreamCopier::copyStream(rs2, ostr2);
|
||||
assert (ostr2.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_GET);
|
||||
request.setURI("/large");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs3 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getChunkedTransferEncoding());
|
||||
assert (response.getKeepAlive());
|
||||
std::ostringstream ostr3;
|
||||
StreamCopier::copyStream(rs3, ostr3);
|
||||
assert (ostr3.str() == HTTPSTestServer::LARGE_BODY);
|
||||
|
||||
request.setMethod(HTTPRequest::HTTP_HEAD);
|
||||
request.setURI("/large");
|
||||
s.sendRequest(request);
|
||||
std::istream& rs4 = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
|
||||
assert (response.getContentType() == "text/plain");
|
||||
assert (!response.getKeepAlive());
|
||||
std::ostringstream ostr4;
|
||||
assert (StreamCopier::copyStream(rs4, ostr4) == 0);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testInterop()
|
||||
{
|
||||
HTTPSClient s("secure.appinf.com");
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/public/poco/NetSSL.txt");
|
||||
s.sendRequest(request);
|
||||
X509Certificate cert = s.serverCertificate();
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
std::string str(ostr.str());
|
||||
assert (str == "This is a test file for NetSSL.\n");
|
||||
assert (cert.commonName() == "secure.appinf.com" || cert.commonName() == "*.appinf.com");
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testProxy()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("secure.appinf.com");
|
||||
s.setProxy(
|
||||
Application::instance().config().getString("testsuite.proxy.host"),
|
||||
Application::instance().config().getInt("testsuite.proxy.port")
|
||||
);
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/public/poco/NetSSL.txt");
|
||||
s.sendRequest(request);
|
||||
X509Certificate cert = s.serverCertificate();
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
std::string str(ostr.str());
|
||||
assert (str == "This is a test file for NetSSL.\n");
|
||||
assert (cert.commonName() == "secure.appinf.com" || cert.commonName() == "*.appinf.com");
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testCachedSession()
|
||||
{
|
||||
// ensure OpenSSL machinery is fully setup
|
||||
Context::Ptr pDefaultServerContext = SSLManager::instance().defaultServerContext();
|
||||
Context::Ptr pDefaultClientContext = SSLManager::instance().defaultClientContext();
|
||||
|
||||
Context::Ptr pServerContext = new Context(
|
||||
Context::SERVER_USE,
|
||||
Application::instance().config().getString("openSSL.server.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.server.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.server.caConfig"),
|
||||
Context::VERIFY_NONE,
|
||||
9,
|
||||
true,
|
||||
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
|
||||
pServerContext->enableSessionCache(true, "TestSuite");
|
||||
pServerContext->setSessionTimeout(10);
|
||||
pServerContext->setSessionCacheSize(1000);
|
||||
pServerContext->disableStatelessSessionResumption();
|
||||
|
||||
HTTPSTestServer srv(pServerContext);
|
||||
|
||||
Context::Ptr pClientContext = new Context(
|
||||
Context::CLIENT_USE,
|
||||
Application::instance().config().getString("openSSL.client.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.client.privateKeyFile"),
|
||||
Application::instance().config().getString("openSSL.client.caConfig"),
|
||||
Context::VERIFY_RELAXED,
|
||||
9,
|
||||
true,
|
||||
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
|
||||
pClientContext->enableSessionCache(true);
|
||||
|
||||
HTTPSClient s1("localhost", srv.port(), pClientContext);
|
||||
HTTPRequest request1(HTTPRequest::HTTP_GET, "/small");
|
||||
s1.sendRequest(request1);
|
||||
Session::Ptr pSession1 = s1.sslSession();
|
||||
HTTPResponse response1;
|
||||
std::istream& rs1 = s1.receiveResponse(response1);
|
||||
assert (response1.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response1.getContentType() == "text/plain");
|
||||
std::ostringstream ostr1;
|
||||
StreamCopier::copyStream(rs1, ostr1);
|
||||
assert (ostr1.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
HTTPSClient s2("localhost", srv.port(), pClientContext, pSession1);
|
||||
HTTPRequest request2(HTTPRequest::HTTP_GET, "/small");
|
||||
s2.sendRequest(request2);
|
||||
Session::Ptr pSession2 = s2.sslSession();
|
||||
HTTPResponse response2;
|
||||
std::istream& rs2 = s2.receiveResponse(response2);
|
||||
assert (response2.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response2.getContentType() == "text/plain");
|
||||
std::ostringstream ostr2;
|
||||
StreamCopier::copyStream(rs2, ostr2);
|
||||
assert (ostr2.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
assert (pSession1 == pSession2);
|
||||
|
||||
HTTPRequest request3(HTTPRequest::HTTP_GET, "/small");
|
||||
s2.sendRequest(request3);
|
||||
Session::Ptr pSession3 = s2.sslSession();
|
||||
HTTPResponse response3;
|
||||
std::istream& rs3 = s2.receiveResponse(response3);
|
||||
assert (response3.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response3.getContentType() == "text/plain");
|
||||
std::ostringstream ostr3;
|
||||
StreamCopier::copyStream(rs3, ostr3);
|
||||
assert (ostr3.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
assert (pSession1 == pSession3);
|
||||
|
||||
Thread::sleep(15000); // wait for session to expire
|
||||
pServerContext->flushSessionCache();
|
||||
|
||||
HTTPRequest request4(HTTPRequest::HTTP_GET, "/small");
|
||||
s2.sendRequest(request4);
|
||||
Session::Ptr pSession4 = s2.sslSession();
|
||||
HTTPResponse response4;
|
||||
std::istream& rs4 = s2.receiveResponse(response4);
|
||||
assert (response4.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
|
||||
assert (response4.getContentType() == "text/plain");
|
||||
std::ostringstream ostr4;
|
||||
StreamCopier::copyStream(rs4, ostr4);
|
||||
assert (ostr4.str() == HTTPSTestServer::SMALL_BODY);
|
||||
|
||||
assert (pSession1 != pSession4);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testUnknownContentLength()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/nolength");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::testServerAbort()
|
||||
{
|
||||
HTTPSTestServer srv;
|
||||
HTTPSClient s("localhost", srv.port());
|
||||
HTTPRequest request(HTTPRequest::HTTP_GET, "/nolength/connection/abort");
|
||||
s.sendRequest(request);
|
||||
HTTPResponse response;
|
||||
std::istream& rs = s.receiveResponse(response);
|
||||
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
|
||||
assert (response.getContentType() == "text/plain");
|
||||
std::ostringstream ostr;
|
||||
StreamCopier::copyStream(rs, ostr);
|
||||
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
|
||||
assert ( dynamic_cast<const Poco::Net::SSLConnectionUnexpectedlyClosedException*>(
|
||||
s.networkException()) != NULL );
|
||||
}
|
||||
*/
|
||||
|
||||
void HTTPSClientTest::setUp()
|
||||
{
|
||||
_contentType.clear();
|
||||
_contentLength = HTTPMessage::UNKNOWN_CONTENT_LENGTH;
|
||||
_chunked = false;
|
||||
_keepAlive = false;
|
||||
_body.clear();
|
||||
_error = HTTPResponse::HTTP_OK;
|
||||
_done = false;
|
||||
}
|
||||
|
||||
|
||||
void HTTPSClientTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CppUnit::Test* HTTPSClientTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientTest");
|
||||
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testGetSmall);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testGetLarge);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testHead);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testPostSmallIdentity);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testPostLargeIdentity);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testPostSmallChunked);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testPostLargeChunked);
|
||||
/*
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testPostLargeChunkedKeepAlive);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testKeepAlive);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testInterop);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testProxy);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testCachedSession);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testUnknownContentLength);
|
||||
CppUnit_addTest(pSuite, HTTPSClientTest, testServerAbort);
|
||||
*/
|
||||
return pSuite;
|
||||
}
|
||||
68
NetSSL_OpenSSL/testsuite/src/HTTPSClientTest.h
Normal file
68
NetSSL_OpenSSL/testsuite/src/HTTPSClientTest.h
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// HTTPSClientTest.h
|
||||
//
|
||||
// $Id: //poco/1.4/NetSSL_OpenSSL/testsuite/src/HTTPSClientTest.h#1 $
|
||||
//
|
||||
// Definition of the HTTPSClientTest class.
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef HTTPSClientTest_INCLUDED
|
||||
#define HTTPSClientTest_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Net/Net.h"
|
||||
#include "Poco/Net/HTTPEventArgs.h"
|
||||
#include "CppUnit/TestCase.h"
|
||||
|
||||
|
||||
class HTTPSClientTest: public CppUnit::TestCase
|
||||
{
|
||||
public:
|
||||
HTTPSClientTest(const std::string& name);
|
||||
~HTTPSClientTest();
|
||||
|
||||
void onResponse(const void* pSender, Poco::Net::HTTPEventArgs& args);
|
||||
void onError(const void* pSender, Poco::Net::HTTPEventArgs& args);
|
||||
|
||||
void testGetSmall();
|
||||
void testGetLarge();
|
||||
void testHead();
|
||||
void testPostSmallIdentity();
|
||||
void testPostLargeIdentity();
|
||||
void testPostSmallChunked();
|
||||
void testPostLargeChunked();
|
||||
/*
|
||||
void testPostLargeChunkedKeepAlive();
|
||||
void testKeepAlive();
|
||||
void testInterop();
|
||||
void testProxy();
|
||||
void testCachedSession();
|
||||
void testUnknownContentLength();
|
||||
void testServerAbort();
|
||||
*/
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
typedef Poco::Net::HTTPResponse::HTTPStatus Status;
|
||||
|
||||
std::string _body;
|
||||
std::string _contentType;
|
||||
int _contentLength;
|
||||
bool _chunked;
|
||||
bool _keepAlive;
|
||||
Status _error;
|
||||
bool _done;
|
||||
};
|
||||
|
||||
|
||||
#endif // HTTPSClientTest_INCLUDED
|
||||
Reference in New Issue
Block a user