Skip to main content

Ctx

Struct Ctx 

Source
pub struct Ctx {
    pub conn: ConnectionInfo,
    pub upstream: UpstreamInfo,
    pub timing: Timing,
    pub state: RequestState,
    pub cache: Option<Box<CacheInfo>>,
    pub features: Option<Box<Features>>,
    pub plugins: Option<Arc<[NamedPlugin]>>,
    pub response_plugins: Option<Arc<[NamedPlugin]>>,
}
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: ConnectionInfo

Information about the client connection.

§upstream: UpstreamInfo

Information about the upstream server.

§timing: Timing

Timing metrics for the request lifecycle.

§state: RequestState

State related to the current request.

§cache: Option<Box<CacheInfo>>

Cache-related information. Boxed behind an Option: most requests never touch the cache, and a None costs a pointer instead of the whole struct on every Ctx.

§features: Option<Box<Features>>

Optional features, boxed for the same reason.

§plugins: Option<Arc<[NamedPlugin]>>

Plugins for the current location, shared with the location’s cache of resolved plugins so a request only bumps a reference count.

§response_plugins: Option<Arc<[NamedPlugin]>>

The same list, left here while the request plugins run when one of them asks to see the responses of the others (handles_plugin_response), for a plugin that writes its response itself: see crate::decorate_plugin_response.

Implementations§

Source§

impl Ctx

Source

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.

Source

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.
Source

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.
Source

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.

Source

pub fn set_plugin_note(&mut self, name: &str, note: &'static str)

Keeps note for the plugin that calls itself name, until the request ends.

For a decision that is made once and has to hold: the request a later step sees is not the one the first step saw - a location rewrites the path, another plugin takes a parameter out of the query or changes a header - and a plugin that decided again from what is there then could come to the other answer.

Source

pub fn get_plugin_note(&self, name: &str) -> Option<&'static str>

What Ctx::set_plugin_note kept under name.

Source

pub fn keep_response_body_plain(&mut self, types: Option<&[String]>)

Says that a plugin reads the body of the answer to this request on its way to the client - signs it as a token, rewrites it by rules - and so has to have it as the upstream sends it.

What looks at the response before the plugins of the response step do goes by it: compression in its upstream mode, which codes the body as it arrives from the upstream, leaves such a response as it comes. Coded there, the body was opaque bytes to every plugin after it, whatever their order in the list.

types are the content types the plugin reads (prefixes, in lower case), where it reads only some: the others are none of its business and are coded as before. None is every response.

Source

pub fn response_body_stays_plain( &self, is_of: impl FnOnce(&[String]) -> bool, ) -> bool

Whether a plugin asked for the body of this response as the upstream sends it (Ctx::keep_response_body_plain). is_of says whether the response is of one of the content types it is given.

Source

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.
Source

pub fn add_request_body_handler(&mut self, handler: Box<dyn HandleRequestBody>)

Has handler see the body of the request as the proxy passes it to the upstream.

Source

pub fn get_modify_body_handler( &mut self, name: &str, ) -> Option<&mut Box<dyn ModifyResponseBody>>

Returns the modify body handler by name.

Source

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 representing milliseconds, or None if time exceeds 1 hour.

Source

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 representing milliseconds, or None if time exceeds 1 hour.

Source

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 representing milliseconds, or None if time exceeds 1 hour.

Source

pub fn add_plugin_processing_time(&mut self, name: &Arc<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.
Source

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.

Resolves key on every call; a caller that knows the key ahead of time (the access log format) should parse it into a CtxLogField once and use Ctx::append_log_field.

§Arguments
  • buf - The BytesMut buffer to append the value to.
  • key - The key identifying which state value to format and append.
Source

pub fn append_log_field(&self, buf: &mut BytesMut, field: CtxLogField)

Appends the value of field to the log buffer.

Source

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.

Source

pub fn push_cache_key(&mut self, key: String)

Pushes a single cache key component to the context.

Source

pub fn extend_cache_keys(&mut self, keys: Vec<String>)

Extends the cache key components with a vector of keys.

Source

pub fn push_cache_key_variant( &mut self, current: Vec<String>, alternatives: Arc<Vec<Vec<String>>>, )

Adds current to the cache key as the one of alternatives this request stands for. It may be empty: the place is noted all the same, for the request that has to know what else could be there.

Source

pub fn cache_key_alternatives(&self) -> Vec<Vec<String>>

Every list of key components a request for this url can have: the one of this request, with each CacheKeyVariant in it replaced by each of its alternatives. The list of this request itself when it has no variants, or when there would be too many lists to go through.

Source

pub fn update_upstream_timing_from_digest( &mut self, digest: &Digest, reused: bool, )

Updates the upstream timing from the digest.

Trait Implementations§

Source§

impl Default for Ctx

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl !RefUnwindSafe for Ctx

§

impl !UnwindSafe for Ctx

§

impl Freeze for Ctx

§

impl Send for Ctx

§

impl Sync for Ctx

§

impl Unpin for Ctx

§

impl UnsafeUnpin for Ctx

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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