forked from r-lib/httr2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurl.R
More file actions
363 lines (316 loc) · 10.4 KB
/
Copy pathcurl.R
File metadata and controls
363 lines (316 loc) · 10.4 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#' Translate a curl command to a httr2 request
#'
#' @description
#' The curl command line tool is commonly used to demonstrate HTTP APIs and can
#' easily be generated from
#' [browser developer tools](https://everything.curl.dev/cmdline/copyas.html).
#' `curl_translate()` saves you the pain of manually translating these calls
#' by implementing a partial, but frequently used, subset of curl options.
#' Use `curl_help()` to see the supported options, and `curl_translate()`
#' to translate a curl invocation copy and pasted from elsewhere.
#'
#' Inspired by [curlconverter](https://github.com/hrbrmstr/curlconverter)
#' written by [Bob Rudis](https://rud.is/b/).
#'
#' @param cmd Call to curl. If omitted and the clipr package is installed,
#' will be retrieved from the clipboard.
#' @param simplify_headers Remove typically unimportant headers included when
#' copying a curl command from the browser. This includes:
#'
#' * `sec-fetch-*`
#' * `sec-ch-ua*`
#' * `referer`, `pragma`, `connection`
#' @returns A string containing the translated httr2 code. If the input
#' was copied from the clipboard, the translation will be copied back
#' to the clipboard.
#' @export
#' @seealso [httr2_translate()]
#' @examples
#' curl_translate("curl http://example.com")
#' curl_translate("curl http://example.com -X DELETE")
#' curl_translate("curl http://example.com --header A:1 --header B:2")
#' curl_translate("curl http://example.com --verbose")
curl_translate <- function(cmd, simplify_headers = TRUE) {
if (missing(cmd)) {
if (is_interactive() && is_installed("clipr") && clipr::clipr_available()) {
clip <- TRUE
cmd <- clipr::read_clip()
cmd <- paste0(cmd, collapse = "\n")
} else {
cli::cli_abort("Must supply {.arg cmd}.")
}
} else {
check_string(cmd)
clip <- FALSE
}
data <- curl_normalize(cmd)
url_pieces <- httr2::url_parse(data$url)
query <- url_pieces$query
url_pieces$query <- NULL
url <- url_build(url_pieces)
steps <- glue('request("{url}")')
steps <- add_curl_step(steps, "req_method", main_args = data$method)
steps <- add_curl_step(steps, "req_url_query", dots = query)
# Cookies
cookies <- data$headers$`Cookie`
data$headers$`Cookie` <- NULL
if (!is.null(cookies)) {
steps <- add_curl_step(
steps,
"req_cookies_set",
dots = cookies_parse(cookies)
)
}
# Content type set with data
type <- data$headers$`Content-Type`
if (!identical(data$data, "")) {
data$headers$`Content-Type` <- NULL
}
headers <- curl_simplify_headers(data$headers, simplify_headers)
steps <- add_curl_step(steps, "req_headers", dots = headers)
if (!identical(data$data, "")) {
type <- type %||% "application/x-www-form-urlencoded"
if (type == "application/json" && idempotent_json(data$data)) {
json <- jsonlite::parse_json(data$data)
args <- list(data = I(deparse1(json)))
steps <- add_curl_step(steps, "req_body_json", dots = args)
} else {
body <- data$data
steps <- add_curl_step(steps, "req_body_raw", main_args = c(body, type))
}
}
steps <- add_curl_step(steps, "req_auth_basic", main_args = unname(data$auth))
perform_args <- list()
if (data$verbose) {
perform_args$verbosity <- 1
}
steps <- add_curl_step(
steps,
"req_perform",
main_args = perform_args,
keep_if_empty = TRUE
)
out <- paste0(steps, collapse = paste0(" |>\n "))
if (clip) {
cli::cli_alert_success("Copying to clipboard:")
clipr::write_clip(out)
}
out <- paste0(out, "\n")
structure(out, class = "httr2_cmd")
}
#' @export
print.httr2_cmd <- function(x, ...) {
cat(x)
invisible(x)
}
#' @rdname curl_translate
#' @export
curl_help <- function() {
cat(curl_opts)
}
curl_translate_eval <- function(cmd, env = caller_env()) {
code <- curl_translate(cmd)
eval(parse_expr(code), envir = env)
}
curl_normalize <- function(cmd, error_call = caller_env()) {
args <- curl_args(cmd, error_call = error_call)
url <- args[["--url"]] %||%
args[["<url>"]] %||%
cli::cli_abort("Must supply url.", call = error_call)
if (has_name(args, "--header")) {
headers <- as_headers(args[["--header"]])
} else {
headers <- as_headers(list())
}
if (has_name(args, "--referer")) {
headers[["referer"]] <- args[["--referer"]]
}
if (has_name(args, "--user-agent")) {
headers[["user-agent"]] <- args[["--user-agent"]]
}
if (has_name(args, "--user")) {
pieces <- parse_in_half(args[["--user"]], ":")
auth <- list(
username = pieces$left,
password = pieces$right
)
} else {
auth <- NULL
}
if (has_name(args, "--request")) {
method <- args[["--request"]]
} else if (has_name(args, "--head")) {
method <- "HEAD"
} else if (has_name(args, "--get")) {
method <- "GET"
} else {
method <- NULL
}
if (has_name(args, "--json")) {
args <- c(args, list(`--data-raw` = args[["--json"]]))
headers[["Content-Type"]] <- "application/json"
}
# https://curl.se/docs/manpage.html#-d
# --data-ascii, --data
# * if first element is @, treat as path to read from, stripping CRLF
# * if multiple, combine with &
# --data-raw - like data, but don't handle @ specially
# --data-binary - like data, but don't strip CRLF
# --data-urlencode - not supported for now
data <- unlist(c(
lapply(args[["--data"]], curl_data),
lapply(args[["--data-ascii"]], curl_data),
lapply(args[["--data-raw"]], curl_data, raw = TRUE),
lapply(args[["--data-binary"]], curl_data, binary = TRUE)
))
data <- paste0(data, collapse = "&")
list(
method = method,
url = url,
headers = headers,
auth = auth,
verbose = isTRUE(args[["--verbose"]]),
data = data
)
}
curl_simplify_headers <- function(headers, simplify_headers) {
if (simplify_headers) {
header_names <- tolower(names(headers))
to_drop <- startsWith(header_names, "sec-fetch") |
startsWith(header_names, "sec-ch-ua") |
header_names %in% c("referer", "pragma", "connection")
headers <- headers[!to_drop]
}
headers
}
curl_data <- function(x, binary = FALSE, raw = FALSE) {
if (!raw && grepl("^@", x)) {
path <- sub("^@", "", x)
if (binary) {
x <- readBin(path, "character", n = file.size(path))
} else {
x <- paste(readLines(path, warn = FALSE))
}
} else {
x
}
}
# Format described at <http://docopt.org>
curl_opts <- "Usage: curl [<url>] [-H <header> ...] [-d <data> ...] [options] [<url>]
--basic (IGNORED)
--compressed (IGNORED)
--digest (IGNORED)
-d, --data <data> HTTP POST data
--data-raw <data> HTTP POST data, '@' allowed
--data-ascii <data> HTTP POST ASCII data
--data-binary <data> HTTP POST binary data
--data-urlencode <data> HTTP POST data url encoded
--json <data> HTTP POST JSON
-G, --get Put the post data in the URL and use GET
-I, --head Show document info only
-H, --header <header> Pass custom header(s) to server
-i, --include (IGNORED)
-k, --insecure (IGNORED)
-L, --location (IGNORED)
-m, --max-time <seconds> Maximum time allowed for the transfer
-u, --user <user:password> Server user and password
-A, --user-agent <name> Send User-Agent STRING to server
-#, --progress-bar Display transfer progress as a progress bar
-e, --referer <referer> Referer URL
-X, --request <command> Specify request command to use
--url <url> URL to work with
-v, --verbose Make the operation more talkative
"
curl_args <- function(cmd, error_call = caller_env()) {
check_installed("docopt")
pieces <- parse_in_half(cmd, " ")
if (pieces$left != "curl") {
cli::cli_abort(
"Expecting call to {.str curl} not to {.str {pieces[[1]]}}.",
call = error_call
)
}
if (grepl("'", cmd, fixed = TRUE)) {
args <- parse_delim(pieces$right, " ", quote = "'")
} else {
args <- parse_delim(pieces$right, " ", quote = '"')
}
args <- args[args != "" & args != "\\"]
parsed <- docopt::docopt(curl_opts, args = args, help = FALSE, strict = TRUE)
# Drop default options
parsed <- compact(parsed)
is_false <- map_lgl(parsed, identical, FALSE)
parsed <- parsed[!is_false]
parsed
}
# Helpers -----------------------------------------------------------------
is_syntactic <- function(x) {
x == "" | x == make.names(x)
}
quote_name <- function(x) {
ifelse(is_syntactic(x), x, encodeString(x, quote = "`"))
}
add_curl_step <- function(
steps,
f,
main_args = NULL,
dots = NULL,
keep_if_empty = FALSE
) {
args <- c(main_args, dots)
if (is_empty(args) && !keep_if_empty) {
return(steps)
}
names <- quote_name(names2(args))
string <- map_lgl(args, function(x) is.character(x) && !inherits(x, "AsIs"))
values <- unlist(args)
values <- ifelse(string, encode_string2(values), values)
args_named <- ifelse(
names == "",
paste0(values),
paste0(names, " = ", values)
)
if (is_empty(dots)) {
args_string <- paste0(args_named, collapse = ", ")
new_step <- paste0(f, "(", args_string, ")")
} else {
args_string <- paste0(" ", args_named, ",\n", collapse = "")
new_step <- paste0(f, "(\n", args_string, " )")
}
c(steps, new_step)
}
encode_string2 <- function(x) {
supports_raw_string <- getRversion() >= "4.0.0"
has_double_quote <- grepl('"', x, fixed = TRUE)
has_single_quote <- grepl("'", x, fixed = TRUE)
use_double <- !has_double_quote | has_single_quote
out <- ifelse(
use_double,
encodeString(x, quote = '"'),
encodeString(x, quote = "'")
)
if (supports_raw_string) {
has_unprintable <- grepl("[^[[:cntrl:]]]", x)
x_encoded <- encodeString(x)
has_both_quotes <- has_double_quote & has_single_quote
use_raw_string <- !has_unprintable & (x != x_encoded | has_both_quotes)
out[use_raw_string] <- paste0('r"---{', x[use_raw_string], '}---"')
}
names(out) <- names(x)
out
}
cookies_parse <- function(x) {
pairs <- strsplit(x, "; ?")[[1]]
cookies <- parse_name_equals_value(pairs)
if (length(cookies) == 0) {
return(NULL)
}
out <- as.list(curl::curl_unescape(cookies))
names(out) <- curl::curl_unescape(names(cookies))
out
}
idempotent_json <- function(old) {
args <- formals(req_body_json)[c("auto_unbox", "null", "digits")]
new <- exec(jsonlite::toJSON, jsonlite::parse_json(old), !!!args)
jsonlite::minify(old) == jsonlite::minify(new)
}