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

Add support for quoted boundary for multipart request parsing. #924

Merged
merged 6 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# plumber (development version)

* Add support for quoted boundary for multipart request parsing. (@meztez #924)
* Fix #916, related to `parseUTF8` return value attribute `srcfile` on Windows. (#930)

# plumber 1.2.1
Expand Down
2 changes: 2 additions & 0 deletions R/parse-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ parser_multi <- function() {
if (!stri_detect_fixed(content_type, "boundary=", case_insensitive = TRUE))
stop("No boundary found in multipart content-type header: ", content_type)
boundary <- stri_match_first_regex(content_type, "boundary=([^; ]{2,})", case_insensitive = TRUE)[,2]
# Remove surrounding quotes if they exist
boundary <- stri_replace_first_regex(boundary, '^"([^"]+)"$', "$1")
meztez marked this conversation as resolved.
Show resolved Hide resolved
toparse <- parse_multipart(value, boundary)

# set the names of the items as the `name` of each item
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-parse-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ test_that("Test multipart parser", {
expect_true(is.raw(body_args[["img2"]][["ragnarok_small.png"]]))
expect_gt(length(body_args[["img2"]][["ragnarok_small.png"]]), 100)
expect_equal(body_args[["json"]], list(a=2,b=4,c=list(w=3,t=5)))

# Quoted boundary
req$HTTP_CONTENT_TYPE = "multipart/form-data; boundary=\"----WebKitFormBoundaryMYdShB9nBc32BUhQ\""
req$body <- req_body_parser(req, make_parser(c("multi", "json", "rds", "octet")))
expect_equal(names(req$body), c("json", "img1", "img2", "rds"))

})


Expand Down
Loading