Skip to content

Commit 2013285

Browse files
author
kalibera
committed
Support for .netrc with libcurl (PR#18892).
git-svn-id: https://svn.r-project.org/R/trunk@88391 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 9c4e67f commit 2013285

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

src/library/base/man/connections.Rd

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ socketTimeout(socket, timeout = -1)
172172
\code{"libcurl"} for all others. Which methods support which schemes
173173
has varied by \R version -- currently \code{"internal"} supports only
174174
\samp{file://}; \code{"wininet"} supports \samp{file://},
175-
\samp{http://} and \samp{https://}. Proxies can be specified: see
176-
\code{\link{download.file}}.
175+
\samp{http://} and \samp{https://}. Proxies and user credentials can
176+
be specified: see \code{\link{download.file}}.
177177

178178
For \code{gzfile} the description is the path to a file compressed by
179179
\command{gzip}: it can also open for reading uncompressed files and
@@ -681,8 +681,9 @@ socketTimeout(socket, timeout = -1)
681681
\code{\link{gzcon}} to wrap \command{gzip} (de)compression around a
682682
connection.
683683
684-
\code{\link{options}} \code{HTTPUserAgent}, \code{internet.info} and
685-
\code{timeout} are used by some of the methods for URL connections.
684+
\code{\link{options}} \code{HTTPUserAgent}, \code{internet.info},
685+
\code{netrc} and \code{timeout} are used by some of the methods for URL
686+
connections.
686687
687688
\code{\link{memCompress}} for more ways to (de)compress and references
688689
on data compression.

src/library/base/man/options.Rd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/base/man/options.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2025 R Core Team
3+
% Copyright 1995-2026 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{options}
@@ -264,6 +264,12 @@ getOption(x, default = NULL)
264264
to something in the order of (and typically slightly less than)
265265
\code{max.print} \emph{entries}.}
266266

267+
\item{\code{netrc}:}{character string. Path to netrc file with
268+
user credentials for URLs requiring authentication. The file, if it
269+
exists, will be used by \samp{libcurl} in \code{\link{download.file}},
270+
\code{\link{url}} and \code{\link{curlGetHeaders}}. By default unset,
271+
but can be set from environment variable \env{R_DEFAULT_NETRC}.}
272+
267273
\item{\code{OutDec}:}{character string containing a single
268274
character. The preferred character to be used as the decimal
269275
point in output conversions, that is in printing, plotting,

src/library/profile/Common.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
### Additional commands can be placed in site or user Rprofile files
33
### (see ?Rprofile).
44

5-
### Copyright (C) 1995-2023 The R Core Team
5+
### Copyright (C) 1995-2025 The R Core Team
66

77
### Notice that it is a bad idea to use this file as a template for
88
### personal startup files, since things will be executed twice and in
@@ -33,6 +33,10 @@ local({to <- as.integer(Sys.getenv("R_DEFAULT_INTERNET_TIMEOUT", 60))
3333
if (is.na(to) || to <= 0) to <- 60L
3434
options(timeout = to)
3535
})
36+
local({
37+
if(nzchar(nr <- Sys.getenv("R_DEFAULT_NETRC")))
38+
options(netrc = nr)
39+
})
3640
options(encoding = "native.enc")
3741
options(show.error.messages = TRUE)
3842
## keep in sync with PrintDefaults() in ../../main/print.c :

src/library/utils/man/download.file.Rd

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/utils/man/download.file.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2024 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{download.file}
@@ -133,6 +133,21 @@ download.file(url, destfile, method, quiet = FALSE, mode = "w",
133133
}
134134
(It is unrealistic to require download times of less than 1s/MB.)
135135

136+
A HTTP[S] URL may require authentication. One may specify a username and
137+
password as part of the URL, i.e.
138+
\code{"https://username:password@machine/..."}. This is not recommended
139+
with HTTP as the credentials will be sent in plaintext. With HTTPS, they
140+
will be encrypted, but still may appear in plaintext e.g. in server logs.
141+
Alternatively, the credentials may be specified via \code{Authorization}
142+
in argument \code{headers}, but that is more involved for the user and
143+
wouldn't allow simultaneous download from different URLs requiring
144+
authentication. With \code{"libcurl"}, it is possible to have the
145+
credentials in a netrc file which can be specified by the option
146+
\code{netrc} and the default may be set by the environment variable
147+
\env{R_DEFAULT_NETRC}. The file should not be readable by other users.
148+
See \url{https://everything.curl.dev/usingcurl/netrc.html} for further
149+
details.
150+
136151
The level of detail provided during transfer can be set by the
137152
\code{quiet} argument and the \code{internet.info} option: the details
138153
depend on the platform and scheme. For the \code{"libcurl"} method

src/modules/internet/libcurl.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,16 @@ void curlCommon(CURL *hnd, int redirect, int verify)
316316

317317
// enable the cookie engine, keep cookies in memory
318318
curl_easy_setopt(hnd, CURLOPT_COOKIEFILE, "");
319+
320+
SEXP snetrc = GetOption1(install("netrc"));
321+
if (TYPEOF(snetrc) == STRSXP && LENGTH(snetrc) == 1) {
322+
const void *vmax = vmaxget();
323+
const char *p;
324+
p = R_ExpandFileName(translateCharFP(STRING_ELT(snetrc, 0)));
325+
curl_easy_setopt(hnd, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
326+
curl_easy_setopt(hnd, CURLOPT_NETRC_FILE, p);
327+
vmaxset(vmax);
328+
}
319329
}
320330

321331
static char headers[500][2049]; // allow for terminator

0 commit comments

Comments
 (0)