net-cat 0.1.0

Minimal hand-rolled HTTP/1.1 client over std::net::TcpStream. Plain HTTP only in v0 (no TLS); used to give web-api-cat's fetch a concrete backend. No external HTTP crate; all parsing and framing are local. No mut beyond the FFI carve-out for TcpStream::read_to_end. Sixth sub-crate of a Servo-replacement webview runtime targeting Tauri.
//! HTTP request.

use crate::headers::Headers;
use crate::method::Method;
use crate::url::Url;

/// An HTTP request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
    method: Method,
    url: Url,
    headers: Headers,
    body: Vec<u8>,
}

impl Request {
    /// Build a request with empty headers and no body.
    #[must_use]
    pub fn new(method: Method, url: Url) -> Self {
        Self {
            method,
            url,
            headers: Headers::new(),
            body: Vec::new(),
        }
    }

    /// The method.
    #[must_use]
    pub fn method(&self) -> Method {
        self.method
    }

    /// The URL.
    #[must_use]
    pub fn url(&self) -> &Url {
        &self.url
    }

    /// The headers.
    #[must_use]
    pub fn headers(&self) -> &Headers {
        &self.headers
    }

    /// The body bytes.
    #[must_use]
    pub fn body(&self) -> &[u8] {
        &self.body
    }

    /// Return a copy with `name: value` appended to the headers.
    #[must_use]
    pub fn with_header(self, name: impl Into<String>, value: impl Into<String>) -> Self {
        let headers = self.headers.with(name, value);
        Self { headers, ..self }
    }

    /// Return a copy with `body` replacing the current body.
    #[must_use]
    pub fn with_body(self, body: Vec<u8>) -> Self {
        Self { body, ..self }
    }
}