解析和操作URL的命令行工具-trurl
curl 作者 Daniel 于2023年4月上旬宣布推出新的开源项目:trurl,这是一款用于解析和操作 URL 的命令行工具,主要是面向 shell 脚本作者提供。Daniel 称项目名字中的 'tr' 代表 translate 或 transpose。采用C语言开发并在类BSD协议下授权。
trurl is a tool in a similar spirit of tr but for URLs. Here, 'tr' stands for translate or transpose. trurl is a command line tool that parses and manipulates URLs, designed to help shell script authors everywhere.
其表示URL很难解析,因此导致软件存在许多安全问题。trurl 希望通过消除脚本和命令行作者到处重复发明轮子的需求来帮助缓解这个问题。trurl 使用了 libcurl 的 URL 解析器,因此解析和理解 URL 的方式与命令行工具 curl 完全相同 —— 使其成为完美的配套工具。
示例
$ trurl --url https://curl.se --set host=example.com
https://example.com/
$ trurl --set host=example.com --set scheme=ftp
ftp://example.com/
$ trurl --url https://curl.se/we/are.html --redirect here.html
https://curl.se/we/here.html
$ trurl --url https://curl.se/we/../are.html --set port=8080
https://curl.se:8080/are.html
$ trurl --url https://curl.se/we/are.html --get '{path}'
/we/are.html
$ trurl --url https://curl.se/we/are.html --get '{port}'
443
$ trurl https://example.com/hello.html --get '{scheme} {port} {path}'
https 443 /hello.html
$ trurl --url https://curl.se/hello --append path=you
https://curl.se/hello/you
$ trurl --url "https://curl.se?name=hello" --append query=search=string
https://curl.se/?name=hello&search=string
$ trurl --url-file url-list.txt --get '{host}'
[one host name per URL in the input file]
$ cat url-list.txt | trurl --url-file - --get '{host}'
[one host name per URL in the input file]
$ trurl "https://fake.host/hello#frag" --set user=::moo:: --json
[
{
"url": "https://%3a%3amoo%3a%3a@fake.host/hello#frag",
"scheme": "https",
"user": "::moo::",
"host": "fake.host",
"port": "443",
"path": "/hello",
"fragment": "frag"
}
]
$ trurl "https://example.com?search=hello&utm_source=tracker" --trim query="utm_*"
https://example.com/?search=hello
最新版本:0.6
2023年4月下旬发布的0.6版本有如下变化:
为 --get 提供 {puny:host} 和 {puny:url}
为 --version 提供内置的 libcurl 版本信息
支持跳过测试
test.py 取代了 test.pl
test.py 支持 --with-valgrind
tests.json:扩展并更新
URL-QUIRKS:指定它们是构建时还是运行时的需求
修复自上一版以来的错误:
Makefile:修复了在 GNU Make 3.x 下的构建
Makefile:使测试目标依赖于 trurl
几个新的 CI jobs
test.py:如果 stdout 不是有效的 JSON,就不会崩溃
test:更好地打印失败的测试
trurl:修复查询分割
trurl:json:仅在打印第一个有效组件后打印逗号
trurl:当使用 --verify 中止时,确保终止 JSON 数组
trurl:修改 URL 后重新解析以确保其仍然有效
更多详情可查看此处。
项目主页:https://curl.se/trurl/