forked from r-lib/httr2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttr2-translate.R
More file actions
216 lines (195 loc) · 6.37 KB
/
Copy pathhttr2-translate.R
File metadata and controls
216 lines (195 loc) · 6.37 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#' Translate a httr2 request to a curl command
#'
#' Convert a httr2 request object to an approximate curl command line call.
#' This is useful for debugging, for sharing a request with someone who doesn't
#' use R, or for handing off to another tool.
#'
#' @inheritParams req_perform
#' @inheritParams req_get_body
#' @return A string containing the curl command.
#' @seealso [curl_translate()] to translate in the other direction.
#' @export
#' @examples
#' # Basic GET request
#' request("https://httpbin.org/get") |>
#' httr2_translate()
#'
#' # POST with JSON body
#' request("https://httpbin.org/post") |>
#' req_body_json(list(name = "value")) |>
#' httr2_translate()
#'
#' # Secrets are redacted by default, but can be revealed
#' request("https://example.com") |>
#' req_headers_redacted(Authorization = "secret") |>
#' httr2_translate(obfuscated = "reveal")
httr2_translate <- function(req, obfuscated = c("redact", "reveal")) {
check_request(req)
obfuscated <- arg_match(obfuscated)
req <- req_prepare(req)
req <- auth_sign(req)
body <- curl_body(req, obfuscated)
args <- c(
dquote(req_get_url(req)),
curl_method(req, has_body = !is.null(body)),
curl_headers(req, obfuscated),
curl_options(req),
body
)
indent <- c("", rep(" ", length(args) - 1))
backslash <- c(rep(" \\", length(args) - 1), "")
out <- paste0("curl ", paste0(indent, args, backslash, collapse = "\n"))
structure(out, class = "httr2_cmd")
}
curl_method <- function(req, has_body = FALSE) {
method <- req_get_method(req)
if (method == "GET" || (method == "POST" && has_body)) {
NULL
} else if (method == "HEAD") {
"--head"
} else {
paste0("--request ", method)
}
}
curl_headers <- function(req, obfuscated = c("redact", "reveal")) {
obfuscated <- arg_match(obfuscated)
headers <- req_get_headers(req, redacted = obfuscated)
if (is_empty(headers)) {
return(NULL)
}
paste0("--header ", dquote(paste0(names(headers), ": ", unlist(headers))))
}
curl_options <- function(req) {
options <- req$options
known_options <- c(
"timeout_ms", # req_timeout()
"connecttimeout", # req_timeout()
"proxy", # req_proxy()
"proxyport", # req_proxy()
"proxyuserpwd", # req_proxy()
"useragent", # req_user_agent()
"followlocation",
"verbose", # req_verbose()
"cookiejar", # req_cookie_preserve()
"cookiefile", # req_cookie_preserve()
"cookie" # req_cookies_set()
)
# R callbacks and the like, with no command line equivalent
ignored_options <- c(
"debugfunction", # req_verbose()
"xferinfofunction", # req_progress()
"noprogress", # req_progress()
"proxyauth", # req_proxy()
"forbid_reuse", # req_verbose_test()
"nobody", # req_method_apply()
"customrequest", # req_method_apply()
"post", # req_body_apply()
"postfieldsize", # req_body_apply()
"postfields", # req_body_apply()
"readfunction", # req_body_apply()
"seekfunction", # req_body_apply()
"postfieldsize_large" # req_body_apply()
)
unknown <- setdiff(names(options), c(known_options, ignored_options))
if (length(unknown) > 0) {
cli::cli_warn("Can't translate option{?s} {.val {unknown}}.")
}
args <- lapply(names(options), function(name) {
value <- options[[name]]
switch(
name,
timeout_ms = paste0("--max-time ", value / 1000),
connecttimeout = paste0("--connect-timeout ", value),
proxy = paste0(
"--proxy ",
dquote(paste0(c(value, options$proxyport), collapse = ":"))
),
proxyuserpwd = paste0("--proxy-user ", dquote(value)),
useragent = paste0("--user-agent ", dquote(value)),
verbose = if (value) "--verbose",
cookiejar = paste0("--cookie-jar ", dquote(value)),
cookiefile = paste0("--cookie ", dquote(value)),
cookie = paste0("--cookie ", dquote(value))
)
})
# httr2 follows redirects by default, but command line curl doesn't
follow <- if (!isFALSE(options$followlocation)) "--location"
c(follow, unlist(args))
}
curl_body <- function(req, obfuscated = c("redact", "reveal")) {
obfuscated <- arg_match(obfuscated)
body <- req_get_body(req, obfuscated = obfuscated)
if (is.null(body)) {
return(NULL)
}
type <- req_get_body_type(req)
if (type == "raw") {
cli::cli_abort("Can't translate a request with a raw body.", call = NULL)
}
c(
curl_content_type(req, type),
curl_body_data(body, type, params = req$body$params)
)
}
# Skip if Content-Type is already set as a header
curl_content_type <- function(req, type) {
if ("content-type" %in% tolower(names(req_get_headers(req)))) {
return(NULL)
}
content_type <- req$body$content_type %||%
switch(
type,
json = "application/json",
form = "application/x-www-form-urlencoded"
)
if (is.null(content_type) || !nzchar(content_type)) {
return(NULL)
}
paste0("--header ", dquote(paste0("Content-Type: ", content_type)))
}
curl_body_data <- function(body, type, params = list(auto_unbox = TRUE)) {
switch(
type,
string = paste0("--data ", dquote(body)),
file = paste0("--data-binary ", dquote(paste0("@", body))),
json = paste0("--data ", dquote(exec(jsonlite::toJSON, body, !!!params))),
form = paste0("--data ", dquote(url_query_build(body))),
multipart = curl_body_multipart(body)
)
}
curl_body_multipart <- function(body) {
unlist(Map(curl_body_multipart_field, names(body), body), use.names = FALSE)
}
curl_body_multipart_field <- function(name, value) {
if (inherits(value, "form_file")) {
spec <- paste0(name, "=@", curl_form_quote(value$path))
if (!is.null(value$type)) {
spec <- paste0(spec, ";type=", value$type)
}
if (!is.null(value$name)) {
spec <- paste0(spec, ";filename=", curl_form_quote(value$name))
}
paste0("--form ", dquote(spec))
} else if (inherits(value, "form_data")) {
type <- value$type
value <- rawToChar(value$value)
if (is.null(type)) {
paste0("--form-string ", dquote(paste0(name, "=", value)))
} else {
spec <- paste0(name, "=", curl_form_quote(value), ";type=", type)
paste0("--form ", dquote(spec))
}
} else {
paste0("--form-string ", dquote(paste0(name, "=", value)))
}
}
curl_form_quote <- function(x) {
paste0('"', gsub('(["\\\\])', "\\\\\\1", x), '"')
}
dquote <- function(x) {
ifelse(
grepl("[^A-Za-z0-9._~:/@%+=,-]", x),
paste0("'", gsub("'", "'\"'\"'", x, fixed = TRUE), "'"),
x
)
}