Skip to content

Instantly share code, notes, and snippets.

@wch wch/proxy.R
Last active Feb 22, 2018

Embed
What would you like to do?
Quick and dirty HTTP proxy in R
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
You can’t perform that action at this time.