encoding/url
Taking a web address apart, building and editing its parameters, and resolving relative addresses.
import encoding.url
Parse takes a full address such as https://user@shop.example.co.uk:8443/path?page=2#top apart. The parts are kept exactly as they appeared in the text; for the decoded form use functions such as DecodedPath.
The parameter functions edit the address's query through the query package, and String builds the finished address.
Types
URL
struct URL| Name | Type | Description |
|---|---|---|
scheme | str = "" | The scheme, such as https. |
userinfo | str = "" | The user name and password before the @, still encoded. |
host | str = "" | The host name or numeric address. |
port | int = 0 | The port, or zero when none was given. |
path | str = "" | The path, still encoded. |
raw_query | str = "" | The query after the ?, still encoded. |
fragment | str = "" | The part after the #. |
import encoding.url
import encoding.query
func main:
mut u := url.Parse("https://user%20a:pw@shop.example.co.uk:8443/books/%D8%B3%D9%84%D8%A7%D9%85?page=2&sort=new#top")
println u.scheme, u.host, u.port, u.path, u.raw_query, u.fragment
println url.Hostname(u), url.HostPort(u), url.IsIP(u)
println url.Labels(u), url.PublicSuffix(u), url.Domain(u), url.Subdomain(u)
println url.DecodedPath(u), url.DecodedUserinfo(u)
println url.ParamNames(u), query.Size(url.Query(u))
url.SetParam(u, "page", "3")
println url.RemoveParam(u, "sort"), url.String(u)
mut keep := Vector {} as Vector<str>
keep.push("page")
url.KeepParams(u, keep)
mut q := query.NewQueryMap()
query.Add(q, "q", "hello world")
url.SetQuery(u, q)
println url.String(u)
url.ClearQuery(u)
println url.String(u)
println url.String(url.Join(u, "../img/a.png")), url.EncodePathSegment("a b/c")
println url.IsIP(url.Parse("http://127.0.0.1/"))
endFunctions
Parse
func Parse(raw: str): URLThe example for this item is shown with the ones above: encoding-url-Parse.salam
HostPort
func HostPort(u: URL): strshop.example.co.uk:8443.The example for this item is shown with the ones above: encoding-url-Parse.salam
String
func String(u: URL): strThe example for this item is shown with the ones above: encoding-url-Parse.salam
DecodedPath
func DecodedPath(u: URL): strThe example for this item is shown with the ones above: encoding-url-Parse.salam
DecodedUserinfo
func DecodedUserinfo(u: URL): strThe example for this item is shown with the ones above: encoding-url-Parse.salam
EncodePathSegment
func EncodePathSegment(s: str): str/ is encoded too.The example for this item is shown with the ones above: encoding-url-Parse.salam
Query
func Query(u: URL): query.QueryMapQueryMap.The example for this item is shown with the ones above: encoding-url-Parse.salam
SetQuery
func SetQuery(u &: URL, q: query.QueryMap)The example for this item is shown with the ones above: encoding-url-Parse.salam
Join
func Join(base: URL, ref: str): URL.. and . in the path are worked out.The example for this item is shown with the ones above: encoding-url-Parse.salam
Hostname
func Hostname(u: URL): strThe example for this item is shown with the ones above: encoding-url-Parse.salam
IsIP
func IsIP(u: URL): boolThe example for this item is shown with the ones above: encoding-url-Parse.salam
Labels
func Labels(u: URL): Vector<str>The example for this item is shown with the ones above: encoding-url-Parse.salam
PublicSuffix
func PublicSuffix(u: URL): strco.uk or ir.The example for this item is shown with the ones above: encoding-url-Parse.salam
Domain
func Domain(u: URL): strexample.co.uk.The example for this item is shown with the ones above: encoding-url-Parse.salam
Subdomain
func Subdomain(u: URL): strshop.The example for this item is shown with the ones above: encoding-url-Parse.salam
RemoveParam
func RemoveParam(u &: URL, key: str): boolThe example for this item is shown with the ones above: encoding-url-Parse.salam
SetParam
func SetParam(u &: URL, key: str, val: str)The example for this item is shown with the ones above: encoding-url-Parse.salam
KeepParams
func KeepParams(u &: URL, keep: Vector<str>)The example for this item is shown with the ones above: encoding-url-Parse.salam
ClearQuery
func ClearQuery(u &: URL)The example for this item is shown with the ones above: encoding-url-Parse.salam
ParamNames
func ParamNames(u: URL): Vector<str>The example for this item is shown with the ones above: encoding-url-Parse.salam