Last active
February 22, 2018 21:14
-
-
Save wch/5fce76ca87033ae6fcbbb45cd744d620 to your computer and use it in GitHub Desktop.
Quick and dirty HTTP proxy in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(curl) | |
library(httpuv) | |
req_rook_to_curl <- function(req, host) { | |
# browser() | |
# Rename headers. Example: HTTP_CACHE_CONTROL => Cache-Control | |
r <- as.list(req) | |
# Uncomment to print out request headers | |
cat("== Original ==\n") | |
cat(capture.output(print(str(r))), sep = "\n") | |
r <- r[grepl("^HTTP_", names(r))] | |
nms <- names(r) | |
nms <- sub("^HTTP_", "", nms) | |
nms <- tolower(nms) | |
nms <- gsub("_", "-", nms, fixed = TRUE) | |
nms <- gsub("\\b([a-z])", "\\U\\1", nms, perl = TRUE) | |
names(r) <- nms | |
# Overwrite host field | |
r$Host <- host | |
# Uncomment to print out modified request headers | |
cat("== Modified ==\n") | |
cat(capture.output(print(str(r))), sep = "\n") | |
r | |
} | |
resp_httr_to_rook <- function(resp) { | |
# browser() | |
status <- as.integer(sub("^HTTP\\S+ (\\d+).*", "\\1", curl::parse_headers(resp$headers)[1])) | |
list( | |
status = status, | |
headers = parse_headers_list(resp$headers), | |
body = resp$content | |
) | |
} | |
s <- startServer("0.0.0.0", 5002, | |
list( | |
call = function(req) { | |
host <- "beta.rstudioconnect.com" | |
req_curl <- req_rook_to_curl(req, host) | |
h <- new_handle() | |
do.call(handle_setheaders, c(h, req_curl)) | |
resp_curl <- curl_fetch_memory(paste0("http://", host, req$PATH_INFO), handle = h) | |
resp_httr_to_rook(resp_curl) | |
} | |
) | |
) | |
# Stop the server | |
stopServer(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment