mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-28 03:20:11 +01:00
trunk: sync from 1.4.3
make & cmake fixes
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
//
|
||||
// Library: Net
|
||||
// Package: HTTP
|
||||
// Module: HTTPCredentials
|
||||
// Module: HTTPCredentials
|
||||
//
|
||||
// Copyright (c) 2011, Anton V. Yabchinskiy (arn at bestmx dot ru).
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
@@ -42,6 +42,7 @@
|
||||
#include "Poco/Net/HTTPResponse.h"
|
||||
#include "Poco/Net/NetException.h"
|
||||
#include "Poco/String.h"
|
||||
#include "Poco/Ascii.h"
|
||||
#include "Poco/URI.h"
|
||||
|
||||
|
||||
@@ -58,7 +59,7 @@ HTTPCredentials::HTTPCredentials()
|
||||
|
||||
|
||||
HTTPCredentials::HTTPCredentials(const std::string& username, const std::string& password):
|
||||
_digest(username, password)
|
||||
_digest(username, password)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -68,77 +69,113 @@ HTTPCredentials::~HTTPCredentials()
|
||||
}
|
||||
|
||||
|
||||
void HTTPCredentials::fromUserInfo(const std::string& userInfo)
|
||||
{
|
||||
std::string username;
|
||||
std::string password;
|
||||
|
||||
extractCredentials(userInfo, username, password);
|
||||
setUsername(username);
|
||||
setPassword(password);
|
||||
// TODO: Reset digest state?
|
||||
}
|
||||
|
||||
|
||||
void HTTPCredentials::fromURI(const URI& uri)
|
||||
{
|
||||
std::string username;
|
||||
std::string password;
|
||||
|
||||
extractCredentials(uri, username, password);
|
||||
setUsername(username);
|
||||
setPassword(password);
|
||||
// TODO: Reset digest state?
|
||||
}
|
||||
|
||||
|
||||
void HTTPCredentials::authenticate(HTTPRequest& request, const HTTPResponse& response)
|
||||
{
|
||||
for (HTTPResponse::ConstIterator iter = response.find("WWW-Authenticate"); iter != response.end(); ++iter)
|
||||
{
|
||||
if (isBasicCredentials(iter->second))
|
||||
{
|
||||
HTTPBasicCredentials(_digest.getUsername(), _digest.getPassword()).authenticate(request);
|
||||
return;
|
||||
}
|
||||
else if (isDigestCredentials(iter->second))
|
||||
{
|
||||
_digest.authenticate(request, HTTPAuthenticationParams(iter->second.substr(7)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (isBasicCredentials(iter->second))
|
||||
{
|
||||
HTTPBasicCredentials(_digest.getUsername(), _digest.getPassword()).authenticate(request);
|
||||
return;
|
||||
}
|
||||
else if (isDigestCredentials(iter->second))
|
||||
{
|
||||
_digest.authenticate(request, HTTPAuthenticationParams(iter->second.substr(7)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HTTPCredentials::updateAuthInfo(HTTPRequest& request)
|
||||
{
|
||||
if (request.has("Authorization"))
|
||||
if (request.has(HTTPRequest::AUTHORIZATION))
|
||||
{
|
||||
const std::string& authorization = request.get("Authorization");
|
||||
const std::string& authorization = request.get(HTTPRequest::AUTHORIZATION);
|
||||
|
||||
if (isBasicCredentials(authorization))
|
||||
{
|
||||
HTTPBasicCredentials(_digest.getUsername(), _digest.getPassword()).authenticate(request);
|
||||
}
|
||||
else if (isDigestCredentials(authorization))
|
||||
{
|
||||
_digest.updateAuthInfo(request);
|
||||
}
|
||||
}
|
||||
HTTPBasicCredentials(_digest.getUsername(), _digest.getPassword()).authenticate(request);
|
||||
}
|
||||
else if (isDigestCredentials(authorization))
|
||||
{
|
||||
_digest.updateAuthInfo(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool HTTPCredentials::isBasicCredentials(const std::string& header)
|
||||
{
|
||||
return icompare(header, 0, 6, "Basic ") == 0;
|
||||
return icompare(header, 0, 5, "Basic") == 0 && (header.size() > 5 ? Poco::Ascii::isSpace(header[5]) : true);
|
||||
}
|
||||
|
||||
|
||||
bool HTTPCredentials::isDigestCredentials(const std::string& header)
|
||||
{
|
||||
return icompare(header, 0, 7, "Digest ") == 0;
|
||||
return icompare(header, 0, 6, "Digest") == 0 && (header.size() > 6 ? Poco::Ascii::isSpace(header[6]) : true);
|
||||
}
|
||||
|
||||
|
||||
bool HTTPCredentials::hasBasicCredentials(const HTTPRequest& request)
|
||||
{
|
||||
return request.has(HTTPRequest::AUTHORIZATION) && isBasicCredentials(request.get(HTTPRequest::AUTHORIZATION));
|
||||
}
|
||||
|
||||
|
||||
bool HTTPCredentials::hasDigestCredentials(const HTTPRequest& request)
|
||||
{
|
||||
return request.has(HTTPRequest::AUTHORIZATION) && isDigestCredentials(request.get(HTTPRequest::AUTHORIZATION));
|
||||
}
|
||||
|
||||
|
||||
void HTTPCredentials::extractCredentials(const std::string& userInfo, std::string& username, std::string& password)
|
||||
{
|
||||
const std::string::size_type p = userInfo.find(':');
|
||||
const std::string::size_type p = userInfo.find(':');
|
||||
|
||||
if (p != std::string::npos)
|
||||
{
|
||||
username.assign(userInfo, 0, p);
|
||||
password.assign(userInfo, p + 1, std::string::npos);
|
||||
}
|
||||
else
|
||||
{
|
||||
username.assign(userInfo);
|
||||
password.clear();
|
||||
}
|
||||
if (p != std::string::npos)
|
||||
{
|
||||
username.assign(userInfo, 0, p);
|
||||
password.assign(userInfo, p + 1, std::string::npos);
|
||||
}
|
||||
else
|
||||
{
|
||||
username.assign(userInfo);
|
||||
password.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HTTPCredentials::extractCredentials(const Poco::URI& uri, std::string& username, std::string& password)
|
||||
{
|
||||
if (!uri.getUserInfo().empty())
|
||||
{
|
||||
extractCredentials(uri.getUserInfo(), username, password);
|
||||
}
|
||||
if (!uri.getUserInfo().empty())
|
||||
{
|
||||
extractCredentials(uri.getUserInfo(), username, password);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user