协议版本宏
2025/11/29大约 4 分钟hyperlane-macros
协议版本宏
协议版本宏用于限制函数只能在特定的 HTTP 协议版本下执行。这些宏确保只有使用相应协议版本的请求才会触发函数执行,其他协议版本的请求将被过滤掉。
#[http]
限制函数仅在标准 HTTP 请求时执行,排除 WebSocket 升级和其他协议升级请求。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/http")]
struct HttpOnly;
impl ServerHook for HttpOnly {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(http, response_body("http"))]
async fn handle(self, ctx: &Context) {}
}
impl HttpOnly {
#[http]
async fn http_with_ref_self(&self, ctx: &Context) {}
}
#[http]
async fn standalone_http_handler(ctx: &Context) {}说明
- 无参数
- 仅处理标准 HTTP 请求
- 排除 WebSocket 升级请求
- 适用于传统的 HTTP API 端点
#[http0_9]
限制函数仅在 HTTP/0.9 协议请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/http0_9")]
struct Http09;
impl ServerHook for Http09 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(http0_9, response_body("http0_9"))]
async fn handle(self, ctx: &Context) {}
}
impl Http09 {
#[http0_9]
async fn http0_9_with_ref_self(&self, ctx: &Context) {}
}
#[http0_9]
async fn standalone_http0_9_handler(ctx: &Context) {}说明
- 无参数
- HTTP/0.9 是最早的 HTTP 协议版本
- 极其简单的协议,仅支持 GET 方法
- 现代应用中很少使用,主要用于兼容性考虑
#[http1_0]
限制函数仅在 HTTP/1.0 协议请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/http1_0")]
struct Http10;
impl ServerHook for Http10 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(http1_0, response_body("http1_0"))]
async fn handle(self, ctx: &Context) {}
}
impl Http10 {
#[http1_0]
async fn http1_0_with_ref_self(&self, ctx: &Context) {}
}
#[http1_0]
async fn standalone_http1_0_handler(ctx: &Context) {}说明
- 无参数
- HTTP/1.0 引入了状态码和头部
- 每个请求都需要新的 TCP 连接
- 性能较差,逐渐被 HTTP/1.1 替代
#[http1_1]
限制函数仅在 HTTP/1.1 协议请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/http1_1")]
struct Http11;
impl ServerHook for Http11 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(http1_1, response_body("http1_1"))]
async fn handle(self, ctx: &Context) {}
}
impl Http11 {
#[http1_1]
async fn http1_1_with_ref_self(&self, ctx: &Context) {}
}
#[http1_1]
async fn standalone_http1_1_handler(ctx: &Context) {}说明
- 无参数
- HTTP/1.1 是目前最广泛使用的 HTTP 协议版本
- 支持持久连接和管道化
- 引入了 Host 头部,支持虚拟主机
#[http1_1_or_higher]
限制函数仅在 HTTP/1.1 或更高协议版本请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/http1_1_or_higher")]
struct Http11OrHigher;
impl ServerHook for Http11OrHigher {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(http1_1_or_higher, response_body("http1_1_or_higher"))]
async fn handle(self, ctx: &Context) {}
}
impl Http11OrHigher {
#[http1_1_or_higher]
async fn http1_1_or_higher_with_ref_self(&self, ctx: &Context) {}
}
#[http1_1_or_higher]
async fn standalone_http1_1_or_higher_handler(ctx: &Context) {}说明
- 无参数
- 包含 HTTP/1.1、HTTP/2、HTTP/3 及未来版本
- 适用于需要现代 HTTP 特性的场景
- 排除过时的 HTTP/1.0 及更早版本
#[http2]
限制函数仅在 HTTP/2 协议请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/http2")]
struct Http2;
impl ServerHook for Http2 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(http2, response_body("http2"))]
async fn handle(self, ctx: &Context) {}
}
impl Http2 {
#[http2]
async fn http2_with_ref_self(&self, ctx: &Context) {}
}
#[http2]
async fn standalone_http2_handler(ctx: &Context) {}说明
- 无参数
- HTTP/2 提供多路复用、头部压缩等特性
- 显著提高性能和效率
- 通常需要 TLS 加密(h2),但也支持明文(h2c)
#[http3]
限制函数仅在 HTTP/3 协议请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/http3")]
struct Http3;
impl ServerHook for Http3 {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(http3, response_body("http3"))]
async fn handle(self, ctx: &Context) {}
}
impl Http3 {
#[http3]
async fn http3_with_ref_self(&self, ctx: &Context) {}
}
#[http3]
async fn standalone_http3_handler(ctx: &Context) {}说明
- 无参数
- HTTP/3 基于 QUIC 协议
- 解决了 HTTP/2 的队头阻塞问题
- 提供更好的性能和连接迁移支持
#[h2c]
限制函数仅在 HTTP/2 Cleartext (h2c) 升级请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/h2c")]
struct H2c;
impl ServerHook for H2c {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(h2c, response_body("h2c"))]
async fn handle(self, ctx: &Context) {}
}
impl H2c {
#[h2c]
async fn h2c_with_ref_self(&self, ctx: &Context) {}
}
#[h2c]
async fn standalone_h2c_handler(ctx: &Context) {}说明
- 无参数
- h2c 是 HTTP/2 的明文版本
- 通过 HTTP/1.1 升级机制实现
- 适用于不需要 TLS 加密的开发环境
#[tls]
限制函数仅在 TLS 加密请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/tls")]
struct Tls;
impl ServerHook for Tls {
async fn new(_ctx: &Context) -> Self {
Self
}
#[prologue_macros(tls, response_body("tls"))]
async fn handle(self, ctx: &Context) {}
}
impl Tls {
#[tls]
async fn tls_with_ref_self(&self, ctx: &Context) {}
}
#[tls]
async fn standalone_tls_handler(ctx: &Context) {}说明
- 无参数
- 确保请求通过 TLS/SSL 加密
- 适用于需要安全传输的场景
- 包含 HTTPS、WSS 等加密协议
#[ws]
限制函数仅在 WebSocket 升级请求时执行。
用法
use hyperlane::*;
use hyperlane_macros::*;
#[route("/ws")]
struct Websocket;
impl ServerHook for Websocket {
async fn new(_ctx: &Context) -> Self {
Self
}
#[ws]
#[ws_from_stream]
async fn handle(self, ctx: &Context) {
let body: RequestBody = ctx.get_request_body().await;
let body_list: Vec<ResponseBody> = WebSocketFrame::create_frame_list(&body);
ctx.send_body_list_with_data(&body_list).await.unwrap();
}
}
impl Websocket {
#[ws]
async fn ws_with_ref_self(&self, ctx: &Context) {}
}
#[ws]
async fn standalone_ws_handler(ctx: &Context) {}