Skip to main content

rust_web_server/http_client/
mod.rs

1//! Outbound HTTP/1.1 client.
2//!
3//! A minimal synchronous HTTP/1.1 + HTTPS client with no third-party HTTP
4//! dependency.  TLS is backed by `rustls` (the same crate used by the server's
5//! inbound TLS stack).
6//!
7//! # Plain HTTP (always available)
8//!
9//! ```rust,no_run
10//! use rust_web_server::http_client::Client;
11//!
12//! let resp = Client::new()
13//!     .get("http://httpbin.org/get")
14//!     .header("X-Request-Id", "abc123")
15//!     .timeout_ms(5_000)
16//!     .send()
17//!     .unwrap();
18//!
19//! assert!(resp.is_success());
20//! println!("{}", resp.text().unwrap());
21//! ```
22//!
23//! # HTTPS
24//!
25//! Requires the `http-client` feature (or `http2`/`http3`, which already pull
26//! in `rustls`):
27//!
28//! ```toml
29//! [dependencies]
30//! rust-web-server = { version = "17", features = ["http-client"] }
31//! ```
32//!
33//! Then use exactly the same API — the scheme in the URL selects the transport.
34//!
35//! # Async client
36//!
37//! Gated on the `http2` feature:
38//!
39//! ```rust,no_run
40//! # #[cfg(feature = "http2")]
41//! # async fn example() -> Result<(), rust_web_server::http_client::HttpClientError> {
42//! use rust_web_server::http_client::AsyncClient;
43//!
44//! let resp = AsyncClient::new()
45//!     .get("https://api.example.com/users")
46//!     .header("Authorization", "Bearer tok_…")
47//!     .send()
48//!     .await?;
49//!
50//! println!("{}", resp.text()?);
51//! # Ok(())
52//! # }
53//! ```
54
55#[cfg(test)]
56mod tests;
57
58use std::io::{Read, Write};
59#[cfg(not(target_arch = "wasm32"))]
60use std::net::TcpStream;
61#[cfg(not(target_arch = "wasm32"))]
62use std::time::Duration;
63
64#[cfg(any(feature = "http-client", feature = "http2"))]
65use std::sync::Arc;
66
67// ── form encoding ─────────────────────────────────────────────────────────────
68
69/// Percent-encode a single `application/x-www-form-urlencoded` value.
70fn form_encode(s: &str) -> String {
71    let mut out = String::with_capacity(s.len());
72    for b in s.bytes() {
73        match b {
74            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
75                out.push(b as char)
76            }
77            _ => out.push_str(&format!("%{:02X}", b)),
78        }
79    }
80    out
81}
82
83/// Encode `pairs` as an `application/x-www-form-urlencoded` body.
84fn form_urlencode(pairs: &[(&str, &str)]) -> String {
85    pairs
86        .iter()
87        .map(|(k, v)| format!("{}={}", form_encode(k), form_encode(v)))
88        .collect::<Vec<_>>()
89        .join("&")
90}
91
92// ── Error type ────────────────────────────────────────────────────────────────
93
94/// Error returned by the HTTP client.
95#[derive(Debug)]
96pub struct HttpClientError(pub String);
97
98impl std::fmt::Display for HttpClientError {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        f.write_str(&self.0)
101    }
102}
103
104impl std::error::Error for HttpClientError {}
105
106// ── URL parser ────────────────────────────────────────────────────────────────
107
108struct ParsedUrl {
109    scheme: String,
110    host: String,
111    port: u16,
112    path_and_query: String,
113}
114
115impl ParsedUrl {
116    fn parse(url: &str) -> Result<Self, HttpClientError> {
117        // Expect "scheme://rest"
118        let rest = if let Some(r) = url.strip_prefix("https://") {
119            ("https", r)
120        } else if let Some(r) = url.strip_prefix("http://") {
121            ("http", r)
122        } else {
123            return Err(HttpClientError(format!(
124                "unsupported or missing URL scheme in '{url}'"
125            )));
126        };
127
128        let (scheme, authority_and_path) = rest;
129        let default_port: u16 = if scheme == "https" { 443 } else { 80 };
130
131        // Split authority from path at the first '/'
132        let (authority, path_and_query) = match authority_and_path.find('/') {
133            Some(idx) => {
134                let (a, p) = authority_and_path.split_at(idx);
135                (a, p.to_string())
136            }
137            None => (authority_and_path, "/".to_string()),
138        };
139
140        // Split host and optional port
141        let (host, port) = if let Some(bracket_end) = authority.find(']') {
142            // IPv6 literal: [::1]:port
143            let host = &authority[..=bracket_end];
144            let port_part = &authority[bracket_end + 1..];
145            let port = if let Some(p) = port_part.strip_prefix(':') {
146                p.parse::<u16>().map_err(|_| {
147                    HttpClientError(format!("invalid port in URL '{url}'"))
148                })?
149            } else {
150                default_port
151            };
152            (host.to_string(), port)
153        } else {
154            match authority.rfind(':') {
155                Some(idx) => {
156                    let port_str = &authority[idx + 1..];
157                    let port = port_str.parse::<u16>().map_err(|_| {
158                        HttpClientError(format!("invalid port in URL '{url}'"))
159                    })?;
160                    (authority[..idx].to_string(), port)
161                }
162                None => (authority.to_string(), default_port),
163            }
164        };
165
166        if host.is_empty() {
167            return Err(HttpClientError(format!("missing host in URL '{url}'")));
168        }
169
170        Ok(ParsedUrl {
171            scheme: scheme.to_string(),
172            host,
173            port,
174            path_and_query,
175        })
176    }
177}
178
179/// Resolve `location` against `base_url`.  If `location` is already absolute
180/// it is returned as-is.  A path starting with '/' is resolved against the
181/// origin of `base_url`.
182fn resolve_url(base_url: &str, location: &str) -> String {
183    if location.starts_with("http://") || location.starts_with("https://") {
184        return location.to_string();
185    }
186    // relative — reconstruct origin from base
187    if let Ok(base) = ParsedUrl::parse(base_url) {
188        let default_port = if base.scheme == "https" { 443 } else { 80 };
189        let port_str = if base.port == default_port {
190            String::new()
191        } else {
192            format!(":{}", base.port)
193        };
194        if location.starts_with('/') {
195            return format!("{}://{}{}{}", base.scheme, base.host, port_str, location);
196        }
197        // relative path — resolve against directory of current path
198        let base_path = base.path_and_query;
199        let dir = match base_path.rfind('/') {
200            Some(i) => &base_path[..=i],
201            None => "/",
202        };
203        return format!(
204            "{}://{}{}{}{}",
205            base.scheme, base.host, port_str, dir, location
206        );
207    }
208    location.to_string()
209}
210
211// ── Response ──────────────────────────────────────────────────────────────────
212
213/// HTTP response from the outbound client.
214#[derive(Debug)]
215pub struct Response {
216    status: u16,
217    headers: Vec<(String, String)>,
218    body: Vec<u8>,
219}
220
221impl Response {
222    /// HTTP status code.
223    pub fn status(&self) -> u16 {
224        self.status
225    }
226
227    /// `true` if the status code is in the 200–299 range.
228    pub fn is_success(&self) -> bool {
229        (200..300).contains(&self.status)
230    }
231
232    /// `true` if the status code is 301, 302, 303, 307, or 308.
233    pub fn is_redirect(&self) -> bool {
234        matches!(self.status, 301 | 302 | 303 | 307 | 308)
235    }
236
237    /// Look up a response header by name (case-insensitive).
238    pub fn header(&self, name: &str) -> Option<&str> {
239        let lower = name.to_lowercase();
240        self.headers
241            .iter()
242            .find(|(k, _)| k.to_lowercase() == lower)
243            .map(|(_, v)| v.as_str())
244    }
245
246    /// All response headers, in the order the server sent them. Use this
247    /// when you need to enumerate every header rather than look one up by
248    /// name — e.g. forwarding a response verbatim.
249    pub fn headers(&self) -> &[(String, String)] {
250        &self.headers
251    }
252
253    /// Raw response body bytes.
254    pub fn bytes(&self) -> &[u8] {
255        &self.body
256    }
257
258    /// Decode the body as UTF-8.
259    pub fn text(&self) -> Result<String, HttpClientError> {
260        String::from_utf8(self.body.clone())
261            .map_err(|e| HttpClientError(format!("body is not valid UTF-8: {e}")))
262    }
263
264    /// Parse the body as JSON.
265    #[cfg(feature = "serde")]
266    pub fn json<T: serde::de::DeserializeOwned>(&self) -> Result<T, HttpClientError> {
267        serde_json::from_slice(&self.body)
268            .map_err(|e| HttpClientError(format!("JSON parse error: {e}")))
269    }
270}
271
272// ── Wire-level helpers ────────────────────────────────────────────────────────
273
274/// Build the HTTP/1.1 request bytes.
275fn build_request_bytes(
276    method: &str,
277    path_and_query: &str,
278    host: &str,
279    headers: &[(String, String)],
280    body: &Option<Vec<u8>>,
281) -> Vec<u8> {
282    let mut out: Vec<u8> = Vec::new();
283
284    // Status line
285    let _ = write!(
286        out,
287        "{method} {path_and_query} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\nUser-Agent: rust-web-server/{}\r\n",
288        env!("CARGO_PKG_VERSION"),
289    );
290
291    // Content-Length (before custom headers, so callers can override)
292    if let Some(b) = body {
293        if !b.is_empty() {
294            let _ = write!(out, "Content-Length: {}\r\n", b.len());
295        }
296    }
297
298    // Custom headers
299    for (k, v) in headers {
300        let _ = write!(out, "{k}: {v}\r\n");
301    }
302
303    out.extend_from_slice(b"\r\n");
304
305    if let Some(b) = body {
306        out.extend_from_slice(b);
307    }
308
309    out
310}
311
312/// Parse an HTTP/1.1 response from any `Read` source.
313fn read_response(stream: &mut dyn Read, is_head: bool) -> Result<Response, HttpClientError> {
314    let mut buf: Vec<u8> = Vec::with_capacity(8192);
315    let mut tmp = [0u8; 4096];
316
317    // Read until we find the end of headers (\r\n\r\n)
318    let header_end = loop {
319        let n = stream
320            .read(&mut tmp)
321            .map_err(|e| HttpClientError(format!("read error: {e}")))?;
322        if n == 0 {
323            if buf.is_empty() {
324                return Err(HttpClientError(
325                    "server closed connection without sending a response".into(),
326                ));
327            }
328            // EOF before \r\n\r\n — try to parse whatever we got
329            break buf.len();
330        }
331        buf.extend_from_slice(&tmp[..n]);
332        if let Some(pos) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
333            break pos + 4;
334        }
335    };
336
337    // Split header block
338    let header_block = std::str::from_utf8(&buf[..header_end])
339        .map_err(|_| HttpClientError("response headers are not valid UTF-8".into()))?;
340
341    let mut lines = header_block.lines();
342
343    // Status line
344    let status_line = lines
345        .next()
346        .ok_or_else(|| HttpClientError("empty response".into()))?;
347    let status = parse_status(status_line)?;
348
349    // Headers
350    let response_headers: Vec<(String, String)> = lines
351        .filter_map(|line| {
352            let mut parts = line.splitn(2, ':');
353            let name = parts.next()?.trim().to_string();
354            let value = parts.next()?.trim().to_string();
355            if name.is_empty() {
356                None
357            } else {
358                Some((name, value))
359            }
360        })
361        .collect();
362
363    // Body — already-buffered bytes beyond the header block
364    let mut body = buf[header_end..].to_vec();
365
366    if !is_head {
367        // Determine body reading strategy from headers
368        let transfer_encoding = response_headers
369            .iter()
370            .find(|(k, _)| k.to_lowercase() == "transfer-encoding")
371            .map(|(_, v)| v.to_lowercase());
372
373        let content_length: Option<usize> = response_headers
374            .iter()
375            .find(|(k, _)| k.to_lowercase() == "content-length")
376            .and_then(|(_, v)| v.trim().parse().ok());
377
378        if transfer_encoding
379            .as_deref()
380            .map(|te| te.contains("chunked"))
381            .unwrap_or(false)
382        {
383            // Read remaining chunked data then decode
384            loop {
385                let n = stream
386                    .read(&mut tmp)
387                    .map_err(|e| HttpClientError(format!("read error: {e}")))?;
388                if n == 0 {
389                    break;
390                }
391                body.extend_from_slice(&tmp[..n]);
392            }
393            body = decode_chunked(&body)?;
394        } else if let Some(len) = content_length {
395            while body.len() < len {
396                let n = stream
397                    .read(&mut tmp)
398                    .map_err(|e| HttpClientError(format!("read error: {e}")))?;
399                if n == 0 {
400                    break;
401                }
402                body.extend_from_slice(&tmp[..n]);
403            }
404            body.truncate(len);
405        } else {
406            // Read until EOF (Connection: close)
407            loop {
408                let n = stream
409                    .read(&mut tmp)
410                    .map_err(|e| HttpClientError(format!("read error: {e}")))?;
411                if n == 0 {
412                    break;
413                }
414                body.extend_from_slice(&tmp[..n]);
415            }
416        }
417    } else {
418        body.clear();
419    }
420
421    Ok(Response {
422        status,
423        headers: response_headers,
424        body,
425    })
426}
427
428fn parse_status(line: &str) -> Result<u16, HttpClientError> {
429    // "HTTP/1.x 200 Reason ..."
430    let mut parts = line.splitn(3, ' ');
431    let _version = parts
432        .next()
433        .ok_or_else(|| HttpClientError("malformed status line".into()))?;
434    let code_str = parts
435        .next()
436        .ok_or_else(|| HttpClientError("missing status code".into()))?;
437    code_str
438        .parse::<u16>()
439        .map_err(|_| HttpClientError(format!("invalid status code '{code_str}'")))
440}
441
442/// Decode chunked transfer encoding.
443fn decode_chunked(data: &[u8]) -> Result<Vec<u8>, HttpClientError> {
444    let mut out = Vec::new();
445    let mut pos = 0;
446
447    while pos < data.len() {
448        // Find end of chunk-size line
449        let line_end = data[pos..]
450            .windows(2)
451            .position(|w| w == b"\r\n")
452            .ok_or_else(|| HttpClientError("invalid chunked encoding: missing CRLF".into()))?;
453        let size_line = std::str::from_utf8(&data[pos..pos + line_end])
454            .map_err(|_| HttpClientError("chunked size is not ASCII".into()))?
455            .trim();
456        // Strip optional chunk extensions (;ext)
457        let size_str = size_line.split(';').next().unwrap_or("").trim();
458        let chunk_size = usize::from_str_radix(size_str, 16)
459            .map_err(|_| HttpClientError(format!("invalid chunk size '{size_str}'")))?;
460        pos += line_end + 2; // skip size line + CRLF
461
462        if chunk_size == 0 {
463            break; // last chunk
464        }
465
466        let end = pos + chunk_size;
467        if end > data.len() {
468            return Err(HttpClientError("chunked body truncated".into()));
469        }
470        out.extend_from_slice(&data[pos..end]);
471        pos = end + 2; // skip trailing CRLF after chunk data
472    }
473
474    Ok(out)
475}
476
477// ── TLS connector (sync) ──────────────────────────────────────────────────────
478
479#[cfg(all(any(feature = "http-client", feature = "http2"), not(target_arch = "wasm32")))]
480fn tls_connect(
481    host: &str,
482    tcp: TcpStream,
483) -> Result<rustls::StreamOwned<rustls::ClientConnection, TcpStream>, HttpClientError> {
484    use rustls::pki_types::ServerName;
485
486    let root_store =
487        rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
488    let config = Arc::new(
489        crate::tls_client_builder()
490            .with_root_certificates(root_store)
491            .with_no_client_auth(),
492    );
493    let server_name = ServerName::try_from(host.to_string())
494        .map_err(|e| HttpClientError(format!("invalid hostname '{host}': {e}")))?;
495    let conn = rustls::ClientConnection::new(config, server_name)
496        .map_err(|e| HttpClientError(e.to_string()))?;
497    Ok(rustls::StreamOwned::new(conn, tcp))
498}
499
500// ── Core send (one hop, no redirect) ─────────────────────────────────────────
501
502#[cfg(not(target_arch = "wasm32"))]
503fn send_once(
504    method: &str,
505    parsed: &ParsedUrl,
506    headers: &[(String, String)],
507    body: &Option<Vec<u8>>,
508    timeout_ms: u64,
509) -> Result<Response, HttpClientError> {
510    let addr = format!("{}:{}", parsed.host, parsed.port);
511    let timeout = Duration::from_millis(timeout_ms);
512
513    // Resolve + connect
514    let sock_addr = addr
515        .parse::<std::net::SocketAddr>()
516        .or_else(|_| {
517            use std::net::ToSocketAddrs;
518            addr.to_socket_addrs()
519                .map_err(|e| HttpClientError(format!("DNS lookup for '{addr}' failed: {e}")))?
520                .next()
521                .ok_or_else(|| HttpClientError(format!("no address for '{addr}'")))
522        })
523        .map_err(|e: HttpClientError| e)?;
524
525    let tcp = TcpStream::connect_timeout(&sock_addr, timeout)
526        .map_err(|e| HttpClientError(format!("connect to '{addr}' failed: {e}")))?;
527    tcp.set_read_timeout(Some(timeout))
528        .map_err(|e| HttpClientError(e.to_string()))?;
529    tcp.set_write_timeout(Some(timeout))
530        .map_err(|e| HttpClientError(e.to_string()))?;
531
532    let request_bytes =
533        build_request_bytes(method, &parsed.path_and_query, &parsed.host, headers, body);
534
535    let is_head = method.eq_ignore_ascii_case("HEAD");
536
537    // Dispatch on scheme
538    #[cfg(any(feature = "http-client", feature = "http2"))]
539    if parsed.scheme == "https" {
540        let mut tls_stream = tls_connect(&parsed.host, tcp)?;
541        tls_stream
542            .write_all(&request_bytes)
543            .map_err(|e| HttpClientError(format!("write error: {e}")))?;
544        return read_response(&mut tls_stream, is_head);
545    }
546
547    // Plain HTTP
548    let mut stream = tcp;
549    stream
550        .write_all(&request_bytes)
551        .map_err(|e| HttpClientError(format!("write error: {e}")))?;
552    read_response(&mut stream, is_head)
553}
554
555/// wasm32 backend: no `TcpStream`/`rustls` in a wasi:http guest, so outbound
556/// requests go through the `wasi:http` `outgoing-handler` interface instead
557/// (imported by the same `wasi:http/proxy` world `rws-wasm-shim` already
558/// builds against for the incoming side — see spec/WASM_SHIM.md Phase 2
559/// item 7). The host performs the actual connect/TLS; this function only
560/// builds the outgoing-request, blocks on the response future, and reads
561/// the result back into the same `Response` shape the native backend uses.
562///
563/// Not exercised by `cargo test` — constructing real `wasi:http` resources
564/// (`OutgoingRequest`, `outgoing_handler::handle`, ...) requires a live WASI
565/// host underneath, same limitation `rws-wasm-shim`'s own tests document.
566/// Verified instead against a real `wasmtime serve` process: both plain HTTP
567/// and HTTPS (TLS terminated by the host, not this code) round-tripped
568/// correctly through this exact `Client`/`RequestBuilder` API.
569#[cfg(target_arch = "wasm32")]
570fn send_once(
571    method: &str,
572    parsed: &ParsedUrl,
573    headers: &[(String, String)],
574    body: &Option<Vec<u8>>,
575    timeout_ms: u64,
576) -> Result<Response, HttpClientError> {
577    use wasip2::http::outgoing_handler;
578    use wasip2::http::types::{Fields, OutgoingBody, OutgoingRequest, RequestOptions, Scheme};
579
580    let wasi_headers = Fields::new();
581    for (name, value) in headers {
582        let _ = wasi_headers.append(name, value.as_bytes());
583    }
584
585    let request = OutgoingRequest::new(wasi_headers);
586    request
587        .set_method(&to_wasi_method(method))
588        .map_err(|_| HttpClientError(format!("invalid method '{method}'")))?;
589    let scheme = if parsed.scheme == "https" { Scheme::Https } else { Scheme::Http };
590    request
591        .set_scheme(Some(&scheme))
592        .map_err(|_| HttpClientError(format!("invalid scheme '{}'", parsed.scheme)))?;
593    let authority = format!("{}:{}", parsed.host, parsed.port);
594    request
595        .set_authority(Some(&authority))
596        .map_err(|_| HttpClientError(format!("invalid authority '{authority}'")))?;
597    request
598        .set_path_with_query(Some(&parsed.path_and_query))
599        .map_err(|_| HttpClientError(format!("invalid path '{}'", parsed.path_and_query)))?;
600
601    let outgoing_body = request
602        .body()
603        .map_err(|_| HttpClientError("outgoing-request body handle taken twice".to_string()))?;
604    if let Some(bytes) = body {
605        let mut out = outgoing_body
606            .write()
607            .map_err(|_| HttpClientError("outgoing-body write stream taken twice".to_string()))?;
608        out.write_all(bytes)
609            .map_err(|e| HttpClientError(format!("write error: {e}")))?;
610    }
611    OutgoingBody::finish(outgoing_body, None)
612        .map_err(|e| HttpClientError(format!("failed to finish request body: {e:?}")))?;
613
614    // Best-effort — a host that doesn't support transport timeouts returns an
615    // error from these setters, which we don't treat as fatal (the request
616    // just proceeds without a host-enforced timeout in that case).
617    let options = RequestOptions::new();
618    let timeout_ns = timeout_ms.saturating_mul(1_000_000);
619    let _ = options.set_connect_timeout(Some(timeout_ns));
620    let _ = options.set_first_byte_timeout(Some(timeout_ns));
621    let _ = options.set_between_bytes_timeout(Some(timeout_ns));
622
623    let future_response = outgoing_handler::handle(request, Some(options))
624        .map_err(|e| HttpClientError(format!("failed to dispatch request: {e:?}")))?;
625
626    future_response.subscribe().block();
627
628    let incoming_response = match future_response.get() {
629        Some(Ok(Ok(response))) => response,
630        Some(Ok(Err(code))) => return Err(HttpClientError(format!("{code:?}"))),
631        Some(Err(())) => {
632            return Err(HttpClientError("response already retrieved".to_string()))
633        }
634        None => {
635            return Err(HttpClientError(
636                "response not ready after blocking on it".to_string(),
637            ))
638        }
639    };
640
641    let status = incoming_response.status();
642    let resp_headers = incoming_response
643        .headers()
644        .entries()
645        .into_iter()
646        .map(|(name, value)| (name, String::from_utf8_lossy(&value).to_string()))
647        .collect();
648
649    let mut resp_body = Vec::new();
650    if !method.eq_ignore_ascii_case("HEAD") {
651        if let Ok(incoming_body) = incoming_response.consume() {
652            if let Ok(mut stream) = incoming_body.stream() {
653                let _ = stream.read_to_end(&mut resp_body);
654            }
655        }
656    }
657
658    Ok(Response { status, headers: resp_headers, body: resp_body })
659}
660
661#[cfg(target_arch = "wasm32")]
662fn to_wasi_method(method: &str) -> wasip2::http::types::Method {
663    use wasip2::http::types::Method;
664    match method.to_ascii_uppercase().as_str() {
665        "GET" => Method::Get,
666        "HEAD" => Method::Head,
667        "POST" => Method::Post,
668        "PUT" => Method::Put,
669        "DELETE" => Method::Delete,
670        "CONNECT" => Method::Connect,
671        "OPTIONS" => Method::Options,
672        "TRACE" => Method::Trace,
673        "PATCH" => Method::Patch,
674        other => Method::Other(other.to_string()),
675    }
676}
677
678// ── Client ────────────────────────────────────────────────────────────────────
679
680/// Synchronous HTTP/1.1 client.
681///
682/// Construct with [`Client::new()`], then call one of the method helpers
683/// (`.get()`, `.post()`, …) to get a [`RequestBuilder`], configure it, and
684/// call `.send()`.
685pub struct Client {
686    timeout_ms: u64,
687    max_redirects: u8,
688}
689
690impl Client {
691    /// Create a client with default settings:
692    /// - `timeout_ms`: 30 000 (30 seconds)
693    /// - `max_redirects`: 10
694    pub fn new() -> Self {
695        Self {
696            timeout_ms: 30_000,
697            max_redirects: 10,
698        }
699    }
700
701    /// Override the per-request timeout (connect + read combined).
702    pub fn timeout_ms(mut self, ms: u64) -> Self {
703        self.timeout_ms = ms;
704        self
705    }
706
707    /// Maximum number of redirects to follow (default: 10).
708    pub fn max_redirects(mut self, n: u8) -> Self {
709        self.max_redirects = n;
710        self
711    }
712
713    /// Start building a GET request.
714    pub fn get(&self, url: &str) -> RequestBuilder<'_> {
715        self.request("GET", url)
716    }
717
718    /// Start building a POST request.
719    pub fn post(&self, url: &str) -> RequestBuilder<'_> {
720        self.request("POST", url)
721    }
722
723    /// Start building a PUT request.
724    pub fn put(&self, url: &str) -> RequestBuilder<'_> {
725        self.request("PUT", url)
726    }
727
728    /// Start building a PATCH request.
729    pub fn patch(&self, url: &str) -> RequestBuilder<'_> {
730        self.request("PATCH", url)
731    }
732
733    /// Start building a DELETE request.
734    pub fn delete(&self, url: &str) -> RequestBuilder<'_> {
735        self.request("DELETE", url)
736    }
737
738    /// Start building a HEAD request.
739    pub fn head(&self, url: &str) -> RequestBuilder<'_> {
740        self.request("HEAD", url)
741    }
742
743    /// Start building a request with an arbitrary HTTP method.
744    pub fn request(&self, method: &str, url: &str) -> RequestBuilder<'_> {
745        RequestBuilder {
746            client: self,
747            method: method.to_uppercase(),
748            url: url.to_string(),
749            headers: Vec::new(),
750            body: None,
751            timeout_ms: None,
752        }
753    }
754}
755
756impl Default for Client {
757    fn default() -> Self {
758        Self::new()
759    }
760}
761
762// ── RequestBuilder ────────────────────────────────────────────────────────────
763
764/// Builder for a single HTTP request.
765pub struct RequestBuilder<'a> {
766    client: &'a Client,
767    method: String,
768    url: String,
769    headers: Vec<(String, String)>,
770    body: Option<Vec<u8>>,
771    timeout_ms: Option<u64>,
772}
773
774impl<'a> RequestBuilder<'a> {
775    /// Add a request header.
776    pub fn header(mut self, name: &str, value: &str) -> Self {
777        self.headers.push((name.to_string(), value.to_string()));
778        self
779    }
780
781    /// Set a raw byte body.
782    pub fn body(mut self, bytes: Vec<u8>) -> Self {
783        self.body = Some(bytes);
784        self
785    }
786
787    /// Set a plain-text body (also sets `Content-Type: text/plain`).
788    pub fn body_text(mut self, s: &str) -> Self {
789        self.headers
790            .push(("Content-Type".to_string(), "text/plain".to_string()));
791        self.body = Some(s.as_bytes().to_vec());
792        self
793    }
794
795    /// Set a JSON body (also sets `Content-Type: application/json`).
796    pub fn body_json(mut self, s: &str) -> Self {
797        self.headers.push((
798            "Content-Type".to_string(),
799            "application/json".to_string(),
800        ));
801        self.body = Some(s.as_bytes().to_vec());
802        self
803    }
804
805    /// Set an `application/x-www-form-urlencoded` body from key/value pairs
806    /// (also sets `Content-Type: application/x-www-form-urlencoded`).
807    ///
808    /// This is the body shape OAuth 2.0 token endpoints require, e.g.:
809    ///
810    /// ```rust,no_run
811    /// use rust_web_server::http_client::Client;
812    ///
813    /// let resp = Client::new()
814    ///     .post("https://oauth2.googleapis.com/token")
815    ///     .form(&[("grant_type", "authorization_code"), ("code", "abc123")])
816    ///     .send()
817    ///     .unwrap();
818    /// ```
819    pub fn form(mut self, pairs: &[(&str, &str)]) -> Self {
820        self.headers.push((
821            "Content-Type".to_string(),
822            "application/x-www-form-urlencoded".to_string(),
823        ));
824        self.body = Some(form_urlencode(pairs).into_bytes());
825        self
826    }
827
828    /// Override the timeout for this request.
829    pub fn timeout_ms(mut self, ms: u64) -> Self {
830        self.timeout_ms = Some(ms);
831        self
832    }
833
834    /// Send the request and return the response.
835    ///
836    /// Automatically follows redirects up to the client's `max_redirects`
837    /// limit.
838    pub fn send(self) -> Result<Response, HttpClientError> {
839        // Inside a traced request: a client span, and `traceparent`/`baggage`
840        // sent on (see `otel`).
841        #[cfg(not(target_arch = "wasm32"))]
842        let span = crate::otel::outbound_span(&self.method);
843        #[cfg(not(target_arch = "wasm32"))]
844        if let Some(span) = &span {
845            for (key, value) in crate::otel::http_client_attributes(&self.method, &self.url) {
846                span.set_attribute(key, value);
847            }
848        }
849        let mut headers = self.headers;
850        #[cfg(not(target_arch = "wasm32"))]
851        crate::otel::inject(&mut headers);
852        let result = Self::send_following(self.client, self.method, self.url, headers, self.body, self.timeout_ms);
853        #[cfg(not(target_arch = "wasm32"))]
854        if let Some(span) = &span {
855            span.finish_http_client(result.as_ref().map(|r| r.status()).map_err(|e| e.0.as_str()));
856        }
857        result
858    }
859
860    fn send_following(
861        client: &Client,
862        method: String,
863        url: String,
864        headers: Vec<(String, String)>,
865        body: Option<Vec<u8>>,
866        timeout_ms: Option<u64>,
867    ) -> Result<Response, HttpClientError> {
868        let timeout = timeout_ms.unwrap_or(client.timeout_ms);
869        let max_redirects = client.max_redirects;
870
871        let mut method = method;
872        let mut url = url;
873        let mut body = body;
874        let mut redirects = 0u8;
875
876        loop {
877            let parsed = ParsedUrl::parse(&url)?;
878            let resp = send_once(&method, &parsed, &headers, &body, timeout)?;
879
880            if resp.is_redirect() && redirects < max_redirects {
881                let location = resp
882                    .header("location")
883                    .ok_or_else(|| HttpClientError("redirect with no Location header".into()))?
884                    .to_string();
885                url = resolve_url(&url, &location);
886                redirects += 1;
887                if matches!(resp.status(), 301 | 302 | 303) {
888                    method = "GET".to_string();
889                    body = None;
890                }
891                continue;
892            }
893
894            return Ok(resp);
895        }
896    }
897}
898
899// ── Async client (`http2` feature) ───────────────────────────────────────────
900
901#[cfg(feature = "http2")]
902pub use async_impl::{AsyncClient, AsyncRequestBuilder};
903
904#[cfg(feature = "http2")]
905mod async_impl {
906    use super::{
907        build_request_bytes, decode_chunked, parse_status, resolve_url, HttpClientError,
908        ParsedUrl, Response,
909    };
910    use std::sync::Arc;
911    use tokio::io::{AsyncReadExt, AsyncWriteExt};
912
913    async fn async_tls_connect(
914        host: &str,
915        stream: tokio::net::TcpStream,
916    ) -> Result<tokio_rustls::client::TlsStream<tokio::net::TcpStream>, HttpClientError> {
917        use rustls::pki_types::ServerName;
918        use tokio_rustls::TlsConnector;
919
920        let root_store = rustls::RootCertStore::from_iter(
921            webpki_roots::TLS_SERVER_ROOTS.iter().cloned(),
922        );
923        let config = Arc::new(
924            crate::tls_client_builder()
925                .with_root_certificates(root_store)
926                .with_no_client_auth(),
927        );
928        let connector = TlsConnector::from(config);
929        let server_name = ServerName::try_from(host.to_string())
930            .map_err(|e| HttpClientError(format!("invalid hostname '{host}': {e}")))?;
931        connector
932            .connect(server_name, stream)
933            .await
934            .map_err(|e| HttpClientError(format!("TLS handshake failed: {e}")))
935    }
936
937    async fn async_read_response(
938        stream: &mut (impl AsyncReadExt + Unpin),
939        is_head: bool,
940    ) -> Result<Response, HttpClientError> {
941        let mut buf: Vec<u8> = Vec::with_capacity(8192);
942        let mut tmp = vec![0u8; 4096];
943
944        let header_end = loop {
945            let n = stream
946                .read(&mut tmp)
947                .await
948                .map_err(|e| HttpClientError(format!("read error: {e}")))?;
949            if n == 0 {
950                if buf.is_empty() {
951                    return Err(HttpClientError(
952                        "server closed connection without a response".into(),
953                    ));
954                }
955                break buf.len();
956            }
957            buf.extend_from_slice(&tmp[..n]);
958            if let Some(pos) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
959                break pos + 4;
960            }
961        };
962
963        let header_block = std::str::from_utf8(&buf[..header_end])
964            .map_err(|_| HttpClientError("response headers not UTF-8".into()))?;
965
966        let mut lines = header_block.lines();
967        let status_line = lines
968            .next()
969            .ok_or_else(|| HttpClientError("empty response".into()))?;
970        let status = parse_status(status_line)?;
971
972        let response_headers: Vec<(String, String)> = lines
973            .filter_map(|line| {
974                let mut parts = line.splitn(2, ':');
975                let name = parts.next()?.trim().to_string();
976                let value = parts.next()?.trim().to_string();
977                if name.is_empty() { None } else { Some((name, value)) }
978            })
979            .collect();
980
981        let mut body = buf[header_end..].to_vec();
982
983        if !is_head {
984            let transfer_encoding = response_headers
985                .iter()
986                .find(|(k, _)| k.to_lowercase() == "transfer-encoding")
987                .map(|(_, v)| v.to_lowercase());
988
989            let content_length: Option<usize> = response_headers
990                .iter()
991                .find(|(k, _)| k.to_lowercase() == "content-length")
992                .and_then(|(_, v)| v.trim().parse().ok());
993
994            if transfer_encoding
995                .as_deref()
996                .map(|te| te.contains("chunked"))
997                .unwrap_or(false)
998            {
999                loop {
1000                    let n = stream.read(&mut tmp).await
1001                        .map_err(|e| HttpClientError(format!("read error: {e}")))?;
1002                    if n == 0 { break; }
1003                    body.extend_from_slice(&tmp[..n]);
1004                }
1005                body = decode_chunked(&body)?;
1006            } else if let Some(len) = content_length {
1007                while body.len() < len {
1008                    let n = stream.read(&mut tmp).await
1009                        .map_err(|e| HttpClientError(format!("read error: {e}")))?;
1010                    if n == 0 { break; }
1011                    body.extend_from_slice(&tmp[..n]);
1012                }
1013                body.truncate(len);
1014            } else {
1015                loop {
1016                    let n = stream.read(&mut tmp).await
1017                        .map_err(|e| HttpClientError(format!("read error: {e}")))?;
1018                    if n == 0 { break; }
1019                    body.extend_from_slice(&tmp[..n]);
1020                }
1021            }
1022        } else {
1023            body.clear();
1024        }
1025
1026        Ok(Response { status, headers: response_headers, body })
1027    }
1028
1029    async fn async_send_once(
1030        method: &str,
1031        parsed: &ParsedUrl,
1032        headers: &[(String, String)],
1033        body: &Option<Vec<u8>>,
1034        timeout_ms: u64,
1035    ) -> Result<Response, HttpClientError> {
1036        use std::time::Duration;
1037        use tokio::net::TcpStream;
1038        use tokio::time::timeout;
1039
1040        let addr = format!("{}:{}", parsed.host, parsed.port);
1041        let dur = Duration::from_millis(timeout_ms);
1042        let request_bytes =
1043            build_request_bytes(method, &parsed.path_and_query, &parsed.host, headers, body);
1044        let is_head = method.eq_ignore_ascii_case("HEAD");
1045
1046        let tcp = timeout(dur, TcpStream::connect(&addr))
1047            .await
1048            .map_err(|_| HttpClientError(format!("connect to '{addr}' timed out")))?
1049            .map_err(|e| HttpClientError(format!("connect to '{addr}' failed: {e}")))?;
1050
1051        if parsed.scheme == "https" {
1052            let tls_stream = timeout(dur, async_tls_connect(&parsed.host, tcp))
1053                .await
1054                .map_err(|_| HttpClientError("TLS handshake timed out".into()))??;
1055            let mut stream = tls_stream;
1056            timeout(dur, stream.write_all(&request_bytes))
1057                .await
1058                .map_err(|_| HttpClientError("write timed out".into()))?
1059                .map_err(|e| HttpClientError(format!("write error: {e}")))?;
1060            return timeout(dur, async_read_response(&mut stream, is_head))
1061                .await
1062                .map_err(|_| HttpClientError("read timed out".into()))?;
1063        }
1064
1065        let mut stream = tcp;
1066        timeout(dur, stream.write_all(&request_bytes))
1067            .await
1068            .map_err(|_| HttpClientError("write timed out".into()))?
1069            .map_err(|e| HttpClientError(format!("write error: {e}")))?;
1070        timeout(dur, async_read_response(&mut stream, is_head))
1071            .await
1072            .map_err(|_| HttpClientError("read timed out".into()))?
1073    }
1074
1075    /// Asynchronous HTTP/1.1 client (`http2` feature required).
1076    pub struct AsyncClient {
1077        timeout_ms: u64,
1078        max_redirects: u8,
1079    }
1080
1081    impl AsyncClient {
1082        /// Create with default settings (30 s timeout, 10 redirects).
1083        pub fn new() -> Self {
1084            Self {
1085                timeout_ms: 30_000,
1086                max_redirects: 10,
1087            }
1088        }
1089
1090        /// Override the per-request timeout.
1091        pub fn timeout_ms(mut self, ms: u64) -> Self {
1092            self.timeout_ms = ms;
1093            self
1094        }
1095
1096        /// Maximum redirects to follow.
1097        pub fn max_redirects(mut self, n: u8) -> Self {
1098            self.max_redirects = n;
1099            self
1100        }
1101
1102        /// Start a GET request.
1103        pub fn get(&self, url: &str) -> AsyncRequestBuilder<'_> {
1104            self.request("GET", url)
1105        }
1106
1107        /// Start a POST request.
1108        pub fn post(&self, url: &str) -> AsyncRequestBuilder<'_> {
1109            self.request("POST", url)
1110        }
1111
1112        /// Start a PUT request.
1113        pub fn put(&self, url: &str) -> AsyncRequestBuilder<'_> {
1114            self.request("PUT", url)
1115        }
1116
1117        /// Start a PATCH request.
1118        pub fn patch(&self, url: &str) -> AsyncRequestBuilder<'_> {
1119            self.request("PATCH", url)
1120        }
1121
1122        /// Start a DELETE request.
1123        pub fn delete(&self, url: &str) -> AsyncRequestBuilder<'_> {
1124            self.request("DELETE", url)
1125        }
1126
1127        /// Start a request with an arbitrary method.
1128        pub fn request(&self, method: &str, url: &str) -> AsyncRequestBuilder<'_> {
1129            AsyncRequestBuilder {
1130                client: self,
1131                method: method.to_uppercase(),
1132                url: url.to_string(),
1133                headers: Vec::new(),
1134                body: None,
1135                timeout_ms: None,
1136            }
1137        }
1138    }
1139
1140    impl Default for AsyncClient {
1141        fn default() -> Self {
1142            Self::new()
1143        }
1144    }
1145
1146    /// Builder for an async HTTP request.
1147    pub struct AsyncRequestBuilder<'a> {
1148        client: &'a AsyncClient,
1149        method: String,
1150        url: String,
1151        headers: Vec<(String, String)>,
1152        body: Option<Vec<u8>>,
1153        timeout_ms: Option<u64>,
1154    }
1155
1156    impl<'a> AsyncRequestBuilder<'a> {
1157        /// Add a request header.
1158        pub fn header(mut self, name: &str, value: &str) -> Self {
1159            self.headers.push((name.to_string(), value.to_string()));
1160            self
1161        }
1162
1163        /// Set a raw byte body.
1164        pub fn body(mut self, bytes: Vec<u8>) -> Self {
1165            self.body = Some(bytes);
1166            self
1167        }
1168
1169        /// Set a plain-text body (sets `Content-Type: text/plain`).
1170        pub fn body_text(mut self, s: &str) -> Self {
1171            self.headers
1172                .push(("Content-Type".to_string(), "text/plain".to_string()));
1173            self.body = Some(s.as_bytes().to_vec());
1174            self
1175        }
1176
1177        /// Set a JSON body (sets `Content-Type: application/json`).
1178        pub fn body_json(mut self, s: &str) -> Self {
1179            self.headers.push((
1180                "Content-Type".to_string(),
1181                "application/json".to_string(),
1182            ));
1183            self.body = Some(s.as_bytes().to_vec());
1184            self
1185        }
1186
1187        /// Set an `application/x-www-form-urlencoded` body from key/value
1188        /// pairs (sets `Content-Type: application/x-www-form-urlencoded`).
1189        pub fn form(mut self, pairs: &[(&str, &str)]) -> Self {
1190            self.headers.push((
1191                "Content-Type".to_string(),
1192                "application/x-www-form-urlencoded".to_string(),
1193            ));
1194            self.body = Some(super::form_urlencode(pairs).into_bytes());
1195            self
1196        }
1197
1198        /// Override the timeout for this request.
1199        pub fn timeout_ms(mut self, ms: u64) -> Self {
1200            self.timeout_ms = Some(ms);
1201            self
1202        }
1203
1204        /// Send the request asynchronously.
1205        pub async fn send(self) -> Result<Response, HttpClientError> {
1206            // A detached client span: this future may resume on another thread.
1207            let mut span = crate::otel::detached_span(&self.method, crate::otel::SpanKind::Client);
1208            let mut headers = self.headers;
1209            if let Some(span) = &mut span {
1210                for (key, value) in crate::otel::http_client_attributes(&self.method, &self.url) {
1211                    span.set_attribute(key, value);
1212                }
1213                span.enter(|| crate::otel::inject(&mut headers));
1214            }
1215            let result = Self::send_following(self.client, self.method, self.url, headers, self.body, self.timeout_ms).await;
1216            if let Some(span) = &mut span {
1217                span.finish_http_client(result.as_ref().map(|r| r.status()).map_err(|e| e.0.as_str()));
1218            }
1219            result
1220        }
1221
1222        async fn send_following(
1223            client: &AsyncClient,
1224            method: String,
1225            url: String,
1226            headers: Vec<(String, String)>,
1227            body: Option<Vec<u8>>,
1228            timeout_ms: Option<u64>,
1229        ) -> Result<Response, HttpClientError> {
1230            let timeout = timeout_ms.unwrap_or(client.timeout_ms);
1231            let max_redirects = client.max_redirects;
1232
1233            let mut method = method;
1234            let mut url = url;
1235            let mut body = body;
1236            let mut redirects = 0u8;
1237
1238            loop {
1239                let parsed = ParsedUrl::parse(&url)?;
1240                let resp = async_send_once(&method, &parsed, &headers, &body, timeout).await?;
1241
1242                if resp.is_redirect() && redirects < max_redirects {
1243                    let location = resp
1244                        .header("location")
1245                        .ok_or_else(|| {
1246                            HttpClientError("redirect with no Location header".into())
1247                        })?
1248                        .to_string();
1249                    url = resolve_url(&url, &location);
1250                    redirects += 1;
1251                    if matches!(resp.status(), 301 | 302 | 303) {
1252                        method = "GET".to_string();
1253                        body = None;
1254                    }
1255                    continue;
1256                }
1257
1258                return Ok(resp);
1259            }
1260        }
1261    }
1262}