The R Project SVN R

Rev

Rev 84365 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 84365 Rev 84382
Line 11... Line 11...
11
    if (urlel[1] != "http:") stop("Not an http:// URL")
11
    if (urlel[1] != "http:") stop("Not an http:// URL")
12
    host <- urlel[3]
12
    host <- urlel[3]
13
    rurl <- paste(c("", urlel[-(1:3)]), collapse = "/")
13
    rurl <- paste(c("", urlel[-(1:3)]), collapse = "/")
14
    a <- make.socket(host, port = port)
14
    a <- make.socket(host, port = port)
15
    on.exit(close.socket(a))
15
    on.exit(close.socket(a))
-
 
16
    ## Technically, Host: is not defined in HTTP/1.0, but it is required
-
 
17
    ## to distinguish virtual servers on the same IP address. We could bump
-
 
18
    ## required HTTP version to 1.1, but since HTTP/1.0 defines that
-
 
19
    ## unrecognized headers should be ignored, it is legal as well (and works).
16
    headreq <- paste("HEAD", rurl, "HTTP/1.0\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n")
20
    headreq <- paste0("HEAD ", rurl, " HTTP/1.0\r\nHost: ", host, "\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n")
17
    write.socket(a, headreq)
21
    write.socket(a, headreq)
18
    head <- read.socket(a, maxlen = 8000)
22
    head <- read.socket(a, maxlen = 8000)
19
    b <- strsplit(head, "\n")[[1]]
23
    b <- strsplit(head, "\n")[[1]]
20
    if (length(grep("200 OK", b[1])) == 0) stop(b[1])
24
    if (length(grep("200 OK", b[1])) == 0) stop(b[1])
21
    len <- as.numeric(strsplit(grep("Content-Length", b, value = TRUE),
25
    len <- as.numeric(strsplit(grep("Content-Length", b, value = TRUE),
22
                               ":")[[1]][2])
26
                               ":")[[1]][2])
23
    getreq <- paste("GET", rurl, "HTTP/1.0\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n")
27
    getreq <- paste0("GET ", rurl, " HTTP/1.0\r\nHost: ", host, "\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n")
24
    write.socket(a, getreq)
28
    write.socket(a, getreq)
25
    junk <- read.socket(a, maxlen = nchar(head))
29
    junk <- read.socket(a, maxlen = nchar(head))
26
    data <- ""
30
    data <- ""
27
    b <- strsplit(c(head, junk), "\n")
31
    b <- strsplit(c(head, junk), "\n")
28
    nn <- length(b[[1]])
32
    nn <- length(b[[1]])
Line 37... Line 41...
37
}
41
}
38
 
42
 
39
if(nzchar(Sys.getenv("http_proxy")) || nzchar(Sys.getenv("HTTP_PROXY"))) {
43
if(nzchar(Sys.getenv("http_proxy")) || nzchar(Sys.getenv("HTTP_PROXY"))) {
40
    cat("http proxy is set, so skip test of http over sockets\n")
44
    cat("http proxy is set, so skip test of http over sockets\n")
41
} else {
45
} else {
42
## 2022-04-07: needs a URL which does not redirect to https://
-
 
43
    f <- httpget("http://httpbin.org/get")
46
    f <- httpget("http://developer.R-project.org/inet-tests/ch11b.dat")
44
    cat(f, sep = "\n")
47
    str(f)
45
    if (!length(grep('"headers":', f))) stop("Data not fetched via socket")
48
    if (length(f) != 100L) stop("Data not fetched via socket")
46
}
49
}
47
 
50
 
48
proc.time()
51
proc.time()