| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/parser.R |
| 3 |
\name{curl_parse_url} |
| 4 |
\alias{curl_parse_url} |
| 5 |
\alias{curl_modify_url} |
| 6 |
\title{Normalizing URL parser} |
| 7 |
\usage{ |
| 8 |
curl_parse_url( |
| 9 |
url, |
| 10 |
baseurl = NULL, |
| 11 |
decode = TRUE, |
| 12 |
params = TRUE, |
| 13 |
default_scheme = FALSE |
| 14 |
) |
| 15 |
|
| 16 |
curl_modify_url( |
| 17 |
url = NULL, |
| 18 |
scheme = NULL, |
| 19 |
host = NULL, |
| 20 |
port = NULL, |
| 21 |
path = NULL, |
| 22 |
query = NULL, |
| 23 |
fragment = NULL, |
| 24 |
user = NULL, |
| 25 |
password = NULL, |
| 26 |
params = NULL |
| 27 |
) |
| 28 |
} |
| 29 |
\arguments{ |
| 30 |
\item{url}{either URL string or list returned by \link{curl_parse_url}. |
| 31 |
Use this to modify a URL using the other parameters.} |
| 32 |
|
| 33 |
\item{baseurl}{use this as the parent if \code{url} may be a relative path} |
| 34 |
|
| 35 |
\item{decode}{automatically \link[=curl_escape]{url-decode} output into the actual |
| 36 |
values. If set to \code{FALSE}, values for \code{query}, \code{path}, \code{fragment}, \code{user} and \code{password} are returned in url-encoded format.} |
| 37 |
|
| 38 |
\item{params}{named character vector with http GET parameters. This will automatically |
| 39 |
be converted to \code{application/x-www-form-urlencoded} and override \code{query},} |
| 40 |
|
| 41 |
\item{default_scheme}{when \code{url} is provided without a scheme prefix, assume \verb{https://}.} |
| 42 |
|
| 43 |
\item{scheme}{string with e.g. \code{https}. Required if no \code{url} parameter was given.} |
| 44 |
|
| 45 |
\item{host}{string with hostname. Required if no \code{url} parameter was given.} |
| 46 |
|
| 47 |
\item{port}{string or number with port, e.g. \code{"443"}.} |
| 48 |
|
| 49 |
\item{path}{piece of the url starting with \code{/} up till \verb{?} or \verb{#}} |
| 50 |
|
| 51 |
\item{query}{piece of url starting with \verb{?} up till \verb{#}. Only used if no \code{params} is given.} |
| 52 |
|
| 53 |
\item{fragment}{part of url starting with \verb{#}.} |
| 54 |
|
| 55 |
\item{user}{string with username} |
| 56 |
|
| 57 |
\item{password}{string with password} |
| 58 |
} |
| 59 |
\description{ |
| 60 |
Interfaces the libcurl \href{https://curl.se/libcurl/c/libcurl-url.html}{URL parser}. |
| 61 |
URLs are automatically normalized where possible, such as in the case of |
| 62 |
relative paths or url-encoded queries (see examples). |
| 63 |
When parsing hyperlinks from a HTML document, it is possible to set \code{baseurl} |
| 64 |
to the location of the document itself such that relative links can be resolved. |
| 65 |
} |
| 66 |
\details{ |
| 67 |
A valid URL contains at least a scheme and a host, other pieces are optional. |
| 68 |
If these are missing, the parser raises an error. Otherwise it returns |
| 69 |
a list with the following elements: |
| 70 |
\itemize{ |
| 71 |
\item \emph{url}: the normalized input URL |
| 72 |
\item \emph{scheme}: the protocol part before the \verb{://} (required) |
| 73 |
\item \emph{host}: name of host without port (required) |
| 74 |
\item \emph{port}: decimal between 0 and 65535 |
| 75 |
\item \emph{path}: normalized path up till the \verb{?} of the url |
| 76 |
\item \emph{query}: search query: part between the \verb{?} and \verb{#} of the url. Use \code{params} below to get individual parameters from the query. |
| 77 |
\item \emph{fragment}: the hash part after the \verb{#} of the url |
| 78 |
\item \emph{user}: authentication username |
| 79 |
\item \emph{password}: authentication password |
| 80 |
\item \emph{params}: named vector with parameters from \code{query} if set |
| 81 |
} |
| 82 |
|
| 83 |
Each element above is either a string or \code{NULL}, except for \code{params} which |
| 84 |
is always a character vector with the length equal to the number of parameters. |
| 85 |
|
| 86 |
Note that the \code{params} field is only usable if the \code{query} is in the usual |
| 87 |
\code{application/x-www-form-urlencoded} format which is technically not part of |
| 88 |
the RFC. Some services may use e.g. a json blob as the query, in which case |
| 89 |
the parsed \code{params} field here can be ignored. There is no way for the parser |
| 90 |
to automatically infer or validate the query format, this is up to the caller. |
| 91 |
|
| 92 |
For more details on the URL format see |
| 93 |
\href{https://datatracker.ietf.org/doc/html/rfc3986}{rfc3986} |
| 94 |
or the steps explained in the \href{https://url.spec.whatwg.org/#concept-basic-url-parser}{whatwg basic url parser}. |
| 95 |
|
| 96 |
You can use \code{\link[=curl_modify_url]{curl_modify_url()}} both to modify an existing URL, or to |
| 97 |
create new URL from scratch. Arguments get automatically URL-encoded where |
| 98 |
needed, unless wrapped in \code{I()}. If \code{params} is given, this gets converted |
| 99 |
into a \code{application/x-www-form-urlencoded} string which overrides \code{query}. |
| 100 |
When modifying a URL, use an empty string \code{""} to unset a piece of the URL. |
| 101 |
} |
| 102 |
\examples{ |
| 103 |
url <- "https://jerry:[email protected]:888/foo/bar?test=123#bla" |
| 104 |
curl_parse_url(url) |
| 105 |
|
| 106 |
# Resolve relative links from a baseurl |
| 107 |
curl_parse_url("/somelink", baseurl = url) |
| 108 |
|
| 109 |
# Paths get normalized |
| 110 |
curl_parse_url("https://foobar.com/foo/bar/../baz/../yolo")$url |
| 111 |
|
| 112 |
# Also normalizes URL-encoding (these URLs are equivalent): |
| 113 |
url1 <- "https://ja.wikipedia.org/wiki/\u5bff\u53f8" |
| 114 |
url2 <- "https://ja.wikipedia.org/wiki/\%e5\%af\%bf\%e5\%8f\%b8" |
| 115 |
curl_parse_url(url1)$path |
| 116 |
curl_parse_url(url2)$path |
| 117 |
curl_parse_url(url1, decode = FALSE)$path |
| 118 |
curl_parse_url(url1, decode = FALSE)$path |
| 119 |
} |