Null out curl CA list to prevent misleading access to it.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / CurlURLInputStream.h
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  * @file xmltooling/util/CurlURLInputStream.h
20  *
21  * Asynchronous use of curl to fetch data from a URL.
22  */
23
24 #if !defined(__xmltooling_curlinstr_h__) && !defined(XMLTOOLING_LITE)
25 #define __xmltooling_curlinstr_h__
26
27 #include <xmltooling/logging.h>
28
29 #include <string>
30 #include <vector>
31 #include <curl/curl.h>
32 #include <xercesc/util/BinInputStream.hpp>
33
34 namespace xmltooling {
35
36     /**
37      * Adapted from Xerces-C as a more advanced input stream implementation
38      * for subsequent use in parsing remote documents.
39      */
40     class XMLTOOL_API CurlURLInputStream : public xercesc::BinInputStream
41     {
42     public :
43         /**
44          * Constructor.
45          *
46          * @param url   the URL of the resource to fetch
47          */
48         CurlURLInputStream(const char* url);
49
50         /**
51          * Constructor.
52          *
53          * @param url   the URL of the resource to fetch
54          */
55         CurlURLInputStream(const XMLCh* url);
56
57         /**
58          * Constructor taking a DOM element supporting the following content:
59          * 
60          * <dl>
61          *  <dt>uri | url</dt>
62          *  <dd>identifies the remote resource</dd>
63          *  <dt>verifyHost</dt>
64          *  <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
65          *  <dt>TransportOption elements, like so:</dt>
66          *  <dd>&lt;TransportOption provider="CURL" option="150"&gt;0&lt;/TransportOption&gt;</dd>
67          * </dl>
68          * 
69          * @param e     DOM to supply configuration
70          */
71         CurlURLInputStream(const xercesc::DOMElement* e);
72
73         ~CurlURLInputStream();
74
75 #ifdef XMLTOOLING_XERCESC_64BITSAFE
76         XMLFilePos
77 #else
78         unsigned int
79 #endif
80         curPos() const {
81             return fTotalBytesRead;
82         }
83
84 #ifdef XMLTOOLING_XERCESC_INPUTSTREAM_HAS_CONTENTTYPE
85         const XMLCh* getContentType() const {
86             return fContentType;
87         }
88 #endif
89
90         xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
91
92     private :
93         CurlURLInputStream(const CurlURLInputStream&);
94         CurlURLInputStream& operator=(const CurlURLInputStream&);
95
96         // libcurl callbacks for data read/write
97         static size_t staticWriteCallback(char *buffer, size_t size, size_t nitems, void *outstream);
98         size_t writeCallback(char *buffer, size_t size, size_t nitems);
99
100         void init(const xercesc::DOMElement* e=NULL);
101         bool readMore(int *runningHandles);
102
103         logging::Category&  fLog;
104         std::string         fURL;
105         std::vector<std::string>    fSavedOptions;
106
107         CURLM*              fMulti;
108         CURL*               fEasy;
109
110         unsigned long       fTotalBytesRead;
111         XMLByte*            fWritePtr;
112         xsecsize_t          fBytesRead;
113         xsecsize_t          fBytesToRead;
114         bool                fDataAvailable;
115
116         // Overflow buffer for when curl writes more data to us
117         // than we've asked for.
118         XMLByte             fBuffer[CURL_MAX_WRITE_SIZE];
119         XMLByte*            fBufferHeadPtr;
120         XMLByte*            fBufferTailPtr;
121
122         XMLCh*              fContentType;
123
124         char                fError[CURL_ERROR_SIZE];
125     };
126 };
127
128 #endif // __xmltooling_curlinstr_h__