Changeset 8619

Show
Ignore:
Timestamp:
07/17/08 13:55:55
Author:
robert
Message:

Added osgDB::AuthenticationMap/Details? to osgDB and curl plugin to add the ability
to authenticate http transfers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenSceneGraph/trunk/include/osgDB/ReaderWriter

    r8582 r8619  
    3333typedef std::deque<std::string> FilePathList; 
    3434 
     35class AuthenticationDetails : public osg::Referenced 
     36{ 
     37public: 
     38 
     39    /** Http authentication techniques, see libcurl docs for details on names and associated functionality.*/  
     40    enum HttpAuthentication 
     41    { 
     42        BASIC           = 1<<0, 
     43        DIGEST          = 1<<1, 
     44        NTLM            = 1<<2, 
     45        GSSNegotiate    = 1<<2, 
     46        ANY             = ~0, 
     47        ANYSAFE         = ~BASIC 
     48    }; 
     49 
     50    AuthenticationDetails(const std::string& u, const std::string& p, HttpAuthentication auth=BASIC): 
     51        username(u), 
     52        password(p), 
     53        httpAuthentication(auth) {} 
     54 
     55    std::string         username; 
     56    std::string         password; 
     57    HttpAuthentication  httpAuthentication; 
     58 
     59protected: 
     60    virtual ~AuthenticationDetails() {} 
     61}; 
     62 
     63class OSGDB_EXPORT AuthenticationMap : public osg::Referenced 
     64{ 
     65    public: 
     66     
     67        AuthenticationMap() {} 
     68         
     69 
     70        virtual void addAuthenticationDetails(const std::string& path, AuthenticationDetails* details); 
     71 
     72        virtual const AuthenticationDetails* getAuthenticationDetails(const std::string& path) const; 
     73         
     74    protected: 
     75     
     76        virtual ~AuthenticationMap() {} 
     77         
     78        typedef std::map<std::string, osg::ref_ptr<AuthenticationDetails> > AuthenticationDetailsMap; 
     79        AuthenticationDetailsMap _authenticationMap; 
     80         
     81}; 
     82 
    3583/** pure virtual base class for reading and writing of non native formats. */ 
    3684class OSGDB_EXPORT ReaderWriter : public osg::Object 
     
    160208 
    161209 
     210                /** Set the password map to be used by plugins when access files from secure locations.*/ 
     211                void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; } 
     212 
     213                /** Get the password map to be used by plugins when access files from secure locations.*/ 
     214                const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); } 
     215 
    162216 
    163217                /** Sets a plugindata value PluginData with a string */ 
     
    181235                virtual ~Options() {} 
    182236 
    183                 std::string         _str; 
    184                 FilePathList        _databasePaths; 
    185                 CacheHintOptions    _objectCacheHint; 
    186                 BuildKdTreesHint    _buildKdTreesHint; 
     237                std::string                     _str; 
     238                FilePathList                    _databasePaths; 
     239                CacheHintOptions                _objectCacheHint; 
     240                BuildKdTreesHint                _buildKdTreesHint; 
     241                osg::ref_ptr<AuthenticationMap> _authenticationMap; 
    187242 
    188243                typedef std::map<std::string,void*> PluginDataMap; 
     
    248303                std::string                 _message; 
    249304                osg::ref_ptr<osg::Object>   _object; 
     305 
    250306        }; 
    251307 
  • OpenSceneGraph/trunk/include/osgDB/Registry

    r8582 r8619  
    335335 
    336336 
     337        /** Set whether the KdTrees should be built for geometry in the loader model. */ 
    337338        void setBuildKdTreesHint(ReaderWriter::Options::BuildKdTreesHint hint) { _buildKdTreesHint = hint; } 
     339 
     340        /** Get whether the KdTrees should be built for geometry in the loader model. */ 
    338341        ReaderWriter::Options::BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; } 
    339342 
     343        /** Set the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/ 
    340344        void setKdTreeBuilder(osg::KdTreeBuilder* builder) { _kdTreeBuilder = builder; } 
     345 
     346        /** Get the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/ 
    341347        osg::KdTreeBuilder* getKdTreeBuilder() { return _kdTreeBuilder.get(); } 
     348 
     349 
     350        /** Set the password map to be used by plugins when access files from secure locations.*/ 
     351        void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; } 
     352 
     353        /** Get the password map to be used by plugins when access files from secure locations.*/ 
     354        const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); } 
    342355 
    343356 
    344357        void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; } 
    345358        bool getCreateNodeFromImage() const { return _createNodeFromImage; } 
    346  
     359         
    347360 
    348361        void setOptions(ReaderWriter::Options* opt) { _options = opt; } 
     
    478491        ReaderWriter::Options::BuildKdTreesHint     _buildKdTreesHint; 
    479492        osg::ref_ptr<osg::KdTreeBuilder>            _kdTreeBuilder; 
     493         
     494        osg::ref_ptr<AuthenticationMap>             _authenticationMap; 
    480495         
    481496        bool                                        _createNodeFromImage; 
  • OpenSceneGraph/trunk/src/osgDB/ReaderWriter.cpp

    r8578 r8619  
    1616#include <osgDB/Archive> 
    1717 
     18#include <map> 
     19 
    1820using namespace osgDB; 
     21 
     22/////////////////////////////////////////////////////////////////////////////////////////////////////// 
     23// 
     24//  PasswordMap 
     25// 
     26 
     27void AuthenticationMap::addAuthenticationDetails(const std::string& path, AuthenticationDetails* details) 
     28{ 
     29   _authenticationMap[path] = details; 
     30} 
     31 
     32const AuthenticationDetails* AuthenticationMap::getAuthenticationDetails(const std::string& path) const 
     33{ 
     34    // see if the full filename has its own authentication details 
     35    AuthenticationDetailsMap::const_iterator itr = _authenticationMap.find(path); 
     36    if (itr != _authenticationMap.end()) return itr->second.get(); 
     37 
     38    // now look to see if the paths to the file have their own authentication details 
     39    std::string basePath = osgDB::getFilePath(path); 
     40    while(!basePath.empty()) 
     41    { 
     42        itr = _authenticationMap.find(basePath); 
     43        if (itr != _authenticationMap.end()) return itr->second.get(); 
     44         
     45        basePath = osgDB::getFilePath(basePath); 
     46    } 
     47    return 0; 
     48} 
     49 
     50/////////////////////////////////////////////////////////////////////////////////////////////////////// 
     51// 
     52//  ReaderWriter 
     53// 
    1954 
    2055osg::Object* ReaderWriter::ReadResult::getObject() { return _object.get(); } 
  • OpenSceneGraph/trunk/src/osgPlugins/curl/ReaderWriterCURL.cpp

    r8578 r8619  
    7676{ 
    7777    osg::notify(osg::INFO)<<"EasyCurl::EasyCurl()"<<std::endl; 
     78     
     79    _previousHttpAuthentication = 0; 
    7880 
    7981    _curl = curl_easy_init(); 
     
    9395 
    9496 
    95 osgDB::ReaderWriter::ReadResult EasyCurl::read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp) 
    96 
     97osgDB::ReaderWriter::ReadResult EasyCurl::read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options) 
     98
     99    const osgDB::AuthenticationMap* authenticationMap = (options && options->getAuthenticationMap()) ?  
     100            options->getAuthenticationMap() : 
     101            osgDB::Registry::instance()->getAuthenticationMap(); 
     102 
    97103    if(!proxyAddress.empty()) 
    98104    { 
    99         osg::notify(osg::NOTICE)<<"Setting proxy: "<<proxyAddress<<std::endl; 
     105        osg::notify(osg::INFO)<<"Setting proxy: "<<proxyAddress<<std::endl; 
    100106        curl_easy_setopt(_curl, CURLOPT_PROXY, proxyAddress.c_str()); //Sets proxy address and port on libcurl 
     107    } 
     108 
     109    const osgDB::AuthenticationDetails* details = authenticationMap ? 
     110        authenticationMap->getAuthenticationDetails(fileName) : 
     111        0; 
     112 
     113    // configure/reset authentication if required.         
     114    if (details) 
     115    { 
     116        const std::string colon(":"); 
     117        std::string password(details->username + colon + details->password); 
     118        curl_easy_setopt(_curl, CURLOPT_USERPWD, password.c_str()); 
     119        _previousPassword = password; 
     120 
     121        // use for https. 
     122        // curl_easy_setopt(_curl, CURLOPT_KEYPASSWD, password.c_str()); 
     123 
     124        if (details->httpAuthentication != _previousHttpAuthentication) 
     125        {  
     126            curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, details->httpAuthentication);  
     127            _previousHttpAuthentication = details->httpAuthentication; 
     128        } 
     129    } 
     130    else 
     131    { 
     132        if (!_previousPassword.empty()) 
     133        { 
     134            curl_easy_setopt(_curl, CURLOPT_USERPWD, 0); 
     135            _previousPassword.clear(); 
     136        } 
     137     
     138        // need to reset if previously set. 
     139        if (_previousHttpAuthentication!=0) 
     140        { 
     141            curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, 0);  
     142            _previousHttpAuthentication = 0; 
     143        } 
    101144    } 
    102145 
     
    249292    EasyCurl::StreamObject sp(&buffer, std::string()); 
    250293 
    251     ReadResult curlResult = getEasyCurl().read(proxyAddress, fileName, sp); 
     294    ReadResult curlResult = getEasyCurl().read(proxyAddress, fileName, sp, options); 
    252295 
    253296    if (curlResult.status()==ReadResult::FILE_LOADED) 
    254297    { 
    255         osg::ref_ptr<Options> local_opt = const_cast<Options*>(options); 
    256         if (!local_opt) local_opt = new Options; 
     298        osg::ref_ptr<Options> local_opt = options ?  
     299            static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) :  
     300            new Options; 
    257301 
    258302        local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); 
  • OpenSceneGraph/trunk/src/osgPlugins/curl/ReaderWriterCURL.h

    r8326 r8619  
    5151        EasyCurl(); 
    5252 
    53         osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp); 
     53        osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options); 
    5454 
    5555    protected: 
     
    6363         
    6464        CURL* _curl; 
     65         
     66        std::string     _previousPassword; 
     67        long            _previousHttpAuthentication; 
    6568}; 
    6669