|
1 | | -default_http_version <- function() { |
2 | | - os <- Sys.info()["sysname"] |
3 | | - if (!is.na(os) && os %in% c("Darwin", "Linux")) { |
4 | | - # FIXME: when is it safe to remove this? Does it depend on the OS |
5 | | - # version? The libcurl version? |
6 | | - # UPDATE: HTTP/2 now also causes issues on Linux: |
7 | | - # https://github.com/r-lib/pak/issues/358 |
8 | | - # https://github.com/r-lib/actions/issues/483 |
9 | | - # So this will be here for now. :( |
10 | | - 2 # HTTP 1.1 |
11 | | - } else { |
12 | | - 0 # whatever curl chooses |
13 | | - } |
14 | | -} |
| 1 | +# The embedded async `http_get()` etc. only look at the `async_http_*` options |
| 2 | +# (via `get_default_curl_options()`). `set_pkgcache_curl_options()` reads the |
| 3 | +# pkgcache-level options and environment variables and sets the corresponding |
| 4 | +# curl options, so that pkgcache's HTTP requests honor them. For each curl |
| 5 | +# option it looks, in decreasing priority, at the `pkgcache_*` option, the |
| 6 | +# `PKGCACHE_*` environment variable, the `pkg_http_*` option and the |
| 7 | +# `PKG_HTTP_*` environment variable. |
| 8 | +# |
| 9 | +# It only sets an option when it is actually configured. An unset option must |
| 10 | +# not be turned into an explicit curl option, otherwise it would override the |
| 11 | +# `async_http_*` option and the async default further downstream in |
| 12 | +# `get_default_curl_options()`. |
15 | 13 |
|
16 | | -#' @importFrom utils modifyList |
17 | | - |
18 | | -update_async_timeouts <- function(options) { |
| 14 | +set_pkgcache_curl_options <- function(options) { |
| 15 | + nms <- c( |
| 16 | + "timeout", |
| 17 | + "connecttimeout", |
| 18 | + "low_speed_time", |
| 19 | + "low_speed_limit", |
| 20 | + "http_version" |
| 21 | + ) |
| 22 | + prefixes <- c("pkgcache_", "pkg_http_") |
19 | 23 | getopt <- function(nm) { |
20 | | - if (!is.null(v <- options[[nm]])) { |
21 | | - return(v) |
| 24 | + for (prefix in prefixes) { |
| 25 | + anm <- paste0(prefix, nm) |
| 26 | + if (!is.null(v <- getOption(anm))) { |
| 27 | + return(v) |
| 28 | + } |
| 29 | + if (!is.na(v <- Sys.getenv(toupper(anm), NA_character_))) { |
| 30 | + return(v) |
| 31 | + } |
| 32 | + } |
| 33 | + NULL |
| 34 | + } |
| 35 | + for (nm in nms) { |
| 36 | + # An option passed explicitly to the request always wins. |
| 37 | + if (!is.null(options[[nm]])) { |
| 38 | + next |
22 | 39 | } |
23 | | - anm <- paste0("pkgcache_", nm) |
24 | | - if (!is.null(v <- getOption(anm))) { |
25 | | - return(v) |
| 40 | + if (!is.null(v <- getopt(nm))) { |
| 41 | + options[[nm]] <- as.integer(v) |
26 | 42 | } |
27 | | - if (!is.na(v <- Sys.getenv(toupper(anm), NA_character_))) return(v) |
28 | 43 | } |
29 | | - utils::modifyList( |
30 | | - options, |
31 | | - list( |
32 | | - timeout = as.integer(getopt("timeout") %||% 0), |
33 | | - connecttimeout = as.integer(getopt("connecttimeout") %||% 300), |
34 | | - low_speed_time = as.integer(getopt("low_speed_time") %||% 0), |
35 | | - low_speed_limit = as.integer(getopt("low_speed_limit") %||% 0), |
36 | | - http_version = as.integer( |
37 | | - getopt("http_version") %||% default_http_version() |
38 | | - ) |
39 | | - ) |
40 | | - ) |
| 44 | + options |
41 | 45 | } |
42 | 46 |
|
| 47 | +# Wrap an embedded async HTTP function so that it applies the pkgcache-level |
| 48 | +# HTTP options and environment variables (see `set_pkgcache_curl_options()`) |
| 49 | +# to every request. |
| 50 | +wrap_pkgcache_http <- function(fun) { |
| 51 | + force(fun) |
| 52 | + mark_as_async(function(url, ..., options = list()) { |
| 53 | + fun(url, ..., options = set_pkgcache_curl_options(options)) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +http_get <- wrap_pkgcache_http(http_get) |
| 58 | +http_head <- wrap_pkgcache_http(http_head) |
| 59 | +http_post <- wrap_pkgcache_http(http_post) |
| 60 | +http_delete <- wrap_pkgcache_http(http_delete) |
| 61 | + |
43 | 62 | add_auth_header <- function(url, headers) { |
44 | 63 | c(headers, repo_auth_headers(url)$headers) |
45 | 64 | } |
@@ -127,7 +146,6 @@ download_file <- function( |
127 | 146 | ) |
128 | 147 | force(list(...)) |
129 | 148 |
|
130 | | - options <- update_async_timeouts(options) |
131 | 149 | destfile <- normalizePath(destfile, mustWork = FALSE) |
132 | 150 | tmp_destfile <- normalizePath(tmp_destfile, mustWork = FALSE) |
133 | 151 | mkdirp(dirname(tmp_destfile)) |
@@ -259,7 +277,6 @@ download_if_newer <- function( |
259 | 277 | ) |
260 | 278 | force(list(...)) |
261 | 279 |
|
262 | | - options <- update_async_timeouts(options) |
263 | 280 | etag_old <- get_etag_header_from_file(destfile, etag_file) |
264 | 281 | headers <- c(headers, etag_old) |
265 | 282 | headers <- add_auth_header(url, headers) |
@@ -390,7 +407,6 @@ download_one_of <- function( |
390 | 407 | ) |
391 | 408 | force(list(...)) |
392 | 409 |
|
393 | | - options <- update_async_timeouts(options) |
394 | 410 | tmps <- paste0(destfile, ".tmp.", seq_along(urls)) |
395 | 411 | dls <- mapply( |
396 | 412 | download_if_newer, |
@@ -428,7 +444,6 @@ download_files <- function( |
428 | 444 | ) |
429 | 445 | } |
430 | 446 |
|
431 | | - options <- update_async_timeouts(options) |
432 | 447 | bar <- create_progress_bar(data) |
433 | 448 | prog_cb <- function(upd) update_progress_bar_progress(bar, upd) |
434 | 449 |
|
|
0 commit comments