Skip to content

std.url

rfc 3986 parsing, percent-encoding, query strings; parsed parts stay encoded

pub UrlError :: errors {
no_scheme,
bad_port,
bad_escape,
}
  • no_scheme: no valid scheme before “:”
  • bad_port: a port that is not a number up to 65535
  • bad_escape: a “%” without two hex digits after it
pub Url :: struct {
scheme: []const u8,
user: []const u8,
host: []const u8,
port: ?u16,
path: []const u8,
query: []const u8,
fragment: []const u8,
has_authority: bool,
}

parts are slices of the parsed text; missing ones empty, port null

  • user: before “@”, password included
  • host: name, IPv4, or [IPv6]
  • query: after the “?”, without it
  • fragment: after the “#”, without it
  • has_authority: whether a “//” followed the scheme
pub port_or :: fn(self: *Self, fallback: u16) -> u16

fallback when there is no port

pub parse :: fn(text: []const u8) -> Url ! UrlError

absolute URL (scheme:...); splits and checks, doesn’t decode

pub Keep :: enum {
component,
path,
form,
}

bytes encode keeps; others become %XX

pub encoded_len :: fn(s: []const u8, keep: Keep) -> usize

length encode writes for s

pub encode :: fn(out: []u8, s: []const u8, keep: Keep) -> usize

into out (>= encoded_len); returns length written

pub encode_in :: fn(a: *Arena, s: []const u8, keep: Keep) -> []u8

in a

pub decode :: fn(out: []u8, s: []const u8, plus_is_space: bool) -> usize ! UrlError

%XX decoded into out (>= s.len); returns length; plus_is_space for form queries

pub decode_in :: fn(a: *Arena, s: []const u8) -> []const u8 ! UrlError

in a; returns s itself when there is no escape

pub decode_form_in :: fn(a: *Arena, s: []const u8) -> []const u8 ! UrlError

decode_in for form values: + is a space too

pub Pair :: struct {
key: []const u8,
value: []const u8,
}

both still encoded; no = means an empty value

pub Pairs :: struct {
rest: []const u8,
}

query pairs in order; empty ones (a=1&&b=2) skipped

pub next :: fn(self: *Self) -> ?Pair
pub query_pairs :: fn(query: []const u8) -> Pairs
pub query_get :: fn(query: []const u8, key: []const u8) -> ?[]const u8

first pair named key (compared encoded), or null