Skip to content

Network

Generated from crates/niso-core/include/network.h when the site was built at 7aa23e2.

This is the network section of niso_runtime.h, owned by niso-fetch and kept in crates/niso-core/include/network.h. The build composes it with the other sections in the order crates/niso-compose/sections.txt gives.

Functions: niso_net_fetch, niso_net_fetch_request, niso_net_fetch_request_sync, niso_net_cancel, niso_net_take, niso_fetch_forbidden_request_header, niso_fetch_cors_safelisted_request_header

The network: fetch’s transport, for every language, which also loads the document’s images, stylesheets and fonts. Requests run off the DOM thread.

niso_net_fetch starts a request with headers_len strings of header name and value pairs and a body (body_len 0: none), and stores its id in *id. It is the host’s own request, which no page made: redirects are followed, any HTTP status is a response, and neither CORS nor a referrer applies.

niso_net_fetch_request starts one as the page’s fetch() does, with the document or worker as its client and the Fetch standard’s options: mode 1 same-origin, 2 no-cors, 3 cors (0, navigate, is no page’s request and an error); credentials 0 omit, 1 same-origin, 2 include; redirect 0 follow, 1 error, 2 manual; referrer_policy 0 “” (the client’s), 1 no-referrer, 2 no-referrer-when-downgrade, 3 same-origin, 4 origin, 5 strict-origin, 6 origin-when-cross-origin, 7 strict-origin-when-cross-origin, 8 unsafe-url; destination 0 “”, 1 script, 2 style, 3 worker, 4 image, 5 font; use_cors_preflight, Fetch’s use-CORS-preflight flag, sends a CORS request’s preflight whatever its method and headers (an XMLHttpRequest with upload listeners); referrer “” for none, “about:client” for the client, else a URL. It is held to what fetch() holds a page to, whatever the host passes: a forbidden method is a network error, forbidden request-headers are dropped, a no-cors request keeps only no-CORS-safelisted headers and is a GET, HEAD or POST, and a referrer of another origin is the client.

niso_net_fetch_request_sync runs such a request to completion while the calling thread waits, as a synchronous XMLHttpRequest does, and stores its response in *out as niso_net_take encodes one, valid until the next call into the DOM.

niso_fetch_forbidden_request_header and niso_fetch_cors_safelisted_request_header are Fetch’s predicates for a header name and value (UTF-8), for a binding’s Headers guards.

niso_net_take returns the responses that finished since the last call, as bytes valid until the next call into the DOM: u32 count, then per response u64 id, u16 status (0 for a network error and an opaque response), u8 type (0 basic, 1 cors, 2 default, 3 error, 4 opaque, 5 opaqueredirect), u8 redirected (1 if a redirect led to it), string status text, string final URL, u32 header count and that many name and value strings (names lower-case), string error (empty unless the type is error), and the body as a string of bytes. A string is a u32 length and that many bytes (UTF-8 but for the body); numbers are little-endian. Hosts need not call it: a language binding takes them in its task hook, and niso_dom_run gives the hook a turn when responses wait and nothing else does, then drops the ones it left.

niso_net_cancel cancels a request niso_net_fetch started: its body stops downloading and niso_net_take never returns it.

int32_t niso_net_fetch(niso_dom* dom, const char* method, size_t method_len, const char* url, size_t url_len,
const niso_str* headers, size_t headers_len, const uint8_t* body, size_t body_len, uint64_t* id);
struct niso_fetch_options {
uint8_t mode;
uint8_t credentials;
uint8_t redirect;
uint8_t referrer_policy;
uint8_t destination;
bool use_cors_preflight;
niso_str referrer;
};
int32_t niso_net_fetch_request(niso_dom* dom, const char* method, size_t method_len, const char* url, size_t url_len,
const niso_str* headers, size_t headers_len, const uint8_t* body, size_t body_len,
const niso_fetch_options* options, uint64_t* id);
int32_t niso_net_fetch_request_sync(niso_dom* dom, const char* method, size_t method_len, const char* url, size_t url_len,
const niso_str* headers, size_t headers_len, const uint8_t* body, size_t body_len,
const niso_fetch_options* options, niso_bytes* out);
int32_t niso_net_cancel(niso_dom* dom, uint64_t id);
int32_t niso_net_take(niso_dom* dom, niso_bytes* out);
bool niso_fetch_forbidden_request_header(const char* name, size_t name_len, const char* value, size_t value_len);
bool niso_fetch_cors_safelisted_request_header(const char* name, size_t name_len, const char* value, size_t value_len);