| 1 |
#' Echo Service |
| 2 |
#' |
| 3 |
#' This function is only for testing purposes. It starts a local httpuv server to |
| 4 |
#' echo the request body and content type in the response. |
| 5 |
#' |
| 6 |
#' @export |
| 7 |
#' @rdname curl_echo |
| 8 |
#' @param handle a curl handle object |
| 9 |
#' @param port the port number on which to run httpuv server |
| 10 |
#' @param progress show progress meter during http transfer |
| 11 |
#' @param file path or connection to write body. Default returns body as raw vector. |
| 12 |
#' @examples if(require('httpuv')){ |
| 13 |
#' h <- new_handle(url = 'https://hb.cran.dev/post') |
| 14 |
#' handle_setform(h, foo = "blabla", bar = charToRaw("test"), |
| 15 |
#' myfile = form_file(system.file("DESCRIPTION"), "text/description")) |
| 16 |
#' |
| 17 |
#' # Echo the POST request data |
| 18 |
#' formdata <- curl_echo(h) |
| 19 |
#' |
| 20 |
#' # Show the multipart body |
| 21 |
#' cat(rawToChar(formdata$body)) |
| 22 |
#' |
| 23 |
#' # Parse multipart |
| 24 |
#' webutils::parse_http(formdata$body, formdata$content_type) |
| 25 |
#' } |
| 26 |
curl_echo <- function(handle, port = find_port(), progress = interactive(), file = NULL){ |
| 27 |
progress <- isTRUE(progress) |
| 28 |
if(!(is.null(file) || inherits(file, "connection") || is.character(file))) |
| 29 |
stop("Argument 'file' must be a file path or connection object") |
| 30 |
output <- NULL |
| 31 |
echo_handler <- function(req){ |
| 32 |
if(progress){ |
| 33 |
cat("\nRequest Complete!\n", file = stderr()) |
| 34 |
progress <<- FALSE |
| 35 |
} |
| 36 |
body <- if(tolower(req[["REQUEST_METHOD"]]) %in% c("post", "put")){ |
| 37 |
if(!length(file)){ |
| 38 |
req[["rook.input"]]$read() |
| 39 |
} else { |
| 40 |
write_to_file(req[["rook.input"]]$read, file) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
output <<- list( |
| 45 |
method = req[["REQUEST_METHOD"]], |
| 46 |
path = req[["PATH_INFO"]], |
| 47 |
query = req[["QUERY_STRING"]], |
| 48 |
content_type = req[["CONTENT_TYPE"]], |
| 49 |
body = body, |
| 50 |
headers = req[["HEADERS"]] |
| 51 |
) |
| 52 |
|
| 53 |
# Dummy response |
| 54 |
list( |
| 55 |
status = 200, |
| 56 |
body = "", |
| 57 |
headers = c("Content-Type" = "text/plain") |
| 58 |
) |
| 59 |
} |
| 60 |
|
| 61 |
# Start httpuv |
| 62 |
server_id <- httpuv::startServer("0.0.0.0", port, list(call = echo_handler)) |
| 63 |
on.exit(httpuv::stopServer(server_id), add = TRUE) |
| 64 |
|
| 65 |
# Workaround bug in httpuv on windows that keeps protecting handler until next startServer() |
| 66 |
on.exit(rm(handle), add = TRUE) |
| 67 |
|
| 68 |
# Post data from curl |
| 69 |
xfer <- function(down, up){ |
| 70 |
if(progress){ |
| 71 |
if(up[1] == 0 && down[1] == 0){ |
| 72 |
cat("\rConnecting...", file = stderr()) |
| 73 |
} else { |
| 74 |
cat(sprintf("\rUpload: %s (%.0f%%) ", format_size(up[2]), |
| 75 |
as.integer(100 * up[2] / up[1])), file = stderr()) |
| 76 |
} |
| 77 |
} |
| 78 |
TRUE |
| 79 |
} |
| 80 |
handle_setopt(handle, connecttimeout = 2, xferinfofunction = xfer, noprogress = FALSE, forbid_reuse = TRUE) |
| 81 |
if(progress) cat("\n", file = stderr()) |
| 82 |
input_url <- handle_data(handle)$url |
| 83 |
if(length(input_url) && nchar(input_url)){ |
| 84 |
target_url <- replace_host(input_url, paste0("http://127.0.0.1:", port)) |
| 85 |
host <- sub("https?://([^/]+).*", "\\1", input_url) |
| 86 |
#hostname <- gsub(":[0-9]+$", "", host) |
| 87 |
#handle_setopt(handle, port = port, resolve = paste0(hostname, ":", port, ':127.0.0.1')) |
| 88 |
request_headers <- handle_getheaders(handle) |
| 89 |
if(!any(grepl("^Host:", request_headers, ignore.case = TRUE))){ |
| 90 |
request_headers <- c(paste("Host:", host), request_headers) |
| 91 |
} |
| 92 |
handle_setopt(handle, httpheader = request_headers) |
| 93 |
} else { |
| 94 |
target_url <- paste0("http://127.0.0.1:", port) |
| 95 |
} |
| 96 |
handle_setopt(handle, url = target_url) |
| 97 |
curl_dryrun(handle) |
| 98 |
output$url <- input_url |
| 99 |
if(progress) cat("\n", file = stderr()) |
| 100 |
return(output) |
| 101 |
} |
| 102 |
|
| 103 |
replace_host <- function(url, new_host = 'http://127.0.0.1'){ |
| 104 |
sub("[a-zA-Z]+://[^/]+", new_host, url) |
| 105 |
} |
| 106 |
|
| 107 |
write_to_file <- function(readfun, filename){ |
| 108 |
con <- if(inherits(filename, "connection")){ |
| 109 |
filename |
| 110 |
} else { |
| 111 |
base::file(filename) |
| 112 |
} |
| 113 |
if(!isOpen(con)){ |
| 114 |
open(con, "wb") |
| 115 |
on.exit(close(con)) |
| 116 |
} |
| 117 |
while(length(buf <- readfun(1e6))){ |
| 118 |
writeBin(buf, con) |
| 119 |
} |
| 120 |
return(filename) |
| 121 |
} |
| 122 |
|
| 123 |
format_size <- function(x){ |
| 124 |
if(x < 1024) |
| 125 |
return(sprintf("%d b", x)) |
| 126 |
if(x < 1048576) |
| 127 |
return(sprintf("%.2f Kb", x / 1024)) |
| 128 |
return(sprintf("%.2f Mb", x / 1048576)) |
| 129 |
} |
| 130 |
|
| 131 |
#' @export |
| 132 |
#' @rdname curl_echo |
| 133 |
#' @useDynLib curl R_findport |
| 134 |
#' @param range optional integer vector of ports to consider |
| 135 |
find_port <- function(range = NULL){ |
| 136 |
if(!length(range)) |
| 137 |
range <- sample(1024:49151) |
| 138 |
range <- as.integer(range) |
| 139 |
.Call(R_findport, range) |
| 140 |
} |
| 141 |
|
| 142 |
#' @useDynLib curl R_curl_dryrun |
| 143 |
curl_dryrun <- function(handle){ |
| 144 |
.Call(R_curl_dryrun, handle) |
| 145 |
} |
| 146 |
|
| 147 |
later_wrapper <- function(){ |
| 148 |
later::run_now() |
| 149 |
} |