-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathinternet2.R
55 lines (48 loc) · 1.93 KB
/
internet2.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
## These are tests that require socket and internet functionality, and
## a working Internet connection.
## We attempt to test for those.
if(.Platform$OS.type == "unix" &&
is.null(nsl("cran.r-project.org"))) q()
## check graceful failure:
try(url("http://foo.bar", "r"))
if(.Platform$OS.type == "windows")
try(url("http://foo.bar", "r", method = "wininet"))
## everything from here on is directly over sockets
if(!capabilities("sockets")) stop("no socket capabilities")
# do the same thing via sockets (cut-down httpclient)
httpget <- function (url, port = 80)
{
urlel <- strsplit(url, "/")[[1]]
if (urlel[1] != "http:") stop("Not an http:// URL")
host <- urlel[3]
rurl <- paste(c("", urlel[-(1:3)]), collapse = "/")
a <- make.socket(host, port = port)
on.exit(close.socket(a))
headreq <- paste("HEAD", rurl, "HTTP/1.0\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n")
write.socket(a, headreq)
head <- read.socket(a, maxlen = 8000)
b <- strsplit(head, "\n")[[1]]
if (length(grep("200 OK", b[1])) == 0) stop(b[1])
len <- as.numeric(strsplit(grep("Content-Length", b, value = TRUE),
":")[[1]][2])
getreq <- paste("GET", rurl, "HTTP/1.0\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n")
write.socket(a, getreq)
junk <- read.socket(a, maxlen = nchar(head))
data <- ""
b <- strsplit(c(head, junk), "\n")
nn <- length(b[[1]])
if (length(b[[2]]) > nn)
data <- paste(b[[2]][-(1:nn)], collapse = "\n")
while (nchar(data) < len) {
data <- paste(data, read.socket(a, maxlen = len - nchar(data)),
sep = "")
}
strsplit(data, "\n")[[1]]
}
if(nzchar(Sys.getenv("http_proxy")) || nzchar(Sys.getenv("HTTP_PROXY"))) {
cat("http proxy is set, so skip test of http over sockets\n")
} else {
f <- httpget("http://www.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat")
str(f)
stopifnot(length(f) == 100L)
}