Skip to content

Commit

Permalink
Add support for fragments in URL building/parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigcitro committed Feb 12, 2014
1 parent 2faedbf commit a4ea9a3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ httr 0.2.99
httpd server). This makes the dance work in Rstudio, and also seems a little
faster. (#32, thanks to @jdeboer)

* Add support for `fragment` in url building/parsing. Contributed by
Craig Citro. (#70)

httr 0.2
-----------

Expand Down
32 changes: 17 additions & 15 deletions R/url.r
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' \item port
#' \item path
#' \item params
#' \item fragment
#' \item query, a list
#' \item username
#' \item password
Expand Down Expand Up @@ -78,8 +79,9 @@ parse_url <- function(url) {
params <- pull_off(";(.*)$")

structure(list(
scheme = scheme, hostname = hostname, port = port, path = url,
query = query, params = params, username = username, password = password),
scheme = scheme, hostname = hostname, port = port, path = url,
query = query, params = params, fragment = fragment,
username = username, password = password),
class = "url")
}

Expand Down Expand Up @@ -126,18 +128,15 @@ build_url <- function(url) {
query <- str_c("?", query)
}

if (!is.null(url$username)) {
if (!is.null(url$password)) {
password <- str_c(":", url$password)
} else {
password <- NULL
}
login <- str_c(url$username, password, "@")
} else {
login <- NULL
if (is.null(url$username) && !is.null(url$password)) {
stop("Cannot set password without username")
}

str_c(scheme, "://", login, hostname, port, "/", path, params, query)
str_c(scheme, "://",
url$username, if (!is.null(url$password)) ":", url$password,
if (!is.null(url$username)) "@",
hostname, port, "/", path, params, query,
if (!is.null(url$fragment)) "#", url$fragment)
}

#' Modify a url.
Expand All @@ -147,14 +146,17 @@ build_url <- function(url) {
#'
#' @export
#' @param url the url to modify
#' @param scheme,hostname,port,path,query,params,username,password
#' @param scheme,hostname,port,path,query,params,fragment,username,password
#' components of the url to change
modify_url <- function(url, scheme = NULL, hostname = NULL, port = NULL, path = NULL, query = NULL, params = NULL, username = NULL, password = NULL) {
modify_url <- function(url, scheme = NULL, hostname = NULL, port = NULL,
path = NULL, query = NULL, params = NULL, fragment = NULL,
username = NULL, password = NULL) {

old <- parse_url(url)
new <- compact(list(
scheme = scheme, hostname = hostname, port = port, path = path,
query = query, params = params, username = username, password = password))
query = query, params = params, fragment = fragment,
username = username, password = password))

build_url(modifyList(old, new))
}
Expand Down
11 changes: 10 additions & 1 deletion inst/tests/test-url.r
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test_that("parse_url works as expected", {
"http://google.com/path?a=1&b=2",
"http://google.com/path;param?a=1&b=2",
"http://google.com:80/path;param?a=1&b=2",
"http://google.com:80/path;param?a=1&b=2#frag",
"http://user@google.com:80/path;param?a=1&b=2",
"http://user:pass@google.com:80/path;param?a=1&b=2",
"svn+ssh://my.svn.server/repo/trunk"
Expand All @@ -31,4 +32,12 @@ test_that("query strings escaped and unescaped correctly", {
parsed <- parse_url(url)
expect_equal(parsed$query, list("x y" = "a b"))
expect_equal(build_url(parsed), url)
})
})

test_that("password and no username is an error", {
url <- "http://www.example.com/"
parsed <- parse_url(url)
expect_equal(build_url(parsed), url)
parsed$password <- "secret"
expect_error(build_url(parsed), "password without username")
})
6 changes: 3 additions & 3 deletions man/modify_url.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
\title{Modify a url.}
\usage{
modify_url(url, scheme = NULL, hostname = NULL, port = NULL,
path = NULL, query = NULL, params = NULL, username = NULL,
password = NULL)
path = NULL, query = NULL, params = NULL, fragment = NULL,
username = NULL, password = NULL)
}
\arguments{
\item{url}{the url to modify}

\item{scheme,hostname,port,path,query,params,username,password}{components
\item{scheme,hostname,port,path,query,params,fragment,username,password}{components
of the url to change}
}
\description{
Expand Down

0 comments on commit a4ea9a3

Please sign in to comment.