Skip to content

Commit

Permalink
Don't autoparse text formats into text first.
Browse files Browse the repository at this point in the history
Fixes #209
  • Loading branch information
hadley committed May 4, 2015
1 parent 77a3f7b commit 9f8d21e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# httr 0.6.1.9000

* `context(type = "auto")` uses a better strategy for text based formats (#209).
This should allow the `encoding` argument to work more reliably.

* `content(type = "text")` compares encodings in a case-insensitive manner
(#209).

Expand Down
46 changes: 25 additions & 21 deletions R/content-parse.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ parse_auto <- function(content, type = NULL, encoding = NULL, ...) {
}

if (is.null(type)) {
stop("Unknown mime type: can't parse automatically. Use the type ",
stop("Unknown mime type: can't parse automatically. Use the `type` ",
"argument to specify manually.", call. = FALSE)
}

mt <- parse_media(type)
if (mt$type == "text") {
content <- parse_text(content, type, encoding = encoding)
}

parser <- parsers[[mt$complete]]
if (is.null(parser)) {
stop("No automatic parser available for ", mt$complete, ".",
call. = FALSE)
} else {
parser(content, ...)
}

parser(content, type = type, encoding = encoding, ...)
}

parseability <- function(type) {
Expand All @@ -53,45 +49,53 @@ parsers <- new.env(parent = emptyenv())
# Binary formats ---------------------------------------------------------------

# http://www.ietf.org/rfc/rfc4627.txt - section 3. (encoding)
parsers$`application/json` <- function(x, simplifyVector = FALSE, ...) {
parsers$`application/json` <- function(x, type = NULL, encoding = NULL,
simplifyVector = FALSE, ...) {
jsonlite::fromJSON(parse_text(x, encoding = "UTF-8"),
simplifyVector = simplifyVector, ...)
}
parsers$`application/x-www-form-urlencoded` <- function(x) {
parsers$`application/x-www-form-urlencoded` <- function(x, type = NULL,
encoding = NULL, ...) {
parse_query(parse_text(x, encoding = "UTF-8"))
}
parsers$`application/xml` <- function(x, ...) {
parsers$`application/xml` <- function(x, type = NULL, encoding = NULL, ...) {
need_package("XML")
XML::xmlParse(parse_text(x, encoding = "UTF-8"), ...)
}

# Text formats -----------------------------------------------------------------
parsers$`image/jpeg` <- function(x) {
parsers$`image/jpeg` <- function(x, type = NULL, encoding = NULL, ...) {
need_package("jpeg")
jpeg::readJPEG(x)
}

parsers$`image/png` <- function(x) {
parsers$`image/png` <- function(x, type = NULL, encoding = NULL, ...) {
need_package("png")
png::readPNG(x)
}

parsers$`text/plain` <- function(x) x
parsers$`text/plain` <- function(x, type = NULL, encoding = NULL, ...) {
parse_text(x, type = type, encoding = encoding)
}

parsers$`text/html` <- function(x, ...) {
parsers$`text/html` <- function(x, type = NULL, encoding = NULL, ...) {
need_package("XML")
XML::htmlParse(x, ...)
text <- parse_text(x, type = type, encoding = encoding)
XML::htmlParse(text, ...)
}

parsers$`text/xml` <- function(x, ...) {
parsers$`text/xml` <- function(x, type = NULL, encoding = NULL, ...) {
need_package("XML")
XML::xmlParse(x, ...)
text <- parse_text(x, type = type, encoding = encoding)
XML::xmlParse(text, ...)
}

parsers$`text/csv` <- function(x, ...) {
read.csv(text = x, stringsAsFactors = FALSE, ...)
parsers$`text/csv` <- function(x, type = NULL, encoding = NULL, ...) {
text <- parse_text(x, type = type, encoding = encoding)
read.csv(text = text, stringsAsFactors = FALSE, ...)
}

parsers$`text/tab-separated-values` <- function(x, ...) {
read.delim(text = x, stringsAsFactors = FALSE, ...)
parsers$`text/tab-separated-values` <- function(x, type = NULL, encoding = NULL, ...) {
text <- parse_text(x, type = type, encoding = encoding)
read.delim(text = text, stringsAsFactors = FALSE, ...)
}

0 comments on commit 9f8d21e

Please sign in to comment.