Skip to content

Commit 2f98a5d

Browse files
committed
add redirects support to ncurl
1 parent 3fdde2e commit 2f98a5d

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# nanonext 0.1.0.9000 (under development)
22

33
* Add `ncurl()` minimalistic http(s) client.
4-
* Allow setting the environment variable 'NANONEXT_TLS=1' prior to package installation to enable TLS where the system NNG library has been built with TLS support (using Mbed TLS).
4+
* Allow setting the environment variable 'NANONEXT_TLS' prior to package installation to enable TLS where the system NNG library has been built with TLS support (using Mbed TLS).
55

66
# nanonext 0.1.0
77

R/utils.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#'
99
#' @section TLS Support:
1010
#'
11-
#' The environment variable 'NANONEXT_TLS=1' may be set, e.g. by using
11+
#' The environment variable 'NANONEXT_TLS' may be set, e.g. by
1212
#' \code{Sys.setenv(NANONEXT_TLS=1)}, prior to package installation to enable
1313
#' TLS where the system NNG library has been built with TLS support (using
1414
#' Mbed TLS). Note: this is not applicable to Windows systems.
@@ -54,19 +54,22 @@ nng_error <- function(error) {
5454
#' @return Named list of 2 elements: 'raw' containing a raw vector of the received
5555
#' resource and 'data', the raw vector converted to a character string.
5656
#'
57-
#' @details Only performs HTTP GET operations, and does not follow HTTP redirects.
57+
#' @details In interactive sessions, will prompt upon receiving a redirect
58+
#' location whether to follow or not (default is Yes). In non-interactive
59+
#' sessions, redirects are never followed.
5860
#'
5961
#' The raw vector may be saved to a file using \code{\link{writeBin}} to
6062
#' re-create the original resource.
6163
#'
6264
#' The data vector is a character string allowing further parsing within R,
63-
#' as HTML, JSON etc.
65+
#' as HTML, JSON etc. if the served content was a valid text format, or NULL
66+
#' otherwise, e.g. content was a binary file).
6467
#'
6568
#' @section TLS Support:
6669
#'
6770
#' Connecting to secure https sites is supported if your version of the NNG
6871
#' library was built with TLS support (using Mbed TLS) and the environment
69-
#' variable NANONEXT_TLS=1 was set when installing the package e.g. by
72+
#' variable 'NANONEXT_TLS' was set when installing the package e.g. by
7073
#' \code{Sys.setenv(NANONEXT_TLS=1)}. Note: not applicable for Windows systems.
7174
#'
7275
#' @export
@@ -78,8 +81,13 @@ ncurl <- function(http) {
7881
if (is.integer(res)) {
7982
message(res, " : ", nng_error(res))
8083
return(invisible(res))
84+
} else if (is.character(res)) {
85+
continue <- if (interactive()) readline(paste0("Follow redirect to <", res, ">? [Y/n] ")) else "n"
86+
continue %in% c("n", "N", "no", "NO") && return(invisible(res))
87+
return(ncurl(res))
8188
}
82-
list(raw = res, data = rawToChar(res))
89+
data <- tryCatch(rawToChar(res), error = function(e) NULL)
90+
list(raw = res, data = data)
8391

8492
}
8593

man/ncurl.Rd

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/nng_version.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,18 @@ SEXP rnng_ncurl(SEXP http) {
114114

115115
code = nng_http_res_get_status(res);
116116
if (code != 200)
117-
REprintf("HTTP Server Responded: %d %s\n", code, nng_http_res_get_reason(res));
117+
REprintf("HTTP Server Response: %d %s\n", code, nng_http_res_get_reason(res));
118+
if (code >= 300 && code < 400) {
119+
const char *location = nng_http_res_get_header(res, "Location");
120+
SEXP ret = Rf_mkString(location);
121+
if (tls)
122+
nng_tls_config_free(cfg);
123+
nng_http_res_free(res);
124+
nng_http_req_free(req);
125+
nng_http_client_free(client);
126+
nng_url_free(url);
127+
return ret;
128+
}
118129

119130
nng_http_res_get_data(res, &data, &sz);
120131
SEXP vec = PROTECT(Rf_allocVector(RAWSXP, sz));

0 commit comments

Comments
 (0)