crater-browser-http

HTTP, cache, cookie, and request sandbox helpers for crater browser

moon add mizchi/crater-browser-http@0.19.0
Download zip
Author
Version
0.19.0
License
Apache-2.0
Last updated
last month
Downloads
944

Dependencies

README

mizchi/crater-browser-http does not have a README file

#
HttpCacheBackend

pub(open) trait HttpCacheBackend {
lookup(Self, String) -> CacheEntry?
store(Self, CacheEntry) -> Unit
remove(Self, String) -> Unit
clear(Self) -> Unit
}

Shared cache backend contract used by cached_fetch.

#
HttpError

pub suberror HttpError {
NetworkError(String)
InvalidUrl(String)
TimeoutError
CorsError(String)
SandboxError(String)
}

HTTP Error types
impl Show for HttpError

#
CacheDirectives

pub(all) struct CacheDirectives {
max_age : Int?
no_cache : Bool
no_store : Bool
must_revalidate : Bool
public_ : Bool
private_ : Bool
immutable : Bool
} derive(
Debug
)

Parsed Cache-Control header directives

#
CacheDirectives::default

#
CacheEntry

pub(all) struct CacheEntry {
url : String
status : Int
headers : Map[String, String]
body : String
etag : String?
last_modified : String?
directives : CacheDirectives
stored_at : Double
} derive(
Debug
)

Cached HTTP response entry

#
CookieJar

pub struct CookieJar {
cookies : Array[ParsedCookie]
enabled : Bool
}

Cookie Jar — stores cookies per-domain with security enforcement

#
CookieJar::clear

fn CookieJar::clear(self : CookieJar) -> Unit

Clear all cookies
fn CookieJar::get_cookie_header(self : CookieJar, request_url : String, is_secure : Bool, is_http_api : Bool, site_origin? : String, is_top_level_navigation? : Bool) -> String?

Get cookies for a request URL as "Cookie: name=value; name2=value2" pairs. Enforces: domain matching, path matching, secure flag, httpOnly, SameSite.

site_origin: the origin of the page making the request (for SameSite checks). Empty string means "same-site" (no cross-site enforcement). is_top_level_navigation: true for top-level navigations (SameSite=Lax allows these).

#
CookieJar::new

fn CookieJar::new(enabled? : Bool) -> CookieJar

#
CookieJar::size

fn CookieJar::size(self : CookieJar) -> Int

Get number of stored cookies

#
CookieJar::store_from_header

fn CookieJar::store_from_header(self : CookieJar, set_cookie_header : String, request_url : String, is_secure_origin : Bool) -> Unit

Store a cookie from a Set-Cookie header. Enforces: domain validation, secure flag, httpOnly flag.

#
CredentialsMode

pub(all) enum CredentialsMode {
Omit
SameOrigin
Include
} derive(Eq,
Debug
)

Credentials mode for CORS checks

#
FetchOptions

pub(all) struct FetchOptions {
http_method : String
headers : Map[String, String]
body : String
timeout_ms : Int
mode : RequestMode
credentials : CredentialsMode
origin : String
sandbox : RequestSandbox
}

Fetch options

#
FetchOptions::default

fn FetchOptions::default() -> FetchOptions

#
HttpResponse

pub(all) struct HttpResponse {
status : Int
headers : Map[String, String]
body : String
}

HTTP Response structure

#
MemoryCacheBackend

pub(all) struct MemoryCacheBackend {
entries : Map[String, CacheEntry]
max_entries : Int
}

In-memory cache backend with LRU-style eviction by oldest stored_at.

#
MemoryCacheBackend::clear

fn MemoryCacheBackend::clear(self : MemoryCacheBackend) -> Unit

Clear all cache entries.

#
MemoryCacheBackend::lookup

fn MemoryCacheBackend::lookup(self : MemoryCacheBackend, url : String) -> CacheEntry?

Look up a cache entry by URL. Returns None if not found.

#
MemoryCacheBackend::new

fn MemoryCacheBackend::new(max_entries? : Int) -> MemoryCacheBackend

#
MemoryCacheBackend::remove

fn MemoryCacheBackend::remove(self : MemoryCacheBackend, url : String) -> Unit

Remove a cache entry by URL.

#
MemoryCacheBackend::store

fn MemoryCacheBackend::store(self : MemoryCacheBackend, entry : CacheEntry) -> Unit

Store a cache entry. If at capacity and key is new, evict the oldest entry by stored_at.

#
ParsedCookie

pub(all) struct ParsedCookie {
name : String
value : String
domain : String
path : String
secure : Bool
http_only : Bool
same_site : String
max_age : Int?
expires : String?
} derive(
Debug
)

Parsed Set-Cookie attributes

#
RequestMode

pub(all) enum RequestMode {
Navigate
SameOrigin
NoCors
Cors
} derive(Eq,
Debug
)

Request mode (subset of Fetch spec)

#
RequestSandbox

pub(all) enum RequestSandbox {
Open
SameOrigin
Allowlist(Array[String])
} derive(
Debug
)

Request sandbox policy

#
cached_fetch

fn[CacheBackend : HttpCacheBackend] cached_fetch(url~ : String, options~ : FetchOptions, cache~ : CacheBackend, fetcher~ : (String, FetchOptions) -> HttpResponse raise HttpError) -> HttpResponse raise HttpError

Fetch with transparent HTTP caching (sync version for testing). Only GET requests are cached. The fetcher parameter allows callers to inject any transport function.

#
cached_fetch_async

async fn[CacheBackend : HttpCacheBackend] cached_fetch_async(url~ : String, options~ : FetchOptions, cache~ : CacheBackend, fetcher~ : async (String, FetchOptions) -> HttpResponse raise HttpError) -> HttpResponse raise HttpError

Fetch with transparent HTTP caching (async version). Wraps an async fetcher (e.g. @http.fetch) with cache lookup/store. Only GET requests are cached.

#
fetch

async fn fetch(url : String, options? : FetchOptions) -> HttpResponse raise HttpError

Fetch URL using JavaScript fetch() API

#
headers_to_json

fn headers_to_json(headers : Map[String, String]) -> String

Convert headers map to JSON string

#
is_cacheable_status

fn is_cacheable_status(status : Int) -> Bool

Check if an HTTP status code is cacheable. Only 200, 203, 204, 206, 300, 301, 308 are cacheable by default (RFC 9111 §4.2.2).

#
is_fresh

fn is_fresh(entry : CacheEntry, now : Double) -> Bool

Check whether a cache entry is still fresh at the given time.

#
now_seconds

fn now_seconds() -> Double

Get current time in seconds since epoch. Delegates to platform-specific FFI.

#
parse_cache_control

fn parse_cache_control(header : String) -> CacheDirectives

Parse a Cache-Control header value into CacheDirectives.
fn parse_set_cookie(header : String, request_domain : String, request_path : String) -> ParsedCookie?

Parse a Set-Cookie header value into a ParsedCookie. Returns None if the cookie is malformed.