pub struct Ctx {
pub conn: ConnectionInfo,
pub upstream: UpstreamInfo,
pub timing: Timing,
pub state: RequestState,
pub cache: Option<CacheInfo>,
pub features: Option<Features>,
pub plugins: Option<Vec<(String, Arc<dyn Plugin>)>>,
}Expand description
Represents the state of a request/response cycle, tracking various metrics and properties including connection details, caching information, and upstream server interactions.
Fields§
§conn: ConnectionInfoInformation about the client connection.
upstream: UpstreamInfoInformation about the upstream server.
timing: TimingTiming metrics for the request lifecycle.
state: RequestStateState related to the current request.
cache: Option<CacheInfo>Cache-related information. Wrapped in Option to save memory when not in use.
features: Option<Features>Optional features. Wrapped in Option to save memory when not in use.
plugins: Option<Vec<(String, Arc<dyn Plugin>)>>Plugins for the current location
Implementations§
Source§impl Ctx
impl Ctx
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Ctx instance with the current timestamp and default values.
Returns a new Ctx struct initialized with the current timestamp and all other fields set to their default values.
Sourcepub fn add_variable(&mut self, key: &str, value: &str)
pub fn add_variable(&mut self, key: &str, value: &str)
Adds a variable to the state’s variables map with the given key and value.
§Arguments
key- The variable name.value- The value to store for this variable.
Sourcepub fn extend_variables(&mut self, values: AHashMap<String, String>)
pub fn extend_variables(&mut self, values: AHashMap<String, String>)
Extends the variables map with the given key-value pairs.
§Arguments
values- A HashMap containing the key-value pairs to add.
Sourcepub fn get_variable(&self, key: &str) -> Option<&str>
pub fn get_variable(&self, key: &str) -> Option<&str>
Returns the value of a variable by key.
§Arguments
key- The key of the variable to retrieve.
Returns: Option<&str> representing the value of the variable, or None if the variable does not exist.
Sourcepub fn add_modify_body_handler(
&mut self,
name: &str,
handler: Box<dyn ModifyResponseBody>,
)
pub fn add_modify_body_handler( &mut self, name: &str, handler: Box<dyn ModifyResponseBody>, )
Adds a modify body handler to the context.
§Arguments
name- The name of the handler.handler- The handler to add.
Sourcepub fn get_modify_body_handler(
&mut self,
name: &str,
) -> Option<&mut Box<dyn ModifyResponseBody>>
pub fn get_modify_body_handler( &mut self, name: &str, ) -> Option<&mut Box<dyn ModifyResponseBody>>
Returns the modify body handler by name.
Sourcepub fn get_upstream_response_time(&self) -> Option<u32>
pub fn get_upstream_response_time(&self) -> Option<u32>
Returns the upstream response time if it’s less than one hour, otherwise None. This helps filter out potentially invalid or stale timing data.
Returns: Option
Sourcepub fn get_upstream_connect_time(&self) -> Option<u32>
pub fn get_upstream_connect_time(&self) -> Option<u32>
Returns the upstream connect time if it’s less than one hour, otherwise None. This helps filter out potentially invalid or stale timing data.
Returns: Option
Sourcepub fn get_upstream_processing_time(&self) -> Option<u32>
pub fn get_upstream_processing_time(&self) -> Option<u32>
Returns the upstream processing time if it’s less than one hour, otherwise None. This helps filter out potentially invalid or stale timing data.
Returns: Option
Sourcepub fn add_plugin_processing_time(&mut self, name: &str, time: u32)
pub fn add_plugin_processing_time(&mut self, name: &str, time: u32)
Adds a plugin processing time to the context.
§Arguments
name- The name of the plugin.time- The time taken by the plugin in milliseconds.
Sourcepub fn append_log_value(&self, buf: &mut BytesMut, key: &str)
pub fn append_log_value(&self, buf: &mut BytesMut, key: &str)
Appends a formatted value to the provided log buffer based on the given key. Handles various metrics including connection info, timing data, and TLS details.
§Arguments
buf- The BytesMut buffer to append the value to.key- The key identifying which state value to format and append.
Returns: The modified BytesMut buffer.
Sourcepub fn generate_server_timing(&self) -> String
pub fn generate_server_timing(&self) -> String
Generates a Server-Timing header value based on the context’s timing metrics.
The Server-Timing header allows servers to communicate performance metrics about the request-response cycle to the client. This implementation includes various timing metrics like connection time, processing time, and cache operations.
Returns a String containing the formatted Server-Timing header value.
Sourcepub fn push_cache_key(&mut self, key: String)
pub fn push_cache_key(&mut self, key: String)
Pushes a single cache key component to the context.
Sourcepub fn extend_cache_keys(&mut self, keys: Vec<String>)
pub fn extend_cache_keys(&mut self, keys: Vec<String>)
Extends the cache key components with a vector of keys.
Sourcepub fn update_upstream_timing_from_digest(
&mut self,
digest: &Digest,
reused: bool,
)
pub fn update_upstream_timing_from_digest( &mut self, digest: &Digest, reused: bool, )
Updates the upstream timing from the digest.