| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #ifndef READERWRITERCURL_H |
|---|
| 15 | #define READERWRITERCURL_H 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/ReaderWriter> |
|---|
| 18 | #include <osgDB/FileNameUtils> |
|---|
| 19 | |
|---|
| 20 | namespace osg_curl |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | enum ObjectType |
|---|
| 24 | { |
|---|
| 25 | OBJECT, |
|---|
| 26 | ARCHIVE, |
|---|
| 27 | IMAGE, |
|---|
| 28 | HEIGHTFIELD, |
|---|
| 29 | NODE |
|---|
| 30 | }; |
|---|
| 31 | |
|---|
| 32 | class EasyCurl : public osg::Referenced |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | |
|---|
| 36 | struct StreamObject |
|---|
| 37 | { |
|---|
| 38 | StreamObject(std::ostream* outputStream, std::istream* inputStream, const std::string& cacheFileName); |
|---|
| 39 | |
|---|
| 40 | void write(const char* ptr, size_t realsize); |
|---|
| 41 | size_t read(char* ptr, size_t maxsize); |
|---|
| 42 | |
|---|
| 43 | std::ostream* _outputStream; |
|---|
| 44 | std::istream* _inputStream; |
|---|
| 45 | |
|---|
| 46 | bool _foutOpened; |
|---|
| 47 | std::string _cacheFileName; |
|---|
| 48 | std::ofstream _fout; |
|---|
| 49 | std::string _resultMimeType; |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | static size_t StreamMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data); |
|---|
| 53 | |
|---|
| 54 | EasyCurl(); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | inline void setConnectionTimeout(long val) { _connectTimeout = val; } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | inline void setTimeout(long val) { _timeout = val; } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | osgDB::ReaderWriter::WriteResult write(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options); |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | std::string getResultMimeType(const StreamObject& sp) const; |
|---|
| 73 | |
|---|
| 74 | std::string getMimeTypeForExtension(const std::string& ext) const; |
|---|
| 75 | static std::string getFileNameFromURL(const std::string& url); |
|---|
| 76 | |
|---|
| 77 | protected: |
|---|
| 78 | |
|---|
| 79 | virtual ~EasyCurl(); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | EasyCurl(const EasyCurl& rhs):_curl(rhs._curl) {} |
|---|
| 83 | EasyCurl& operator = (const EasyCurl&) { return *this; } |
|---|
| 84 | |
|---|
| 85 | void setOptions(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options); |
|---|
| 86 | osgDB::ReaderWriter::ReadResult processResponse(CURLcode responseCode, const std::string& proxyAddress, const std::string& fileName, StreamObject& sp); |
|---|
| 87 | |
|---|
| 88 | CURL* _curl; |
|---|
| 89 | |
|---|
| 90 | std::string _previousPassword; |
|---|
| 91 | long _previousHttpAuthentication; |
|---|
| 92 | long _connectTimeout; |
|---|
| 93 | long _timeout; |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | class ReaderWriterCURL : public osgDB::ReaderWriter |
|---|
| 98 | { |
|---|
| 99 | public: |
|---|
| 100 | |
|---|
| 101 | ReaderWriterCURL(); |
|---|
| 102 | |
|---|
| 103 | ~ReaderWriterCURL(); |
|---|
| 104 | |
|---|
| 105 | virtual const char* className() const { return "HTTP Protocol Model Reader"; } |
|---|
| 106 | |
|---|
| 107 | virtual bool acceptsExtension(const std::string& extension) const |
|---|
| 108 | { |
|---|
| 109 | return osgDB::equalCaseInsensitive(extension,"curl"); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | virtual bool fileExists(const std::string& filename, const osgDB::Options* options) const; |
|---|
| 113 | |
|---|
| 114 | virtual ReadResult openArchive(const std::string& fileName,ArchiveStatus status, unsigned int , const Options* options) const |
|---|
| 115 | { |
|---|
| 116 | if (status!=READ) return ReadResult(ReadResult::FILE_NOT_HANDLED); |
|---|
| 117 | else return readFile(ARCHIVE,fileName,options); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | virtual ReadResult readObject(const std::string& fileName, const Options* options) const |
|---|
| 121 | { |
|---|
| 122 | return readFile(OBJECT,fileName,options); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | virtual ReadResult readImage(const std::string& fileName, const Options *options) const |
|---|
| 126 | { |
|---|
| 127 | return readFile(IMAGE,fileName,options); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | virtual ReadResult readHeightField(const std::string& fileName, const Options *options) const |
|---|
| 131 | { |
|---|
| 132 | return readFile(HEIGHTFIELD,fileName,options); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | virtual ReadResult readNode(const std::string& fileName, const Options *options) const |
|---|
| 136 | { |
|---|
| 137 | return readFile(NODE,fileName,options); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | virtual WriteResult writeObject(const osg::Object& obj, const std::string& fileName, const Options* options) const |
|---|
| 141 | { |
|---|
| 142 | return writeFile(obj,fileName,options); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | virtual WriteResult writeImage(const osg::Image& image, const std::string& fileName, const Options* options) const |
|---|
| 146 | { |
|---|
| 147 | return writeFile(image,fileName,options); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | virtual WriteResult writeHeightField(const osg::HeightField& heightField, const std::string& fileName, const Options* options) const |
|---|
| 151 | { |
|---|
| 152 | return writeFile(heightField,fileName,options); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | virtual WriteResult writeNode(const osg::Node& node, const std::string& fileName, const Options* options) const |
|---|
| 156 | { |
|---|
| 157 | return writeFile(node,fileName,options); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | ReadResult readFile(ObjectType objectType, osgDB::ReaderWriter* rw, std::istream& fin, const Options *options) const; |
|---|
| 161 | WriteResult writeFile(const osg::Object& obj, osgDB::ReaderWriter* rw, std::ostream& fout, const Options *options) const; |
|---|
| 162 | |
|---|
| 163 | virtual ReadResult readFile(ObjectType objectType, const std::string& fullFileName, const Options *options) const; |
|---|
| 164 | virtual WriteResult writeFile(const osg::Object& obj, const std::string& fullFileName, const Options *options) const; |
|---|
| 165 | |
|---|
| 166 | EasyCurl& getEasyCurl() const |
|---|
| 167 | { |
|---|
| 168 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadCurlMapMutex); |
|---|
| 169 | |
|---|
| 170 | osg::ref_ptr<EasyCurl>& ec = _threadCurlMap[OpenThreads::Thread::CurrentThread()]; |
|---|
| 171 | if (!ec) ec = new EasyCurl; |
|---|
| 172 | |
|---|
| 173 | return *ec; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | bool read(std::istream& fin, std::string& destination) const; |
|---|
| 177 | |
|---|
| 178 | protected: |
|---|
| 179 | void getConnectionOptions(const osgDB::ReaderWriter::Options *options, std::string& proxyAddress, long& connectTimeout, long& timeout) const; |
|---|
| 180 | |
|---|
| 181 | typedef std::map< OpenThreads::Thread*, osg::ref_ptr<EasyCurl> > ThreadCurlMap; |
|---|
| 182 | |
|---|
| 183 | mutable OpenThreads::Mutex _threadCurlMapMutex; |
|---|
| 184 | mutable ThreadCurlMap _threadCurlMap; |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | #endif |
|---|