eHttpStream: allow to set custom User-Agent header
authormx3L <mx3ldev@gmail.com>
Sat, 5 Dec 2015 14:39:22 +0000 (15:39 +0100)
committermx3L <mx3ldev@gmail.com>
Sat, 5 Dec 2015 14:41:58 +0000 (15:41 +0100)
lib/base/httpstream.cpp

index ef5e10f..a5fce10 100644 (file)
@@ -42,12 +42,24 @@ int eHttpStream::openUrl(const std::string &url, std::string &newurl)
 
        close();
 
+       std::string user_agent = "Enigma2";
        std::string extra_headers = "";
        size_t pos = uri.find('#');
        if (pos != std::string::npos)
        {
                extra_headers = uri.substr(pos + 1);
                uri = uri.substr(0, pos);
+
+               pos = extra_headers.find("User-Agent=");
+               if (pos != std::string::npos)
+               {
+                       size_t hpos_start = pos + 11;
+                       size_t hpos_end = extra_headers.find('&', hpos_start);
+                       if (hpos_end != std::string::npos)
+                               user_agent = extra_headers.substr(hpos_start, hpos_end - hpos_start);
+                       else
+                               user_agent = extra_headers.substr(hpos_start);
+               }
        }
 
        int pathindex = uri.find("/", 7);
@@ -83,7 +95,6 @@ int eHttpStream::openUrl(const std::string &url, std::string &newurl)
                port = 80;
        }
 
-
        streamSocket = Connect(hostname.c_str(), port, 10);
        if (streamSocket < 0)
                goto error;
@@ -91,7 +102,7 @@ int eHttpStream::openUrl(const std::string &url, std::string &newurl)
        request = "GET ";
        request.append(uri).append(" HTTP/1.1\r\n");
        request.append("Host: ").append(hostname).append("\r\n");
-       request.append("User-Agent: ").append("Enigma2").append("\r\n");
+       request.append("User-Agent: ").append(user_agent).append("\r\n");
        if (authorizationData != "")
        {
                request.append("Authorization: Basic ").append(authorizationData).append("\r\n");
@@ -121,6 +132,8 @@ int eHttpStream::openUrl(const std::string &url, std::string &newurl)
                }
                if (!name.empty() && !value.empty())
                {
+                       if (name.compare("User-Agent") == 0)
+                               continue;
                        eDebug("[eHttpStream] setting extra-header '%s:%s'", name.c_str(), value.c_str());
                        request.append(name).append(": ").append(value).append("\r\n");
                }