Standard library · Encoding and data

encoding/url

Taking a web address apart, building and editing its parameters, and resolving relative addresses.

import encoding.url
On this page
  1. Types
  2. Functions

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
A parsed web address.
NameTypeDescription
schemestr = ""The scheme, such as https.
userinfostr = ""The user name and password before the @, still encoded.
hoststr = ""The host name or numeric address.
portint = 0The port, or zero when none was given.
pathstr = ""The path, still encoded.
raw_querystr = ""The query after the ?, still encoded.
fragmentstr = ""The part after the #.
encoding-url-Parse.salam
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/"))
end
Outputhttps shop.example.co.uk 8443 /books/%D8%B3%D9%84%D8%A7%D9%85 page=2&sort=new top shop.example.co.uk shop.example.co.uk:8443 false ["shop", "example", "co", "uk"] co.uk example.co.uk shop /books/سلام user a:pw ["page", "sort"] 2 true https://user%20a:pw@shop.example.co.uk:8443/books/%D8%B3%D9%84%D8%A7%D9%85?page=3#top https://user%20a:pw@shop.example.co.uk:8443/books/%D8%B3%D9%84%D8%A7%D9%85?q=hello+world#top https://user%20a:pw@shop.example.co.uk:8443/books/%D8%B3%D9%84%D8%A7%D9%85#top https://user%20a:pw@shop.example.co.uk:8443/img/a.png a%20b%2Fc true

Functions

Parse

func Parse(raw: str): URL
Takes an address apart into its pieces.

The example for this item is shown with the ones above: encoding-url-Parse.salam

HostPort

func HostPort(u: URL): str
The host and port together, such as shop.example.co.uk:8443.

The example for this item is shown with the ones above: encoding-url-Parse.salam

String

func String(u: URL): str
Turns the address back into text.

The example for this item is shown with the ones above: encoding-url-Parse.salam

DecodedPath

func DecodedPath(u: URL): str
The path with its percent encoding undone.

The example for this item is shown with the ones above: encoding-url-Parse.salam

DecodedUserinfo

func DecodedUserinfo(u: URL): str
The user information, decoded.

The example for this item is shown with the ones above: encoding-url-Parse.salam

EncodePathSegment

func EncodePathSegment(s: str): str
Encodes one path segment; / is encoded too.

The example for this item is shown with the ones above: encoding-url-Parse.salam

Query

func Query(u: URL): query.QueryMap
Turns the address's query into a QueryMap.

The example for this item is shown with the ones above: encoding-url-Parse.salam

SetQuery

func SetQuery(u &: URL, q: query.QueryMap)
Replaces the address's query with the given map.

The example for this item is shown with the ones above: encoding-url-Parse.salam

Join

func Join(base: URL, ref: str): URL
Resolves a relative address against a base one, the way a browser does; .. 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): str
The host name without the port.

The example for this item is shown with the ones above: encoding-url-Parse.salam

IsIP

func IsIP(u: URL): bool
True when the host is a numeric IP address.

The example for this item is shown with the ones above: encoding-url-Parse.salam

Labels

func Labels(u: URL): Vector<str>
The parts of the host name, separated by dots.

The example for this item is shown with the ones above: encoding-url-Parse.salam

PublicSuffix

func PublicSuffix(u: URL): str
The public suffix of the domain, such as co.uk or ir.

The example for this item is shown with the ones above: encoding-url-Parse.salam

Domain

func Domain(u: URL): str
The registered domain: one label before the public suffix, such as example.co.uk.

The example for this item is shown with the ones above: encoding-url-Parse.salam

Subdomain

func Subdomain(u: URL): str
Whatever comes before the registered domain, such as shop.

The example for this item is shown with the ones above: encoding-url-Parse.salam

RemoveParam

func RemoveParam(u &: URL, key: str): bool
Removes every value of a parameter and says whether it was there.

The example for this item is shown with the ones above: encoding-url-Parse.salam

SetParam

func SetParam(u &: URL, key: str, val: str)
Sets the value of a parameter.

The example for this item is shown with the ones above: encoding-url-Parse.salam

KeepParams

func KeepParams(u &: URL, keep: Vector<str>)
Keeps only the parameters you name, for stripping tracking parameters.

The example for this item is shown with the ones above: encoding-url-Parse.salam

ClearQuery

func ClearQuery(u &: URL)
Removes the whole query.

The example for this item is shown with the ones above: encoding-url-Parse.salam

ParamNames

func ParamNames(u: URL): Vector<str>
The names of the parameters, in order.

The example for this item is shown with the ones above: encoding-url-Parse.salam