https://issues.shibboleth.net/jira/browse/CPPXT-83
[shibboleth/cpp-xmltooling.git] / xmltooling / util / CurlURLInputStream.cpp
index 5526024..2c91f4b 100644 (file)
@@ -1,18 +1,21 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *      http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
@@ -72,7 +75,7 @@ namespace {
     size_t curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream)
     {
         // only handle single-byte data
-        if (size!=1 || nmemb<5 || !stream)
+        if (size != 1 || nmemb < 5 || !stream)
             return nmemb;
         string* cacheTag = reinterpret_cast<string*>(stream);
         const char* hdr = reinterpret_cast<char*>(ptr);
@@ -114,9 +117,9 @@ namespace {
                 }
                 break;
             }
-            // append until whitespace
+            // append until data's gone or we see a CR/LF
             while (remaining > 0) {
-                if (!isspace(*hdr)) {
+                if (*hdr != '\r' && *hdr != '\n') {
                     (*cacheTag) += *hdr++;
                     --remaining;
                     continue;
@@ -273,12 +276,6 @@ void CurlURLInputStream::init(const DOMElement* e)
     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);
@@ -292,13 +289,21 @@ void CurlURLInputStream::init(const DOMElement* e)
         // Outgoing tag.
         if (!fCacheTag->empty()) {
             fHeaders = curl_slist_append(fHeaders, fCacheTag->c_str());
-            curl_easy_setopt(fEasy, CURLOPT_HTTPHEADER, fHeaders);
         }
         // Incoming tag.
         curl_easy_setopt(fEasy, CURLOPT_HEADERFUNCTION, curl_header_hook);
         curl_easy_setopt(fEasy, CURLOPT_HEADERDATA, fCacheTag);
     }
 
+    // Add User-Agent as a header for now. TODO: Add private member to hold the
+    // value for the standard UA option.
+    string ua = string("User-Agent: ") + XMLToolingConfig::getConfig().user_agent +
+        " libcurl/" + LIBCURL_VERSION + ' ' + OPENSSL_VERSION_TEXT;
+    fHeaders = curl_slist_append(fHeaders, ua.c_str());
+
+    // Add User-Agent and cache headers.
+    curl_easy_setopt(fEasy, CURLOPT_HTTPHEADER, fHeaders);
+
     if (e) {
         const XMLCh* flag = e->getAttributeNS(nullptr, verifyHost);
         if (flag && (*flag == chLatin_f || *flag == chDigit_0))