From def075db88ae87104c38437a57e9327b078bb804 Mon Sep 17 00:00:00 2001 From: ripley Date: Wed, 6 Mar 2019 12:29:44 +0000 Subject: [PATCH] change the default HTTP user agent when libcurl is used git-svn-id: https://svn.r-project.org/R/trunk@76204 00db46b3-68df-0310-9c12-caf00c1e9a41 --- doc/NEWS.Rd | 5 +++++ src/library/base/man/options.Rd | 13 ++++++++----- src/modules/internet/libcurl.c | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/doc/NEWS.Rd b/doc/NEWS.Rd index b1cab3d97ee..e6fe6126be0 100644 --- a/doc/NEWS.Rd +++ b/doc/NEWS.Rd @@ -302,6 +302,11 @@ \item \code{stopifnot()} has been simplified thanks to Suharto Anggono's proposals to become considerably faster for cheap expressions. + + \item The default \sQuote{user agent} has been changed when + accessing \samp{http://} and \samp{https://} sites using + \samp{libcurl}. (A site was found which caused \samp{libcurl} to + infinite-loop with the previous default.) } } diff --git a/src/library/base/man/options.Rd b/src/library/base/man/options.Rd index 58f0e88672b..34b396a2e79 100644 --- a/src/library/base/man/options.Rd +++ b/src/library/base/man/options.Rd @@ -1,6 +1,6 @@ % File src/library/base/man/options.Rd % Part of the R package, https://www.R-project.org -% Copyright 1995-2018 R Core Team +% Copyright 1995-2019 R Core Team % Distributed under GPL 2 or later \name{options} @@ -632,10 +632,13 @@ getOption(x, default = NULL) \item{\code{help_type}:}{default for an argument of \code{\link{help}}, used also as the help type by \code{\link{?}}.} - \item{\code{HTTPUserAgent}:}{string used as the user agent in HTTP(S) - requests. If \code{NULL}, requests will be made without a - user agent header. - The default is \code{R ( )} .} + \item{\code{HTTPUserAgent}:}{string used as the \sQuote{user agent} in + HTTP(S) requests by \code{\link{download.file}}, \code{\link{url}} + and \code{\link{curlGetHeaders}}, or \code{NULL} when requests will + be made without a user agent header. The default is + \code{R ( )} + except when \samp{libcurl} is used when it is + \code{libcurl/7..} for the \samp{libcurl} version in use.} \item{\code{install.lock}:}{logical: should per-directory package locking be used by \code{\link{install.packages}}? Most useful diff --git a/src/modules/internet/libcurl.c b/src/modules/internet/libcurl.c index 2b4d8e73d1a..407a71f5150 100644 --- a/src/modules/internet/libcurl.c +++ b/src/modules/internet/libcurl.c @@ -236,6 +236,7 @@ static void curlCommon(CURL *hnd, int redirect, int verify) curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L); } +#if 0 // for consistency, but all utils:::makeUserAgent does is look up an option. SEXP sMakeUserAgent = install("makeUserAgent"); SEXP agentFun = PROTECT(lang2(sMakeUserAgent, ScalarLogical(0))); @@ -246,6 +247,24 @@ static void curlCommon(CURL *hnd, int redirect, int verify) if(TYPEOF(sua) != NILSXP) curl_easy_setopt(hnd, CURLOPT_USERAGENT, CHAR(STRING_ELT(sua, 0))); UNPROTECT(2); +#else + int Default = 1; + SEXP sua = GetOption1(install("HTTPUserAgent")); // set in utils startup + if (TYPEOF(sua) == STRSXP && LENGTH(sua) == 1 ) { + const char *p = CHAR(STRING_ELT(sua, 0)); + if (p[0] && p[1] && p[2] && p[0] == 'R' && p[1] == ' ' && p[2] == '(') { + } else { + Default = 0; + curl_easy_setopt(hnd, CURLOPT_USERAGENT, p); + } + } + if (Default) { + char buf[20]; + curl_version_info_data *d = curl_version_info(CURLVERSION_NOW); + snprintf(buf, 20, "libcurl/%s", d->version); + curl_easy_setopt(hnd, CURLOPT_USERAGENT, buf); + } +#endif int timeout0 = asInteger(GetOption1(install("timeout"))); long timeout = timeout0 = NA_INTEGER ? 0 : 1000L * timeout0; curl_easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT_MS, timeout);