-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for relative urls (#610)
Add `base_url` parameter to `url_parse()` and implement new `req_url_relative()`. Fixes #449
- Loading branch information
Showing
8 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#' | ||
#' @param url For `url_parse()` a string to parse into a URL; | ||
#' for `url_build()` a URL to turn back into a string. | ||
#' @param base_url Use this as a parent, if `url` is a relative URL. | ||
#' @returns | ||
#' * `url_build()` returns a string. | ||
#' * `url_parse()` returns a URL: a S3 list with class `httr2_url` | ||
|
@@ -18,15 +19,20 @@ | |
#' url_parse("http://google.com:80/?a=1&b=2") | ||
#' url_parse("http://[email protected]:80/path;test?a=1&b=2#40") | ||
#' | ||
#' # You can parse a relative URL if you also provide a base url | ||
#' url_parse("foo", "http://google.com/bar/") | ||
#' url_parse("..", "http://google.com/bar/") | ||
#' | ||
#' url <- url_parse("http://google.com/") | ||
#' url$port <- 80 | ||
#' url$hostname <- "example.com" | ||
#' url$query <- list(a = 1, b = 2, c = 3) | ||
#' url_build(url) | ||
url_parse <- function(url) { | ||
url_parse <- function(url, base_url = NULL) { | ||
check_string(url) | ||
check_string(base_url, allow_null = TRUE) | ||
|
||
curl <- curl::curl_parse_url(url) | ||
curl <- curl::curl_parse_url(url, baseurl = base_url) | ||
|
||
parsed <- list( | ||
scheme = curl$scheme, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,14 @@ test_that("can round trip urls", { | |
expect_equal(map(urls, ~ url_build(url_parse(.x))), urls) | ||
}) | ||
|
||
test_that("can parse relative urls", { | ||
base <- "http://example.com/a/b/c/" | ||
expect_equal(url_parse("d", base)$path, "/a/b/c/d") | ||
expect_equal(url_parse("..", base)$path, "/a/b/") | ||
|
||
expect_equal(url_parse("//archive.org", base)$scheme, "http") | ||
}) | ||
|
||
test_that("can print all url details", { | ||
expect_snapshot( | ||
url_parse("http://user:[email protected]:80/path?a=1&b=2&c={1{2}3}#frag") | ||
|