Added osgDB::AuthenticationMap/Details to osgDB and curl plugin to add the ability

to authenticate http transfers
This commit is contained in:
Robert Osfield
2008-07-17 11:55:55 +00:00
parent a6c72dc21c
commit adaf71fa19
5 changed files with 164 additions and 11 deletions

View File

@@ -15,8 +15,43 @@
#include <osgDB/FileNameUtils>
#include <osgDB/Archive>
#include <map>
using namespace osgDB;
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PasswordMap
//
void AuthenticationMap::addAuthenticationDetails(const std::string& path, AuthenticationDetails* details)
{
_authenticationMap[path] = details;
}
const AuthenticationDetails* AuthenticationMap::getAuthenticationDetails(const std::string& path) const
{
// see if the full filename has its own authentication details
AuthenticationDetailsMap::const_iterator itr = _authenticationMap.find(path);
if (itr != _authenticationMap.end()) return itr->second.get();
// now look to see if the paths to the file have their own authentication details
std::string basePath = osgDB::getFilePath(path);
while(!basePath.empty())
{
itr = _authenticationMap.find(basePath);
if (itr != _authenticationMap.end()) return itr->second.get();
basePath = osgDB::getFilePath(basePath);
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ReaderWriter
//
osg::Object* ReaderWriter::ReadResult::getObject() { return _object.get(); }
osg::Image* ReaderWriter::ReadResult::getImage() { return dynamic_cast<osg::Image*>(_object.get()); }
osg::HeightField* ReaderWriter::ReadResult::getHeightField() { return dynamic_cast<osg::HeightField*>(_object.get()); }

View File

@@ -75,6 +75,8 @@ size_t EasyCurl::StreamMemoryCallback(void *ptr, size_t size, size_t nmemb, void
EasyCurl::EasyCurl()
{
osg::notify(osg::INFO)<<"EasyCurl::EasyCurl()"<<std::endl;
_previousHttpAuthentication = 0;
_curl = curl_easy_init();
@@ -92,14 +94,55 @@ EasyCurl::~EasyCurl()
}
osgDB::ReaderWriter::ReadResult EasyCurl::read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp)
osgDB::ReaderWriter::ReadResult EasyCurl::read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options)
{
const osgDB::AuthenticationMap* authenticationMap = (options && options->getAuthenticationMap()) ?
options->getAuthenticationMap() :
osgDB::Registry::instance()->getAuthenticationMap();
if(!proxyAddress.empty())
{
osg::notify(osg::NOTICE)<<"Setting proxy: "<<proxyAddress<<std::endl;
osg::notify(osg::INFO)<<"Setting proxy: "<<proxyAddress<<std::endl;
curl_easy_setopt(_curl, CURLOPT_PROXY, proxyAddress.c_str()); //Sets proxy address and port on libcurl
}
const osgDB::AuthenticationDetails* details = authenticationMap ?
authenticationMap->getAuthenticationDetails(fileName) :
0;
// configure/reset authentication if required.
if (details)
{
const std::string colon(":");
std::string password(details->username + colon + details->password);
curl_easy_setopt(_curl, CURLOPT_USERPWD, password.c_str());
_previousPassword = password;
// use for https.
// curl_easy_setopt(_curl, CURLOPT_KEYPASSWD, password.c_str());
if (details->httpAuthentication != _previousHttpAuthentication)
{
curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, details->httpAuthentication);
_previousHttpAuthentication = details->httpAuthentication;
}
}
else
{
if (!_previousPassword.empty())
{
curl_easy_setopt(_curl, CURLOPT_USERPWD, 0);
_previousPassword.clear();
}
// need to reset if previously set.
if (_previousHttpAuthentication!=0)
{
curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, 0);
_previousHttpAuthentication = 0;
}
}
curl_easy_setopt(_curl, CURLOPT_URL, fileName.c_str());
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void *)&sp);
@@ -248,12 +291,13 @@ osgDB::ReaderWriter::ReadResult ReaderWriterCURL::readFile(ObjectType objectType
EasyCurl::StreamObject sp(&buffer, std::string());
ReadResult curlResult = getEasyCurl().read(proxyAddress, fileName, sp);
ReadResult curlResult = getEasyCurl().read(proxyAddress, fileName, sp, options);
if (curlResult.status()==ReadResult::FILE_LOADED)
{
osg::ref_ptr<Options> local_opt = const_cast<Options*>(options);
if (!local_opt) local_opt = new Options;
osg::ref_ptr<Options> local_opt = options ?
static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) :
new Options;
local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));

View File

@@ -50,7 +50,7 @@ class EasyCurl : public osg::Referenced
EasyCurl();
osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp);
osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options);
protected:
@@ -62,6 +62,9 @@ class EasyCurl : public osg::Referenced
CURL* _curl;
std::string _previousPassword;
long _previousHttpAuthentication;
};