Skip to content

Commit

Permalink
Use same http options as pak/pkgcache
Browse files Browse the repository at this point in the history
In particular the path to the CA bundle, for
r-lib/pak#693.
  • Loading branch information
gaborcsardi committed Sep 24, 2024
1 parent d613795 commit 04ef802
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
26 changes: 24 additions & 2 deletions R/http.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
get_default_curl_options <- function(options) {
getopt <- function(nm) {
if (!is.null(v <- options[[nm]])) return(v)
anm <- paste0("async_http_", nm)
if (!is.null(v <- getOption(anm))) return(v)
if (!is.na(v <- Sys.getenv(toupper(anm), NA_character_))) return (v)
}
modifyList(
options,
drop_nulls(list(
timeout = as.integer(getopt("timeout") %||% 0),
connecttimeout = as.integer(getopt("connecttimeout") %||% 300),
low_speed_time = as.integer(getopt("low_speed_time") %||% 0),
low_speed_limit = as.integer(getopt("low_speed_limit") %||% 0),
cainfo = getopt("cainfo")
))
)
}

http_get <- function(url) {
curl::curl_fetch_memory(url)
http_get <- function(url, options = list()) {
handle <- curl::new_handle(url = url)
options <- get_default_curl_options(options)
curl::handle_setopt(handle, .list = options)
curl::curl_fetch_memory(url, handle = handle)
}

http_post <- function(url, body, headers = character(), options = list()) {
if (!is.raw(body)) body <- charToRaw(body)
handle <- curl::new_handle(url = url)
curl::handle_setheaders(handle, .list = headers)
options <- get_default_curl_options(options)
curl::handle_setopt(
handle,
customrequest = "POST",
Expand Down
18 changes: 11 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ contains <- function(x, y) y %in% x
isin <- function(x, y) x %in% y

remove_special <- function(list, level = 1) {

assert_that(is_positive_count(level))

if (level == 1) {
replace(
grepl(pattern = "^_", names(list)),
Expand All @@ -92,7 +92,7 @@ remove_special <- function(list, level = 1) {
} else {
lapply(list, remove_special, level = level - 1)
}

}

pluck <- function(list, idx) list[[idx]]
Expand All @@ -104,25 +104,25 @@ needs_packages <- function(pkgs) {

if (!all(has)) {
not_installed_pkgs <- pkgs[!has]

if (length(not_installed_pkgs) == 1) {

throw(new_error(
"The ",
sQuote(not_installed_pkgs),
" package is needed for this addin.",
call. = FALSE
))
} else {

throw(new_error(
"The ",
paste(sQuote(not_installed_pkgs), collapse = ", "),
" packages are needed for this addin.",
call. = FALSE
))
}

}
}

Expand All @@ -133,3 +133,7 @@ clean_description <- function(txt) {
zap_null <- function(x) {
x[! map_lgl(x, is.null)]
}

drop_nulls <- function (x) {
x[!map_lgl(x, is.null)]
}

0 comments on commit 04ef802

Please sign in to comment.