Skip to main content

CanonicalRequest

Struct CanonicalRequest 

Source
pub struct CanonicalRequest {
    pub method: Method,
    pub protocol: Protocol,
    pub transport: Transport,
    pub generation: u64,
    pub deadline_ms: u64,
    pub cancelled: bool,
    pub resource_lease: u64,
    /* private fields */
}
Expand description

统一规范化请求

所有HTTP协议(HTTP/1.1、HTTP/2、HTTP/3)解析后都生成此结构, 全链路统一使用,避免协议差分攻击。

§设计特点

  • 零堆分配(小 Body):Body <= MAX_BODY_LEN 使用栈上固定数组,避免堆分配
  • 大 Body 溢出:超过 MAX_BODY_LEN 时使用堆分配(Option<Vec>),保证正确性
  • 治理字段:generation、deadline、cancelled、resource_lease

§Copy 语义

由于大 Body 场景下引入了 Option<Vec<u8>>,CanonicalRequest 不再是 Copy, 仅保留 Clone。绝大多数小请求 (<= 4KiB body) 的 clone 开销仅为栈数组拷贝。

Fields§

§method: Method

请求方法

§protocol: Protocol

协议类型

§transport: Transport

传输层类型

§generation: u64

代际号(用于 ChangeSet 版本控制)

§deadline_ms: u64

截止时间(毫秒,0 表示无限制)

§cancelled: bool

是否已取消

§resource_lease: u64

资源租约(令牌数,用于限流/配额)

Implementations§

Source§

impl CanonicalRequest

Source

pub const fn empty() -> CanonicalRequest

创建空请求

Source

pub fn reset(&mut self)

重置请求(清空所有字段) 用于对象池复用,避免重新创建对象的开销

Source

pub fn set_body(&mut self, body: impl Into<Vec<u8>>)

设置请求 Body

性能设计:

  • Body 长度 <= MAX_BODY_LEN:使用栈上缓冲区,零堆分配
  • Body 长度 > MAX_BODY_LEN:使用堆分配,保证正确性不截断
Source

pub fn body(&self) -> &[u8]

获取请求 Body 的字节切片(零分配)

Source

pub fn scheme_str(&self) -> &str

获取方案字符串

Source

pub fn set_scheme(&mut self, scheme: &str) -> bool

设置方案(fail-closed)

§Returns
  • true - 设置成功
  • false - 方案超长(拒绝静默截断,不修改原值直接返回 false; 截断会篡改语义,下游必须检查返回值)
Source

pub fn authority_str(&self) -> &str

获取授权主机字符串

Source

pub fn set_authority(&mut self, authority: &str) -> bool

设置授权主机(fail-closed)

§Returns
  • true - 设置成功
  • false - authority 超长(拒绝静默截断,不修改原值直接返回 false)
Source

pub fn path_str(&self) -> &str

获取规范化路径

Source

pub fn path_bytes(&self) -> &[u8]

获取规范化路径的字节切片(零分配)

Source

pub fn set_path(&mut self, path: &str) -> bool

设置规范化路径(fail-closed)

输入为 request target 时,其中的 ? 之后的部分会被拆分到 query 字段 (路由只匹配路径部分;若不拆分,/api?x=1 会作为整体参与路由匹配而被击穿)。 输入不含 ? 时 query 字段保持不变。

§Returns
  • true - 设置成功
  • false - 路径部分超过 MAX_PATH_LEN 或拆分出的 query 超过 MAX_QUERY_LEN(拒绝静默截断,path 与 query 均不修改,原子拒绝)
Source

pub fn query_str(&self) -> &str

获取规范化查询字符串

Source

pub fn set_query(&mut self, query: &str) -> bool

设置规范化查询字符串(fail-closed)

§Returns
  • true - 设置成功
  • false - query 超长(拒绝静默截断,不修改原值直接返回 false)
Source

pub fn header_count(&self) -> u8

获取头部数量

Source

pub fn get_header(&self, idx: usize) -> Option<&CanonicalHeader>

按索引获取头部

Source

pub fn find_header(&self, name: &str) -> Option<&CanonicalHeader>

按名称查找头部(返回第一个匹配,ASCII 大小写不敏感)

HTTP 头部名称按 RFC 7230 §3.2 为大小写不敏感; 大小写敏感查找会被 Host/host 差分击穿,故统一不敏感比较。

Source

pub fn add_header( &mut self, name: &[u8], value: &[u8], ) -> Result<(), &'static str>

添加头部(超过最大数量或字段超长时返回错误)

§安全
  • 拒绝包含 \r\n 的头部名称和值,防止 HTTP 响应拆分攻击。
  • 名称/值超长时 fail-closed 拒绝,禁止静默截断(截断会篡改头部语义)。
Source

pub fn headers_iter(&self) -> &[CanonicalHeader]

迭代所有头部

Source

pub fn cancel(&mut self)

设置已取消

Source

pub fn is_cancelled(&self) -> bool

检查是否已取消

Trait Implementations§

Source§

impl Clone for CanonicalRequest

Source§

fn clone(&self) -> CanonicalRequest

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CanonicalRequest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for CanonicalRequest

Source§

fn default() -> CanonicalRequest

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more