Skip to content

Commit

Permalink
Add support for quoted boundary for multipart request parsing. (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
meztez authored Nov 20, 2023
1 parent 44f8172 commit 72d68c6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
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
3 changes: 2 additions & 1 deletion R/parse-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,8 @@ parser_multi <- function() {
function(value, content_type, parsers, ...) {
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]
# Also remove surrounding quotes if they exist
boundary <- stri_match_first_regex(content_type, "boundary=\"?([^; \"]{2,})\"?", case_insensitive = TRUE)[,2]
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

0 comments on commit 72d68c6

Please sign in to comment.