Allow to query HTTP headers
authorRobin Gareus <robin@gareus.org>
Thu, 15 Sep 2016 23:39:04 +0000 (01:39 +0200)
committerRobin Gareus <robin@gareus.org>
Thu, 15 Sep 2016 23:39:04 +0000 (01:39 +0200)
gtk2_ardour/ardour_http.cc
gtk2_ardour/ardour_http.h

index 3ffea61b33013647fa8097663677b8794fef8b69..ba028d6f002e2ef9b18b7851b59ca1723a7114b5 100644 (file)
@@ -105,6 +105,22 @@ WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) {
        return realsize;
 }
 
+static size_t headerCallback (char* ptr, size_t size, size_t nmemb, void* data)
+{
+       size_t realsize = size * nmemb;
+       struct HttpGet::HeaderInfo *nfo = (struct HttpGet::HeaderInfo*)data;
+       std::string header (static_cast<const char*>(ptr), realsize);
+       std::string::size_type index = header.find (':', 0);
+       if (index != std::string::npos) {
+               std::string k = header.substr (0, index);
+               std::string v = header.substr (index + 2);
+               k.erase(k.find_last_not_of (" \n\r\t")+1);
+               v.erase(v.find_last_not_of (" \n\r\t")+1);
+               nfo->h[k] = v;
+       }
+
+       return realsize;
+}
 
 HttpGet::HttpGet (bool p, bool ssl)
        : persist (p)
@@ -116,6 +132,8 @@ HttpGet::HttpGet (bool p, bool ssl)
 
        curl_easy_setopt (_curl, CURLOPT_WRITEDATA, (void *)&mem);
        curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+       curl_easy_setopt (_curl, CURLOPT_HEADERDATA, (void *)&nfo);
+       curl_easy_setopt (_curl, CURLOPT_HEADERFUNCTION, headerCallback);
        curl_easy_setopt (_curl, CURLOPT_USERAGENT, PROGRAM_NAME VERSIONSTRING);
        curl_easy_setopt (_curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
        curl_easy_setopt (_curl, CURLOPT_NOSIGNAL, 1);
index 06cf46f408751851827455363a7152330292189d..aa6b9f3936658aba6119e3af6ca9727258974c3e 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <curl/curl.h>
 #include <string>
+#include <map>
 
 namespace ArdourCurl {
 
@@ -35,6 +36,10 @@ class HttpGet {
                size_t size;
        };
 
+       struct HeaderInfo {
+               std::map<std::string, std::string> h;
+       };
+
        char* get (const char* url);
 
        std::string get (const std::string& url) {
@@ -47,6 +52,8 @@ class HttpGet {
 
        long int status () const { return _status; }
 
+       std::map<std::string, std::string> header () const { return nfo.h; }
+
        char* escape (const char* s, int l) const {
                return curl_easy_escape (_curl, s, l);
        }
@@ -76,6 +83,7 @@ class HttpGet {
        char error_buffer[CURL_ERROR_SIZE];
 
        struct MemStruct mem;
+       struct HeaderInfo nfo;
 
        static const char* ca_path;
        static const char* ca_info;