-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME.Rmd
More file actions
309 lines (246 loc) · 12.2 KB
/
Copy pathREADME.Rmd
File metadata and controls
309 lines (246 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
---
title: "rurl"
output: github_document
---
<!-- badges: start -->
[](https://github.com/bart-turczynski/rurl/actions/workflows/verify.yml)
[](https://CRAN.R-project.org/package=rurl)
[](https://CRAN.R-project.org/package=rurl)
[](https://app.codecov.io/gh/bart-turczynski/rurl)
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://doi.org/10.5281/zenodo.20972584)
[](https://zenodo.org/search?q=metadata.creators.person_or_org.identifiers.identifier:0000-0002-8788-7980)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbart-turczynski%2Frurl?ref=badge_shield&issueType=security)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbart-turczynski%2Frurl?ref=badge_shield&issueType=license)
[](https://www.bestpractices.dev/projects/13394)
[](https://github.com/bart-turczynski/rurl/actions/workflows/security-audit.yml)
[](https://github.com/bart-turczynski/rurl/actions/workflows/osv-audit.yml)
<!-- badges: end -->
`rurl` is a lightweight, vectorized toolkit for URL parsing, normalization,
extraction, and matching in R.
Current package capabilities include:
- Robust parsing via `safe_parse_url()` and `safe_parse_urls()`
- URL normalization with fine-grained controls for protocol, `www`, case,
trailing slashes, index pages, path normalization, scheme-relative URLs,
host encoding, and path encoding
- Opt-in query-string handling: drop trackers, keep contentful params, and
audit params across a URL set with `query_param_summary()`
- URL component extractors (`get_*` helpers)
- URL-based joins with `canonical_join()`
- Built-in memoization caches with introspection and configuration (`rurl_cache_info()`, `rurl_cache_config()`, `rurl_clear_caches()`)
## Installation
```r
# From CRAN
install.packages("rurl")
# Development version from GitHub
# install.packages("remotes")
remotes::install_github("bart-turczynski/rurl")
```
## Function Overview
- Parsing and normalization: `safe_parse_url()`, `safe_parse_urls()`,
`get_clean_url()`
- Accessors: `get_scheme()`, `get_host()`, `get_subdomain()`, `get_domain()`,
`get_tld()`, `get_path()`, `get_query()`, `get_fragment()`, `get_port()`,
`get_user()`, `get_password()`, `get_userinfo()`, `get_parse_status()`
- Query introspection: `query_param_summary()`
- Matching/joining: `canonical_join()` for deterministic canonical-key joins
- Cache control: `rurl_cache_info()`, `rurl_cache_config()`, `rurl_clear_caches()`
## Quick Start
`safe_parse_url()` is the core workhorse. It returns parsed components and a
normalized `clean_url`.
```r
library(rurl)
parsed <- safe_parse_url(
"HTTP://www.Example.com/a//b/../index.html?x=1#frag",
protocol_handling = "https",
www_handling = "strip",
case_handling = "lower_host",
trailing_slash_handling = "strip",
index_page_handling = "strip",
path_normalization = "both",
host_encoding = "idna",
path_encoding = "encode"
)
parsed$clean_url
#> [1] "https://example.com/a"
parsed$parse_status
#> [1] "ok"
```
`clean_url` is a normalized canonical key built from **scheme, host, and path**.
By default the query is dropped and port, fragment, and userinfo are always
excluded — read them from the dedicated components (`get_port()`,
`get_query()`, `get_fragment()`, `get_userinfo()`) instead. The query can be
selectively kept on `clean_url` via `query_handling` on either
`safe_parse_url()` or `get_clean_url()` (see [Query handling](#query-handling)).
With `path_encoding = "decode"` the path is shown decoded, so `clean_url` is
human-readable rather than guaranteed URL-safe.
Scheme-relative URL handling is configurable:
```r
safe_parse_url("//example.com/path", scheme_relative_handling = "keep")$parse_status
#> [1] "ok-scheme-relative"
safe_parse_url("//example.com/path", scheme_relative_handling = "https")$clean_url
#> [1] "https://example.com/path"
```
For vectors, use `safe_parse_urls()`:
```r
safe_parse_urls(c("example.com", "https://www.example.com/path"))[, c("original_url", "clean_url", "parse_status")]
```
## Normalization Controls
`safe_parse_url()` and `get_clean_url()` support these controls:
- `protocol_handling`: `keep`, `none`, `strip`, `http`, `https`
- `www_handling`: `none`, `strip`, `keep`, `if_no_subdomain`
- `case_handling`: `lower_host` (default), `keep`, `lower`, `upper`
- `trailing_slash_handling`: `none`, `keep`, `strip`
- `index_page_handling`: `keep`, `strip`
- `path_normalization`: `none`, `collapse_slashes`, `dot_segments`, `both`
- `scheme_relative_handling`: `keep`, `http`, `https`, `error`
- `host_encoding`: `keep`, `idna`, `unicode`
- `path_encoding`: `keep`, `encode`, `decode`
- `subdomain_levels_to_keep`: `NULL`, `0`, or `N > 0`
- `query_handling`: `drop` (default), `filter`, `allow`, `keep` (see
[Query handling](#query-handling))
Subdomain retention is applied after `www_handling`:
```r
get_host("http://www.three.two.one.example.com", www_handling = "strip", subdomain_levels_to_keep = 1)
#> [1] "one.example.com"
get_clean_url("http://www.deep.sub.example.com/path", subdomain_levels_to_keep = 0)
#> [1] "http://www.example.com/path"
```
Host and path encoding controls:
```r
get_clean_url("http://münich.com/a%20b",
host_encoding = "idna",
path_encoding = "encode",
case_handling = "lower_host")
#> [1] "http://xn--mnich-kva.com/a%20b"
get_clean_url("http://xn--mnich-kva.com/a%20b",
host_encoding = "unicode",
path_encoding = "decode",
case_handling = "keep")
#> [1] "http://münich.com/a b"
```
## Accessors
```r
u <- "https://user:pass@www.blog.example.co.uk/path/to/page?a=1&b=2#frag"
get_scheme(u)
get_host(u)
get_subdomain(u)
get_domain(u)
get_tld(u)
get_path(u)
get_query(u)
get_query(u, format = "list")
get_fragment(u)
get_port(u)
get_user(u)
get_password(u)
get_userinfo(u)
get_parse_status(c(u, "mailto:test@example.com"))
```
## Query handling
By default `clean_url` is query-free (`query_handling = "drop"`), so a tracker
like `?utm_source=` never changes the canonical key. Opt in to keep contentful
params while stripping known trackers (`utm_*`, `fbclid`, `gclid`, …) via a
built-in denylist:
```r
# "filter": keep contentful params, drop known trackers
get_clean_url("https://youtube.com/watch?v=dQw4w9&utm_source=nl",
query_handling = "filter")
#> [1] "https://youtube.com/watch?v=dQw4w9"
# "allow": keep ONLY the names you list (params_keep is the allowlist)
get_clean_url("https://shop.com/p?id=42&ref=abc&fbclid=xyz",
query_handling = "allow", params_keep = "id")
#> [1] "https://shop.com/p?id=42"
# "keep": keep every param, canonicalized (optionally sorted)
get_clean_url("https://ex.com/?b=2&a=1",
query_handling = "keep", sort_params = TRUE)
#> [1] "https://ex.com/?a=1&b=2"
```
The same engine arguments flow through `safe_parse_urls()` (and therefore
`canonical_join()`), so filtering also shapes the join key — `?v=1` and `?v=2`
stay distinct while `utm`-only differences still collapse under `"filter"`:
```r
safe_parse_urls(c("https://ex.com/?v=1&utm_source=x", "https://ex.com/?v=2"),
query_handling = "filter")[, c("original_url", "clean_url")]
#> original_url clean_url
#> 1 https://ex.com/?v=1&utm_source=x https://ex.com/?v=1
#> 2 https://ex.com/?v=2 https://ex.com/?v=2
```
`get_query()` takes the same arguments (defaulting to `query_handling = "keep"`)
to pull a cleaned query directly:
```r
get_query("https://youtube.com/watch?v=dQw4w9&utm_source=nl",
query_handling = "filter")
#> [1] "v=dQw4w9"
```
Audit a URL set before choosing a policy with `query_param_summary()`, whose
`would_drop` column previews what `"filter"` would remove:
```r
urls <- c(
"https://ex.com/?utm_source=nl&id=42",
"https://ex.com/watch?v=abc&utm_source=x",
"https://ex.com/?id=99"
)
query_param_summary(urls)
#> param n n_urls example_value example_url would_drop
#> 1 utm_source 2 2 nl https://ex.com/?utm_source=nl&id=42 TRUE
#> 2 id 2 2 42 https://ex.com/?utm_source=nl&id=42 FALSE
#> 3 v 1 1 abc https://ex.com/watch?v=abc&utm_source=x FALSE
```
## URL Joins
`canonical_join()` matches on one canonicalized key per URL and is the
preferred option for large datasets:
```r
A <- data.frame(URL = c("http://Example.com/Page", "http://example.com/Other"),
ValA = 1:2, stringsAsFactors = FALSE)
B <- data.frame(URL = c("https://www.example.com/Page/", "http://example.com/Miss"),
ValB = c("x", "y"), stringsAsFactors = FALSE)
canonical_join(
A, B,
protocol_handling = "strip",
www_handling = "strip",
case_handling = "lower_host",
trailing_slash_handling = "strip"
)
```
## Caching
`rurl` memoizes URL parsing and punycode round-trips to speed repeated operations
over large URL vectors; PSL query caching lives in `pslr`. Inspect, clear, and
configure the caches:
```r
rurl_cache_info() # entries / enabled / max per cache
rurl_clear_caches() # free memory in a long-running session
rurl_cache_config(max_full_parse = 1e5) # bound the full-parse cache
rurl_cache_config(puny_encode = FALSE) # disable a cache entirely
```
`rurl_cache_config()` covers three caches: `full_parse`, `puny_encode`, and
`puny_decode`. The `full_parse` cache is unbounded by default
(`max_full_parse = Inf`); set a bound to cap its peak memory. The
`puny_encode` and `puny_decode` caches are unbounded by design and can be
disabled for workloads with very many unique hosts.
## Public Suffix List
Domain and TLD extraction is delegated to the
[`pslr`](https://github.com/bart-turczynski/pslr) package, which owns the
Public Suffix List and its refresh cycle. `rurl` ships no embedded copy of the
list. To update the PSL, call `pslr::psl_refresh()` (see the `pslr`
documentation for details).
## Acknowledgments
These packages build on data, libraries, and prior work from many others.
See [ACKNOWLEDGMENTS.md](ACKNOWLEDGMENTS.md) for the full list of thanks.
## Related packages
`rurl` is part of a small ecosystem of R packages by the same author:
- **[pslr](https://bart-turczynski.github.io/pslr/)** — the Public Suffix List engine that powers domain and TLD extraction in `rurl`. Use it directly when you need raw eTLD / registrable-domain queries without full URL parsing.
- **[punycoder](https://bart-turczynski.github.io/punycoder/)** — the Punycode and IDNA codec that `rurl` uses for internationalized host handling. Useful on its own for host normalization and Unicode ↔ ACE round-trips.
## Citation
If you use `rurl` in your work, please cite it. Run `citation("rurl")` for the
current citation, or see [`CITATION.cff`](CITATION.cff).
Each release is archived on Zenodo. Cite the concept DOI
[10.5281/zenodo.20972584](https://doi.org/10.5281/zenodo.20972584) to refer to
the software in general (it always resolves to the latest version), or the
version-specific DOI shown on the [Zenodo
record](https://doi.org/10.5281/zenodo.20972584) for a particular release.
## Code of Conduct
Please note that this package is released with a [Contributor Code of Conduct](https://ropensci.org/code-of-conduct/).
By contributing to this project, you agree to abide by its terms.
## License
MIT © 2026 Bart Turczynski