Leyline API
This page and the API reference are the
public contract of leyline-http (import name leyline). The API reference
lists every public item and is generated from the compiler; cargo truesight check fails when it no longer matches the code. This page maps each job to
its call and to the guide that explains it, and holds the error kinds and the
rules the API follows.
The bench-internals feature and the leyline_unstable_bssl compiler flag
expose internals for Leyline’s own tests and the BoringSSL context builder.
Neither is part of the contract, and the API reference leaves both out.
Overview
one-off leyline::get(url) one GET from a plain session
session Session::new() plain client, cannot fail
Session::browser(browser) browser session, cannot fail
Session::builder() → SessionBuilder → build() → Session (no browser: plain client)
session.with_proxy(config) clone that shares the pool, other proxy
session.fresh_pool() clone with a new pool and TLS session cache
session.with_cookie_jar(jar) clone that shares the pool, other jar
session.with_redirect(policy) clone that shares the pool, other redirect policy
session.with_identity(id) clone with another identity, own pool partition
session.with_base_url(url) clone that shares the pool, other base URL
session.tab() → Tab current page, initiator for each request
session.shutdown() · is_shut_down() stop every clone and derived session
session.state() → SessionState TLS tickets, Alt-Svc, HSTS
Device::capture / open / save_to / load_from one saved account device
device.autosave(&session, path, interval or DeviceAutosaveOptions) → DeviceAutosave · update / track / device / flush / shutdown
device.tab(&session) → Tab tab on the saved page
crawl SessionBuilder::host_limits(HostLimits) · proxy_pool(ProxyPool) · RequestBuilder::tag
HostLimits: max_in_flight · per_second · host · max_total_in_flight · pause_on · pause_for · max_pause
ProxyPool::identified · rotate_on_block · Session::host_stats · BlockRules::statuses
Session::robots(url, agent) → Robots: allows · allows_use(url, Purpose) · crawl_delay · sitemaps · source
Session::crawl(seed) → Crawl: respect(Respect) · ignore_robots · scope(Scope) · limit · host_limits · next → CrawlPage · stats → CrawlStats (feature `html`)
request session.get / post / put / patch / delete / head / request(Method, url) → RequestBuilder
send RequestBuilder.send() or .await → Response
RequestBuilder.download(path, limit) → bytes written
RequestBuilder.pages() → Pages follows Link rel="next"
session.execute(http::Request<Body>) for prebuilt requests and tower
read Response: status · headers · header · text · bytes · json · read_until
raw: into_stream · copy_to decoded and capped: into_decoded_stream · copy_decoded_to
Response: url · redirect_chain · version · timing · attempts · proxy
Response: link · links · relay_headers(RelayBody) · block · download_to
fail Error · err.kind() → Kind · err.category() → ErrorCategory · gateway_status()
RequestBuilder::error_for_status · err.status / body / body_text / headers / header / retry_after / retries_exhausted / attempts / proxy
Error::find(&dyn std::error::Error) leyline error under other layers
policy TimeoutConfig · RetryPolicy · RedirectPolicy (session default, request override)
RetryPolicy: retry_if · wait_header · retry_unsent · skip_blocks · max_retry_after
TimeoutConfig: body · error_body CompressionConfig: max_body_size · max_error_body
identity Browser · Platform · ChromiumBrand · Identity
observe Trace hooks · Trace::summary · trace::Metrics · trace::Fanout · Response::timing · Response::tls · Response::audit
tower LeylineService (feature `tower`) · relay_headers(&HeaderMap, RelayBody) · redact_url
html html::forms · Form::find · FormEnctype · Tab::submit_form · html::meta · html::links → Anchor · html::text · html::canonical · html::Directives (feature `html`)
test testing::TestServer::http / https / http_on · TestResponse::delay / chunks / close
RecordedRequest: raw · request_line · header_values · header_count · text (feature `test-util`)
One function per job. A setter that takes a config type accepts
impl Into<Config> where a shorthand exists, so the common case stays one
call:
#![allow(unused)]
fn main() {
use std::time::Duration;
use leyline::{Browser, Family, Platform, ProtocolPolicy, Session, TimeoutConfig};
async fn run() -> leyline::Result<()> {
let session = Session::browser(Browser::default());
let body = session.get("https://example.com/").await?.text().await?;
let session = Session::builder()
.browser(Browser::latest(Family::Firefox))
.platform(Platform::MacOS)
.protocol(ProtocolPolicy::Http2)
.timeout(Duration::from_secs(15))
.proxy("http://user:pass@host:8080")
.build()?;
let resp = session
.post("https://example.com/api")
.header("x-trace", "1")
.json(&serde_json::json!({ "q": 1 }))
.timeout(TimeoutConfig::new().total(Duration::from_secs(5)))
.await?;
drop((body, resp));
Ok(())
}
}
Where each job is documented
Sessions and identities
| Job | Call | Guide |
|---|---|---|
| Plain or browser session | Session::new, Session::browser, Session::builder | Sessions |
| Newest or pinned browser | Browser::latest, Browser::get, Browser::Chrome148 | Choose a browser |
| Platform and brand | SessionBuilder::platform, brand | Choose a platform |
| Token and base URL | bearer_auth, base_url, with_base_url | Sessions |
| Languages and user agent | languages, user_agent | Set the languages |
| Derived sessions | with_proxy, with_cookie_jar, with_redirect, with_identity, fresh_pool | Derive a session |
| Stop every clone | Session::shutdown, Error::is_shut_down | Stop a session |
| Save TLS tickets, Alt-Svc, HSTS | Session::state, SessionState::restore_into | Sessions |
| Read what a session sends | Session::identity, Browser::identity, SessionIdentity::profile_id | Sessions |
| Mix and rotate identities | Identity::locked, rotate_tls, rotate_hello, switch_family | Fingerprints |
| Detect a profile change | expect_profile_id, Error::is_profile_changed | Choosing a profile |
| Load or build a profile | ProfileRegistry::load, BrowserProfile::from_toml, from_fingerprint | Profiles |
| Saved account device | Device, DeviceAutosave | Accounts |
| Device save intervals | Device::autosave, DeviceAutosaveOptions | Save the device as it changes |
Requests and responses
| Job | Call | Guide |
|---|---|---|
| Methods, headers, query, bodies | RequestBuilder | Requests |
| Header order and merge | SessionBuilder::headers, RequestBuilder::header | Requests |
| Page initiator and tabs | initiator, Session::tab, Tab | Requests |
Prebuilt http::Request | Session::execute | Requests |
| Status, headers, bodies | Response | Responses |
| Status as an error | error_for_status, error_for_status_ref | Responses |
| Body kept in a status error | CompressionConfig::max_error_body, TimeoutConfig::error_body | Responses |
| Response body cap | CompressionConfig::max_body_size | Responses |
Link pagination | RequestBuilder::pages, Response::links | Responses |
| Stream, download, stop at a marker | .stream(), into_decoded_stream, download, read_until | Streaming |
| Redirect policy | RedirectPolicy | Redirects |
| Cookies | cookie::Jar, Jar::save_to, Jar::autosave | Cookies |
| WebSocket | Session::websocket | WebSocket |
| HTML forms, meta, links | html::forms, Form::set, FormEnctype, Tab::submit_form, html::meta, html::links | Log in with the page’s form |
| Visible text of a page | html::text | Read the text of a page |
| Page robots directives, canonical URL | html::Directives::read, html::canonical | Obey a page’s own rules |
| Cancel a request | drop, Session::shutdown | Cancellation |
Policy, transport, and observation
| Job | Call | Guide |
|---|---|---|
| Timeouts and retries | TimeoutConfig, RetryPolicy | Retries and timeouts |
| Retry on a condition, wait on a header | RetryPolicy::retry_if, wait_header, WaitFormat | Retry on a condition |
| Retry a request the server did not process | RetryPolicy::retry_unsent | The idempotency rule |
| Cap a server-requested wait | RetryPolicy::max_retry_after | Retry-After wins |
| Return a block without a retry | RetryPolicy::skip_blocks | Retry a block through another proxy |
| Bound the body read | TimeoutConfig::body | The timeouts |
| Proxies and proxy pools | ProxyConfig, ProxyUrl, ProxyPool | Proxies |
| Host limits, blocks, crawl metrics | HostLimits, BlockRules, host_stats, trace::Metrics | Crawling |
| Pause a host on a status | HostLimits::pause_on, pause_for, max_pause | Limit each host |
| Read and obey robots.txt | Session::robots, Robots::parse, Robots::allows, Purpose | Obey robots.txt |
| Crawl a site | Session::crawl, Crawl, Respect, Scope, CrawlPage, CrawlStats, SkipReason | Crawl a site |
| DNS, sockets, pool, Happy Eyeballs | DnsConfig, SocketConfig, PoolConfig, HappyEyeballsConfig | Network |
| Trust roots, pins, TLS floor | TlsTrustConfig | TLS trust |
| HTTP/3 | ProtocolPolicy | HTTP/3 |
| Logs and trace hooks | Trace, TracingTrace, trace::Fanout | Logging and tracing |
| One event per request | Trace::summary, trace::Summary | One event per request |
| Fingerprint audit | audit(true), Response::audit, AuditData::compare | Fingerprints |
| Errors | Error, Kind, ErrorCategory | Errors |
| Services, Tower, axum | LeylineService, relay_headers, redact_url | Service integration |
| Tests | testing::TestServer, RecordedRequest::raw, header_values | Testing |
Errors
One error type: leyline::Error, Send + Sync + 'static. Read
err.kind() for the Kind below, or err.category() for the
ErrorCategory that groups it for logs, metrics, and gateway statuses.
Errors covers categories, predicates, attempts, source
errors, and status mapping.
| Kind | Meaning |
|---|---|
Request | HTTP-level failure (bad header, malformed request) |
Redirect | A redirect could not be followed: a custom policy passed 32 hops, a Location scheme other than http or https, or a redirect that must resend a streamed request body. A policy that stops returns the 3xx response and is not an error |
Status | error_for_status rejected the status |
Body | A body cannot be used: a response body over max_body_size or a caller limit, a response body already taken, a request body that cannot be compressed or resent, or a WebSocket protocol violation or oversized message |
Decode | Decompression failed: bad data, a truncated stream, or more than 4 codings |
Timeout | total or response_header expired, or read or body expired in bytes, text, or json |
Connect | DNS resolution or TCP connect failed, or the connect limit expired during DNS, TCP, a proxy handshake, or a TLS or HTTP/3 handshake. An expired limit also makes is_timeout() true |
Tls | TLS handshake or certificate verification failed |
Http2 | HTTP/2 protocol failure |
Http3 | HTTP/3 protocol or handshake failure |
Proxy | Proxy dial, handshake, authentication, or CONNECT failed, the proxy could not reach the origin, or a request of a session that rejected an invalid environment proxy |
Io | Socket I/O, or an HTTP/1.1 response that breaks the wire format; err.io() then has kind InvalidData |
Config | Invalid session, request, TLS profile, trust store, or proxy configuration, including an invalid environment proxy at build() |
Url | URL parse failure |
Json | JSON serialize or deserialize failure |
The profile loaders return profile::ProfileError, and
multipart::Form::file and Part::file return std::io::Result; neither
has a Kind.
Features
Default: charset, compression-gzip, compression-brotli,
compression-deflate, compression-zstd, multipart, websocket,
http3, html. Opt-in: socks, tower, test-util, bench-internals.
full enables every feature except test-util and bench-internals. See
Features and targets.
API shape
- One function per job, and one owner each for timing, retries, redirects, cookies, and body decoding.
- Config types (
TimeoutConfig,RetryPolicy,RedirectPolicy,ProxyConfig,DnsConfig,TlsTrustConfig,PoolConfig,SocketConfig,HappyEyeballsConfig,CompressionConfig,WebSocketConfig) have private fields and consuming setters named after the field, with nowith_prefix. A setter for an optional value takesimpl Into<Option<T>>, andNoneturns the setting off;RetryPolicy::max_retry_aftertakes aDuration:transient()sets it to 60 s, andnone()has no cap. A setter that adds to a list starts withadd_or appends one item (rule,on_status). - Every
SessionBuilderconfig setter replaces the whole value. Start from::new(), which carries the defaults. A request setter merges over the session value field by field. - Public enums, and public structs with public fields, are
#[non_exhaustive],CookieandSameSiteincluded. The exceptions areaudit::Ja3Input,Ja4Input, andJa4hInput, which callers fill with a struct literal. - Builder input errors surface at
build(),send(), orconnect(), never eagerly.Identityis a value type, so its checks run at the call. IntoUrlis sealed.Session::new()andSession::browser(..)never fail: a trust-store problem fails later requests withKind::Tls, and an invalid environment proxy fails them withKind::Proxy.- A patch release never removes a
Browservariant. A retired profile keeps its variant, marked#[deprecated]through its[meta] deprecatednote, until a minor release removes it.Browser::latestandBrowser::for_platformnever return a deprecated profile. Browser,Family,Platform, andChromiumBrandserialize as the stable lowercase id thatid()returns, andFromStrreads it back.
Profile schema
BrowserProfile and the leyline::profile types mirror the bundled
profiles/<family>/<version>.toml files; field names are the TOML keys.
profiles/bare.toml holds the bare profile and profiles/platforms.toml
the per-platform TCP values. The [meta] keys capture, variant,
hello, platform_browser, deprecated, and verified_at are valid and
ignored at runtime; they are not fields of ProfileMeta, and the build reads
the first five to generate Browser. The schema is part of the API: a patch
release can add fields, and removing or renaming one needs a minor release.
See Profiles.
Do not add
- A second constructor for a browser, brand, or platform
(
Session::chrome(),SessionBuilder::macos()). Usebrowser,platform,brand. - A per-field copy of a config type on
SessionBuilderorRequestBuilder(connect_timeout,resolve_host,add_root_certificate_der,max_redirects). Add the field to the config type. - A second setter with other header semantics (
append_header,set_header) or a typed shortcut for one header (referer).SessionBuilder::user_agentis the one session shortcut, because it replaces a profile default. - A second request type.
Session::executetakeshttp::Request<Body>. - A second middleware model inside the send path. Wrap
LeylineServicewith tower layers. - A second body reader (
text_utf8,into_text,as_bytes) or a secondContent-Encodingdecoder. - A second cookie store, such as a per-response cookie map.
- A getter that copies session configuration back out. The exceptions
report the state of a built session:
Session::identity,Session::proxy_url,Session::cookies,Session::pool_stats, andSession::host_stats. - A second timing, retry, or redirect owner.
- A browser, version, or brand table in Rust. Profile data owns it.