added https->http redirect, improved redirect support in streamfactory, fixed client cert validation error

This commit is contained in:
Peter Schojer
2008-12-19 08:10:04 +00:00
parent f84e0947f7
commit 8a2502bce4
5 changed files with 92 additions and 47 deletions

View File

@@ -75,17 +75,25 @@ HTTPSStreamFactory::~HTTPSStreamFactory()
std::istream* HTTPSStreamFactory::open(const URI& uri)
{
poco_assert (uri.getScheme() == "https");
poco_assert (uri.getScheme() == "https" || uri.getScheme() == "http");
URI resolvedURI(uri);
URI proxyUri;
HTTPClientSession* pSession = 0;
try
{
bool retry = false;
int redirects = 0;
do
{
pSession = new HTTPSClientSession(resolvedURI.getHost(), resolvedURI.getPort());
pSession->setProxy(_proxyHost, _proxyPort);
if (resolvedURI.getScheme() != "http")
pSession = new HTTPSClientSession(resolvedURI.getHost(), resolvedURI.getPort());
else
pSession = new HTTPClientSession(resolvedURI.getHost(), resolvedURI.getPort());
if (proxyUri.empty())
pSession->setProxy(_proxyHost, _proxyPort);
else
pSession->setProxy(proxyUri.getHost(), proxyUri.getPort());
std::string path = resolvedURI.getPathAndQuery();
if (path.empty()) path = "/";
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
@@ -94,20 +102,38 @@ std::istream* HTTPSStreamFactory::open(const URI& uri)
std::istream& rs = pSession->receiveResponse(res);
bool moved = (res.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY ||
res.getStatus() == HTTPResponse::HTTP_FOUND ||
res.getStatus() == HTTPResponse::HTTP_SEE_OTHER);
res.getStatus() == HTTPResponse::HTTP_SEE_OTHER ||
res.getStatus() == HTTPResponse::HTTP_TEMPORARY_REDIRECT);
if (moved)
{
resolvedURI.resolve(res.get("Location"));
delete pSession;
//throw URIRedirection(resolvedURI.toString());
delete pSession; pSession = 0;
++redirects;
retry = true;
}
else if (res.getStatus() == HTTPResponse::HTTP_OK)
{
return new HTTPResponseStream(rs, pSession);
}
else throw HTTPException(res.getReason(), uri.toString());
else if (res.getStatus() == HTTPResponse::HTTP_USEPROXY && !retry)
{
//The requested resource MUST be accessed through the proxy
//given by the Location field. The Location field gives the
//URI of the proxy. The recipient is expected to repeat this
//single request via the proxy. 305 responses MUST only be generated by origin servers.
// only use for one single request!
proxyUri.resolve(res.get("Location"));
delete pSession; pSession = 0;
retry = true; //only allow useproxy once
}
else
{
delete pSession; pSession = 0;
throw HTTPException(res.getReason(), uri.toString());
}
}
while (redirects < MAX_REDIRECTS);
while (retry && redirects < MAX_REDIRECTS);
throw HTTPException("Too many redirects", uri.toString());
}
catch (...)