Submission by Carlo Camporesi - Added Proxy support to .net loader

This commit is contained in:
Don BURNS
2006-08-11 17:34:04 +00:00
parent 0220448af3
commit a456538e3c

View File

@@ -138,17 +138,23 @@ class NetReader : public osgDB::ReaderWriter
//osg::notify(osg::NOTICE) << "osgPlugin .net: start load" << inFileName << std::endl;
std::string hostname;
std::string serverPrefix;
std::string localCacheDir;
int port = 80;
//CARLO: proxy modf
std::string proxyHost;
int proxyPort = 8080;
enum CacheMode {
Read = 1,
Write = 2,
ReadWrite = 3
};
std::string cacheFile;
CacheMode cacheMode = ReadWrite;
if (options)
@@ -167,6 +173,17 @@ class NetReader : public osgDB::ReaderWriter
opt.substr( 0, index ) == "PORT" )
{
port = atoi( opt.substr(index+1).c_str() );
}
else if( opt.substr( 0, index ) == "proxy_hostname" ||
opt.substr( 0, index ) == "PROXY_HOSTNAME" ) //CARLO: proxy modf
{
proxyHost = opt.substr(index+1);
osg::notify(osg::WARN) << "proxy host " << proxyHost << std::endl;
}
else if( opt.substr( 0, index ) == "proxy_port" ||
opt.substr( 0, index ) == "PROXY_PORT" )
{
proxyPort = atoi( opt.substr(index+1).c_str() );
}
else if( opt.substr( 0, index ) == "server_prefix" ||
opt.substr( 0, index ) == "SERVER_PREFIX" ||
@@ -197,6 +214,20 @@ class NetReader : public osgDB::ReaderWriter
}
}
//if(proxy.empty()) //CARLO: proxy modf
// Env variables should override plug-in options.
{
char * env_proxyHost = getenv("OSG_PROXY_HOST"); //Checking proxy environment variables
char * env_proxyPort = getenv("OSG_PROXY_PORT");
if( env_proxyHost )
{
proxyHost = std::string(env_proxyHost);
if( env_proxyPort )
proxyPort = atoi(std::string(env_proxyPort).c_str());
}
}
ReadResult readResult = ReadResult::FILE_NOT_HANDLED;
/* * we accept all extensions
@@ -234,10 +265,6 @@ class NetReader : public osgDB::ReaderWriter
if( !serverPrefix.empty() )
fileName = serverPrefix + '/' + fileName;
//osg::notify(osg::WARN) << "hostname " << hostname << std::endl;
//osg::notify(osg::WARN) << "filename " << fileName << std::endl;
// Invoke the reader corresponding to the extension
osgDB::ReaderWriter *reader =
osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName));
@@ -263,26 +290,48 @@ class NetReader : public osgDB::ReaderWriter
// Fetch from the network
osg::ref_ptr<iosockinet> sio = new iosockinet(sockbuf::sock_stream);
try {
sio->rdbuf()->connect( hostname.c_str(), port );
}
catch( sockerr e )
{
osg::notify(osg::WARN) << "osgPlugin .net reader: Unable to connect to host " << hostname << std::endl;
return ReadResult::FILE_NOT_FOUND;
}
*sio <<
"GET /" << fileName << " HTTP/1.1\n"
"User-Agent: OSGNetPlugin/1.0\n"
"Accept: */*\n"
"Host: " << hostname << "\n"
"Connection: close\n"
"\n";
std::string requestAdd;
if(!proxyHost.empty())
{
try {
sio->rdbuf()->connect( proxyHost.c_str(), proxyPort );
osg::notify(osg::DEBUG_FP) << "osgPlugin .net reader: connected to Proxy " << proxyHost << " with Port " << proxyPort << std::endl;
}
catch( sockerr e )
{
osg::notify(osg::WARN) << "osgPlugin .net reader: Unable to connect to Proxy " << proxyHost << std::endl;
return ReadResult::FILE_NOT_FOUND;
}
requestAdd = std::string("http://") + hostname + "/" + fileName;
}
else
{
try {
sio->rdbuf()->connect( hostname.c_str(), port );
osg::notify(osg::DEBUG_FP) << "osgPlugin .net reader: connected to Hostname " << hostname << " with Port " << port << std::endl;
}
catch( sockerr e )
{
osg::notify(osg::WARN) << "osgPlugin .net reader: Unable to connect to host " << hostname << std::endl;
return ReadResult::FILE_NOT_FOUND;
}
requestAdd = "/" + fileName;
}
*sio <<
"GET " << requestAdd << " HTTP/1.1\n"
"User-Agent: OSGNetPlugin/1.0\n"
"Accept: */*\n"
"Host: " << hostname << "\n"
"Connection: close\n"
"\n";
sio->flush();
char linebuff[256];
char linebuff[512];
do
{
sio->getline( linebuff, sizeof( linebuff ));