mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-24 17:30:44 +02:00
backport changes from 1.4.3 branch
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
//
|
||||
// HTTPStreamFactory.cpp
|
||||
//
|
||||
// $Id: //poco/svn/Net/src/HTTPStreamFactory.cpp#3 $
|
||||
// $Id: //poco/1.4/Net/src/HTTPStreamFactory.cpp#2 $
|
||||
//
|
||||
// Library: Net
|
||||
// Package: HTTP
|
||||
// Module: HTTPStreamFactory
|
||||
//
|
||||
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
|
||||
// Copyright (c) 2005-2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
@@ -39,10 +39,13 @@
|
||||
#include "Poco/Net/HTTPIOStream.h"
|
||||
#include "Poco/Net/HTTPRequest.h"
|
||||
#include "Poco/Net/HTTPResponse.h"
|
||||
#include "Poco/Net/HTTPCredentials.h"
|
||||
#include "Poco/Net/NetException.h"
|
||||
#include "Poco/URI.h"
|
||||
#include "Poco/URIStreamOpener.h"
|
||||
#include "Poco/UnbufferedStreamBuf.h"
|
||||
#include "Poco/NullStream.h"
|
||||
#include "Poco/StreamCopier.h"
|
||||
|
||||
|
||||
using Poco::URIStreamFactory;
|
||||
@@ -86,39 +89,57 @@ std::istream* HTTPStreamFactory::open(const URI& uri)
|
||||
{
|
||||
poco_assert (uri.getScheme() == "http");
|
||||
|
||||
URI resolvedURI(uri);
|
||||
URI proxyUri;
|
||||
HTTPClientSession* pSession = 0;
|
||||
bool retry = false;
|
||||
URI resolvedURI(uri);
|
||||
URI proxyUri;
|
||||
HTTPClientSession* pSession = 0;
|
||||
HTTPResponse res;
|
||||
bool retry = false;
|
||||
bool authorize = false;
|
||||
std::string username;
|
||||
std::string password;
|
||||
|
||||
try
|
||||
{
|
||||
do
|
||||
{
|
||||
pSession = new HTTPClientSession(resolvedURI.getHost(), resolvedURI.getPort());
|
||||
|
||||
if (proxyUri.empty())
|
||||
pSession->setProxy(_proxyHost, _proxyPort);
|
||||
else
|
||||
pSession->setProxy(proxyUri.getHost(), proxyUri.getPort());
|
||||
pSession->setProxyCredentials(_proxyUsername, _proxyPassword);
|
||||
|
||||
std::string path = resolvedURI.getPathAndQuery();
|
||||
if (path.empty()) path = "/";
|
||||
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
||||
pSession->sendRequest(req);
|
||||
HTTPResponse res;
|
||||
std::istream& rs = pSession->receiveResponse(res);
|
||||
bool moved = (res.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY ||
|
||||
res.getStatus() == HTTPResponse::HTTP_FOUND ||
|
||||
try
|
||||
{
|
||||
do
|
||||
{
|
||||
if (!pSession)
|
||||
{
|
||||
pSession = new HTTPClientSession(resolvedURI.getHost(), resolvedURI.getPort());
|
||||
|
||||
if (proxyUri.empty())
|
||||
pSession->setProxy(_proxyHost, _proxyPort);
|
||||
else
|
||||
pSession->setProxy(proxyUri.getHost(), proxyUri.getPort());
|
||||
pSession->setProxyCredentials(_proxyUsername, _proxyPassword);
|
||||
}
|
||||
|
||||
std::string path = resolvedURI.getPathAndQuery();
|
||||
if (path.empty()) path = "/";
|
||||
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
||||
|
||||
if (authorize)
|
||||
{
|
||||
HTTPCredentials::extractCredentials(uri, username, password);
|
||||
HTTPCredentials cred(username, password);
|
||||
cred.authenticate(req, res);
|
||||
}
|
||||
|
||||
pSession->sendRequest(req);
|
||||
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_TEMPORARY_REDIRECT);
|
||||
if (moved)
|
||||
{
|
||||
resolvedURI.resolve(res.get("Location"));
|
||||
throw URIRedirection(resolvedURI.toString());
|
||||
}
|
||||
else if (res.getStatus() == HTTPResponse::HTTP_OK)
|
||||
if (moved)
|
||||
{
|
||||
resolvedURI.resolve(res.get("Location"));
|
||||
if (!username.empty())
|
||||
{
|
||||
resolvedURI.setUserInfo(username + ":" + password);
|
||||
}
|
||||
throw URIRedirection(resolvedURI.toString());
|
||||
}
|
||||
else if (res.getStatus() == HTTPResponse::HTTP_OK)
|
||||
{
|
||||
return new HTTPResponseStream(rs, pSession);
|
||||
}
|
||||
@@ -130,16 +151,20 @@ std::istream* HTTPStreamFactory::open(const URI& uri)
|
||||
// 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
|
||||
{
|
||||
throw HTTPException(res.getReason(), uri.toString());
|
||||
}
|
||||
}
|
||||
while(retry);
|
||||
throw HTTPException("Too many redirects", uri.toString());
|
||||
delete pSession; pSession = 0;
|
||||
retry = true; // only allow useproxy once
|
||||
}
|
||||
else if (res.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED && !authorize)
|
||||
{
|
||||
authorize = true;
|
||||
retry = true;
|
||||
Poco::NullOutputStream null;
|
||||
Poco::StreamCopier::copyStream(rs, null);
|
||||
}
|
||||
else throw HTTPException(res.getReason(), uri.toString());
|
||||
}
|
||||
while (retry);
|
||||
throw HTTPException("Too many redirects", uri.toString());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user