Class TBrookAction

Unit

Declaration

type TBrookAction = class(TBrookPersistent)

Description

Provides features to handle HTTP requests and responses.

Hierarchy

Overview

Methods

Public constructor Create; overload; virtual;
Public constructor Create(ARequest: TBrookRequest; AResponse: TBrookResponse); overload; virtual;
Public destructor Destroy; override;
Public class procedure Register(const APattern: string; const ADefault: Boolean = False); overload;
Public class procedure Register(const APattern: string; const AMethod: TBrookRequestMethod; const ADefault: Boolean = False); overload;
Public class function GetPath: string;
Public procedure DoFillVariables(const ANames, AValues: TBrookArrayOfString); virtual;
Public procedure DoFillingVariables(const AIndex: Integer; const ANames, AValues: TBrookArrayOfString); virtual;
Public procedure SetCookie(const AName, AValue: string; const AExpires: TDateTime = NullDate; const APath: string = ES; const ADomain: string = ES; const ASecure: Boolean = False; const AHttpOnly: Boolean = False);
Public function GetCookie(const AName: string): string;
Public procedure DeleteCookie(const AName: string; const APath: string = ES; const ADomain: string = ES);
Public procedure DoRequest(ARequest: TBrookRequest; AResponse: TBrookResponse); virtual;
Public procedure Request(ARequest: TBrookRequest; AResponse: TBrookResponse); virtual;
Public procedure GetFields(AObject: TObject);
Public procedure GetParams(AObject: TObject);
Public procedure GetVariables(AObject: TObject);
Public function UrlFor(AActionClass: TBrookActionClass): string; overload;
Public function UrlFor(AActionClass: TBrookActionClass; const AParams: array of string): string; overload;
Public function UrlFor(AClassName: string; const AParams: array of string): string; overload;
Public function UrlFor(AClassName: string): string; overload;
Public procedure Get; virtual;
Public procedure Post; virtual;
Public procedure Put; virtual;
Public procedure Patch; virtual;
Public procedure Delete; virtual;
Public procedure Head; virtual;
Public procedure Options; virtual;
Public procedure Redirect(const AUrl: string); overload;
Public procedure Redirect(const AUrl: string; const AStatusCode: Word); overload;
Public procedure Redirect(const AUrl: string; const AUseRootUrl: Boolean); overload;
Public procedure Redirect(const AUrl: string; const AUseRootUrl: Boolean; const AStatusCode: Word); overload;
Public procedure Error(const AMsg: string); overload;
Public procedure Error(const AMsg: string; const AArgs: array of const); overload;
Public procedure Stop(const AMsg: string); overload;
Public procedure Stop(const AMsg: string; const AArgs: array of const); overload;
Public procedure Render(const AFileName: TFileName); overload; virtual;
Public procedure Render(const AFileName: TFileName; const AArgs: array of const); overload; virtual;
Public procedure Clear;
Public function Exists(const AName: string): Boolean;
Public procedure Write(const AString: string); overload;
Public procedure Write(const ABoolean: Boolean); overload;
Public procedure Write(const AInteger: Integer); overload;
Public procedure Write(const AFloat: Double); overload;
Public procedure Write(AObject: TObject); overload;
Public procedure Write(AObject: TObject; const AIgnoredProps: TStrings); overload;
Public procedure Write(AObject: TObject; const AIgnoredProps: array of string); overload;
Public procedure Write(AStream: TStream); overload;
Public procedure Write(const AFmt: string; const AArgs: array of const); overload;

Properties

Public property Field[constAName:string]: string read GetField write SetField;
Public property Param[constAName:string]: string read GetParam write SetParam;
Public property Variable[constAName:string]: string read GetVariable write SetVariable;
Public property Files: TBrookUploadedFiles read FFiles;
Public property Fields: TStrings read FFields;
Public property Params: TStrings read FParams;
Public property Variables: TStrings read FVariables;
Public property Method: string read GetMethod;
Public property HttpRequest: TBrookRequest read FHttpRequest;
Public property HttpResponse: TBrookResponse read FHttpResponse;

Description

Methods

Public constructor Create; overload; virtual;

Creates an instance of a TBrookAction class.

Public constructor Create(ARequest: TBrookRequest; AResponse: TBrookResponse); overload; virtual;

Creates an instance of a TBrookAction class passing params to request/response.

Public destructor Destroy; override;

Frees an instance of TBrookAction class.

Public class procedure Register(const APattern: string; const ADefault: Boolean = False); overload;

Registers an action.

Parameters
APattern
Is an expression defining which URLs is used to call an action. It is possible to use variables inside URLs:

:name – Represents a variable that spans single URL component between slashes.

Examples:

TMyAction.Register('/foo/:myvar');

Value of a variable "myvar" can be read from the property Variables or Variable, e.g.:

Write(Variables.Values['myvar']);

Write(Variable['myvar']);

Any number of variables can be combined:

TMyAction.Register('/foo/:cat/:id');

*name – Represents a variable that spans one or more levels between slashes in the current URL.

Examples:

TMyAction.Register('/home/*path');

Any of the following URLs will match:

http://localhost/cgi-bin/cgi1/home/file
http://localhost/cgi-bin/cgi1/home/dir/file
http://localhost/cgi-bin/cgi1/home/dir/subdir/file etc.

Variable Variables.Values['path'] will receive 'file', 'dir/file' or 'dir/subdir/file' correspondingly.

You can also add static text after variable part:

TMyAction.Register('/home/*path/download');

http://localhost/cgi-bin/cgi1/home/dir/file/download – This will match,
http://localhost/cgi-bin/cgi1/home/dir/file/info – but not this, because ending is different.

Multi-level variable can be combined with any number of single-level variables in any order:

TMyAction.Register('/home/user/:uid/file/*fpath/version/:vid/info');

NOTE: Only one multi-level variable can be specified per URL.

url/ – Adds a slash to the end of the URL if does not exist.

Example:

TMyAction.Register('/foo/');

An action can be accessed as http://localhost/cgi-bin/cgi1/foo or http://localhost/cgi-bin/cgi1/foo/. When called as http://localhost/cgi-bin/cgi1/foo, it will be automatically redirected to http://localhost/cgi-bin/cgi1/foo/. If the pathinfo is different from /foo a 404 page is returned;

NOTE: Two actions can't be registered with the same pattern except when they are called by means of different HTTP methods.

ADefault
A action registered as Default will be called automatically if the URL does not match with Pattern of any registered actions. It is not allowed to register more than one action as default. A typical example of use is:

TMyAction.Register('*', True);

Public class procedure Register(const APattern: string; const AMethod: TBrookRequestMethod; const ADefault: Boolean = False); overload;

Registers an action specifying the HTTP request method.

Parameters
AMethod
Informs the HTTP request method being valid the following options: rmAll, rmGet, rmHead, rmOptions, rmPost, rmPut or rmDelete. The only way to register two actions with the same pattern is differentiating the value of this parameter. If at least one action has this parameter changed, the route mapping is enabled in Mapped. A typical example of use is:

@(longcode
procedure TMyAction1.Get;
begin
  Write('GET');
end;

procedure TMyAction1.Put;
begin
  Write('PUT');
end;

procedure TMyAction2.Post;
begin
  Write('POST');
end;

initialization
  TMyAction1.Register('/foo1', rmGet);
  TMyAction1.Register('/foo1', rmPut);
  TMyAction2.Register('/foo1', rmPost);)

Public class function GetPath: string;

Returns the path of action. Exemple:

/cgi-bin/cgi1/myaction.

Public procedure DoFillVariables(const ANames, AValues: TBrookArrayOfString); virtual;

Fills the Variables with the registered variables passed through the URL.

Public procedure DoFillingVariables(const AIndex: Integer; const ANames, AValues: TBrookArrayOfString); virtual;

Fills the Variables with the registered variables passed one by one through the URL.

Public procedure SetCookie(const AName, AValue: string; const AExpires: TDateTime = NullDate; const APath: string = ES; const ADomain: string = ES; const ASecure: Boolean = False; const AHttpOnly: Boolean = False);

Creates a cookie.

Public function GetCookie(const AName: string): string;

Get a cookie value.

Public procedure DeleteCookie(const AName: string; const APath: string = ES; const ADomain: string = ES);

Deletes a cookie.

Public procedure DoRequest(ARequest: TBrookRequest; AResponse: TBrookResponse); virtual;

Calls the method Request.

Public procedure Request(ARequest: TBrookRequest; AResponse: TBrookResponse); virtual;

Is triggered by a request of any HTTP method.

Public procedure GetFields(AObject: TObject);

Get an object with the fields coming from a x-www-form-urlencoded form.

Public procedure GetParams(AObject: TObject);

Get an object with the params coming from a QUERY_STRING.

Public procedure GetVariables(AObject: TObject);

Get an object with the variables coming from an URL.

Public function UrlFor(AActionClass: TBrookActionClass): string; overload;

Creates an URL for action.

Public function UrlFor(AActionClass: TBrookActionClass; const AParams: array of string): string; overload;

Creates an URL for an action informing an array of parameters. Exemple:

@(longcode
      procedure TMyAction.Get;
      begin
        // When calling with http://localhost/cgi-bin/cgi1/foo/myvalue
        // the output will be /cgi-bin/cgi1/foo/myvalue
        Write(UrlFor(TMyAction, ['myvalue']));
      end;

      initialization
        TMyAction.Register('/foo/:myvar');)

Public function UrlFor(AClassName: string; const AParams: array of string): string; overload;

Creates an URL for an action passing an array of parameters however informing the class name as string.

Public function UrlFor(AClassName: string): string; overload;

Creates an URL for an action informing the class name as string.

Public procedure Get; virtual;

Is triggered by a GET HTTP request method.

Public procedure Post; virtual;

Is triggered by a POST HTTP request method.

Public procedure Put; virtual;

Is triggered by a PUT HTTP request method.

Public procedure Patch; virtual;

Is triggered by a PATCH HTTP request method.

Public procedure Delete; virtual;

Is triggered by a DELETE HTTP request method.

Public procedure Head; virtual;

Is triggered by a HEAD HTTP request method.

Public procedure Options; virtual;

Is triggered by an OPTIONS HTTP request method.

Public procedure Redirect(const AUrl: string); overload;

Redirects to an URL.

Public procedure Redirect(const AUrl: string; const AStatusCode: Word); overload;

Redirects to an URL informing the (302, 307) status code.

Public procedure Redirect(const AUrl: string; const AUseRootUrl: Boolean); overload;

Redirects to an URL informing the root URL.

Public procedure Redirect(const AUrl: string; const AUseRootUrl: Boolean; const AStatusCode: Word); overload;

Redirects to an URL informing the (302, 307) status code and the ScriptName.

Public procedure Error(const AMsg: string); overload;

Raises a message for action exceptions.

Public procedure Error(const AMsg: string; const AArgs: array of const); overload;

Raises a formated message for action exceptions.

Public procedure Stop(const AMsg: string); overload;

Stops the action showing an exception message.

Public procedure Stop(const AMsg: string; const AArgs: array of const); overload;

Stops the action showing a formatted exception message.

Public procedure Render(const AFileName: TFileName); overload; virtual;

Writes the content of a file.

Public procedure Render(const AFileName: TFileName; const AArgs: array of const); overload; virtual;

Writes the content of a file passing parameters to the output.

Public procedure Clear;

Clears all written content with Write(), WriteLn(), Render() etc.

Public function Exists(const AName: string): Boolean;

Checks if a name exists in fields.

Public procedure Write(const AString: string); overload;

Writes a string.

Public procedure Write(const ABoolean: Boolean); overload;

Writes a boolean.

Public procedure Write(const AInteger: Integer); overload;

Writes an integer.

Public procedure Write(const AFloat: Double); overload;

Writes a float.

Public procedure Write(AObject: TObject); overload;

Writes an object.

Public procedure Write(AObject: TObject; const AIgnoredProps: TStrings); overload;

Writes an object allowing to ignore properties via an array of strings.

Public procedure Write(AObject: TObject; const AIgnoredProps: array of string); overload;

Writes an object allowing to ignore properties via a list of strings.

Public procedure Write(AStream: TStream); overload;

Writes a content of stream.

Public procedure Write(const AFmt: string; const AArgs: array of const); overload;

Writes a formatted string.

Properties

Public property Field[constAName:string]: string read GetField write SetField;

Handles the fields of a form.

Public property Param[constAName:string]: string read GetParam write SetParam;

Handles the Query_String parameters of a URL.

Public property Variable[constAName:string]: string read GetVariable write SetVariable;

Handles variables from a parametrized URL.

Public property Files: TBrookUploadedFiles read FFiles;

Handles a file list of fields of a form.

Public property Fields: TStrings read FFields;

Handles a string list of fields of a form.

Public property Params: TStrings read FParams;

Handles a string list of the Query_String parameters of a URL.

Public property Variables: TStrings read FVariables;

Handles a string list of variables from a parametrized URL.

Public property Method: string read GetMethod;

Returns the HTTP request method.

Public property HttpRequest: TBrookRequest read FHttpRequest;

Provides services related to HTTP requests drived to a webserver.

Public property HttpResponse: TBrookResponse read FHttpResponse;

Provides services related to the HTTP responses comming back from a webserver.


Generated by PasDoc 0.15.0.