root/OpenSceneGraph/trunk/src/osgPlugins/curl/ReaderWriterCURL.h @ 13041

Revision 13041, 7.1 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef READERWRITERCURL_H
15#define READERWRITERCURL_H 1
16
17#include <osgDB/ReaderWriter>
18#include <osgDB/FileNameUtils>
19
20namespace osg_curl
21{
22
23enum ObjectType
24{
25    OBJECT,
26    ARCHIVE,
27    IMAGE,
28    HEIGHTFIELD,
29    NODE
30};
31
32class 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        // Added this function to set the desired connection timeout if needed (in case someone needs to try to connect
57        // to offline servers without freezing the process for a very long time) [even if, as stated on curl website,
58        // some normal transfer may be timed out this way].
59        inline void setConnectionTimeout(long val) { _connectTimeout = val; }
60
61        // the timeout variable is used to limit the whole transfer duration instead of the connection phase only.
62        inline void setTimeout(long val) { _timeout = val; }
63
64        // Perform HTTP GET to download data from web server.
65        osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options);
66
67        // Perform HTTP POST to upload data using "multipart/form-data" encoding to web server.
68        osgDB::ReaderWriter::WriteResult write(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options);
69
70        /** Returns the mime type of the data retrieved with the provided stream object on a
71          * previous call to EasyCurl::read(). */
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        // disallow copying
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
97class 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
Note: See TracBrowser for help on using the browser.