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        let timeout = self.timeout_ms.unwrap_or(self.client.timeout_ms);
840        let max_redirects = self.client.max_redirects;
841
842        let mut method = self.method;
843        let mut url = self.url;
844        let headers = self.headers;
845        let mut body = self.body;
846        let mut redirects = 0u8;
847
848        loop {
849            let parsed = ParsedUrl::parse(&url)?;
850            let resp = send_once(&method, &parsed, &headers, &body, timeout)?;
851
852            if resp.is_redirect() && redirects < max_redirects {
853                let location = resp
854                    .header("location")
855                    .ok_or_else(|| HttpClientError("redirect with no Location header".into()))?
856                    .to_string();
857                url = resolve_url(&url, &location);
858                redirects += 1;
859                if matches!(resp.status(), 301 | 302 | 303) {
860                    method = "GET".to_string();
861                    body = None;
862                }
863                continue;
864            }
865
866            return Ok(resp);
867        }
868    }
869}
870
871// ── Async client (`http2` feature) ───────────────────────────────────────────
872
873#[cfg(feature = "http2")]
874pub use async_impl::{AsyncClient, AsyncRequestBuilder};
875
876#[cfg(feature = "http2")]
877mod async_impl {
878    use super::{
879        build_request_bytes, decode_chunked, parse_status, resolve_url, HttpClientError,
880        ParsedUrl, Response,
881    };
882    use std::sync::Arc;
883    use tokio::io::{AsyncReadExt, AsyncWriteExt};
884
885    async fn async_tls_connect(
886        host: &str,
887        stream: tokio::net::TcpStream,
888    ) -> Result<tokio_rustls::client::TlsStream<tokio::net::TcpStream>, HttpClientError> {
889        use rustls::pki_types::ServerName;
890        use tokio_rustls::TlsConnector;
891
892        let root_store = rustls::RootCertStore::from_iter(
893            webpki_roots::TLS_SERVER_ROOTS.iter().cloned(),
894        );
895        let config = Arc::new(
896            crate::tls_client_builder()
897                .with_root_certificates(root_store)
898                .with_no_client_auth(),
899        );
900        let connector = TlsConnector::from(config);
901        let server_name = ServerName::try_from(host.to_string())
902            .map_err(|e| HttpClientError(format!("invalid hostname '{host}': {e}")))?;
903        connector
904            .connect(server_name, stream)
905            .await
906            .map_err(|e| HttpClientError(format!("TLS handshake failed: {e}")))
907    }
908
909    async fn async_read_response(
910        stream: &mut (impl AsyncReadExt + Unpin),
911        is_head: bool,
912    ) -> Result<Response, HttpClientError> {
913        let mut buf: Vec<u8> = Vec::with_capacity(8192);
914        let mut tmp = vec![0u8; 4096];
915
916        let header_end = loop {
917            let n = stream
918                .read(&mut tmp)
919                .await
920                .map_err(|e| HttpClientError(format!("read error: {e}")))?;
921            if n == 0 {
922                if buf.is_empty() {
923                    return Err(HttpClientError(
924                        "server closed connection without a response".into(),
925                    ));
926                }
927                break buf.len();
928            }
929            buf.extend_from_slice(&tmp[..n]);
930            if let Some(pos) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
931                break pos + 4;
932            }
933        };
934
935        let header_block = std::str::from_utf8(&buf[..header_end])
936            .map_err(|_| HttpClientError("response headers not UTF-8".into()))?;
937
938        let mut lines = header_block.lines();
939        let status_line = lines
940            .next()
941            .ok_or_else(|| HttpClientError("empty response".into()))?;
942        let status = parse_status(status_line)?;
943
944        let response_headers: Vec<(String, String)> = lines
945            .filter_map(|line| {
946                let mut parts = line.splitn(2, ':');
947                let name = parts.next()?.trim().to_string();
948                let value = parts.next()?.trim().to_string();
949                if name.is_empty() { None } else { Some((name, value)) }
950            })
951            .collect();
952
953        let mut body = buf[header_end..].to_vec();
954
955        if !is_head {
956            let transfer_encoding = response_headers
957                .iter()
958                .find(|(k, _)| k.to_lowercase() == "transfer-encoding")
959                .map(|(_, v)| v.to_lowercase());
960
961            let content_length: Option<usize> = response_headers
962                .iter()
963                .find(|(k, _)| k.to_lowercase() == "content-length")
964                .and_then(|(_, v)| v.trim().parse().ok());
965
966            if transfer_encoding
967                .as_deref()
968                .map(|te| te.contains("chunked"))
969                .unwrap_or(false)
970            {
971                loop {
972                    let n = stream.read(&mut tmp).await
973                        .map_err(|e| HttpClientError(format!("read error: {e}")))?;
974                    if n == 0 { break; }
975                    body.extend_from_slice(&tmp[..n]);
976                }
977                body = decode_chunked(&body)?;
978            } else if let Some(len) = content_length {
979                while body.len() < len {
980                    let n = stream.read(&mut tmp).await
981                        .map_err(|e| HttpClientError(format!("read error: {e}")))?;
982                    if n == 0 { break; }
983                    body.extend_from_slice(&tmp[..n]);
984                }
985                body.truncate(len);
986            } else {
987                loop {
988                    let n = stream.read(&mut tmp).await
989                        .map_err(|e| HttpClientError(format!("read error: {e}")))?;
990                    if n == 0 { break; }
991                    body.extend_from_slice(&tmp[..n]);
992                }
993            }
994        } else {
995            body.clear();
996        }
997
998        Ok(Response { status, headers: response_headers, body })
999    }
1000
1001    async fn async_send_once(
1002        method: &str,
1003        parsed: &ParsedUrl,
1004        headers: &[(String, String)],
1005        body: &Option<Vec<u8>>,
1006        timeout_ms: u64,
1007    ) -> Result<Response, HttpClientError> {
1008        use std::time::Duration;
1009        use tokio::net::TcpStream;
1010        use tokio::time::timeout;
1011
1012        let addr = format!("{}:{}", parsed.host, parsed.port);
1013        let dur = Duration::from_millis(timeout_ms);
1014        let request_bytes =
1015            build_request_bytes(method, &parsed.path_and_query, &parsed.host, headers, body);
1016        let is_head = method.eq_ignore_ascii_case("HEAD");
1017
1018        let tcp = timeout(dur, TcpStream::connect(&addr))
1019            .await
1020            .map_err(|_| HttpClientError(format!("connect to '{addr}' timed out")))?
1021            .map_err(|e| HttpClientError(format!("connect to '{addr}' failed: {e}")))?;
1022
1023        if parsed.scheme == "https" {
1024            let tls_stream = timeout(dur, async_tls_connect(&parsed.host, tcp))
1025                .await
1026                .map_err(|_| HttpClientError("TLS handshake timed out".into()))??;
1027            let mut stream = tls_stream;
1028            timeout(dur, stream.write_all(&request_bytes))
1029                .await
1030                .map_err(|_| HttpClientError("write timed out".into()))?
1031                .map_err(|e| HttpClientError(format!("write error: {e}")))?;
1032            return timeout(dur, async_read_response(&mut stream, is_head))
1033                .await
1034                .map_err(|_| HttpClientError("read timed out".into()))?;
1035        }
1036
1037        let mut stream = tcp;
1038        timeout(dur, stream.write_all(&request_bytes))
1039            .await
1040            .map_err(|_| HttpClientError("write timed out".into()))?
1041            .map_err(|e| HttpClientError(format!("write error: {e}")))?;
1042        timeout(dur, async_read_response(&mut stream, is_head))
1043            .await
1044            .map_err(|_| HttpClientError("read timed out".into()))?
1045    }
1046
1047    /// Asynchronous HTTP/1.1 client (`http2` feature required).
1048    pub struct AsyncClient {
1049        timeout_ms: u64,
1050        max_redirects: u8,
1051    }
1052
1053    impl AsyncClient {
1054        /// Create with default settings (30 s timeout, 10 redirects).
1055        pub fn new() -> Self {
1056            Self {
1057                timeout_ms: 30_000,
1058                max_redirects: 10,
1059            }
1060        }
1061
1062        /// Override the per-request timeout.
1063        pub fn timeout_ms(mut self, ms: u64) -> Self {
1064            self.timeout_ms = ms;
1065            self
1066        }
1067
1068        /// Maximum redirects to follow.
1069        pub fn max_redirects(mut self, n: u8) -> Self {
1070            self.max_redirects = n;
1071            self
1072        }
1073
1074        /// Start a GET request.
1075        pub fn get(&self, url: &str) -> AsyncRequestBuilder<'_> {
1076            self.request("GET", url)
1077        }
1078
1079        /// Start a POST request.
1080        pub fn post(&self, url: &str) -> AsyncRequestBuilder<'_> {
1081            self.request("POST", url)
1082        }
1083
1084        /// Start a PUT request.
1085        pub fn put(&self, url: &str) -> AsyncRequestBuilder<'_> {
1086            self.request("PUT", url)
1087        }
1088
1089        /// Start a PATCH request.
1090        pub fn patch(&self, url: &str) -> AsyncRequestBuilder<'_> {
1091            self.request("PATCH", url)
1092        }
1093
1094        /// Start a DELETE request.
1095        pub fn delete(&self, url: &str) -> AsyncRequestBuilder<'_> {
1096            self.request("DELETE", url)
1097        }
1098
1099        /// Start a request with an arbitrary method.
1100        pub fn request(&self, method: &str, url: &str) -> AsyncRequestBuilder<'_> {
1101            AsyncRequestBuilder {
1102                client: self,
1103                method: method.to_uppercase(),
1104                url: url.to_string(),
1105                headers: Vec::new(),
1106                body: None,
1107                timeout_ms: None,
1108            }
1109        }
1110    }
1111
1112    impl Default for AsyncClient {
1113        fn default() -> Self {
1114            Self::new()
1115        }
1116    }
1117
1118    /// Builder for an async HTTP request.
1119    pub struct AsyncRequestBuilder<'a> {
1120        client: &'a AsyncClient,
1121        method: String,
1122        url: String,
1123        headers: Vec<(String, String)>,
1124        body: Option<Vec<u8>>,
1125        timeout_ms: Option<u64>,
1126    }
1127
1128    impl<'a> AsyncRequestBuilder<'a> {
1129        /// Add a request header.
1130        pub fn header(mut self, name: &str, value: &str) -> Self {
1131            self.headers.push((name.to_string(), value.to_string()));
1132            self
1133        }
1134
1135        /// Set a raw byte body.
1136        pub fn body(mut self, bytes: Vec<u8>) -> Self {
1137            self.body = Some(bytes);
1138            self
1139        }
1140
1141        /// Set a plain-text body (sets `Content-Type: text/plain`).
1142        pub fn body_text(mut self, s: &str) -> Self {
1143            self.headers
1144                .push(("Content-Type".to_string(), "text/plain".to_string()));
1145            self.body = Some(s.as_bytes().to_vec());
1146            self
1147        }
1148
1149        /// Set a JSON body (sets `Content-Type: application/json`).
1150        pub fn body_json(mut self, s: &str) -> Self {
1151            self.headers.push((
1152                "Content-Type".to_string(),
1153                "application/json".to_string(),
1154            ));
1155            self.body = Some(s.as_bytes().to_vec());
1156            self
1157        }
1158
1159        /// Set an `application/x-www-form-urlencoded` body from key/value
1160        /// pairs (sets `Content-Type: application/x-www-form-urlencoded`).
1161        pub fn form(mut self, pairs: &[(&str, &str)]) -> Self {
1162            self.headers.push((
1163                "Content-Type".to_string(),
1164                "application/x-www-form-urlencoded".to_string(),
1165            ));
1166            self.body = Some(super::form_urlencode(pairs).into_bytes());
1167            self
1168        }
1169
1170        /// Override the timeout for this request.
1171        pub fn timeout_ms(mut self, ms: u64) -> Self {
1172            self.timeout_ms = Some(ms);
1173            self
1174        }
1175
1176        /// Send the request asynchronously.
1177        pub async fn send(self) -> Result<Response, HttpClientError> {
1178            let timeout = self.timeout_ms.unwrap_or(self.client.timeout_ms);
1179            let max_redirects = self.client.max_redirects;
1180
1181            let mut method = self.method;
1182            let mut url = self.url;
1183            let headers = self.headers;
1184            let mut body = self.body;
1185            let mut redirects = 0u8;
1186
1187            loop {
1188                let parsed = ParsedUrl::parse(&url)?;
1189                let resp = async_send_once(&method, &parsed, &headers, &body, timeout).await?;
1190
1191                if resp.is_redirect() && redirects < max_redirects {
1192                    let location = resp
1193                        .header("location")
1194                        .ok_or_else(|| {
1195                            HttpClientError("redirect with no Location header".into())
1196                        })?
1197                        .to_string();
1198                    url = resolve_url(&url, &location);
1199                    redirects += 1;
1200                    if matches!(resp.status(), 301 | 302 | 303) {
1201                        method = "GET".to_string();
1202                        body = None;
1203                    }
1204                    continue;
1205                }
1206
1207                return Ok(resp);
1208            }
1209        }
1210    }
1211}