Skip to content

Commit f269e1c

Browse files
committed
ssl & unclean shutdown response
1 parent 9619dfb commit f269e1c

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

NetSSL_OpenSSL/include/Poco/Net/HTTPSClientSession.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class NetSSL_API HTTPSClientSession: public HTTPClientSession
170170
void connect(const SocketAddress& address);
171171
std::string proxyRequestPrefix() const;
172172
void proxyAuthenticate(HTTPRequest& request);
173+
int read(char* buffer, std::streamsize length);
173174

174175
private:
175176
HTTPSClientSession(const HTTPSClientSession&);

NetSSL_OpenSSL/src/HTTPSClientSession.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "Poco/Net/SecureStreamSocket.h"
3939
#include "Poco/Net/SecureStreamSocketImpl.h"
4040
#include "Poco/Net/SSLManager.h"
41+
#include "Poco/Net/SSLException.h"
4142
#include "Poco/Net/HTTPRequest.h"
4243
#include "Poco/Net/HTTPResponse.h"
4344
#include "Poco/Net/NetException.h"
@@ -190,6 +191,15 @@ void HTTPSClientSession::connect(const SocketAddress& address)
190191
}
191192

192193

194+
int HTTPSClientSession::read(char* buffer, std::streamsize length) {
195+
try {
196+
return HTTPClientSession::read(buffer, length);
197+
} catch(SSLConnectionUnexpectedlyClosedException&) {
198+
return 0;
199+
}
200+
}
201+
202+
193203
Session::Ptr HTTPSClientSession::sslSession()
194204
{
195205
return _pSession;

NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "Poco/Net/Context.h"
4747
#include "Poco/Net/Session.h"
4848
#include "Poco/Net/SSLManager.h"
49+
#include "Poco/Net/SSLException.h"
4950
#include "Poco/Util/Application.h"
5051
#include "Poco/Util/AbstractConfiguration.h"
5152
#include "Poco/StreamCopier.h"
@@ -434,6 +435,40 @@ void HTTPSClientSessionTest::testCachedSession()
434435
}
435436

436437

438+
void HTTPSClientSessionTest::testUnknownContentLength()
439+
{
440+
HTTPSTestServer srv;
441+
HTTPSClientSession s("localhost", srv.port());
442+
HTTPRequest request(HTTPRequest::HTTP_GET, "/nolength");
443+
s.sendRequest(request);
444+
HTTPResponse response;
445+
std::istream& rs = s.receiveResponse(response);
446+
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
447+
assert (response.getContentType() == "text/plain");
448+
std::ostringstream ostr;
449+
StreamCopier::copyStream(rs, ostr);
450+
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
451+
}
452+
453+
454+
void HTTPSClientSessionTest::testServerAbort()
455+
{
456+
HTTPSTestServer srv;
457+
HTTPSClientSession s("localhost", srv.port());
458+
HTTPRequest request(HTTPRequest::HTTP_GET, "/nolength/connection/abort");
459+
s.sendRequest(request);
460+
HTTPResponse response;
461+
std::istream& rs = s.receiveResponse(response);
462+
assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
463+
assert (response.getContentType() == "text/plain");
464+
std::ostringstream ostr;
465+
StreamCopier::copyStream(rs, ostr);
466+
assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
467+
assert ( dynamic_cast<const Poco::Net::SSLConnectionUnexpectedlyClosedException*>(
468+
s.networkException()) != NULL );
469+
}
470+
471+
437472
void HTTPSClientSessionTest::setUp()
438473
{
439474
}
@@ -460,6 +495,8 @@ CppUnit::Test* HTTPSClientSessionTest::suite()
460495
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testInterop);
461496
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testProxy);
462497
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testCachedSession);
498+
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testUnknownContentLength);
499+
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testServerAbort);
463500

464501
return pSuite;
465502
}

NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class HTTPSClientSessionTest: public CppUnit::TestCase
5858
void testInterop();
5959
void testProxy();
6060
void testCachedSession();
61+
void testUnknownContentLength();
62+
void testServerAbort();
63+
6164

6265
void setUp();
6366
void tearDown();

NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "HTTPSTestServer.h"
3434
#include "Poco/Net/SecureStreamSocket.h"
3535
#include "Poco/Net/SocketAddress.h"
36+
#include "Poco/Net/SecureStreamSocketImpl.h"
3637
#include "Poco/Timespan.h"
3738
#include "Poco/NumberFormatter.h"
3839
#include <iostream>
@@ -43,6 +44,7 @@ using Poco::Net::StreamSocket;
4344
using Poco::Net::SecureStreamSocket;
4445
using Poco::Net::SecureServerSocket;
4546
using Poco::Net::SocketAddress;
47+
using Poco::Net::SecureStreamSocketImpl;
4648
using Poco::NumberFormatter;
4749

4850

@@ -115,6 +117,10 @@ void HTTPSTestServer::run()
115117
}
116118
std::string response = handleRequest();
117119
ss.sendBytes(response.data(), (int) response.size());
120+
if(_lastRequest.find("/connection/abort")!=std::string::npos) {
121+
SecureStreamSocketImpl* sss = dynamic_cast<SecureStreamSocketImpl*>(ss.impl());
122+
if(sss!=NULL) sss->abort();
123+
}
118124
Poco::Thread::sleep(1000);
119125
}
120126
catch (Poco::Exception& exc)
@@ -167,6 +173,17 @@ std::string HTTPSTestServer::handleRequest() const
167173
if (_lastRequest.substr(0, 3) == "GET")
168174
response.append(body);
169175
}
176+
else if (_lastRequest.substr(0, 13) == "GET /nolength" ||
177+
_lastRequest.substr(0, 14) == "HEAD /nolength")
178+
{
179+
std::string body(SMALL_BODY);
180+
response.append("HTTP/1.0 200 OK\r\n");
181+
response.append("Content-Type: text/plain\r\n");
182+
response.append("Connection: Close\r\n");
183+
response.append("\r\n");
184+
if (_lastRequest.substr(0, 3) == "GET")
185+
response.append(body);
186+
}
170187
else if (_lastRequest.substr(0, 4) == "POST")
171188
{
172189
std::string::size_type pos = _lastRequest.find("\r\n\r\n");

0 commit comments

Comments
 (0)