Skip to content

Commit

Permalink
URL's scheme parser is compliant with RFC 3986 (#615)
Browse files Browse the repository at this point in the history
* URL's scheme parser is compliant with RFC 3986

* RFC3986 reference added, NEWS update
  • Loading branch information
ymarcon authored Apr 3, 2020
1 parent 40f162f commit 6f931cf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# httr (development version)

* `parse_url()` now refers to RFC3986 for the parsing of the URL's
scheme, with a bit more permissive syntax (@ymarcon, #615).

# httr 1.4.1

* Remove the default `cainfo` option on Windows. Providing a CA bundle is not
Expand Down
8 changes: 4 additions & 4 deletions R/url.r
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Good example for testing
# http://stevenlevithan.com/demo/parseuri/js/

#' Parse and build urls according to RFC1808.
#' Parse and build urls according to RFC3986.
#'
#' See <http://tools.ietf.org/html/rfc1808.html> for details of parsing
#' See <https://tools.ietf.org/html/rfc3986> for details of parsing
#' algorithm.
#'
#' @param url For `parse_url` a character vector (of length 1) to parse
Expand All @@ -24,7 +24,7 @@
#' parse_url("http://google.com/")
#' parse_url("http://google.com:80/")
#' parse_url("http://google.com:80/?a=1&b=2")
#'
#'
#' url <- parse_url("http://google.com/")
#' url$scheme <- "https"
#' url$query <- list(q = "hello")
Expand All @@ -45,7 +45,7 @@ parse_url <- function(url) {
}

fragment <- pull_off("#(.*)$")
scheme <- pull_off("^([[:alpha:]+.-]+):")
scheme <- pull_off("^([[:alpha:]][[:alpha:][:digit:]+.-]*):")
netloc <- pull_off("^//([^/?]*)/?")

if (identical(netloc, "")) { # corresponds to ///
Expand Down
4 changes: 2 additions & 2 deletions man/parse_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/testthat/test-url.r
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ test_that("parse_url preserves leading / in path", {
expect_equal(url$path, "/tmp/foobar")
})

test_that("scheme starts with alpha", {
url <- parse_url("+ab://host/tmp/foobar")
expect_equal(url$scheme, NULL)
})

test_that("scheme can contain digits", {
url <- parse_url("ab1://host/tmp/foobar")
expect_equal(url$scheme, "ab1")
})

test_that("scheme can contain plus", {
url <- parse_url("a+b://host/tmp/foobar")
expect_equal(url$scheme, "a+b")
})

test_that("scheme can contain period", {
url <- parse_url("a.b://host/tmp/foobar")
expect_equal(url$scheme, "a.b")
})

test_that("scheme can contain hyphen", {
url <- parse_url("a-b://host/tmp/foobar")
expect_equal(url$scheme, "a-b")
})

test_that("scheme can be a single character", {
url <- parse_url("a://host/tmp/foobar")
expect_equal(url$scheme, "a")
})

# compose_query -----------------------------------------------------------

test_that("I() prevents escaping", {
Expand Down

0 comments on commit 6f931cf

Please sign in to comment.