Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
+ Adds broker for FCL-web's embedded HTTP server (Thank you, leledumb…
Browse files Browse the repository at this point in the history
…o! :) ).
  • Loading branch information
silvioprog committed Jan 1, 2013
1 parent 15f0141 commit 3471703
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 59 deletions.
192 changes: 192 additions & 0 deletions brokers/brookfclhttpappbroker.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
(*
Brook FCL HTTPApp Broker unit.
Copyright (C) 2013 Mario Ray Mahardhika.
http://brookframework.org
All contributors:
Plase see the file CONTRIBUTORS.txt, included in this
distribution.
See the file LICENSE.txt, included in this distribution,
for details about the copyright.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*)

unit BrookFCLHTTPAppBroker;

{$mode objfpc}{$H+}

interface

uses
BrookClasses, BrookApplication, BrookException, BrookMessages, BrookConsts,
BrookHTTPConsts, BrookRouter, BrookUtils, HTTPDefs, CustWeb, CustHTTPApp,
FPJSON, Classes, SysUtils;

type
TBrookHTTPAppApplication = class;

{ TBrookApplication }

TBrookApplication = class(TBrookInterfacedObject, IBrookApplication)
private
FApp: TBrookHTTPAppApplication;
public
constructor Create; virtual;
destructor Destroy; override;
function Instance: TObject;
procedure Run;
end;

{ TBrookHTTPAppApplication }

TBrookHTTPAppApplication = class(TCustomHTTPApplication)
protected
function InitializeWebHandler: TWebHandler; override;
end;

{ TBrookHTTPServerHandler }

TBrookHTTPServerHandler = class(TFPHTTPServerHandler)
protected
function FormatContentType: string;
public
procedure HandleRequest(ARequest: TRequest; AResponse: TResponse); override;
procedure ShowRequestException(R: TResponse; E: Exception); override;
end;

implementation

{ TBrookApplication }

constructor TBrookApplication.Create;
begin
FApp := TBrookHTTPAppApplication.Create(nil);
FApp.Initialize;
end;

destructor TBrookApplication.Destroy;
begin
FApp.Free;
inherited Destroy;
end;

function TBrookApplication.Instance: TObject;
begin
Result := FApp;
end;

procedure TBrookApplication.Run;
begin
FApp.Run;
end;

{ TBrookHTTPAppApplication }

function TBrookHTTPAppApplication.InitializeWebHandler: TWebHandler;
begin
Result := TBrookHTTPServerHandler.Create(Self);
end;

{ TBrookHTTPServerHandler }

function TBrookHTTPServerHandler.FormatContentType: string;
begin
if BrookSettings.Charset <> ES then
Result := BrookSettings.ContentType + BROOK_HTTP_HEADER_CHARSET +
BrookSettings.Charset
else
Result := BrookSettings.ContentType;
end;

procedure TBrookHTTPServerHandler.HandleRequest(ARequest: TRequest;
AResponse: TResponse);
begin
AResponse.ContentType := FormatContentType;
if BrookSettings.Language <> BROOK_DEFAULT_LANGUAGE then
TBrookMessages.Service.SetLanguage(BrookSettings.Language);
TBrookRouter.Service.Route(ARequest, AResponse);
end;

procedure TBrookHTTPServerHandler.ShowRequestException(R: TResponse; E: Exception);
var
VStr: TStrings;
VHandled: Boolean = False;
begin
if R.ContentSent then
Exit;
if Assigned(BrookSettings.OnError) then
begin
BrookSettings.OnError(R, E, VHandled);
if VHandled then
Exit;
end;
if Assigned(OnShowRequestException) then
begin
OnShowRequestException(R, E, VHandled);
if VHandled then
Exit;
end;
if RedirectOnError and not R.HeadersSent then
begin
R.SendRedirect(Format(RedirectOnErrorURL, [HTTPEncode(E.Message)]));
R.SendContent;
Exit;
end;
if (BrookSettings.Page404 <> ES) and (E is EBrookHTTP404) and
(not R.HeadersSent) then
begin
R.Code := BROOK_HTTP_STATUS_CODE_NOT_FOUND;
R.CodeText := BROOK_HTTP_REASON_PHRASE_NOT_FOUND;
R.ContentType := FormatContentType;
if FileExists(BrookSettings.Page404) then
R.Contents.LoadFromFile(BrookSettings.Page404)
else
R.Content := BrookSettings.Page404;
R.Content := Format(R.Content, [ApplicationURL]);
R.SendContent;
Exit;
end;
if (BrookSettings.Page500 <> ES) and (not R.HeadersSent) then
begin
R.Code := BROOK_HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR;
R.CodeText := 'Application error ' + E.ClassName;
R.ContentType := FormatContentType;
if FileExists(BrookSettings.Page500) then
begin
R.Contents.LoadFromFile(BrookSettings.Page500);
R.Content := Format(R.Content, [E.Message]);
end
else
if BrookSettings.ContentType = BROOK_HTTP_CONTENT_TYPE_APP_JSON then
R.Content := Format(BrookSettings.Page500,
[StringToJSONString(E.Message)])
else
R.Content := Format(BrookSettings.Page500, [E.Message]);
R.SendContent;
Exit;
end;
if (R.ContentType = BROOK_HTTP_CONTENT_TYPE_TEXT_HTML) or
(BrookSettings.ContentType = BROOK_HTTP_CONTENT_TYPE_TEXT_HTML) then
begin
VStr := TStringList.Create;
try
R.ContentType := FormatContentType;
ExceptionToHTML(VStr, E, Title, Email, Administrator);
R.Content := VStr.Text;
R.SendContent;
finally
VStr.Free;
end;
end;
end;

initialization
BrookRegisterApp(TBrookApplication.Create);

end.
122 changes: 63 additions & 59 deletions packages/brookrt.lpk
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
</CompilerOptions>
<Description Value="Brook runtime package."/>
<License Value="See the file LICENSE, included in this distribution, for details about the copyright."/>
<Version Release="1"/>
<Files Count="32">
<Version Release="2"/>
<Files Count="33">
<Item1>
<Filename Value="..\brokers\brookfclcgibroker.pas"/>
<UnitName Value="BrookFCLCGIBroker"/>
Expand All @@ -32,125 +32,129 @@
<UnitName Value="BrookFCLFCGIBroker"/>
</Item2>
<Item3>
<Filename Value="..\brokers\brookmessages_enus.pas"/>
<UnitName Value="BrookMessages_enUS"/>
<Filename Value="..\brokers\brookfclhttpappbroker.pas"/>
<UnitName Value="BrookFCLHTTPAppBroker"/>
</Item3>
<Item4>
<Filename Value="..\brokers\brookmessages_ptbr.pas"/>
<UnitName Value="BrookMessages_ptBR"/>
<Filename Value="..\brokers\brookmessages_enus.pas"/>
<UnitName Value="BrookMessages_enUS"/>
</Item4>
<Item5>
<Filename Value="..\brokers\brooksqldbbroker.pas"/>
<UnitName Value="BrookSQLdbBroker"/>
<Filename Value="..\brokers\brookmessages_ptbr.pas"/>
<UnitName Value="BrookMessages_ptBR"/>
</Item5>
<Item6>
<Filename Value="..\core\brookaction.pas"/>
<UnitName Value="BrookAction"/>
<Filename Value="..\brokers\brooksqldbbroker.pas"/>
<UnitName Value="BrookSQLdbBroker"/>
</Item6>
<Item7>
<Filename Value="..\core\brookapplication.pas"/>
<UnitName Value="BrookApplication"/>
<Filename Value="..\core\brookaction.pas"/>
<UnitName Value="BrookAction"/>
</Item7>
<Item8>
<Filename Value="..\core\brookclasses.pas"/>
<UnitName Value="BrookClasses"/>
<Filename Value="..\core\brookapplication.pas"/>
<UnitName Value="BrookApplication"/>
</Item8>
<Item9>
<Filename Value="..\core\brookconfigurator.pas"/>
<UnitName Value="BrookConfigurator"/>
<Filename Value="..\core\brookclasses.pas"/>
<UnitName Value="BrookClasses"/>
</Item9>
<Item10>
<Filename Value="..\core\brookconsts.pas"/>
<UnitName Value="BrookConsts"/>
<Filename Value="..\core\brookconfigurator.pas"/>
<UnitName Value="BrookConfigurator"/>
</Item10>
<Item11>
<Filename Value="..\core\brookdatabase.pas"/>
<UnitName Value="BrookDataBase"/>
<Filename Value="..\core\brookconsts.pas"/>
<UnitName Value="BrookConsts"/>
</Item11>
<Item12>
<Filename Value="..\core\brookdbaction.pas"/>
<UnitName Value="BrookDBAction"/>
<Filename Value="..\core\brookdatabase.pas"/>
<UnitName Value="BrookDataBase"/>
</Item12>
<Item13>
<Filename Value="..\core\brookdbconsts.pas"/>
<UnitName Value="BrookDBConsts"/>
<Filename Value="..\core\brookdbaction.pas"/>
<UnitName Value="BrookDBAction"/>
</Item13>
<Item14>
<Filename Value="..\core\brookdbutils.pas"/>
<UnitName Value="BrookDBUtils"/>
<Filename Value="..\core\brookdbconsts.pas"/>
<UnitName Value="BrookDBConsts"/>
</Item14>
<Item15>
<Filename Value="..\core\brookexception.pas"/>
<UnitName Value="BrookException"/>
<Filename Value="..\core\brookdbutils.pas"/>
<UnitName Value="BrookDBUtils"/>
</Item15>
<Item16>
<Filename Value="..\core\brookhttpconsts.pas"/>
<UnitName Value="BrookHTTPConsts"/>
<Filename Value="..\core\brookexception.pas"/>
<UnitName Value="BrookException"/>
</Item16>
<Item17>
<Filename Value="..\core\brookhttputils.pas"/>
<UnitName Value="BrookHTTPUtils"/>
<Filename Value="..\core\brookhttpconsts.pas"/>
<UnitName Value="BrookHTTPConsts"/>
</Item17>
<Item18>
<Filename Value="..\core\brookmessages.pas"/>
<UnitName Value="BrookMessages"/>
<Filename Value="..\core\brookhttputils.pas"/>
<UnitName Value="BrookHTTPUtils"/>
</Item18>
<Item19>
<Filename Value="..\core\brookquery.pas"/>
<UnitName Value="BrookQuery"/>
<Filename Value="..\core\brookmessages.pas"/>
<UnitName Value="BrookMessages"/>
</Item19>
<Item20>
<Filename Value="..\core\brookrestactions.pas"/>
<UnitName Value="BrookRESTActions"/>
<Filename Value="..\core\brookquery.pas"/>
<UnitName Value="BrookQuery"/>
</Item20>
<Item21>
<Filename Value="..\core\brookrouter.pas"/>
<UnitName Value="BrookRouter"/>
<Filename Value="..\core\brookrestactions.pas"/>
<UnitName Value="BrookRESTActions"/>
</Item21>
<Item22>
<Filename Value="..\core\brooksession.pas"/>
<UnitName Value="BrookSession"/>
<Filename Value="..\core\brookrouter.pas"/>
<UnitName Value="BrookRouter"/>
</Item22>
<Item23>
<Filename Value="..\core\brooktable.pas"/>
<UnitName Value="BrookTable"/>
<Filename Value="..\core\brooksession.pas"/>
<UnitName Value="BrookSession"/>
</Item23>
<Item24>
<Filename Value="..\core\brookutils.pas"/>
<UnitName Value="BrookUtils"/>
<Filename Value="..\core\brooktable.pas"/>
<UnitName Value="BrookTable"/>
</Item24>
<Item25>
<Filename Value="..\helpers\brookactionhelper.pas"/>
<UnitName Value="BrookActionHelper"/>
<Filename Value="..\core\brookutils.pas"/>
<UnitName Value="BrookUtils"/>
</Item25>
<Item26>
<Filename Value="..\helpers\brookjsonhelper.pas"/>
<UnitName Value="BrookJSONHelper"/>
<Filename Value="..\helpers\brookactionhelper.pas"/>
<UnitName Value="BrookActionHelper"/>
</Item26>
<Item27>
<Filename Value="..\helpers\brookrequesthelper.pas"/>
<UnitName Value="BrookRequestHelper"/>
<Filename Value="..\helpers\brookjsonhelper.pas"/>
<UnitName Value="BrookJSONHelper"/>
</Item27>
<Item28>
<Filename Value="..\helpers\brookresponsehelper.pas"/>
<UnitName Value="BrookResponseHelper"/>
<Filename Value="..\helpers\brookrequesthelper.pas"/>
<UnitName Value="BrookRequestHelper"/>
</Item28>
<Item29>
<Filename Value="..\includes\brook.inc"/>
<Type Value="Include"/>
<Filename Value="..\helpers\brookresponsehelper.pas"/>
<UnitName Value="BrookResponseHelper"/>
</Item29>
<Item30>
<Filename Value="..\includes\brookmessages.inc"/>
<Filename Value="..\includes\brook.inc"/>
<Type Value="Include"/>
</Item30>
<Item31>
<Filename Value="..\includes\brookmessages_enus.inc"/>
<Filename Value="..\includes\brookmessages.inc"/>
<Type Value="Include"/>
</Item31>
<Item32>
<Filename Value="..\includes\brookmessages_ptbr.inc"/>
<Filename Value="..\includes\brookmessages_enus.inc"/>
<Type Value="Include"/>
</Item32>
<Item33>
<Filename Value="..\includes\brookmessages_ptbr.inc"/>
<Type Value="Include"/>
</Item33>
</Files>
<UsageOptions>
<UnitPath Value="$(PkgOutDir)"/>
Expand Down

0 comments on commit 3471703

Please sign in to comment.