-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathlibcurl.R
109 lines (89 loc) · 3.07 KB
/
libcurl.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
## These are tests that require libcurl functionality and a working
## Internet connection.
if(!capabilities()["libcurl"]) {
warning("no libcurl support")
q()
}
## fails some of the time
#if(.Platform$OS.type == "windows") q()
if(.Platform$OS.type == "unix" &&
is.null(nsl("cran.r-project.org"))) q()
example(curlGetHeaders, run.donttest = TRUE)
tf <- tempfile()
download.file("http://cran.r-project.org/", tf, method = "libcurl")
file.size(tf)
unlink(tf)
tf <- tempfile()
download.file("ftp://ftp.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat",
tf, method = "libcurl")
file.size(tf) # 2102
unlink(tf)
## test url connections on http
str(readLines(zz <- url("http://cran.r-project.org/", method = "libcurl")))
zz
stopifnot(identical(summary(zz)$class, "url-libcurl"))
close(zz)
## https URL
head(readLines(zz <- url("https://httpbin.org", method = "libcurl"),
warn = FALSE))
close(zz)
## redirection (to a https:// URL)
head(readLines(zz <- url("http://bugs.r-project.org", method = "libcurl"),
warn = FALSE))
close(zz)
## check graceful failure: warnings leading to error
## testUnknownUrlError <- tryCatch(suppressWarnings({
## zz <- url("http://foo.bar", "r", method = "libcurl")
## }), error=function(e) {
## conditionMessage(e) == "cannot open connection"
## })
## close(zz)
## stopifnot(testUnknownUrlError)
## tf <- tempfile()
## testDownloadFileError <- tryCatch(suppressWarnings({
## download.file("http://foo.bar", tf, method="libcurl")
## }), error=function(e) {
## conditionMessage(e) == "cannot download all files"
## })
## stopifnot(testDownloadFileError, !file.exists(tf))
tf <- tempfile()
testDownloadFile404 <- tryCatch(suppressWarnings({
download.file("http://httpbin.org/status/404", tf, method="libcurl")
}), error=function(e) {
conditionMessage(e) == "cannot download all files"
})
stopifnot(testDownloadFile404, !file.exists(tf))
## check specific warnings
## testUnknownUrl <- tryCatch({
## zz <- url("http://foo.bar", "r", method = "libcurl")
## }, warning=function(e) {
## grepl("Couldn't resolve host name", conditionMessage(e))
## })
## close(zz)
## stopifnot(testUnknownUrl)
test404.1 <- tryCatch({
open(zz <- url("http://httpbin.org/status/404", method="libcurl"))
}, warning=function(w) {
grepl("404 Not Found", conditionMessage(w))
})
close(zz)
stopifnot(test404.1)
## via read.table (which closes the connection)
tail(read.table(url("http://www.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat",
method = "libcurl")))
tail(read.table(url("ftp://ftp.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat",
method = "libcurl")))
## check option works
options(url.method = "libcurl")
zz <- url("http://www.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat")
stopifnot(identical(summary(zz)$class, "url-libcurl"))
close(zz)
head(readLines("https://httpbin.org", warn = FALSE))
test404.2 <- tryCatch({
open(zz <- url("http://httpbin.org/status/404"))
}, warning=function(w) {
grepl("404 Not Found", conditionMessage(w))
})
close(zz)
stopifnot(test404.2)
showConnections(all = TRUE)