Skip to content

Commit

Permalink
Internally simplify handle_setheader() as a wrapper for handle_setopt…
Browse files Browse the repository at this point in the history
…(httpheader)
  • Loading branch information
jeroen committed Jan 30, 2024
1 parent 683784c commit e1341b9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Suggests:
VignetteBuilder: knitr
Depends:
R (>= 3.0.0)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ useDynLib(curl,R_handle_getcustom)
useDynLib(curl,R_handle_getheaders)
useDynLib(curl,R_handle_reset)
useDynLib(curl,R_handle_setform)
useDynLib(curl,R_handle_setheaders)
useDynLib(curl,R_handle_setopt)
useDynLib(curl,R_multi_add)
useDynLib(curl,R_multi_cancel)
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- In handle_setheader(), setting a header to a non-empty whitespace string will
now send a blank header. Using an empty string will still remove the header
altogether (this has not changed).
- Internally simplify handle_setheader() as a wrapper for handle_setopt(httpheader)
- Fix an issue where curl_fetch_stream() might not return all bytes received
by the C layer if the server closed the connection mid-response.
- No longer enable CURLOPT_UNRESTRICTED_AUTH by default
Expand Down
20 changes: 10 additions & 10 deletions R/handle.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ handle_setopt <- function(handle, ..., .list = list()){
}

#' @export
#' @useDynLib curl R_handle_setheaders
#' @rdname handle
handle_setheaders <- function(handle, ..., .list = list()){
stopifnot(inherits(handle, "curl_handle"))
opts <- c(list(...), .list)
if(!all(vapply(opts, is.character, logical(1)))){
x <- c(list(...), .list)
if(!all(vapply(x, is.character, logical(1)))){
stop("All headers must be strings.")
}
opts$Expect = ""
names <- names(opts)
values <- as.character(unlist(opts))
handle_setopt(handle, httpheader = format_request_headers(x))
}

format_request_headers <- function(x){
x$Expect = ""
names <- names(x)
values <- as.character(unlist(x))
postfix <- ifelse(grepl("^\\s+$", values), ";", paste(":", values))
headers <- paste0(names, postfix)
.Call(R_handle_setheaders, handle, headers)
invisible(handle)
paste0(names, postfix)
}

#' @useDynLib curl R_handle_getheaders
Expand Down
11 changes: 3 additions & 8 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,6 @@ SEXP R_handle_reset(SEXP ptr){
return ScalarLogical(1);
}

SEXP R_handle_setheaders(SEXP ptr, SEXP vec){
if(!isString(vec))
error("header vector must be a string.");
set_headers(get_ref(ptr), vec_to_slist(vec));
return ScalarLogical(1);
}

SEXP R_handle_getheaders(SEXP ptr){
reference *ref = get_ref(ptr);
return slist_to_vec(ref->headers);
Expand Down Expand Up @@ -327,7 +320,9 @@ SEXP R_handle_setopt(SEXP ptr, SEXP keys, SEXP values){
const char * url_utf8 = translateCharUTF8(STRING_ELT(val, 0));
assert(curl_easy_setopt(handle, CURLOPT_URL, url_utf8));
} else if(key == CURLOPT_HTTPHEADER){
R_handle_setheaders(ptr, val);
if(!isString(val))
error("Value for option %s (%d) must be a string vector", optname, key);
set_headers(get_ref(ptr), vec_to_slist(val));
} else if (r_curl_is_slist_option(key)) {
if(!isString(val))
error("Value for option %s (%d) must be a string vector", optname, key);
Expand Down
1 change: 0 additions & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ static const R_CallMethodDef CallEntries[] = {
{"R_handle_getheaders", (DL_FUNC) &R_handle_getheaders, 1},
{"R_handle_reset", (DL_FUNC) &R_handle_reset, 1},
{"R_handle_setform", (DL_FUNC) &R_handle_setform, 2},
{"R_handle_setheaders", (DL_FUNC) &R_handle_setheaders, 2},
{"R_handle_setopt", (DL_FUNC) &R_handle_setopt, 3},
{"R_option_types", (DL_FUNC) &R_option_types, 0},
{"R_multi_add", (DL_FUNC) &R_multi_add, 5},
Expand Down

0 comments on commit e1341b9

Please sign in to comment.