Daraja HTTP Framework 3.4.0-SNAPSHOT
HTTP server framework for Object Pascal
Loading...
Searching...
No Matches
TdjWebComponent Class Reference

A base class which can be subclassed to create a HTTP component for a Web site. More...

Inheritance diagram for TdjWebComponent:
TdjGenericWebComponent IWebComponent TdjWebComponentHolder TdjWebComponentHolders

Public Member Functions

 TdjWebComponent ()
 ~TdjWebComponent ()
TIdHTTPSession GetSession (TdjServerContext Context, TdjRequest Request, TdjResponse Response, const Boolean Create=True)
 Get or create a HTTP session.
IWebComponentConfig GetWebComponentConfig ()
 Returns a IWebComponentConfig object, which contains initialization parameters for this component.
 Init ()
 A convenience method which can be overridden so that there is no need to call inherited Init(config).
 Init (const IWebComponentConfig Config)
 Called by the container on startup.
 Service (TdjServerContext Context, TdjRequest Request, TdjResponse Response)
 Handle a HTTP request.

Public Attributes

IWebComponentConfig Config

Protected Member Functions

 OnDelete (TdjRequest Request, TdjResponse Response)
 Called by the server to handle a DELETE request.
 OnGet (TdjRequest Request, TdjResponse Response)
 Called by the server (via the service method) to allow a component to handle a GET request.
string OnGetETag (TdjRequest Request)
 Returns the entity tag (ETag) of the resource a GET/HEAD request would return.
TDateTime OnGetLastModified (TdjRequest Request)
 Returns the time the WebComponent object was last modified.
 OnHead (TdjRequest Request, TdjResponse Response)
 Called by the server (via the service method) to allow a component to handle a HEAD request.
 OnOptions (TdjRequest Request, TdjResponse Response)
 Called by the server (via the service method) to allow a component to handle a OPTIONS request.
 OnPatch (TdjRequest Request, TdjResponse Response)
 Called by the server to handle a PATCH request.
 OnPost (TdjRequest Request, TdjResponse Response)
 Called by the server to handle a POST request.
 OnPut (TdjRequest Request, TdjResponse Response)
 Called by the server to handle a PUT request.
 OnTrace (TdjRequest Request, TdjResponse Response)
 Called by the server to handle a TRACE request.

Detailed Description

A base class which can be subclassed to create a HTTP component for a Web site.

A subclass of TdjWebComponent must override at least one method, usually one of these:

  • OnGet, if the web component supports HTTP GET requests
  • OnPost, for HTTP POST requests
  • OnPut, for HTTP PUT requests
  • OnDelete, for HTTP DELETE requests
Note
Method handling notes and current limitations:
  • every On* handler that is not overridden responds with 405 Method Not Allowed, with one exception: HEAD is derived from OnGet (see OnHead).
  • a 405 response, whether from a not-overridden handler or set explicitly, always carries an Allow header listing the methods this component supports.
  • OPTIONS is answered by the framework itself unless OnOptions is overridden: the default response is 200 with an Allow header and no body (see OnOptions).
  • an unrecognised HTTP method responds with 501 Not Implemented.
  • conditional GET/HEAD is supported through OnGetLastModified (If-Modified-Since) and OnGetETag (If-None-Match). If a request carries If-None-Match, it alone decides the outcome, per RFC 7232 Section 3.3; If-Modified-Since is only consulted when the request has no If-None-Match. A 304 response carries Date, and Last-Modified / ETag whenever the component supplies them.
Threading: the framework creates exactly one instance of each registered component, and every concurrent request runs through that same instance's Service method on its own thread. Do not keep per-request state in instance fields — two requests handled at the same time will read and write the same field, corrupting each other's data. Use local variables for anything request-specific, and if state truly must be shared across requests, synchronize access to it explicitly (e.g. with a critical section).

Constructor & Destructor Documentation

◆ TdjWebComponent()

TdjWebComponent::TdjWebComponent ( )

◆ ~TdjWebComponent()

TdjWebComponent::~TdjWebComponent ( )

Member Function Documentation

◆ GetSession()

TIdHTTPSession TdjGenericWebComponent::GetSession ( TdjServerContext Context,
TdjRequest Request,
TdjResponse Response,
const Boolean Create = True )
inherited

Get or create a HTTP session.

Note
it requires the current TdjServerContext, so it cannot be called from one of the HTTP method handlers (OnGet etc.). It can be called from the Service method.
if the context was created with the Auto Session option, this method will always return a session independent of the Create parameter
Parameters
ContextHTTP server context
RequestHTTP request
ResponseHTTP response
Createif True, create a session if no one exists
Returns
HTTP session

◆ GetWebComponentConfig()

IWebComponentConfig TdjGenericWebComponent::GetWebComponentConfig ( )
inherited

Returns a IWebComponentConfig object, which contains initialization parameters for this component.

Exceptions
EDarajaLifecycleExceptionif the method is called before the component has been initialized.

Implements IWebComponent.

◆ Init() [1/2]

TdjGenericWebComponent::Init ( )
inherited

A convenience method which can be overridden so that there is no need to call inherited Init(config).

References TdjGenericWebComponent().

◆ Init() [2/2]

TdjGenericWebComponent::Init ( const IWebComponentConfig Config)
inherited

Called by the container on startup.

Note
if this method is overridden, the overriding code must also call inherited Init.
Parameters
Configthe configuration
Exceptions
EDarajaExceptionif initialization failed

Implements IWebComponent.

References Init().

Referenced by Init().

◆ OnDelete()

TdjWebComponent::OnDelete ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server to handle a DELETE request.

Parameters
RequestThe HTTP request to process
ResponseThe HTTP response to fill
Exceptions
EDarajaExceptionif an exception occurs

References OnGet().

◆ OnGet()

TdjWebComponent::OnGet ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server (via the service method) to allow a component to handle a GET request.

Referenced by OnDelete().

◆ OnGetETag()

string TdjWebComponent::OnGetETag ( TdjRequest Request)
protected

Returns the entity tag (ETag) of the resource a GET/HEAD request would return.

If unknown, this method returns '' (the default), which means no ETag is offered and no If-None-Match comparison is done.

WebComponents that support HTTP GET requests and can cheaply compute a stable identifier for their current representation should override this method. The returned value is sent as the ETag header, quoted per RFC 7232 if not already (e.g. '"abc123"'), and compared against a request's If-None-Match header using a weak comparison (a leading 'W/' is ignored on both sides).

Parameters
RequestHTTP request
Returns
the entity tag, or '' if unknown

◆ OnGetLastModified()

TDateTime TdjWebComponent::OnGetLastModified ( TdjRequest Request)
protected

Returns the time the WebComponent object was last modified.

If the time is unknown, this method returns 0 (the default).

WebComponents that support HTTP GET requests and can quickly determine their last modification time should override this method. This makes browser and proxy caches work more effectively, reducing the load on server and network resources.

Parameters
RequestHTTP request
Returns
the last modified timestamp

◆ OnHead()

TdjWebComponent::OnHead ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server (via the service method) to allow a component to handle a HEAD request.

The default implementation runs the same code path as a GET request, including the OnGetLastModified / OnGetETag conditional handling, and the response body is suppressed. A component which overrides OnGet therefore answers HEAD requests with the GET headers and no content. If OnGet is not overridden either, the response is 405 Method Not Allowed.

Note
because the GET path runs in full, an expensive OnGet does its work for a HEAD request too, and any side effect of OnGet is triggered by a HEAD request as well. Override this method to handle HEAD separately.
Parameters
RequestThe HTTP request to process
ResponseThe HTTP response to fill
Exceptions
EDarajaExceptionif an exception occurs

◆ OnOptions()

TdjWebComponent::OnOptions ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server (via the service method) to allow a component to handle a OPTIONS request.

The default implementation responds with 200 and an Allow header listing the HTTP methods this component supports (i.e. the On* handlers that are overridden), with no response body.

Parameters
RequestThe HTTP request to process
ResponseThe HTTP response to fill
Exceptions
EDarajaExceptionif an exception occurs

◆ OnPatch()

TdjWebComponent::OnPatch ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server to handle a PATCH request.

Parameters
RequestThe HTTP request to process
ResponseThe HTTP response to fill
Exceptions
EDarajaExceptionif an exception occurs
See also
http://tools.ietf.org/html/rfc5789

◆ OnPost()

TdjWebComponent::OnPost ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server to handle a POST request.

Parameters
RequestThe HTTP request to process
ResponseThe HTTP response to fill
Exceptions
EDarajaExceptionif an exception occurs

◆ OnPut()

TdjWebComponent::OnPut ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server to handle a PUT request.

Parameters
RequestThe HTTP request to process
ResponseThe HTTP response to fill
Exceptions
EDarajaExceptionif an exception occurs

◆ OnTrace()

TdjWebComponent::OnTrace ( TdjRequest Request,
TdjResponse Response )
protected

Called by the server to handle a TRACE request.

Parameters
RequestThe HTTP request to process
ResponseThe HTTP response to fill
Exceptions
EDarajaExceptionif an exception occurs

◆ Service()

TdjWebComponent::Service ( TdjServerContext Context,
TdjRequest Request,
TdjResponse Response )

Handle a HTTP request.

The status code of the response always should be set for a component that throws or sends an error.

Note
a custom Web Component should not override this method.
Parameters
ContextHTTP server context
RequestHTTP request
ResponseHTTP response
Exceptions
EDarajaExceptionif an exception occurs that interferes with the component's normal operation

Implements IWebComponent.

Member Data Documentation

◆ Config

IWebComponentConfig TdjGenericWebComponent::Config
inherited