hyperlane-macros
A comprehensive collection of procedural macros for building HTTP servers with enhanced functionality. This crate provides attribute macros that simplify HTTP request handling, protocol validation, response management, and request data extraction.
Installation
To use this crate, you can run cmd:
cargo add hyperlane-macros
Available Macros
Hyperlane Macro
#[hyperlane(server: Server)]
- Creates a newServer
instance with the specified variable name and type, and automatically registers other hooks and routes defined within the crate.#[hyperlane(config: ServerConfig)]
- Creates a newServerConfig
instance with the specified variable name and type.
HTTP Method Macros
#[methods(method1, method2, ...)]
- Accepts multiple HTTP methods#[get]
- GET method handler#[post]
- POST method handler#[put]
- PUT method handler#[delete]
- DELETE method handler#[patch]
- PATCH method handler#[head]
- HEAD method handler#[options]
- OPTIONS method handler#[connect]
- CONNECT method handler#[trace]
- TRACE method handler
Protocol Check Macros
#[ws]
- WebSocket check, ensures function only executes for WebSocket upgrade requests#[http]
- HTTP check, ensures function only executes for standard HTTP requests#[h2c]
- HTTP/2 Cleartext check, ensures function only executes for HTTP/2 cleartext requests#[http0_9]
- HTTP/0.9 check, ensures function only executes for HTTP/0.9 protocol requests#[http1_0]
- HTTP/1.0 check, ensures function only executes for HTTP/1.0 protocol requests#[http1_1]
- HTTP/1.1 check, ensures function only executes for HTTP/1.1 protocol requests#[http1_1_or_higher]
- HTTP/1.1 or higher version check, ensures function only executes for HTTP/1.1 or newer protocol versions#[http2]
- HTTP/2 check, ensures function only executes for HTTP/2 protocol requests#[http3]
- HTTP/3 check, ensures function only executes for HTTP/3 protocol requests#[tls]
- TLS check, ensures function only executes for TLS-secured connections
Response Setting Macros
#[response_status_code(code)]
- Set response status code (supports literals and global constants)#[response_reason_phrase("phrase")]
- Set response reason phrase (supports literals and global constants)#[response_header("key", "value")]
- Add response header (supports literals and global constants)#[response_header("key" => "value")]
- Set response header (supports literals and global constants)#[response_body("data")]
- Set response body (supports literals and global constants)#[response_version(version)]
- Set response HTTP version (supports literals and global constants)
Send Operation Macros
#[send]
- Send complete response (headers and body) after function execution#[send_body]
- Send only response body after function execution#[send_once]
- Send complete response exactly once after function execution#[send_once_body]
- Send response body exactly once after function execution
Flush Macros
#[flush]
- Flush response stream after function execution to ensure immediate data transmission
Aborted Macros
#[aborted]
- Handle aborted requests, providing cleanup logic for prematurely terminated connections
Closed Operation Macros
#[closed]
- Handle closed streams, providing cleanup logic for completed connections
Conditional Macros
#[filter(condition)]
- Continues execution only if thecondition
(a code block returning a boolean) istrue
.#[reject(condition)]
- Continues execution only if thecondition
(a code block returning a boolean) isfalse
.
Request Body Macros
#[request_body(variable_name)]
- Extract raw request body into specified variable with RequestBody type#[request_body_json(variable_name: type)]
- Parse request body as JSON into specified variable and type
Attribute Macros
#[attribute(key => variable_name: type)]
- Extract a specific attribute by key into a typed variable
Attributes Macros
#[attributes(variable_name)]
- Get all attributes as a HashMap for comprehensive attribute access
Route Param Macros
#[route_param(key => variable_name)]
- Extract a specific route parameter by key into a variable
Route Params Macros
#[route_params(variable_name)]
- Get all route parameters as a collection
Request Query Macros
#[request_query(key => variable_name)]
- Extract a specific query parameter by key from the URL query string
Request Querys Macros
#[request_querys(variable_name)]
- Get all query parameters as a collection
Request Header Macros
#[request_header(key => variable_name)]
- Extract a specific HTTP header by name from the request
Request Headers Macros
#[request_headers(variable_name)]
- Get all HTTP headers as a collection
Request Cookie Macros
#[request_cookie(key => variable_name)]
- Extract a specific cookie value by key from the request cookie header
Request Cookies Macros
#[request_cookies(variable_name)]
- Get all cookies as a raw string from the cookie header
Request Version Macros
#[request_version(variable_name)]
- Extract the HTTP request version into a variable
Request Path Macros
#[request_path(variable_name)]
- Extract the HTTP request path into a variable
Host Macros
#[host("hostname")]
- Restrict function execution to requests with a specific host header value#[reject_host("hostname")]
- Reject requests that match a specific host header value
Referer Macros
#[referer("url")]
- Restrict function execution to requests with a specific referer header value#[reject_referer("url")]
- Reject requests that match a specific referer header value
Hook Macros
#[prologue_hook(function_name)]
- Execute specified function before the main handler function#[epilogue_hook(function_name)]
- Execute specified function after the main handler function#[connected_hook]
- Execute function when a new client connection is established#[panic_hook]
- Execute function when a panic occurs within the server#[prologue_upgrade_hook]
- Execute function before any protocol upgrade occurs#[prologue_hooks(macro1, macro2, ...)]
- Injects a list of macros before the decorated function.#[epilogue_hooks(macro1, macro2, ...)]
- Injects a list of macros after the decorated function.
Disable Hook Macros
#[disable_http_hook]
- Disable HTTP handling for a specific route#[disable_ws_hook]
- Disable WebSocket handling for a specific route
Middleware Macros
#[request_middleware]
- Register a function as a request middleware#[response_middleware]
- Register a function as a response middleware
Response Header Macros
#[response_header("key", "value")]
- Add a specific HTTP response header with the given key and value (add to existing headers)#[response_header("key" => "value")]
- Set a specific HTTP response header with the given key and value (overwrite existing)
Response Body Macros
#[response_body(value)]
- Set the HTTP response body with the given value
Route Macros
#[route("path")]
- Register a route handler for the given path using the default server (Prerequisite: requires the #[hyperlane(server: Server)] macro)
Helper Tips
- Request related macros (data extraction) use
get
operations - they retrieve/query data from the request - Response related macros (data setting) use
set
operations - they assign/configure response data - Hook macros For hook-related macros that support an
order
parameter, iforder
is not specified, the hook will have higher priority than hooks with a specifiedorder
(applies only to macros like#[request_middleware]
,#[response_middleware]
,#[panic_hook]
,#[connected_hook]
,#[prologue_upgrade_hook]
)
Best Practice Warning
- Request related macros are mostly query functions, while response related macros are mostly assignment functions.
- When using
prologue_hook
orepilogue_hook
macros, it is not recommended to combine them with other macros (such as#[get]
,#[post]
,#[http]
, etc.) on the same function. These macros should be placed in the hook functions themselves. If you are not clear about how macros are expanded, combining them may lead to problematic code behavior.
Example Usage
use *;
use *;
use ;
const STEP: &str = "step";
const TEST_ATTRIBUTE_KEY: &str = "test_attribute_key";
const CUSTOM_STATUS_CODE: i32 = 200;
const CUSTOM_REASON: &str = "Accepted";
const CUSTOM_HEADER_NAME: &str = "X-Custom-Header";
const CUSTOM_HEADER_VALUE: &str = "custom-value";
const RESPONSE_DATA: &str = "{\"status\": \"success\"}";
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Contact
For any inquiries, please reach out to the author at root@ltpp.vip.