This is the mail archive of the
cygwin-apps
mailing list for the Cygwin project.
[PATCH setup 5/9] Allow user-agent string to be customized
- From: Jon Turney <jon dot turney at dronecode dot org dot uk>
- To: cygwin-apps at cygwin dot com
- Cc: Jon Turney <jon dot turney at dronecode dot org dot uk>
- Date: Tue, 6 Jun 2017 12:54:28 +0100
- Subject: [PATCH setup 5/9] Allow user-agent string to be customized
- Authentication-results: sourceware.org; auth=none
- References: <20170606115432.105020-1-jon.turney@dronecode.org.uk>
If the option is present without a string, this means that no user-agent
header should be added.
Also include version in default user agent string
v2:
Add logging of User-Agent: header override
v3:
Be more careful about scope User-Agent std::string object
---
nio-ie5.cc | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/nio-ie5.cc b/nio-ie5.cc
index 7708d4c..24d2c13 100644
--- a/nio-ie5.cc
+++ b/nio-ie5.cc
@@ -27,6 +27,11 @@
#include "netio.h"
#include "nio-ie5.h"
#include "LogSingleton.h"
+#include "setup_version.h"
+#include "getopt++/StringOption.h"
+
+static StringOption UserAgent ("", '\0', "user-agent", "User agent string for HTTP requests");
+static std::string default_useragent = std::string("Cygwin-Setup/") + setup_version;
static HINTERNET internet_direct = 0;
static HINTERNET internet_preconfig = 0;
@@ -45,7 +50,27 @@ NetIO (_url)
if (*internet == 0)
{
InternetAttemptConnect (0);
- *internet = InternetOpen ("Cygwin Setup",
+
+ const char *lpszAgent = 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,
direct ? INTERNET_OPEN_TYPE_DIRECT : INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
}
--
2.12.3