Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept for better binary detection #427

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions R/content-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ parse_content_type <- function(x) {
list(
type = match[[2]],
subtype = match[[3]],
prefix = paste0(match[[2]], "/", match[[3]]),
suffix = if (match[[4]] != "") match[[4]] else ""
)
}
Expand All @@ -95,9 +96,8 @@ check_content_type <- function(content_type,
inform_check_type = FALSE,
call = caller_env()) {
parsed <- parse_content_type(content_type)
base_type <- paste0(parsed$type, "/", parsed$subtype)

if (is.null(valid_types) || base_type %in% valid_types) {
if (is.null(valid_types) || parsed$prefix %in% valid_types) {
return()
}
if (!is.null(valid_suffix) && parsed$suffix == valid_suffix) {
Expand All @@ -115,3 +115,23 @@ check_content_type <- function(content_type,
call = call
)
}

is_text_type <- function(content_type) {
parsed <- parse_content_type(content_type)
if (parsed$type == "text") {
return(TRUE)
}

special_cases <- c(
"application/xml",
"application/x-www-form-urlencoded",
"application/json",
"application/ld+json",
"multipart/form-data"
)
if (parsed$prefix %in% special_cases) {
return(TRUE)
}

FALSE
}
15 changes: 8 additions & 7 deletions R/req-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,14 @@ req_verbose <- function(req,

# helpers -----------------------------------------------------------------

verbose_message <- function(prefix, x) {
if (any(x > 128)) {
# This doesn't handle unicode, but it seems like most output
# will be compressed in some way, so displaying bodies is unlikely
# to be useful anyway.
lines <- paste0(length(x), " bytes of binary data")
} else {
verbose_message <- function(prefix, x, is_text = NULL) {
is_text <- is_text %||% all(x <= 128)

if (is_text) {
x <- readBin(x, character())
lines <- unlist(strsplit(x, "\r?\n", useBytes = TRUE))
} else {
lines <- paste0(length(x), " bytes of binary data")
}
cli::cat_line(prefix, lines)
}
Expand All @@ -214,6 +213,8 @@ verbose_header <- function(prefix, x, redact = TRUE, to_redact = NULL) {
cli::cat_line(prefix, line)
}
}

invisible(x)
}

auth_flags <- function(x = "basic") {
Expand Down
22 changes: 20 additions & 2 deletions R/req-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,27 @@ req_dry_run <- function(req, quiet = FALSE, redact_headers = TRUE) {

if (!quiet) {
to_redact <- attr(req$headers, "redact")

headers <- raw()
is_text <- NULL

debug <- function(type, msg) {
if (type == 2L) verbose_header("", msg, redact = redact_headers, to_redact = to_redact)
if (type == 4L) verbose_message("", msg)

if (type == 2L) {
headers <<- c(
headers,
verbose_header("", msg, redact = redact_headers, to_redact = to_redact)
)
}
if (type == 4L) {
if (is.character(headers)) {
headers <- as_headers(headers)
if (has_name(headers, "Content-Type")) {
is_text <- is_text_type(headers$"Content-Type")
}
}
verbose_message("", msg, is_text = is_text)
}
}
req <- req_options(req, debugfunction = debug, verbose = TRUE)
}
Expand Down
Loading