a8bb9256bbbf2d795f6f34f72b4974794787479e
[shibboleth/xmltooling.git] / xmltooling / util / CurlURLInputStream.hpp
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*
19  * $Id$
20  */
21
22 #if !defined(XERCESC_INCLUDE_GUARD_CURLURLINPUTSTREAM_HPP) && !defined(XMLTOOLING_LITE)
23 #define XERCESC_INCLUDE_GUARD_CURLURLINPUTSTREAM_HPP
24
25 #include <xmltooling/base.h>
26
27 #include <curl/curl.h>
28 #include <curl/multi.h>
29 #include <curl/easy.h>
30
31 #include <xercesc/util/XMLURL.hpp>
32 #include <xercesc/util/XMLExceptMsgs.hpp>
33 #include <xercesc/util/Janitor.hpp>
34 #include <xercesc/util/BinInputStream.hpp>
35 #include <xercesc/util/XMLNetAccessor.hpp>
36
37 namespace xmltooling {
38
39 //
40 // This class implements the BinInputStream interface specified by the XML
41 // parser.
42 //
43
44 class XMLTOOL_API CurlURLInputStream : public BinInputStream
45 {
46 public :
47     CurlURLInputStream(const XMLURL&  urlSource, const XMLNetHTTPInfo* httpInfo=0);
48     ~CurlURLInputStream();
49
50     unsigned int curPos() const;
51     unsigned int readBytes
52     (
53                 XMLByte* const  toFill
54         , const unsigned int       maxToRead
55     );
56
57
58 private :
59     // -----------------------------------------------------------------------
60     //  Unimplemented constructors and operators
61     // -----------------------------------------------------------------------
62     CurlURLInputStream(const CurlURLInputStream&);
63     CurlURLInputStream& operator=(const CurlURLInputStream&);
64     
65     static size_t staticWriteCallback(char *buffer,
66                                       size_t size,
67                                       size_t nitems,
68                                       void *outstream);
69     size_t writeCallback(                         char *buffer,
70                                       size_t size,
71                                       size_t nitems);
72
73
74     // -----------------------------------------------------------------------
75     //  Private data members
76     //
77     //  fSocket
78     //      The socket representing the connection to the remote file.
79     //  fBytesProcessed
80     //      Its a rolling count of the number of bytes processed off this
81     //      input stream.
82     //  fBuffer
83     //      Holds the http header, plus the first part of the actual
84     //      data.  Filled at the time the stream is opened, data goes
85     //      out to user in response to readBytes().
86     //  fBufferPos, fBufferEnd
87     //      Pointers into fBuffer, showing start and end+1 of content
88     //      that readBytes must return.
89     // -----------------------------------------------------------------------
90         
91     CURLM*                              fMulti;
92     CURL*                               fEasy;
93     
94     MemoryManager*      fMemoryManager;
95     
96     XMLURL                              fURLSource;
97     ArrayJanitor<char>  fURL;
98     
99     unsigned long       fTotalBytesRead;
100     XMLByte*                    fWritePtr;
101     unsigned long               fBytesRead;
102     unsigned long               fBytesToRead;
103     bool                                fDataAvailable;
104     
105     // Overflow buffer for when curl writes more data to us
106     // than we've asked for.
107     XMLByte                             fBuffer[CURL_MAX_WRITE_SIZE];
108     XMLByte*                    fBufferHeadPtr;
109     XMLByte*                    fBufferTailPtr;
110     
111 }; // CurlURLInputStream
112
113
114 inline unsigned int
115 CurlURLInputStream::curPos() const
116 {
117     return fTotalBytesRead;
118 }
119
120 };
121
122 #endif // CURLURLINPUTSTREAM_HPP
123