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

Use bytes charset for multipart input #504

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Encoding: UTF-8
Package: plumber
Type: Package
Title: An API Generator for R
Expand Down Expand Up @@ -45,6 +44,7 @@ Remotes:
rstudio/swagger,
rstudio/httpuv,
sckott/analogsea
Encoding: UTF-8
Collate:
'async.R'
'content-types.R'
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Multipart input is not parsed for query strings (#504, @krlmlr).

plumber 0.5.0
--------------------------------------------------------------------------------
## Full changelog
Expand Down
3 changes: 3 additions & 0 deletions R/content-types.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ getCharacterSet <- function(contentType){
if (is.null(contentType)) {
return(default)
}
if(grepl("^multipart/", contentType)) {
return("bytes")
}
charsetStart <- attr(
gregexpr(".*charset=(.*)", contentType, perl = T)[[1]],
"capture.start"
Expand Down
4 changes: 2 additions & 2 deletions R/post-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ postBodyFilter <- function(req){
parseBody <- function(body, charset = "UTF-8"){
# The body in a curl call can also include querystring formatted data
# Is there data in the request?
if (is.null(body) || length(body) == 0 || body == "") {
if (is.null(body) || length(body) == 0 || body == "" || charset == "bytes") {
return(list())
}

Expand All @@ -28,7 +28,7 @@ parseBody <- function(body, charset = "UTF-8"){
ret <- safeFromJSON(body)
} else {
# If not handle it as a query string
ret <- parseQS(body)
ret <- parseQS(body)
}
ret
}
6 changes: 6 additions & 0 deletions tests/testthat/test-postbody.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ test_that("JSON is consumed on POST", {
expect_equal(parseBody('{"a":"1"}'), list(a = "1"))
})

test_that("bytes are ignored on POST", {
body <- "dummy"
Encoding(body) <- "bytes"
expect_equal(parseBody(body), list())
})

test_that("Query strings on post are handled correctly", {
expect_equivalent(parseBody("a="), list()) # It's technically a named list()
expect_equal(parseBody("a=1&b=&c&d=1"), list(a="1", d="1"))
Expand Down