1
/* -*-c++-*- VirtualPlanetBuilder - Copyright (C) 1998-2007 Robert Osfield
3
* This application is open source and may be redistributed and/or modified
4
* freely and without restriction, both in commericial and non commericial applications,
5
* as long as this copyright notice is maintained.
7
* This application is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
#include <osgDB/FileUtils>
14
#include <osgDB/ReadFile>
15
#include <osgDB/WriteFile>
16
#include <osgDB/Registry>
17
#include <osgDB/FileNameUtils>
22
#include <curl/curl.h>
23
#include <curl/types.h>
26
class ReaderWriterCURL : public osgDB::ReaderWriter
30
static size_t StreamMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
32
size_t realsize = size * nmemb;
33
std::ostream* buffer = (std::ostream*)data;
35
buffer->write((const char*)ptr, realsize);
43
_curl = curl_easy_init();
45
curl_easy_setopt(_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
46
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, StreamMemoryCallback);
51
if (_curl) curl_easy_cleanup(_curl);
56
virtual const char* className() const { return "HTTP Protocol Model Reader"; }
58
virtual bool acceptsExtension(const std::string& extension) const
60
return osgDB::equalCaseInsensitive(extension,"curl");
72
virtual ReadResult openArchive(const std::string& fileName,ArchiveStatus status, unsigned int , const Options* options) const
74
if (status!=READ) return ReadResult(ReadResult::FILE_NOT_HANDLED);
75
else return readFile(ARCHIVE,fileName,options);
78
virtual ReadResult readObject(const std::string& fileName, const Options* options) const
80
return readFile(OBJECT,fileName,options);
83
virtual ReadResult readImage(const std::string& fileName, const Options *options) const
85
return readFile(IMAGE,fileName,options);
88
virtual ReadResult readHeightField(const std::string& fileName, const Options *options) const
90
return readFile(HEIGHTFIELD,fileName,options);
93
virtual ReadResult readNode(const std::string& fileName, const Options *options) const
95
return readFile(NODE,fileName,options);
98
ReadResult readFile(ObjectType objectType, osgDB::ReaderWriter* rw, std::istream& fin, const Options *options) const
102
case(OBJECT): return rw->readObject(fin,options);
103
case(ARCHIVE): return rw->openArchive(fin,options);
104
case(IMAGE): return rw->readImage(fin,options);
105
case(HEIGHTFIELD): return rw->readHeightField(fin,options);
106
case(NODE): return rw->readNode(fin,options);
109
return ReadResult::FILE_NOT_HANDLED;
112
virtual ReadResult readFile(ObjectType objectType, const std::string& fullFileName, const Options *options) const
114
std::string cacheFilePath, cacheFileName;
115
std::string proxyAddress, optProxy, optProxyPort;
119
std::istringstream iss(options->getOptionString());
123
int index = opt.find( "=" );
124
if( opt.substr( 0, index ) == "OSG_FILE_CACHE" )
125
cacheFilePath = opt.substr( index+1 ); //Setting Cache Directory by OSG Options
126
else if( opt.substr( 0, index ) == "OSG_CURL_PROXY" )
127
optProxy = opt.substr( index+1 );
128
else if( opt.substr( 0, index ) == "OSG_CURL_PROXYPORT" )
129
optProxyPort = opt.substr( index+1 );
132
//Setting Proxy by OSG Options
133
if(!optProxy.empty())
134
if(!optProxyPort.empty())
135
proxyAddress = optProxy + ":" + optProxyPort;
137
proxyAddress = optProxy + ":8080"; //Port not found, using default
140
if (!osgDB::containsServerAddress(fullFileName))
142
if (options && !(options->getDatabasePathList().empty()))
144
if (osgDB::containsServerAddress(options->getDatabasePathList().front()))
146
std::string newFileName = options->getDatabasePathList().front() + "/" + fullFileName;
148
return readFile(objectType, newFileName,options);
152
return ReadResult::FILE_NOT_HANDLED;
155
std::string fileName;
156
std::string ext = osgDB::getFileExtension(fullFileName);
159
fileName = osgDB::getNameLessExtension(fullFileName);
163
fileName = fullFileName;
166
osgDB::ReaderWriter *reader =
167
osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName));
171
osg::notify(osg::NOTICE)<<"Error: No ReaderWriter for file "<<fileName<<std::endl;
172
return ReadResult::FILE_NOT_HANDLED;
175
//Getting CURL Environment Variables (If found rewrite OSG Options)
176
const char* fileCachePath = getenv("OSG_FILE_CACHE");
177
if (fileCachePath) //Env Cache Directory
178
cacheFilePath = std::string(fileCachePath);
180
if (!cacheFilePath.empty())
182
cacheFileName = cacheFilePath + "/" +
183
osgDB::getServerAddress(fileName) + "/" +
184
osgDB::getServerFileName(fileName);
187
if (!cacheFilePath.empty() && osgDB::fileExists(cacheFileName))
189
osg::notify(osg::INFO) << "Reading cache file " << cacheFileName << std::endl;
190
ReadResult result = osgDB::Registry::instance()->readObject(cacheFileName,options);
194
const char* proxyEnvAddress = getenv("OSG_CURL_PROXY");
195
if (proxyEnvAddress) //Env Proxy Settings
197
const char* proxyEnvPort = getenv("OSG_CURL_PROXYPORT"); //Searching Proxy Port on Env
200
proxyAddress = std::string(proxyEnvAddress) + ":" + std::string(proxyEnvPort);
202
proxyAddress = std::string(proxyEnvAddress) + ":8080"; //Default
205
if(!proxyAddress.empty())
207
osg::notify(osg::NOTICE)<<"Setting proxy: "<<proxyAddress<<std::endl;
208
curl_easy_setopt(_curl, CURLOPT_PROXY, proxyAddress.c_str()); //Sets proxy address and port on libcurl
211
std::stringstream buffer;
213
curl_easy_setopt(_curl, CURLOPT_URL, fileName.c_str());
214
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void *)&buffer);
216
CURLcode res = curl_easy_perform(_curl);
219
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void *)0);
224
if(!proxyAddress.empty())
226
curl_easy_getinfo(_curl, CURLINFO_HTTP_CONNECTCODE, &code);
230
curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &code);
235
osg::notify(osg::NOTICE)<<"Error: libcurl read error, file="<<fileName<<", error code = "<<code<<std::endl;
236
return ReadResult::FILE_NOT_FOUND;
239
osg::ref_ptr<Options> local_opt = const_cast<Options*>(options);
240
if (!local_opt) local_opt = new Options;
242
local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
244
ReadResult readResult = readFile(objectType, reader, buffer, local_opt.get() );
246
local_opt->getDatabasePathList().pop_front();
248
if (!cacheFilePath.empty() && readResult.validObject())
250
osg::notify(osg::INFO)<<"Writing cache file "<<cacheFileName<<std::endl;
252
std::string filePath = osgDB::getFilePath(cacheFileName);
253
if (osgDB::fileExists(filePath) || osgDB::makeDirectory(filePath))
257
case(OBJECT): osgDB::writeObjectFile( *(readResult.getObject()), cacheFileName ); break;
258
case(IMAGE): osgDB::writeImageFile( *(readResult.getImage()), cacheFileName ); break;
259
case(HEIGHTFIELD): osgDB::writeHeightFieldFile( *(readResult.getHeightField()), cacheFileName ); break;
260
case(NODE): osgDB::writeNodeFile( *(readResult.getNode()), cacheFileName ); break;
266
osg::notify(osg::NOTICE)<<"Error: Failed to created directory "<<filePath<<std::endl;
275
osg::notify(osg::NOTICE)<<"Error: libcurl read error, file="<<fileName<<" error = "<<curl_easy_strerror(res)<<std::endl;
276
return ReadResult::FILE_NOT_HANDLED;
288
// now register with Registry to instantiate the above
290
REGISTER_OSGPLUGIN(rgb, ReaderWriterCURL)