Url
Url is a URL as a value (NSURL in shape).
#import "Url.xc"Overview
Section titled “Overview”Url* u = Url.withCString("https://example.com:8080/dir/page.html?x=1");u.host(); // "example.com"u.port(); // 8080u.path(); // "/dir/page.html"u.lastPathComponent(); // "page.html"
Url* f = Url.fileURL(String.withCString("/Users/ada/My Docs/notes.txt"));f.toString(); // "file:///Users/ada/My%20Docs/notes.txt"f.filePath(); // "/Users/ada/My Docs/notes.txt"The accessors scan the text; nothing is parsed ahead of time.
File URLs (from 0.72): fileURL escapes
every byte of a path that is not a letter, digit or one of - . _ ~ /, and a
Windows path (C:\Work\notes.md) becomes file:///C:/Work/notes.md.
filePath undoes both.
Fetching: url.fetch(block void(u32 status, String* body) { … }) is the same
line of source on every target. The transport is the platform’s: the browser on
wasm32, an app’s own delegate elsewhere. With none, the block is called at once
with status 0 after a logged warning, so the code still runs everywhere.
Topics
Section titled “Topics”Creating · withString / withCString · fileURL · toString
Parts · scheme · host · port · path · query · lastPathComponent · pathExtension
File URLs · isFileURL · filePath · percentDecode
Fetching · fetch
Creating
Section titled “Creating”withString / withCString
Section titled “withString / withCString”static Url* withString(String* s)static Url* withCString(u8* s)The text as it is: nothing is checked or escaped.
fileURL
Section titled “fileURL”static Url* fileURL(String* path)The file URL for path. A relative path is taken as it stands; Url does not
know the working directory. From 0.72.
toString
Section titled “toString”String* toString(void)scheme
Section titled “scheme”String* scheme(void)The text before ://, or "".
String* host(void)u32 port(void)0 when the URL names none.
String* path(void)/ when the URL has none. Still percent-escaped.
String* query(void)The text after ?, or "".
lastPathComponent
Section titled “lastPathComponent”String* lastPathComponent(void)The last component of the decoded path; "" for /. From 0.72.
pathExtension
Section titled “pathExtension”String* pathExtension(void)From 0.72.
File URLs
Section titled “File URLs”isFileURL
Section titled “isFileURL”bool isFileURL(void)Whether the scheme is file, in any case. From 0.72.
filePath
Section titled “filePath”String* filePath(void)A file URL’s path with its escapes decoded (and /C:/x as C:/x), or null for
any other URL. From 0.72.
percentDecode
Section titled “percentDecode”static String* percentDecode(String* s)s with each %XX replaced by its byte; a % not followed by two hex digits
is kept as it is. From 0.72.
Fetching
Section titled “Fetching”void fetch(block cb void(u32, String*))Calls cb with the HTTP status and body when the fetch completes. Status 0 means
no transport, or a failure below HTTP; the body is null then.