osgEarth 2.1.1
|
00001 /* -*-c++-*- */ 00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph 00003 * Copyright 2008-2010 Pelican Mapping 00004 * http://osgearth.org 00005 * 00006 * osgEarth is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/> 00018 */ 00019 #ifndef OSGEARTH_HTTP_CLIENT_H 00020 #define OSGEARTH_HTTP_CLIENT_H 1 00021 00022 #include <osgEarth/Common> 00023 #include <osgEarth/Progress> 00024 #include <osgEarth/TerrainOptions> 00025 #include <OpenThreads/Thread> 00026 #include <osg/ref_ptr> 00027 #include <osg/Referenced> 00028 #include <osgDB/ReaderWriter> 00029 #include <sstream> 00030 #include <iostream> 00031 #include <string> 00032 #include <map> 00033 #include <vector> 00034 00035 namespace osgEarth 00036 { 00040 class OSGEARTH_EXPORT ProxySettings 00041 { 00042 public: 00043 ProxySettings( const Config& conf =Config() ); 00044 ProxySettings( const std::string& host, int port ); 00045 00046 std::string& hostName() { return _hostName; } 00047 const std::string& hostName() const { return _hostName; } 00048 00049 int& port() { return _port; } 00050 const int& port() const { return _port; } 00051 00052 std::string& userName() { return _userName; } 00053 const std::string& userName() const { return _userName; } 00054 00055 std::string& password() { return _password; } 00056 const std::string& password() const { return _password; } 00057 00058 public: 00059 virtual Config getConfig() const; 00060 virtual void mergeConfig( const Config& conf ); 00061 00062 protected: 00063 std::string _hostName; 00064 int _port; 00065 std::string _userName; 00066 std::string _password; 00067 }; 00068 00069 00073 class OSGEARTH_EXPORT HTTPRequest 00074 { 00075 public: 00077 HTTPRequest( const std::string& url ); 00078 00080 HTTPRequest( const HTTPRequest& rhs ); 00081 00083 void addParameter( const std::string& name, const std::string& value ); 00084 void addParameter( const std::string& name, int value ); 00085 void addParameter( const std::string& name, double value ); 00086 00087 typedef std::map<std::string,std::string> Parameters; 00088 00090 const Parameters& getParameters() const; 00091 00093 std::string getURL() const; 00094 00095 private: 00096 Parameters _parameters; 00097 std::string _url; 00098 }; 00099 00104 class OSGEARTH_EXPORT HTTPResponse 00105 { 00106 public: 00107 enum Code { 00108 NONE = 0, 00109 OK = 200, 00110 NOT_FOUND = 404, 00111 SERVER_ERROR = 500 00112 }; 00113 00114 public: 00116 HTTPResponse( long code =0L ); 00117 00119 HTTPResponse( const HTTPResponse& rhs ); 00120 00122 long getCode() const; 00123 00125 bool isOK() const; 00126 00128 bool isCancelled() const; 00129 00131 unsigned int getNumParts() const; 00132 00134 std::istream& getPartStream( unsigned int n ) const; 00135 00137 std::string getPartAsString( unsigned int n ) const; 00138 00140 unsigned int getPartSize( unsigned int n ) const; 00141 00143 const std::string& getPartHeader( unsigned int n, const std::string& name ) const; 00144 00146 const std::string& getMimeType() const; 00147 00148 private: 00149 struct Part : public osg::Referenced 00150 { 00151 Part() : _size(0) { } 00152 typedef std::map<std::string,std::string> Headers; 00153 Headers _headers; 00154 unsigned int _size; 00155 std::stringstream _stream; 00156 }; 00157 typedef std::vector< osg::ref_ptr<Part> > Parts; 00158 Parts _parts; 00159 long _response_code; 00160 std::string _mimeType; 00161 bool _cancelled; 00162 00163 friend class HTTPClient; 00164 }; 00165 00173 class OSGEARTH_EXPORT HTTPClient : public osg::Referenced 00174 { 00175 public: 00176 enum ResultCode { 00177 RESULT_OK, 00178 RESULT_CANCELED, 00179 RESULT_NOT_FOUND, 00180 RESULT_SERVER_ERROR, 00181 RESULT_TIMEOUT, 00182 RESULT_NO_READER, 00183 RESULT_READER_ERROR, 00184 RESULT_UNKNOWN_ERROR 00185 }; 00186 00191 static bool isRecoverable( ResultCode code ) 00192 { 00193 return 00194 code == RESULT_OK || 00195 code == RESULT_SERVER_ERROR || 00196 code == RESULT_TIMEOUT || 00197 code == RESULT_CANCELED; 00198 } 00199 00200 static std::string getResultCodeString( ResultCode code ) 00201 { 00202 return 00203 code == RESULT_OK ? "OK" : 00204 code == RESULT_CANCELED ? "Read canceled" : 00205 code == RESULT_NOT_FOUND ? "Target not found" : 00206 code == RESULT_SERVER_ERROR ? "Server error" : 00207 code == RESULT_TIMEOUT ? "Read timed out" : 00208 code == RESULT_NO_READER ? "No suitable ReaderWriter found" : 00209 code == RESULT_READER_ERROR ? "ReaderWriter error" : 00210 "Unknown error"; 00211 } 00212 00215 static const std::string& getUserAgent(); 00216 00219 static void setUserAgent(const std::string& userAgent); 00220 00223 static void setProxySettings( const ProxySettings &proxySettings ); 00224 00225 00226 public: 00231 static ResultCode readImageFile( 00232 const std::string& uri, 00233 osg::ref_ptr<osg::Image>& output, 00234 const osgDB::ReaderWriter::Options* options = 0, 00235 ProgressCallback* callback = 0 ); 00236 00241 static ResultCode readNodeFile( 00242 const std::string& uri, 00243 osg::ref_ptr<osg::Node>& output, 00244 const osgDB::ReaderWriter::Options* options = 0, 00245 ProgressCallback* callback = 0 ); 00246 00251 static ResultCode readObjectFile( 00252 const std::string& url, 00253 osg::ref_ptr<osg::Object>& output, 00254 const osgDB::ReaderWriter::Options* options = 0, 00255 ProgressCallback* callback = 0); 00256 00261 static ResultCode readString( 00262 const std::string& uri, 00263 std::string& output, 00264 ProgressCallback* callback =0); 00265 00269 static bool download( 00270 const std::string& uri, 00271 const std::string& localPath ); 00272 00273 public: 00274 00278 static HTTPResponse get( const HTTPRequest& request, 00279 const osgDB::ReaderWriter::Options* = 0, 00280 ProgressCallback* callback = 0); 00281 00282 static HTTPResponse get( const std::string& url, 00283 const osgDB::ReaderWriter::Options* options = 0, 00284 ProgressCallback* callback = 0); 00285 00286 private: 00287 HTTPClient(); 00288 ~HTTPClient(); 00289 00290 void readOptions( const osgDB::ReaderWriter::Options* options, std::string &proxy_host, std::string &proxy_port ) const; 00291 00292 HTTPResponse doGet( const HTTPRequest& request, 00293 const osgDB::ReaderWriter::Options* options = 0, 00294 ProgressCallback* callback = 0) const; 00295 00296 HTTPResponse doGet( const std::string& url, 00297 const osgDB::ReaderWriter::Options* options = 0, 00298 ProgressCallback* callback = 0 ) const; 00299 00300 ResultCode doReadObjectFile( 00301 const std::string& url, 00302 osg::ref_ptr<osg::Object>& output, 00303 const osgDB::ReaderWriter::Options* options = 0, 00304 ProgressCallback* callback = 0); 00305 00306 ResultCode doReadImageFile( 00307 const std::string& filename, 00308 osg::ref_ptr<osg::Image>& output, 00309 const osgDB::ReaderWriter::Options* options = 0, 00310 ProgressCallback *callback = 0); 00311 00312 ResultCode doReadNodeFile( 00313 const std::string& filename, 00314 osg::ref_ptr<osg::Node>& output, 00315 const osgDB::ReaderWriter::Options* options = 0, 00316 ProgressCallback *callback = 0); 00317 00318 ResultCode doReadString( 00319 const std::string& filename, 00320 std::string& output, 00321 ProgressCallback *callback = 0); 00322 00323 00327 bool doDownload(const std::string& url, const std::string& filename); 00328 00329 private: 00330 void* _curl_handle; 00331 std::string _previousPassword; 00332 long _previousHttpAuthentication; 00333 00334 00335 static HTTPClient& getClient(); 00336 00337 private: 00338 void decodeMultipartStream( 00339 const std::string& boundary, 00340 HTTPResponse::Part* input, 00341 HTTPResponse::Parts& output) const; 00342 }; 00343 } 00344 00345 #endif // OSGEARTH_HTTP_CLIENT_H