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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file xmltooling/util/CurlURLInputStream.h
21 * Asynchronous use of curl to fetch data from a URL.
24 #if !defined(__xmltooling_curlinstr_h__) && !defined(XMLTOOLING_LITE)
25 #define __xmltooling_curlinstr_h__
27 #include <xmltooling/logging.h>
31 #include <curl/curl.h>
32 #include <xercesc/util/BinInputStream.hpp>
34 namespace xmltooling {
37 * Adapted from Xerces-C as a more advanced input stream implementation
38 * for subsequent use in parsing remote documents.
40 class XMLTOOL_API CurlURLInputStream : public xercesc::BinInputStream
46 * @param url the URL of the resource to fetch
47 * @param cacheTag optional pointer to string used for cache management
49 CurlURLInputStream(const char* url, std::string* cacheTag=NULL);
54 * @param url the URL of the resource to fetch
55 * @param cacheTag optional pointer to string used for cache management
57 CurlURLInputStream(const XMLCh* url, std::string* cacheTag=NULL);
60 * Constructor taking a DOM element supporting the following content:
64 * <dd>identifies the remote resource</dd>
66 * <dd>true iff name of host should be matched against TLS/SSL certificate</dd>
67 * <dt>TransportOption elements, like so:</dt>
68 * <dd><TransportOption provider="CURL" option="150">0</TransportOption></dd>
71 * @param e DOM to supply configuration
72 * @param cacheTag optional pointer to string used for cache management
74 CurlURLInputStream(const xercesc::DOMElement* e, std::string* cacheTag=NULL);
76 ~CurlURLInputStream();
78 #ifdef XMLTOOLING_XERCESC_64BITSAFE
84 return fTotalBytesRead;
87 #ifdef XMLTOOLING_XERCESC_INPUTSTREAM_HAS_CONTENTTYPE
88 const XMLCh* getContentType() const {
93 xsecsize_t readBytes(XMLByte* const toFill, const xsecsize_t maxToRead);
96 * Access the OpenSSL context options in place for this object.
98 * @return bitmask suitable for use with SSL_CTX_set_options
100 int getOpenSSLOps() const {
105 CurlURLInputStream(const CurlURLInputStream&);
106 CurlURLInputStream& operator=(const CurlURLInputStream&);
108 // libcurl callbacks for data read/write
109 static size_t staticWriteCallback(char *buffer, size_t size, size_t nitems, void *outstream);
110 size_t writeCallback(char *buffer, size_t size, size_t nitems);
112 void init(const xercesc::DOMElement* e=NULL);
113 bool readMore(int *runningHandles);
115 logging::Category& fLog;
116 std::string* fCacheTag;
118 std::vector<std::string> fSavedOptions;
123 struct curl_slist* fHeaders;
125 unsigned long fTotalBytesRead;
127 xsecsize_t fBytesRead;
128 xsecsize_t fBytesToRead;
131 // Overflow buffer for when curl writes more data to us
132 // than we've asked for.
133 XMLByte fBuffer[CURL_MAX_WRITE_SIZE];
134 XMLByte* fBufferHeadPtr;
135 XMLByte* fBufferTailPtr;
140 char fError[CURL_ERROR_SIZE];
144 #endif // __xmltooling_curlinstr_h__