bool ReaderWriterCURL::fileExists(const std::string& filename, const osgDB::Options* options) const { if (osgDB::containsServerAddress(filename)) { OSG_NOTICE<<"Checking if file exists using curl plugin: "<<filename<<std::endl; ReadResult result = readFile(OBJECT,filename,options); return result.status()==osgDB::ReaderWriter::ReadResult::FILE_LOADED; } else { return ReaderWriter::fileExists(filename, options); } }
osgDB::ReaderWriter::ReadResult ReaderWriterCURL::readFile(ObjectType objectType, const std::string& fullFileName, const osgDB::ReaderWriter::Options *options) const { std::string fileName(fullFileName); std::string ext = osgDB::getFileExtension(fullFileName); bool curl_ext = ext=="curl"; if (curl_ext) { fileName = osgDB::getNameLessExtension(fullFileName); ext = osgDB::getFileExtension(fileName); } if (!osgDB::containsServerAddress(fileName)) { if (options && !options->getDatabasePathList().empty()) { if (osgDB::containsServerAddress(options->getDatabasePathList().front())) { std::string newFileName = options->getDatabasePathList().front() + "/" + fileName; return readFile(objectType, newFileName,options); } } // if user has explictly specified curl then we don't about at this point, // instead assume the curl can read it any way, if it doesn't explictly // specify curl then we assume that the file is a local file and not appropriate // for the curl plugin to load. if (!curl_ext) return ReadResult::FILE_NOT_HANDLED; } OSG_INFO<<"ReaderWriterCURL::readFile("<<fullFileName<<")"<<std::endl; std::string proxyAddress; long connectTimeout = 0; long timeout = 0; getConnectionOptions(options, proxyAddress, connectTimeout, timeout); bool uncompress = false; if (ext=="gz" || ext=="osgz" || ext=="ivez") { OSG_INFO<<"CURL: Compressed file type "<<ext<<std::endl; #ifndef USE_ZLIB // don't have zlib so can't compile compressed formats return ReadResult::FILE_NOT_HANDLED; #endif uncompress = true; if (ext=="gz") { ext = osgDB::getFileExtension(osgDB::getNameLessExtension(fileName)); } else if (ext=="osgz") { ext = "osg"; } else if (ext=="ivez") { ext = "ive"; } OSG_INFO<<"CURL: assuming file type "<<ext<<std::endl; } std::stringstream buffer; EasyCurl::StreamObject sp(&buffer, NULL, std::string()); EasyCurl& easyCurl = getEasyCurl(); // setup the timeouts: easyCurl.setConnectionTimeout(connectTimeout); easyCurl.setTimeout(timeout); ReadResult curlResult = easyCurl.read(proxyAddress, fileName, sp, options); if (curlResult.status()==ReadResult::FILE_LOADED) { OSG_INFO<<"CURL: ReadResult::FILE_LOADED "<<std::endl; // Try to find a reader by file extension. If this fails, we will fetch the file // anyway and try to get a reader via mime-type. osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension( ext ); // If we do not already have a ReaderWriter, try to find one based on the // mime-type: if ( !reader ) { std::string mimeType = easyCurl.getResultMimeType(sp); OSG_INFO << "CURL: Looking up extension for mime-type " << mimeType << std::endl; if ( mimeType.length() > 0 ) { reader = osgDB::Registry::instance()->getReaderWriterForMimeType(mimeType); } } // If there is still no reader, fail. if ( !reader ) { OSG_NOTICE<<"Error: No ReaderWriter for file "<<fileName<<std::endl; return ReadResult::FILE_NOT_HANDLED; } 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)); local_opt->setPluginStringData("STREAM_FILENAME",osgDB::getSimpleFileName(fileName)); local_opt->setPluginStringData("filename",fileName); if (uncompress) { OSG_INFO<<"Curl:: plugin uncompressing "<<fileName<<std::endl; std::string uncompressed; if (!read(buffer, uncompressed)) { return ReadResult::FILE_NOT_HANDLED; } buffer.str(uncompressed); } ReadResult readResult = readFile(objectType, reader, buffer, local_opt.get() ); local_opt->getDatabasePathList().pop_front(); return readResult; } else { OSG_INFO<<"CURL: not loading successfully "<<std::endl; return curlResult; } }
osgDB::ReaderWriter::ReadResult ReaderWriterCURL::readFile(ObjectType objectType, const std::string& fullFileName, const osgDB::ReaderWriter::Options *options) const { if (!osgDB::containsServerAddress(fullFileName)) { if (options && !options->getDatabasePathList().empty()) { if (osgDB::containsServerAddress(options->getDatabasePathList().front())) { std::string newFileName = options->getDatabasePathList().front() + "/" + fullFileName; return readFile(objectType, newFileName,options); } } return ReadResult::FILE_NOT_HANDLED; } osg::notify(osg::INFO)<<"ReaderWriterCURL::readFile("<<fullFileName<<")"<<std::endl; std::string proxyAddress, optProxy, optProxyPort; if (options) { std::istringstream iss(options->getOptionString()); std::string opt; while (iss >> opt) { int index = opt.find( "=" ); if( opt.substr( 0, index ) == "OSG_CURL_PROXY" ) optProxy = opt.substr( index+1 ); else if( opt.substr( 0, index ) == "OSG_CURL_PROXYPORT" ) optProxyPort = opt.substr( index+1 ); } //Setting Proxy by OSG Options if(!optProxy.empty()) { if(!optProxyPort.empty()) proxyAddress = optProxy + ":" + optProxyPort; else proxyAddress = optProxy + ":8080"; //Port not found, using default } } std::string fileName; std::string ext = osgDB::getFileExtension(fullFileName); if (ext=="curl") { fileName = osgDB::getNameLessExtension(fullFileName); ext = osgDB::getFileExtension(fileName); } else { fileName = fullFileName; } bool uncompress = false; if (ext=="gz" || ext=="osgz" || ext=="ivez") { osg::notify(osg::NOTICE)<<"Compressed file type "<<ext<<std::endl; #ifndef USE_ZLIB // don't have zlib so can't compile compressed formats return ReadResult::FILE_NOT_HANDLED; #endif uncompress = true; if (ext=="gz") { ext = osgDB::getFileExtension(fileName); fileName = osgDB::getNameLessExtension(fileName); } else if (ext=="osgz") { ext = "osg"; } else if (ext=="ivez") { ext = "ive"; } osg::notify(osg::NOTICE)<<" assuming file type "<<ext<<std::endl; } osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension( ext ); if (!reader) { osg::notify(osg::NOTICE)<<"Error: No ReaderWriter for file "<<fileName<<std::endl; return ReadResult::FILE_NOT_HANDLED; } const char* proxyEnvAddress = getenv("OSG_CURL_PROXY"); if (proxyEnvAddress) //Env Proxy Settings { const char* proxyEnvPort = getenv("OSG_CURL_PROXYPORT"); //Searching Proxy Port on Env if(proxyEnvPort) proxyAddress = std::string(proxyEnvAddress) + ":" + std::string(proxyEnvPort); else proxyAddress = std::string(proxyEnvAddress) + ":8080"; //Default } std::stringstream buffer; EasyCurl::StreamObject sp(&buffer, std::string()); ReadResult curlResult = getEasyCurl().read(proxyAddress, fileName, sp, options); if (curlResult.status()==ReadResult::FILE_LOADED) { 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)); if (uncompress) { std::string uncompressed; if (!read(buffer, uncompressed)) { return ReadResult::FILE_NOT_HANDLED; } buffer.str(uncompressed); } ReadResult readResult = readFile(objectType, reader, buffer, local_opt.get() ); local_opt->getDatabasePathList().pop_front(); return readResult; } else { return curlResult; } }