Reorder initializers.
[shibboleth/xmltooling.git] / xmltooling / util / CurlURLInputStream.cpp
index 6824ded..5526024 100644 (file)
@@ -135,8 +135,8 @@ namespace {
 CurlURLInputStream::CurlURLInputStream(const char* url, string* cacheTag)
     : fLog(logging::Category::getInstance(XMLTOOLING_LOGCAT".libcurl.InputStream"))
     , fCacheTag(cacheTag)
-    , fOpenSSLOps(SSL_OP_ALL|SSL_OP_NO_SSLv2)
     , fURL(url ? url : "")
+    , fOpenSSLOps(SSL_OP_ALL|SSL_OP_NO_SSLv2)
     , fMulti(0)
     , fEasy(0)
     , fHeaders(0)
@@ -145,8 +145,10 @@ CurlURLInputStream::CurlURLInputStream(const char* url, string* cacheTag)
     , fBytesRead(0)
     , fBytesToRead(0)
     , fDataAvailable(false)
-    , fBufferHeadPtr(fBuffer)
-    , fBufferTailPtr(fBuffer)
+    , fBuffer(0)
+    , fBufferHeadPtr(0)
+    , fBufferTailPtr(0)
+    , fBufferSize(0)
     , fContentType(0)
     , fStatusCode(200)
 {
@@ -167,8 +169,10 @@ CurlURLInputStream::CurlURLInputStream(const XMLCh* url, string* cacheTag)
     , fBytesRead(0)
     , fBytesToRead(0)
     , fDataAvailable(false)
-    , fBufferHeadPtr(fBuffer)
-    , fBufferTailPtr(fBuffer)
+    , fBuffer(0)
+    , fBufferHeadPtr(0)
+    , fBufferTailPtr(0)
+    , fBufferSize(0)
     , fContentType(0)
     , fStatusCode(200)
 {
@@ -193,14 +197,16 @@ CurlURLInputStream::CurlURLInputStream(const DOMElement* e, string* cacheTag)
     , fBytesRead(0)
     , fBytesToRead(0)
     , fDataAvailable(false)
-    , fBufferHeadPtr(fBuffer)
-    , fBufferTailPtr(fBuffer)
+    , fBuffer(0)
+    , fBufferHeadPtr(0)
+    , fBufferTailPtr(0)
+    , fBufferSize(0)
     , fContentType(0)
     , fStatusCode(200)
 {
-    const XMLCh* attr = e->getAttributeNS(NULL, url);
+    const XMLCh* attr = e->getAttributeNS(nullptr, url);
     if (!attr || !*attr) {
-        attr = e->getAttributeNS(NULL, uri);
+        attr = e->getAttributeNS(nullptr, uri);
         if (!attr || !*attr)
             throw IOException("No URL supplied via DOM to CurlURLInputStream constructor.");
     }
@@ -230,6 +236,7 @@ CurlURLInputStream::~CurlURLInputStream()
     }
 
     XMLString::release(&fContentType);
+    free(fBuffer);
 }
 
 void CurlURLInputStream::init(const DOMElement* e)
@@ -254,17 +261,24 @@ void CurlURLInputStream::init(const DOMElement* e)
     curl_easy_setopt(fEasy, CURLOPT_MAXREDIRS, 6);
 
     // Default settings.
-    curl_easy_setopt(fEasy, CURLOPT_CONNECTTIMEOUT,10);
-    curl_easy_setopt(fEasy, CURLOPT_TIMEOUT,60);
-    curl_easy_setopt(fEasy, CURLOPT_HTTPAUTH,0);
-    curl_easy_setopt(fEasy, CURLOPT_USERPWD,NULL);
+    curl_easy_setopt(fEasy, CURLOPT_CONNECTTIMEOUT, 10);
+    curl_easy_setopt(fEasy, CURLOPT_TIMEOUT, 60);
+    curl_easy_setopt(fEasy, CURLOPT_HTTPAUTH, 0);
+    curl_easy_setopt(fEasy, CURLOPT_USERPWD, nullptr);
     curl_easy_setopt(fEasy, CURLOPT_SSL_VERIFYHOST, 2);
     curl_easy_setopt(fEasy, CURLOPT_SSL_VERIFYPEER, 0);
-    curl_easy_setopt(fEasy, CURLOPT_CAINFO, NULL);
+    curl_easy_setopt(fEasy, CURLOPT_CAINFO, nullptr);
     curl_easy_setopt(fEasy, CURLOPT_SSL_CIPHER_LIST, "ALL:!aNULL:!LOW:!EXPORT:!SSLv2");
     curl_easy_setopt(fEasy, CURLOPT_NOPROGRESS, 1);
     curl_easy_setopt(fEasy, CURLOPT_NOSIGNAL, 1);
     curl_easy_setopt(fEasy, CURLOPT_FAILONERROR, 1);
+    curl_easy_setopt(fEasy, CURLOPT_ENCODING, "");
+    string ua = XMLToolingConfig::getConfig().user_agent;
+    if (!ua.empty()) {
+        ua = ua + " libcurl/" + LIBCURL_VERSION + ' ' + OPENSSL_VERSION_TEXT;
+        curl_easy_setopt(fEasy, CURLOPT_USERAGENT, ua.c_str());
+    }
+
 
     // Install SSL callback.
     curl_easy_setopt(fEasy, CURLOPT_SSL_CTX_FUNCTION, ssl_ctx_callback);
@@ -286,7 +300,7 @@ void CurlURLInputStream::init(const DOMElement* e)
     }
 
     if (e) {
-        const XMLCh* flag = e->getAttributeNS(NULL, verifyHost);
+        const XMLCh* flag = e->getAttributeNS(nullptr, verifyHost);
         if (flag && (*flag == chLatin_f || *flag == chDigit_0))
             curl_easy_setopt(fEasy, CURLOPT_SSL_VERIFYHOST, 0);
 
@@ -294,8 +308,8 @@ void CurlURLInputStream::init(const DOMElement* e)
         bool success;
         DOMElement* child = XMLHelper::getLastChildElement(e, TransportOption);
         while (child) {
-            if (child->hasChildNodes() && XMLString::equals(child->getAttributeNS(NULL,_provider), _OpenSSL)) {
-                auto_ptr_char option(child->getAttributeNS(NULL,_option));
+            if (child->hasChildNodes() && XMLString::equals(child->getAttributeNS(nullptr,_provider), _OpenSSL)) {
+                auto_ptr_char option(child->getAttributeNS(nullptr,_option));
                 auto_ptr_char value(child->getFirstChild()->getNodeValue());
                 if (option.get() && value.get() && !strcmp(option.get(), "SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION") &&
                     (*value.get()=='1' || *value.get()=='t')) {
@@ -319,14 +333,14 @@ void CurlURLInputStream::init(const DOMElement* e)
                 if (!success)
                     fLog.error("failed to set OpenSSL transport option (%s)", option.get());
             }
-            else if (child->hasChildNodes() && XMLString::equals(child->getAttributeNS(NULL,_provider), _CURL)) {
-                auto_ptr_char option(child->getAttributeNS(NULL,_option));
+            else if (child->hasChildNodes() && XMLString::equals(child->getAttributeNS(nullptr,_provider), _CURL)) {
+                auto_ptr_char option(child->getAttributeNS(nullptr,_option));
                 auto_ptr_char value(child->getFirstChild()->getNodeValue());
                 if (option.get() && *option.get() && value.get() && *value.get()) {
                     // For libcurl, the option is an enum and the value type depends on the option.
-                    CURLoption opt = static_cast<CURLoption>(strtol(option.get(), NULL, 10));
+                    CURLoption opt = static_cast<CURLoption>(strtol(option.get(), nullptr, 10));
                     if (opt < CURLOPTTYPE_OBJECTPOINT)
-                        success = (curl_easy_setopt(fEasy, opt, strtol(value.get(), NULL, 10)) == CURLE_OK);
+                        success = (curl_easy_setopt(fEasy, opt, strtol(value.get(), nullptr, 10)) == CURLE_OK);
 #ifdef CURLOPTTYPE_OFF_T
                     else if (opt < CURLOPTTYPE_OFF_T) {
                         fSavedOptions.push_back(value.get());
@@ -334,10 +348,10 @@ void CurlURLInputStream::init(const DOMElement* e)
                     }
 # ifdef HAVE_CURL_OFF_T
                     else if (sizeof(curl_off_t) == sizeof(long))
-                        success = (curl_easy_setopt(fEasy, opt, strtol(value.get(), NULL, 10)) == CURLE_OK);
+                        success = (curl_easy_setopt(fEasy, opt, strtol(value.get(), nullptr, 10)) == CURLE_OK);
 # else
                     else if (sizeof(off_t) == sizeof(long))
-                        success = (curl_easy_setopt(fEasy, opt, strtol(value.get(), NULL, 10)) == CURLE_OK);
+                        success = (curl_easy_setopt(fEasy, opt, strtol(value.get(), nullptr, 10)) == CURLE_OK);
 # endif
                     else
                         success = false;
@@ -369,9 +383,9 @@ void CurlURLInputStream::init(const DOMElement* e)
         catch (XMLException&) {
             curl_multi_remove_handle(fMulti, fEasy);
             curl_easy_cleanup(fEasy);
-            fEasy = NULL;
+            fEasy = nullptr;
             curl_multi_cleanup(fMulti);
-            fMulti = NULL;
+            fMulti = nullptr;
             throw;
         }
         if(runningHandles == 0) break;
@@ -386,8 +400,17 @@ void CurlURLInputStream::init(const DOMElement* e)
                 << fStatusCode
                 << "</" << URLInputSource::asciiStatusCodeElementName << '>';
             string specialxml = specialdoc.str();
+            fBufferTailPtr = fBuffer = reinterpret_cast<XMLByte*>(malloc(specialxml.length()));
+            if (!fBuffer) {
+                curl_multi_remove_handle(fMulti, fEasy);
+                curl_easy_cleanup(fEasy);
+                fEasy = nullptr;
+                curl_multi_cleanup(fMulti);
+                fMulti = nullptr;
+                throw bad_alloc();
+            }
             memcpy(fBuffer, specialxml.c_str(), specialxml.length());
-            fBufferHeadPtr += specialxml.length();
+            fBufferHeadPtr = fBuffer + specialxml.length();
         }
     }
     else {
@@ -395,7 +418,7 @@ void CurlURLInputStream::init(const DOMElement* e)
     }
 
     // Find the content type
-    char* contentType8 = NULL;
+    char* contentType8 = nullptr;
     if(curl_easy_getinfo(fEasy, CURLINFO_CONTENT_TYPE, &contentType8) == CURLE_OK && contentType8)
         fContentType = XMLString::transcode(contentType8);
 }
@@ -419,7 +442,7 @@ size_t CurlURLInputStream::writeCallback(char* buffer, size_t size, size_t nitem
     fTotalBytesRead += consume;
     fBytesToRead    -= consume;
 
-    //fLog.debug("write callback consuming %d bytes", consume);
+    fLog.debug("write callback consuming %u bytes", consume);
 
     // If bytes remain, rebuffer as many as possible into our holding buffer
     buffer          += consume;
@@ -427,13 +450,22 @@ size_t CurlURLInputStream::writeCallback(char* buffer, size_t size, size_t nitem
     cnt             -= consume;
     if (cnt > 0)
     {
-        size_t bufAvail = sizeof(fBuffer) - (fBufferHeadPtr - fBuffer);
-        consume = (cnt > bufAvail) ? bufAvail : cnt;
-        memcpy(fBufferHeadPtr, buffer, consume);
-        fBufferHeadPtr  += consume;
-        buffer          += consume;
-        totalConsumed   += consume;
-        //fLog.debug("write callback rebuffering %d bytes", consume);
+        size_t bufAvail = fBufferSize - (fBufferHeadPtr - fBuffer);
+        if (bufAvail < cnt) {
+            // Enlarge the buffer. TODO: limit max size
+            XMLByte* newbuf = reinterpret_cast<XMLByte*>(realloc(fBuffer, fBufferSize + (cnt - bufAvail)));
+            if (newbuf) {
+                fBufferSize = fBufferSize + (cnt - bufAvail);
+                fLog.debug("enlarged buffer to %u bytes", fBufferSize);
+                fBufferHeadPtr = newbuf + (fBufferHeadPtr - fBuffer);
+                fBuffer = fBufferTailPtr = newbuf;
+            }
+        }
+        memcpy(fBufferHeadPtr, buffer, cnt);
+        fBufferHeadPtr  += cnt;
+        buffer          += cnt;
+        totalConsumed   += cnt;
+        fLog.debug("write callback rebuffering %u bytes", cnt);
     }
 
     // Return the total amount we've consumed. If we don't consume all the bytes
@@ -450,9 +482,9 @@ bool CurlURLInputStream::readMore(int* runningHandles)
 
     // Process messages from curl
     int msgsInQueue = 0;
-    for (CURLMsg* msg = NULL; (msg = curl_multi_info_read(fMulti, &msgsInQueue)) != NULL; )
+    for (CURLMsg* msg = nullptr; (msg = curl_multi_info_read(fMulti, &msgsInQueue)) != nullptr; )
     {
-        //fLog.debug("msg %d, %d from curl", msg->msg, msg->data.result);
+        fLog.debug("msg %d, %d from curl", msg->msg, msg->data.result);
 
         if (msg->msg != CURLMSG_DONE)
             return true;
@@ -544,7 +576,7 @@ xsecsize_t CurlURLInputStream::readBytes(XMLByte* const toFill, const xsecsize_t
             if (fBufferTailPtr == fBufferHeadPtr)
                 fBufferHeadPtr = fBufferTailPtr = fBuffer;
 
-            //fLog.debug("consuming %d buffered bytes", bufCnt);
+            fLog.debug("consuming %d buffered bytes", bufCnt);
 
             tryAgain = true;
             continue;