This is the mail archive of the cygwin-apps mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH setup] Send User-Agent even when setup-specific proxy is used


Setup is sending User-Agent header if direct connection or system proxy
is used, but not when direct(legacy) or setup-specific proxy. Fix this.
---
 Makefile.am  |   2 ++
 nio-http.cc  |   4 +++
 nio-ie5.cc   |  53 ++---------------------------
 useragent.cc | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 useragent.h  |   8 +++++
 5 files changed, 125 insertions(+), 51 deletions(-)
 create mode 100644 useragent.cc
 create mode 100644 useragent.h

diff --git a/Makefile.am b/Makefile.am
index a238d88..3c88020 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -256,6 +256,8 @@ inilint_SOURCES = \
 	String++.h \
 	threebar.cc \
 	threebar.h \
+	useragent.cc \
+	useragent.h \
 	UserSettings.cc \
 	UserSettings.h \
 	win32.cc \
diff --git a/nio-http.cc b/nio-http.cc
index 413ee7f..3f7fd9b 100644
--- a/nio-http.cc
+++ b/nio-http.cc
@@ -29,6 +29,8 @@
 #include "netio.h"
 #include "nio-http.h"
 
+#include "useragent.h"
+
 #ifndef _strnicmp
 #define _strnicmp strncasecmp
 #endif
@@ -125,6 +127,8 @@ retry_get:
     s->printf ("Proxy-Authorization: Basic %s\r\n",
 	       base64_encode (net_proxy_user, net_proxy_passwd));
 
+  s->printf ("%s", get_useragent_header());
+
   s->printf ("\r\n");
 
   char * l = s->gets ();
diff --git a/nio-ie5.cc b/nio-ie5.cc
index a649233..2e36ac6 100644
--- a/nio-ie5.cc
+++ b/nio-ie5.cc
@@ -27,42 +27,12 @@
 #include "netio.h"
 #include "nio-ie5.h"
 #include "LogSingleton.h"
-#include "setup_version.h"
-#include "getopt++/StringOption.h"
+#include "useragent.h"
 #include <sstream>
 
-static StringOption UserAgent ("", '\0', "user-agent", "User agent string for HTTP requests");
-
 static HINTERNET internet_direct = 0;
 static HINTERNET internet_preconfig = 0;
 
-const std::string &
-determine_default_useragent(void)
-{
-  static std::string default_useragent;
-
-  if (!default_useragent.empty())
-    return default_useragent;
-
-  std::stringstream os;
-  os << "Windows NT " << OSMajorVersion() << "." << OSMinorVersion() << "." << OSBuildNumber();
-
-  std::string bitness = "Unknown";
-#ifdef __x86_64__
-  bitness = "Win64";
-#else
-  typedef BOOL (WINAPI *PFNISWOW64PROCESS)(HANDLE, PBOOL);
-  PFNISWOW64PROCESS pfnIsWow64Process = (PFNISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
-  if (pfnIsWow64Process) {
-    BOOL bIsWow64 = FALSE;
-    if (pfnIsWow64Process(GetCurrentProcess(), &bIsWow64))
-      bitness = bIsWow64 ? "WoW64" : "Win32";
-  }
-#endif
-  default_useragent = std::string("Cygwin-Setup/") + setup_version + " (" + os.str() + ";" + bitness + ")";
-  return default_useragent;
-}
-
 NetIO_IE5::NetIO_IE5 (char const *_url, bool direct, bool cachable):
 NetIO (_url)
 {
@@ -78,26 +48,7 @@ NetIO (_url)
     {
       InternetAttemptConnect (0);
 
-      const char *lpszAgent = determine_default_useragent().c_str();
-      if (UserAgent.isPresent())
-        {
-          const std::string &user_agent = UserAgent;
-          if (user_agent.length())
-            {
-              // override the default user agent string
-              lpszAgent = user_agent.c_str();
-              Log (LOG_PLAIN) << "User-Agent: header overridden to \"" << lpszAgent << "\"" << endLog;
-            }
-          else
-            {
-              // user-agent option is present, but no string is specified means
-              // don't add a user-agent header
-              lpszAgent = NULL;
-              Log (LOG_PLAIN) << "User-Agent: header suppressed " << lpszAgent << endLog;
-            }
-        }
-
-      *internet = InternetOpen (lpszAgent,
+      *internet = InternetOpen (get_useragent(),
 				direct ? INTERNET_OPEN_TYPE_DIRECT : INTERNET_OPEN_TYPE_PRECONFIG,
 				NULL, NULL, 0);
     }
diff --git a/useragent.cc b/useragent.cc
new file mode 100644
index 0000000..a871a8b
--- /dev/null
+++ b/useragent.cc
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2000, Red Hat, Inc.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ * Written by DJ Delorie <dj@cygnus.com>
+ *
+ */
+
+#include "win32.h"
+
+#include "LogSingleton.h"
+#include "setup_version.h"
+#include "getopt++/StringOption.h"
+#include "useragent.h"
+#include <sstream>
+
+static StringOption UserAgentOption ("", '\0', "user-agent", "User agent string for HTTP requests");
+
+const std::string &
+determine_default_useragent(void)
+{
+  static std::string default_useragent;
+
+  if (!default_useragent.empty())
+    return default_useragent;
+
+  std::stringstream os;
+  os << "Windows NT " << OSMajorVersion() << "." << OSMinorVersion() << "." << OSBuildNumber();
+
+  std::string bitness = "Unknown";
+#ifdef __x86_64__
+  bitness = "Win64";
+#else
+  typedef BOOL (WINAPI *PFNISWOW64PROCESS)(HANDLE, PBOOL);
+  PFNISWOW64PROCESS pfnIsWow64Process = (PFNISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
+  if (pfnIsWow64Process) {
+    BOOL bIsWow64 = FALSE;
+    if (pfnIsWow64Process(GetCurrentProcess(), &bIsWow64))
+      bitness = bIsWow64 ? "WoW64" : "Win32";
+  }
+#endif
+  default_useragent = std::string("Cygwin-Setup/") + setup_version + " (" + os.str() + ";" + bitness + ")";
+  return default_useragent;
+}
+
+
+// used in nio_ie5.cc
+const char *
+get_useragent(void)
+{
+  static std::string useragent;
+
+  if (UserAgentOption.isPresent())
+    {
+      useragent = UserAgentOption;
+      if (useragent.length())
+        {
+          // override the default user agent string
+          Log (LOG_PLAIN) << "User-Agent: header overridden to \"" << useragent << "\"" << endLog;
+          return useragent.c_str();
+        }
+      else
+        {
+          // user-agent option is present, but no string is specified means
+          // don't add a user-agent header
+          Log (LOG_PLAIN) << "User-Agent: header suppressed " << endLog;
+          return NULL;
+        }
+    }
+  else
+    {
+      useragent = determine_default_useragent();
+      return useragent.c_str();
+    }
+}
+
+
+// used in nio_http.cc
+const char *
+get_useragent_header(void)
+{
+  static std::string header;
+  static const char *cheader = NULL;
+
+  if (!cheader)
+  {
+    const char *useragent = get_useragent();
+
+    if (useragent)
+      {
+        header = std::string("User-Agent: ") + useragent + "\r\n";
+      }
+    else
+      {
+        header = "";
+      }
+
+    cheader = header.c_str();
+  }
+
+  return cheader;
+}
diff --git a/useragent.h b/useragent.h
new file mode 100644
index 0000000..75fa9b9
--- /dev/null
+++ b/useragent.h
@@ -0,0 +1,8 @@
+
+#ifndef SETUP_USERAGENT_H
+#define SETUP_USERAGENT_H
+
+const char *get_useragent(void);
+const char *get_useragent_header(void);
+
+#endif /* SETUP_USERAGENT_H */
-- 
2.14.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]