Table of Contents

THttpServerContext Class

Abstract base class representing the context of a single HTTP request being processed by a Sparkle server, giving access to the request, the response, and a per-request container of arbitrary items shared across middleware and handlers.

Remarks

A context instance lives for the duration of one HTTP request/response cycle. Server implementations (for example, the HTTP.sys or socket-based servers) derive from this class and provide the concrete Request and Response objects. The Item and Data mechanisms let middleware attach values that later stages of the pipeline can read: Item keys values by type (one value per type), while Data keys values by string. The current context for the executing thread can be retrieved through the Current class function.

Syntax

Unit: Sparkle.HttpServer.Context

THttpServerContext = class(TObject);

Constructors

Name Description
Create Creates a new, empty context with no attached items or data.

Methods

Name Description
Current Returns the context associated with the current thread, or nil if none is set.
Item<T> Returns the item of type T previously stored in the context, or the default value of T if no such item exists.
RequiredItem<T> Returns the item of type T stored in the context, raising an exception if it is missing.
SetCurrent Sets the context associated with the current thread, returned later by Current.
SetItem<T> Stores a value in the context keyed by its type T, replacing any existing value for that type.
SetLazyItem<T> Registers a factory that lazily produces the value of type T the first time it is requested.

Properties

Name Description
Data[Key] Gets or sets an arbitrary value associated with the given string Key for this context.
Request Gets the HTTP request being processed by this context.
Response Gets the HTTP response being built for this context.