| 1 |
#' Curl connection interface |
| 2 |
#' |
| 3 |
#' Drop-in replacement for base [url()] that supports https, ftps, |
| 4 |
#' gzip, deflate, etc. Default behavior is identical to [url()], but |
| 5 |
#' request can be fully configured by passing a custom [handle()]. |
| 6 |
#' |
| 7 |
#' As of version 2.3 curl connections support `open(con, blocking = FALSE)`. |
| 8 |
#' In this case `readBin` and `readLines` will return immediately with data |
| 9 |
#' that is available without waiting. For such non-blocking connections the caller |
| 10 |
#' needs to call [isIncomplete()] to check if the download has completed |
| 11 |
#' yet. |
| 12 |
#' |
| 13 |
#' @useDynLib curl R_curl_connection |
| 14 |
#' @export |
| 15 |
#' @param url character string. See examples. |
| 16 |
#' @param open character string. How to open the connection if it should be opened |
| 17 |
#' initially. Currently only "r" and "rb" are supported. |
| 18 |
#' @param handle a curl handle object |
| 19 |
#' @examples \dontrun{ |
| 20 |
#' con <- curl("https://hb.cran.dev/get") |
| 21 |
#' readLines(con) |
| 22 |
#' |
| 23 |
#' # Auto-opened connections can be recycled |
| 24 |
#' open(con, "rb") |
| 25 |
#' bin <- readBin(con, raw(), 999) |
| 26 |
#' close(con) |
| 27 |
#' rawToChar(bin) |
| 28 |
#' |
| 29 |
#' # HTTP error |
| 30 |
#' curl("https://hb.cran.dev/status/418", "r") |
| 31 |
#' |
| 32 |
#' # Follow redirects |
| 33 |
#' readLines(curl("https://hb.cran.dev/redirect/3")) |
| 34 |
#' |
| 35 |
#' # Error after redirect |
| 36 |
#' curl("https://hb.cran.dev/redirect-to?url=https://hb.cran.dev/status/418", "r") |
| 37 |
#' |
| 38 |
#' # Auto decompress Accept-Encoding: gzip / deflate (rfc2616 #14.3) |
| 39 |
#' readLines(curl("https://hb.cran.dev/gzip")) |
| 40 |
#' readLines(curl("https://hb.cran.dev/deflate")) |
| 41 |
#' |
| 42 |
#' # Binary support |
| 43 |
#' buf <- readBin(curl("https://hb.cran.dev/bytes/98765", "rb"), raw(), 1e5) |
| 44 |
#' length(buf) |
| 45 |
#' |
| 46 |
#' # Read file from disk |
| 47 |
#' test <- paste0("file://", system.file("DESCRIPTION")) |
| 48 |
#' readLines(curl(test)) |
| 49 |
#' |
| 50 |
#' # Other protocols |
| 51 |
#' read.csv(curl("ftp://cran.r-project.org/pub/R/CRAN_mirrors.csv")) |
| 52 |
#' readLines(curl("ftps://test.rebex.net:990/readme.txt")) |
| 53 |
#' readLines(curl("gopher://quux.org/1")) |
| 54 |
#' |
| 55 |
#' # Streaming data |
| 56 |
#' con <- curl("http://jeroen.github.io/data/diamonds.json", "r") |
| 57 |
#' while(length(x <- readLines(con, n = 5))){ |
| 58 |
#' print(x) |
| 59 |
#' } |
| 60 |
#' |
| 61 |
#' # Stream large dataset over https with gzip |
| 62 |
#' library(jsonlite) |
| 63 |
#' con <- gzcon(curl("https://jeroen.github.io/data/nycflights13.json.gz")) |
| 64 |
#' nycflights <- stream_in(con) |
| 65 |
#' } |
| 66 |
#' |
| 67 |
curl <- function(url = "https://hb.cran.dev/get", open = "", handle = new_handle()){ |
| 68 |
curl_connection(url, open, handle) |
| 69 |
} |
| 70 |
|
| 71 |
# 'stream' currently only used for non-blocking connections to prevent |
| 72 |
# busy looping in curl_fetch_stream() |
| 73 |
curl_connection <- function(url, mode, handle, partial = FALSE){ |
| 74 |
con <- .Call(R_curl_connection, url, handle, partial) |
| 75 |
if(!identical(mode, "")){ |
| 76 |
withCallingHandlers(open(con, open = mode), error = function(err) { |
| 77 |
close(con) |
| 78 |
}) |
| 79 |
} |
| 80 |
return(con) |
| 81 |
} |