mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 02:18:04 +01:00
Net: near complete merge to 1.4.2
This commit is contained in:
@@ -44,6 +44,8 @@
|
||||
#include "Poco/Net/NetException.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
#include "Poco/CountingStream.h"
|
||||
#include "Poco/Base64Encoder.h"
|
||||
#include <sstream>
|
||||
|
||||
|
||||
using Poco::NumberFormatter;
|
||||
@@ -163,6 +165,25 @@ void HTTPClientSession::setProxyPort(Poco::UInt16 port)
|
||||
}
|
||||
|
||||
|
||||
void HTTPClientSession::setProxyCredentials(const std::string& username, const std::string& password)
|
||||
{
|
||||
_proxyUsername = username;
|
||||
_proxyPassword = password;
|
||||
}
|
||||
|
||||
|
||||
void HTTPClientSession::setProxyUsername(const std::string& username)
|
||||
{
|
||||
_proxyUsername = username;
|
||||
}
|
||||
|
||||
|
||||
void HTTPClientSession::setProxyPassword(const std::string& password)
|
||||
{
|
||||
_proxyPassword = password;
|
||||
}
|
||||
|
||||
|
||||
void HTTPClientSession::setKeepAliveTimeout(const Poco::Timespan& timeout)
|
||||
{
|
||||
_keepAliveTimeout = timeout;
|
||||
@@ -180,43 +201,54 @@ std::ostream& HTTPClientSession::sendRequest(HTTPRequest& request)
|
||||
close();
|
||||
_mustReconnect = false;
|
||||
}
|
||||
if (!connected())
|
||||
reconnect();
|
||||
if (!keepAlive)
|
||||
request.setKeepAlive(false);
|
||||
if (!request.has(HTTPRequest::HOST))
|
||||
request.setHost(_host, _port);
|
||||
if (!_proxyHost.empty())
|
||||
request.setURI(proxyRequestPrefix() + request.getURI());
|
||||
_reconnect = keepAlive;
|
||||
_expectResponseBody = request.getMethod() != HTTPRequest::HTTP_HEAD;
|
||||
if (request.getChunkedTransferEncoding())
|
||||
try
|
||||
{
|
||||
HTTPHeaderOutputStream hos(*this);
|
||||
request.write(hos);
|
||||
_pRequestStream = new HTTPChunkedOutputStream(*this);
|
||||
if (!connected())
|
||||
reconnect();
|
||||
if (!keepAlive)
|
||||
request.setKeepAlive(false);
|
||||
if (!request.has(HTTPRequest::HOST))
|
||||
request.setHost(_host, _port);
|
||||
if (!_proxyHost.empty())
|
||||
{
|
||||
request.setURI(proxyRequestPrefix() + request.getURI());
|
||||
proxyAuthenticate(request);
|
||||
}
|
||||
_reconnect = keepAlive;
|
||||
_expectResponseBody = request.getMethod() != HTTPRequest::HTTP_HEAD;
|
||||
if (request.getChunkedTransferEncoding())
|
||||
{
|
||||
HTTPHeaderOutputStream hos(*this);
|
||||
request.write(hos);
|
||||
_pRequestStream = new HTTPChunkedOutputStream(*this);
|
||||
}
|
||||
else if (request.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH)
|
||||
{
|
||||
Poco::CountingOutputStream cs;
|
||||
request.write(cs);
|
||||
_pRequestStream = new HTTPFixedLengthOutputStream(*this, request.getContentLength() + cs.chars());
|
||||
request.write(*_pRequestStream);
|
||||
}
|
||||
else if (request.getMethod() != HTTPRequest::HTTP_PUT && request.getMethod() != HTTPRequest::HTTP_POST)
|
||||
{
|
||||
Poco::CountingOutputStream cs;
|
||||
request.write(cs);
|
||||
_pRequestStream = new HTTPFixedLengthOutputStream(*this, cs.chars());
|
||||
request.write(*_pRequestStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
_pRequestStream = new HTTPOutputStream(*this);
|
||||
request.write(*_pRequestStream);
|
||||
}
|
||||
_lastRequest.update();
|
||||
return *_pRequestStream;
|
||||
}
|
||||
else if (request.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH)
|
||||
catch (Exception&)
|
||||
{
|
||||
Poco::CountingOutputStream cs;
|
||||
request.write(cs);
|
||||
_pRequestStream = new HTTPFixedLengthOutputStream(*this, request.getContentLength() + cs.chars());
|
||||
request.write(*_pRequestStream);
|
||||
close();
|
||||
throw;
|
||||
}
|
||||
else if (request.getMethod() != HTTPRequest::HTTP_PUT && request.getMethod() != HTTPRequest::HTTP_POST)
|
||||
{
|
||||
Poco::CountingOutputStream cs;
|
||||
request.write(cs);
|
||||
_pRequestStream = new HTTPFixedLengthOutputStream(*this, cs.chars());
|
||||
request.write(*_pRequestStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
_pRequestStream = new HTTPOutputStream(*this);
|
||||
request.write(*_pRequestStream);
|
||||
}
|
||||
_lastRequest.update();
|
||||
return *_pRequestStream;
|
||||
}
|
||||
|
||||
|
||||
@@ -235,11 +267,17 @@ std::istream& HTTPClientSession::receiveResponse(HTTPResponse& response)
|
||||
}
|
||||
catch (MessageException&)
|
||||
{
|
||||
close();
|
||||
if (networkException())
|
||||
networkException()->rethrow();
|
||||
else
|
||||
throw;
|
||||
}
|
||||
catch (Exception&)
|
||||
{
|
||||
close();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
while (response.getStatus() == HTTPResponse::HTTP_CONTINUE);
|
||||
|
||||
@@ -258,6 +296,18 @@ std::istream& HTTPClientSession::receiveResponse(HTTPResponse& response)
|
||||
}
|
||||
|
||||
|
||||
void HTTPClientSession::reset()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
bool HTTPClientSession::secure() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int HTTPClientSession::write(const char* buffer, std::streamsize length)
|
||||
{
|
||||
try
|
||||
@@ -322,7 +372,7 @@ void HTTPClientSession::deleteRequestStream()
|
||||
|
||||
void HTTPClientSession::setResponseStream(std::istream* pRespStream)
|
||||
{
|
||||
poco_assert( !_pResponseStream);
|
||||
poco_assert (!_pResponseStream);
|
||||
_pResponseStream = pRespStream;
|
||||
}
|
||||
|
||||
@@ -345,4 +395,24 @@ bool HTTPClientSession::mustReconnect() const
|
||||
}
|
||||
|
||||
|
||||
void HTTPClientSession::proxyAuthenticate(HTTPRequest& request)
|
||||
{
|
||||
proxyAuthenticateImpl(request);
|
||||
}
|
||||
|
||||
|
||||
void HTTPClientSession::proxyAuthenticateImpl(HTTPRequest& request)
|
||||
{
|
||||
if (!_proxyUsername.empty())
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
ostr << "Basic ";
|
||||
Base64Encoder encoder(ostr);
|
||||
encoder << _proxyUsername << ":" << _proxyPassword;
|
||||
encoder.close();
|
||||
request.set("Proxy-Authorization", ostr.str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
||||
|
||||
Reference in New Issue
Block a user