| 1 |
#' Fetch the contents of a URL |
| 2 |
#' |
| 3 |
#' Low-level bindings to write data from a URL into memory, disk or a callback |
| 4 |
#' function. |
| 5 |
#' |
| 6 |
#' The `curl_fetch_*()` functions automatically raise an error upon protocol problems |
| 7 |
#' (network, disk, TLS, etc.) but do not implement application logic. For example, |
| 8 |
#' you need to check the status code of HTTP requests in the response by yourself, |
| 9 |
#' and deal with it accordingly. |
| 10 |
#' |
| 11 |
#' Both `curl_fetch_memory()` and `curl_fetch_disk` have a blocking and a |
| 12 |
#' non-blocking C implementation. The latter is slightly slower but allows for |
| 13 |
#' interrupting the download prematurely (using e.g. CTRL+C or ESC). Interrupting |
| 14 |
#' is enabled when R runs in interactive mode or when |
| 15 |
#' `getOption("curl_interrupt") == TRUE`. |
| 16 |
#' |
| 17 |
#' The `curl_fetch_multi()` function is the asynchronous equivalent of |
| 18 |
#' `curl_fetch_memory()`. It wraps [`multi_add()`][multi_add] to |
| 19 |
#' schedule requests which are executed concurrently when calling |
| 20 |
#' [`multi_run()`][multi_run]\code{}. For each successful request, the |
| 21 |
#' `done` callback is triggered with response data. For failed requests |
| 22 |
#' (when `curl_fetch_memory()` would raise an error), the `fail` function |
| 23 |
#' is triggered with the error message. |
| 24 |
#' |
| 25 |
#' After a request has been performed, metadata from the request can be read |
| 26 |
#' from the handle object using `handle_data()` (this same information also gets |
| 27 |
#' returned by `curl_fetch_memory()` directly). It includes things like: |
| 28 |
#' - Final URL (after redirects) |
| 29 |
#' - HTTP status code |
| 30 |
#' - Content-type |
| 31 |
#' - Response headers |
| 32 |
#' - Timings |
| 33 |
#' - Http-version |
| 34 |
#' This data remains available in the handle until it is either re-used for a |
| 35 |
#' new request, or `handle_reset()` is called. |
| 36 |
#' |
| 37 |
#' @param url A character string naming the URL of a resource to be downloaded. |
| 38 |
#' @param handle A curl handle object. |
| 39 |
#' @export |
| 40 |
#' @rdname curl_fetch |
| 41 |
#' @useDynLib curl R_curl_fetch_memory |
| 42 |
#' @examples |
| 43 |
#' \donttest{ |
| 44 |
#' # Load in memory |
| 45 |
#' res <- curl_fetch_memory("https://hb.cran.dev/cookies/set?foo=123&bar=ftw") |
| 46 |
#' res$content |
| 47 |
#' |
| 48 |
#' # Save to disk |
| 49 |
#' res <- curl_fetch_disk("https://hb.cran.dev/stream/10", tempfile()) |
| 50 |
#' res$content |
| 51 |
#' readLines(res$content) |
| 52 |
#' |
| 53 |
#' # Stream with callback |
| 54 |
#' drip_url <- "https://hb.cran.dev/drip?duration=3&numbytes=15&code=200" |
| 55 |
#' res <- curl_fetch_stream(drip_url, function(x){ |
| 56 |
#' cat(rawToChar(x)) |
| 57 |
#' }) |
| 58 |
#' |
| 59 |
#' # Async API |
| 60 |
#' data <- list() |
| 61 |
#' success <- function(res){ |
| 62 |
#' cat("Request done! Status:", res$status, "\n") |
| 63 |
#' data <<- c(data, list(res)) |
| 64 |
#' } |
| 65 |
#' failure <- function(msg){ |
| 66 |
#' cat("Oh noes! Request failed!", msg, "\n") |
| 67 |
#' } |
| 68 |
#' curl_fetch_multi("https://hb.cran.dev/get", success, failure) |
| 69 |
#' curl_fetch_multi("https://hb.cran.dev/status/418", success, failure) |
| 70 |
#' curl_fetch_multi("https://urldoesnotexist.xyz", success, failure) |
| 71 |
#' multi_run() |
| 72 |
#' str(data) |
| 73 |
#' } |
| 74 |
curl_fetch_memory <- function(url, handle = new_handle()){ |
| 75 |
nonblocking <- isTRUE(getOption("curl_interrupt", TRUE)) |
| 76 |
output <- .Call(R_curl_fetch_memory, enc2utf8(url), handle, nonblocking) |
| 77 |
res <- handle_data(handle) |
| 78 |
res$content <- output |
| 79 |
res |
| 80 |
} |
| 81 |
|
| 82 |
#' @export |
| 83 |
#' @param path Path to save results |
| 84 |
#' @rdname curl_fetch |
| 85 |
#' @useDynLib curl R_curl_fetch_disk |
| 86 |
curl_fetch_disk <- function(url, path, handle = new_handle()){ |
| 87 |
nonblocking <- isTRUE(getOption("curl_interrupt", TRUE)) |
| 88 |
path <- enc2native(normalizePath(path, mustWork = FALSE)) |
| 89 |
output <- .Call(R_curl_fetch_disk, enc2utf8(url), handle, path, "wb", nonblocking) |
| 90 |
res <- handle_data(handle) |
| 91 |
res$content <- output |
| 92 |
res |
| 93 |
} |
| 94 |
|
| 95 |
#' @export |
| 96 |
#' @param fun Callback function. Should have one argument, which will be |
| 97 |
#' a raw vector. |
| 98 |
#' @rdname curl_fetch |
| 99 |
#' @useDynLib curl R_curl_connection |
| 100 |
curl_fetch_stream <- function(url, fun, handle = new_handle()){ |
| 101 |
# Blocking = TRUE and partial = TRUE to prevent busy-waiting |
| 102 |
con <- curl_connection(url, mode = "", handle = handle, partial = TRUE) |
| 103 |
|
| 104 |
# 'f' means: do not error for status code |
| 105 |
open(con, "rbf") |
| 106 |
on.exit(close(con)) |
| 107 |
while(isIncomplete(con)){ |
| 108 |
buf <- readBin(con, raw(), 32768L) |
| 109 |
if(length(buf)) |
| 110 |
fun(buf) |
| 111 |
} |
| 112 |
handle_data(handle) |
| 113 |
} |
| 114 |
|
| 115 |
#' @export |
| 116 |
#' @rdname curl_fetch |
| 117 |
#' @inheritParams multi |
| 118 |
#' @useDynLib curl R_curl_connection |
| 119 |
curl_fetch_multi <- function(url, done = NULL, fail = NULL, pool = NULL, |
| 120 |
data = NULL, handle = new_handle()){ |
| 121 |
handle_setopt(handle, url = enc2utf8(url)) |
| 122 |
multi_add(handle = handle, done = done, fail = fail, data = data, pool = pool) |
| 123 |
invisible(handle) |
| 124 |
} |
| 125 |
|
| 126 |
#' @export |
| 127 |
#' @rdname curl_fetch |
| 128 |
curl_fetch_echo <- function(url, handle = new_handle()){ |
| 129 |
handle_setopt(handle, url = enc2utf8(url)) |
| 130 |
curl_echo(handle) |
| 131 |
} |
| 132 |
|
| 133 |
#' @export |
| 134 |
#' @rdname curl_fetch |
| 135 |
#' @useDynLib curl R_get_handle_response |
| 136 |
handle_data <- function(handle){ |
| 137 |
stopifnot(inherits(handle, "curl_handle")) |
| 138 |
out <- .Call(R_get_handle_response, handle) |
| 139 |
out$content = NULL |
| 140 |
out |
| 141 |
} |