custweb.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2009 by the Free Pascal development team
  4. TWebApplication class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { $define CGIDEBUG}
  12. {$mode objfpc}
  13. {$H+}
  14. unit custweb;
  15. Interface
  16. uses
  17. CustApp,Classes,SysUtils, httpdefs, fphttp, eventlog;
  18. Const
  19. CGIVarCount = 37;
  20. Type
  21. TCGIVarArray = Array[1..CGIVarCount] of String;
  22. Const
  23. CgiVarNames : TCGIVarArray =
  24. ({ 1 } 'AUTH_TYPE',
  25. { 2 } 'CONTENT_LENGTH',
  26. { 3 } 'CONTENT_TYPE',
  27. { 4 } 'GATEWAY_INTERFACE',
  28. { 5 } 'PATH_INFO',
  29. { 6 } 'PATH_TRANSLATED',
  30. { 7 } 'QUERY_STRING',
  31. { 8 } 'REMOTE_ADDR',
  32. { 9 } 'REMOTE_HOST',
  33. { 10 } 'REMOTE_IDENT',
  34. { 11 } 'REMOTE_USER',
  35. { 12 } 'REQUEST_METHOD',
  36. { 13 } 'SCRIPT_NAME',
  37. { 14 } 'SERVER_NAME',
  38. { 15 } 'SERVER_PORT',
  39. { 16 } 'SERVER_PROTOCOL',
  40. { 17 } 'SERVER_SOFTWARE',
  41. { 18 } 'HTTP_ACCEPT',
  42. { 19 } 'HTTP_ACCEPT_CHARSET',
  43. { 20 } 'HTTP_ACCEPT_ENCODING',
  44. { 21 } 'HTTP_IF_MODIFIED_SINCE',
  45. { 22 } 'HTTP_REFERER',
  46. { 23 } 'HTTP_USER_AGENT',
  47. { 24 } 'HTTP_COOKIE',
  48. // Additional Apache vars
  49. { 25 } 'HTTP_CONNECTION',
  50. { 26 } 'HTTP_ACCEPT_LANGUAGE',
  51. { 27 } 'HTTP_HOST',
  52. { 28 } 'SERVER_SIGNATURE',
  53. { 29 } 'SERVER_ADDR',
  54. { 30 } 'DOCUMENT_ROOT',
  55. { 31 } 'SERVER_ADMIN',
  56. { 32 } 'SCRIPT_FILENAME',
  57. { 33 } 'REMOTE_PORT',
  58. { 34 } 'REQUEST_URI',
  59. { 35 } 'CONTENT',
  60. { 36 } 'HTTP_X_REQUESTED_WITH',
  61. { 37 } 'HTTP_AUTHORIZATION'
  62. );
  63. Type
  64. { TCustomWebApplication }
  65. TGetModuleEvent = Procedure (Sender : TObject; ARequest : TRequest;
  66. Var ModuleClass : TCustomHTTPModuleClass) of object;
  67. TOnShowRequestException = procedure(AResponse: TResponse; AnException: Exception; var handled: boolean);
  68. TLogEvent = Procedure (EventType: TEventType; const Msg: String) of object;
  69. TInitModuleEvent = Procedure (Sender : TObject; Module: TCustomHTTPModule) of object;
  70. { TWebHandler }
  71. TWebHandler = class(TComponent)
  72. private
  73. FDefaultModuleName: String;
  74. FOnIdle: TNotifyEvent;
  75. FOnInitModule: TInitModuleEvent;
  76. FOnUnknownRequestEncoding: TOnUnknownEncodingEvent;
  77. FTerminated: boolean;
  78. FAdministrator: String;
  79. FAllowDefaultModule: Boolean;
  80. FApplicationURL: String;
  81. FEmail: String;
  82. FModuleVar: String;
  83. FOnGetModule: TGetModuleEvent;
  84. FOnShowRequestException: TOnShowRequestException;
  85. FHandleGetOnPost : Boolean;
  86. FRedirectOnError : Boolean;
  87. FRedirectOnErrorURL : String;
  88. FTitle: string;
  89. FOnTerminate : TNotifyEvent;
  90. FOnLog : TLogEvent;
  91. FPreferModuleName : Boolean;
  92. protected
  93. procedure Terminate;
  94. Function GetModuleName(Arequest : TRequest) : string;
  95. function WaitForRequest(out ARequest : TRequest; out AResponse : TResponse) : boolean; virtual; abstract;
  96. procedure EndRequest(ARequest : TRequest;AResponse : TResponse); virtual;
  97. function FindModule(ModuleClass : TCustomHTTPModuleClass): TCustomHTTPModule;
  98. Procedure SetBaseURL(AModule : TCustomHTTPModule; Const AModuleName : String; ARequest : TRequest); virtual;
  99. function GetApplicationURL(ARequest : TRequest): String; virtual;
  100. procedure ShowRequestException(R: TResponse; E: Exception); virtual;
  101. Procedure InitRequest(ARequest : TRequest); virtual;
  102. Procedure InitResponse(AResponse : TResponse); virtual;
  103. Function GetEmail : String; virtual;
  104. Function GetAdministrator : String; virtual;
  105. property Terminated: boolean read FTerminated;
  106. Public
  107. constructor Create(AOwner: TComponent); override;
  108. Procedure Run; virtual;
  109. Procedure Log(EventType : TEventType; Const Msg : String);
  110. Procedure DoHandleRequest(ARequest : TRequest; AResponse : TResponse);
  111. Procedure HandleRequest(ARequest : TRequest; AResponse : TResponse); virtual;
  112. Property HandleGetOnPost : Boolean Read FHandleGetOnPost Write FHandleGetOnPost;
  113. Property RedirectOnError : boolean Read FRedirectOnError Write FRedirectOnError;
  114. Property RedirectOnErrorURL : string Read FRedirectOnErrorURL Write FRedirectOnErrorURL;
  115. Property ApplicationURL : String Read FApplicationURL Write FApplicationURL;
  116. Property AllowDefaultModule : Boolean Read FAllowDefaultModule Write FAllowDefaultModule;
  117. Property DefaultModuleName : String Read FDefaultModuleName Write FDefaultModuleName;
  118. Property ModuleVariable : String Read FModuleVar Write FModuleVar;
  119. Property OnGetModule : TGetModuleEvent Read FOnGetModule Write FOnGetModule;
  120. Property Email : String Read GetEmail Write FEmail;
  121. property Title: string read FTitle write FTitle;
  122. Property Administrator : String Read GetAdministrator Write FAdministrator;
  123. property OnShowRequestException: TOnShowRequestException read FOnShowRequestException write FOnShowRequestException;
  124. property OnIdle: TNotifyEvent read FOnIdle write FOnIdle;
  125. Property OnLog : TLogEvent Read FOnLog Write FOnLog;
  126. Property OnUnknownRequestEncoding : TOnUnknownEncodingEvent Read FOnUnknownRequestEncoding Write FOnUnknownRequestEncoding;
  127. Property OnInitModule: TInitModuleEvent Read FOnInitModule write FOnInitModule;
  128. Property PreferModuleName : Boolean Read FPreferModuleName Write FPreferModuleName;
  129. end;
  130. TCustomWebApplication = Class(TCustomApplication)
  131. Private
  132. FEventLog: TEventLog;
  133. FWebHandler: TWebHandler;
  134. function GetAdministrator: String;
  135. function GetAllowDefaultModule: Boolean;
  136. function GetApplicationURL: String;
  137. function GetDefaultModuleName: String;
  138. function GetEmail: String;
  139. function GetEventLog: TEventLog;
  140. function GetHandleGetOnPost: Boolean;
  141. function GetModuleVar: String;
  142. function GetOnGetModule: TGetModuleEvent;
  143. function GetOnShowRequestException: TOnShowRequestException;
  144. function GetOnUnknownRequestEncoding: TOnUnknownEncodingEvent;
  145. function GetRedirectOnError: boolean;
  146. function GetRedirectOnErrorURL: string;
  147. function GetPreferModuleName: boolean;
  148. procedure SetAdministrator(const AValue: String);
  149. procedure SetAllowDefaultModule(const AValue: Boolean);
  150. procedure SetApplicationURL(const AValue: String);
  151. procedure SetDefaultModuleName(AValue: String);
  152. procedure SetEmail(const AValue: String);
  153. procedure SetHandleGetOnPost(const AValue: Boolean);
  154. procedure SetModuleVar(const AValue: String);
  155. procedure SetOnGetModule(const AValue: TGetModuleEvent);
  156. procedure SetOnShowRequestException(const AValue: TOnShowRequestException);
  157. procedure SetOnUnknownRequestEncoding(AValue: TOnUnknownEncodingEvent);
  158. procedure SetRedirectOnError(const AValue: boolean);
  159. procedure SetRedirectOnErrorURL(const AValue: string);
  160. procedure SetPreferModuleName(const AValue: boolean);
  161. procedure DoOnTerminate(Sender : TObject);
  162. protected
  163. Procedure DoRun; override;
  164. function InitializeWebHandler: TWebHandler; virtual; abstract;
  165. Procedure DoLog(EventType: TEventType; const Msg: String); override;
  166. procedure SetTitle(const AValue: string); override;
  167. property WebHandler: TWebHandler read FWebHandler write FWebHandler;
  168. Public
  169. constructor Create(AOwner: TComponent); override;
  170. destructor Destroy; override;
  171. Procedure CreateForm(AClass : TComponentClass; out Reference);
  172. Procedure Initialize; override;
  173. procedure Terminate; override;
  174. Property HandleGetOnPost : Boolean Read GetHandleGetOnPost Write SetHandleGetOnPost;
  175. Property RedirectOnError : boolean Read GetRedirectOnError Write SetRedirectOnError;
  176. Property RedirectOnErrorURL : string Read GetRedirectOnErrorURL Write SetRedirectOnErrorURL;
  177. Property ApplicationURL : String Read GetApplicationURL Write SetApplicationURL;
  178. Property AllowDefaultModule : Boolean Read GetAllowDefaultModule Write SetAllowDefaultModule;
  179. Property DefaultModuleName : String Read GetDefaultModuleName Write SetDefaultModuleName;
  180. Property ModuleVariable : String Read GetModuleVar Write SetModuleVar;
  181. Property OnGetModule : TGetModuleEvent Read GetOnGetModule Write SetOnGetModule;
  182. Property Email : String Read GetEmail Write SetEmail;
  183. Property Administrator : String Read GetAdministrator Write SetAdministrator;
  184. property OnShowRequestException: TOnShowRequestException read GetOnShowRequestException write SetOnShowRequestException;
  185. Property OnUnknownRequestEncoding : TOnUnknownEncodingEvent Read GetOnUnknownRequestEncoding Write SetOnUnknownRequestEncoding;
  186. Property EventLog: TEventLog read GetEventLog;
  187. Property PreferModuleName : Boolean Read GetPreferModuleName Write SetPreferModuleName;
  188. end;
  189. EFPWebError = Class(Exception);
  190. procedure ExceptionToHTML(S: TStrings; const E: Exception; const Title, Email, Administrator: string);
  191. Implementation
  192. {$ifdef CGIDEBUG}
  193. uses
  194. dbugintf;
  195. {$endif}
  196. resourcestring
  197. SErrNoModuleNameForRequest = 'Could not determine HTTP module name for request';
  198. SErrNoModuleForRequest = 'Could not determine HTTP module for request "%s"';
  199. SErrSendingContent = 'An error (%s) happened while sending response content: %s';
  200. SModuleError = 'Module Error';
  201. SAppEncounteredError = 'The application encountered the following error:';
  202. SError = 'Error: ';
  203. SNotify = 'Notify: ';
  204. procedure ExceptionToHTML(S: TStrings; const E: Exception; const Title, Email, Administrator: string);
  205. var
  206. FrameNumber: Integer;
  207. Frames: PPointer;
  208. FrameCount: integer;
  209. TheEmail: String;
  210. begin
  211. With S do
  212. begin
  213. Add('<html><head><title>'+Title+': '+SModuleError+'</title></head>'+LineEnding);
  214. Add('<body>');
  215. Add('<center><hr><h1>'+Title+': ERROR</h1><hr></center><br><br>');
  216. Add(SAppEncounteredError+'<br>');
  217. Add('<ul>');
  218. Add('<li>'+SError+' <b>'+E.Message+'</b>');
  219. Add('<li> Stack trace:<br>');
  220. Add(BackTraceStrFunc(ExceptAddr)+'<br>');
  221. FrameCount:=ExceptFrameCount;
  222. Frames:=ExceptFrames;
  223. for FrameNumber := 0 to FrameCount-1 do
  224. Add(BackTraceStrFunc(Frames[FrameNumber])+'<br>');
  225. Add('</ul><hr>');
  226. TheEmail:=Email;
  227. If (TheEmail<>'') then
  228. Add('<h5><p><i>'+SNotify+Administrator+': <a href="mailto:'+TheEmail+'">'+TheEmail+'</a></i></p></h5>');
  229. Add('</body></html>');
  230. end;
  231. end;
  232. procedure TWebHandler.Run;
  233. var ARequest : TRequest;
  234. AResponse : TResponse;
  235. begin
  236. while not FTerminated do
  237. begin
  238. if WaitForRequest(ARequest,AResponse) then
  239. DoHandleRequest(ARequest,AResponse);
  240. if assigned(OnIdle) then
  241. OnIdle(Self);
  242. end;
  243. end;
  244. procedure TWebHandler.Log(EventType: TEventType; const Msg: String);
  245. begin
  246. If Assigned(FOnLog) then
  247. FOnLog(EventType,Msg);
  248. end;
  249. procedure TWebHandler.ShowRequestException(R: TResponse; E: Exception);
  250. Var
  251. S : TStrings;
  252. handled: boolean;
  253. begin
  254. if R.ContentSent then exit;
  255. if assigned(OnShowRequestException) then
  256. begin
  257. handled:=false;
  258. OnShowRequestException(R,E,Handled);
  259. if handled then exit;
  260. end;
  261. If RedirectOnError and not R.HeadersSent then
  262. begin
  263. R.SendRedirect(format(RedirectOnErrorURL,[HTTPEncode(E.Message)]));
  264. R.SendContent;
  265. Exit;
  266. end;
  267. If (not R.HeadersSent) then
  268. begin
  269. R.Code:=500;
  270. R.CodeText:='Application error '+E.ClassName;
  271. R.ContentType:='text/html';
  272. end;
  273. If (R.ContentType='text/html') then
  274. begin
  275. S:=TStringList.Create;
  276. Try
  277. ExceptionToHTML(S, E, Title, Email, Administrator);
  278. R.Content:=S.Text;
  279. R.SendContent;
  280. Finally
  281. FreeAndNil(S);
  282. end;
  283. end;
  284. end;
  285. procedure TWebHandler.InitRequest(ARequest: TRequest);
  286. begin
  287. ARequest.OnUnknownEncoding:=Self.OnUnknownRequestEncoding;
  288. end;
  289. procedure TWebHandler.InitResponse(AResponse: TResponse);
  290. begin
  291. // Do nothing
  292. end;
  293. function TWebHandler.GetEmail: String;
  294. begin
  295. Result := FEmail;
  296. end;
  297. function TWebHandler.GetAdministrator: String;
  298. begin
  299. Result := FAdministrator;
  300. end;
  301. procedure TWebHandler.HandleRequest(ARequest: TRequest; AResponse: TResponse);
  302. Var
  303. MC : TCustomHTTPModuleClass;
  304. M : TCustomHTTPModule;
  305. MN : String;
  306. MI : TModuleItem;
  307. begin
  308. try
  309. MC:=Nil;
  310. M:=NIL;
  311. MI:=Nil;
  312. If (OnGetModule<>Nil) then
  313. OnGetModule(Self,ARequest,MC);
  314. If (MC=Nil) then
  315. begin
  316. MN:=GetModuleName(ARequest);
  317. MI:=ModuleFactory.FindModule(MN);
  318. if (MI=Nil) then
  319. Raise EFPWebError.CreateFmt(SErrNoModuleForRequest,[MN]);
  320. MC:=MI.ModuleClass;
  321. end;
  322. M:=FindModule(MC); // Check if a module exists already
  323. If (M=Nil) then
  324. if assigned(MI) and Mi.SkipStreaming then
  325. M:=MC.CreateNew(Self)
  326. else
  327. M:=MC.Create(Self);
  328. SetBaseURL(M,MN,ARequest);
  329. if (OnInitModule<>Nil) then
  330. OnInitModule(Self,M);
  331. M.DoAfterInitModule(ARequest);
  332. if M.Kind=wkOneShot then
  333. begin
  334. try
  335. M.HandleRequest(ARequest,AResponse);
  336. finally
  337. M.Free;
  338. end;
  339. end
  340. else
  341. M.HandleRequest(ARequest,AResponse);
  342. except
  343. On E : Exception do
  344. ShowRequestException(AResponse,E);
  345. end;
  346. end;
  347. function TWebHandler.GetApplicationURL(ARequest: TRequest): String;
  348. begin
  349. Result:=FApplicationURL;
  350. If (Result='') then
  351. Result:=ARequest.ScriptName;
  352. end;
  353. procedure TWebHandler.Terminate;
  354. begin
  355. FTerminated := true;
  356. If Assigned(FOnTerminate) then
  357. FOnTerminate(Self);
  358. end;
  359. function TWebHandler.GetModuleName(Arequest: TRequest): string;
  360. Function GetDefaultModuleName : String;
  361. begin
  362. if (DefaultModuleName<>'') then
  363. Result:=DefaultModuleName
  364. else if (ModuleFactory.Count=1) then
  365. Result:=ModuleFactory[0].ModuleName;
  366. end;
  367. var
  368. S : String;
  369. I : Integer;
  370. begin
  371. If (FModuleVar<>'') then
  372. Result:=ARequest.QueryFields.Values[FModuleVar];//Module name from query parameter using the FModuleVar as parameter name (default is 'Module')
  373. If (Result='') then
  374. begin
  375. S:=ARequest.PathInfo;
  376. If (Length(S)>0) and (S[1]='/') then
  377. Delete(S,1,1); //Delete the leading '/' if exists
  378. I:=Length(S);
  379. If (I>0) and (S[I]='/') then
  380. Delete(S,I,1); //Delete the trailing '/' if exists
  381. I:=Pos('/',S);
  382. if (I>0) or PreferModuleName then
  383. Result:=ARequest.GetNextPathInfo;
  384. end;
  385. If (Result='') then
  386. begin
  387. if Not AllowDefaultModule then
  388. Raise EFPWebError.Create(SErrNoModuleNameForRequest);
  389. Result:=GetDefaultModuleName
  390. end;
  391. end;
  392. procedure TWebHandler.EndRequest(ARequest: TRequest; AResponse: TResponse);
  393. begin
  394. AResponse.Free;
  395. ARequest.Free;
  396. end;
  397. function TWebHandler.FindModule(ModuleClass: TCustomHTTPModuleClass): TCustomHTTPModule;
  398. Var
  399. I : Integer;
  400. begin
  401. I:=ComponentCount-1;
  402. While (I>=0) and (Not ((Components[i] is ModuleClass) and (TCustomHTTPModule(Components[i]).Kind<>wkOneShot))) do
  403. Dec(i);
  404. if (I>=0) then
  405. Result:=Components[i] as TCustomHTTPModule
  406. else
  407. Result:=Nil;
  408. end;
  409. procedure TWebHandler.SetBaseURL(AModule: TCustomHTTPModule;
  410. Const AModuleName : String; ARequest: TRequest);
  411. Var
  412. S,P : String;
  413. begin
  414. S:=IncludeHTTPPathDelimiter(GetApplicationURL(ARequest));
  415. P:=IncludeHTTPPathDelimiter(ARequest.ReturnedPathInfo);
  416. If (P='') or (P='/') then
  417. P:=IncludeHTTPPathDelimiter(AModuleName);
  418. if (Length(P)>0) and (P[1]='/') then
  419. Delete(P,1,1);
  420. {$ifdef CGIDEBUG}
  421. senddebug(Format('SetBaseURL : "%s" "%s"',[S,P]));
  422. {$endif CGIDEBUG}
  423. AModule.BaseURL:=S+P;
  424. end;
  425. procedure TWebHandler.DoHandleRequest(ARequest: TRequest; AResponse: TResponse);
  426. begin
  427. Try
  428. HandleRequest(ARequest,AResponse);
  429. If Not AResponse.ContentSent then
  430. try
  431. AResponse.SendContent;
  432. except
  433. On E : Exception do
  434. Log(etError,Format(SErrSendingContent,[E.ClassName,E.Message]));
  435. end;
  436. Finally
  437. EndRequest(ARequest,AResponse);
  438. end;
  439. end;
  440. constructor TWebHandler.Create(AOwner:TComponent);
  441. begin
  442. inherited Create(AOwner);
  443. FModuleVar:='Module'; // Do not localize
  444. FAllowDefaultModule:=True;
  445. FHandleGetOnPost := True;
  446. FRedirectOnError := False;
  447. FRedirectOnErrorURL := '';
  448. end;
  449. { TCustomWebApplication }
  450. function TCustomWebApplication.GetAdministrator: String;
  451. begin
  452. result := FWebHandler.Administrator;
  453. end;
  454. function TCustomWebApplication.GetAllowDefaultModule: Boolean;
  455. begin
  456. result := FWebHandler.AllowDefaultModule;
  457. end;
  458. function TCustomWebApplication.GetApplicationURL: String;
  459. begin
  460. result := FWebHandler.ApplicationURL;
  461. end;
  462. function TCustomWebApplication.GetDefaultModuleName: String;
  463. begin
  464. Result:=FWebHandler.DefaultModuleName;
  465. end;
  466. function TCustomWebApplication.GetEmail: String;
  467. begin
  468. result := FWebHandler.Email;
  469. end;
  470. function TCustomWebApplication.GetEventLog: TEventLog;
  471. begin
  472. if not assigned(FEventLog) then
  473. begin
  474. FEventLog := TEventLog.Create(Nil);
  475. FEventLog.Name:=Self.Name+'Logger';
  476. FEventLog.Identification:=Title;
  477. FEventLog.RegisterMessageFile(ParamStr(0));
  478. FEventLog.LogType:=ltSystem;
  479. FEventLog.Active:=True;
  480. end;
  481. Result := FEventLog;
  482. end;
  483. function TCustomWebApplication.GetHandleGetOnPost: Boolean;
  484. begin
  485. result := FWebHandler.HandleGetOnPost;
  486. end;
  487. function TCustomWebApplication.GetModuleVar: String;
  488. begin
  489. result := FWebHandler.ModuleVariable;
  490. end;
  491. function TCustomWebApplication.GetOnGetModule: TGetModuleEvent;
  492. begin
  493. result := FWebHandler.OnGetModule;
  494. end;
  495. function TCustomWebApplication.GetOnShowRequestException: TOnShowRequestException;
  496. begin
  497. result := FWebHandler.OnShowRequestException;
  498. end;
  499. function TCustomWebApplication.GetOnUnknownRequestEncoding: TOnUnknownEncodingEvent;
  500. begin
  501. Result := FWebHandler.OnUnknownRequestEncoding
  502. end;
  503. function TCustomWebApplication.GetRedirectOnError: boolean;
  504. begin
  505. result := FWebHandler.RedirectOnError;
  506. end;
  507. function TCustomWebApplication.GetPreferModuleName: boolean;
  508. begin
  509. result := FWebHandler.PreferModuleName;
  510. end;
  511. function TCustomWebApplication.GetRedirectOnErrorURL: string;
  512. begin
  513. result := FWebHandler.RedirectOnErrorURL;
  514. end;
  515. procedure TCustomWebApplication.SetAdministrator(const AValue: String);
  516. begin
  517. FWebHandler.Administrator := AValue;
  518. end;
  519. procedure TCustomWebApplication.SetAllowDefaultModule(const AValue: Boolean);
  520. begin
  521. FWebHandler.AllowDefaultModule := AValue;
  522. end;
  523. procedure TCustomWebApplication.SetApplicationURL(const AValue: String);
  524. begin
  525. FWebHandler.ApplicationURL := AValue;
  526. end;
  527. procedure TCustomWebApplication.SetDefaultModuleName(AValue: String);
  528. begin
  529. FWebHandler.DefaultModuleName:=AValue;
  530. end;
  531. procedure TCustomWebApplication.SetEmail(const AValue: String);
  532. begin
  533. FWebHandler.Email := AValue;
  534. end;
  535. procedure TCustomWebApplication.SetHandleGetOnPost(const AValue: Boolean);
  536. begin
  537. FWebHandler.HandleGetOnPost := AValue;
  538. end;
  539. procedure TCustomWebApplication.SetModuleVar(const AValue: String);
  540. begin
  541. FWebHandler.ModuleVariable := AValue;
  542. end;
  543. procedure TCustomWebApplication.SetOnGetModule(const AValue: TGetModuleEvent);
  544. begin
  545. FWebHandler.OnGetModule := AValue;
  546. end;
  547. procedure TCustomWebApplication.SetOnShowRequestException(const AValue: TOnShowRequestException);
  548. begin
  549. FWebHandler.OnShowRequestException := AValue;
  550. end;
  551. procedure TCustomWebApplication.SetOnUnknownRequestEncoding(
  552. AValue: TOnUnknownEncodingEvent);
  553. begin
  554. FWebHandler.OnUnknownRequestEncoding:=AValue;
  555. end;
  556. procedure TCustomWebApplication.SetRedirectOnError(const AValue: boolean);
  557. begin
  558. FWebHandler.RedirectOnError := AValue;
  559. end;
  560. procedure TCustomWebApplication.SetPreferModuleName(const AValue: boolean);
  561. begin
  562. FWebHandler.PreferModuleName := AValue;
  563. end;
  564. procedure TCustomWebApplication.SetRedirectOnErrorURL(const AValue: string);
  565. begin
  566. FWebHandler.RedirectOnErrorURL :=AValue;
  567. end;
  568. procedure TCustomWebApplication.DoRun;
  569. begin
  570. FWebHandler.Run;
  571. end;
  572. procedure TCustomWebApplication.SetTitle(const AValue: string);
  573. begin
  574. inherited SetTitle(AValue);
  575. FWebHandler.Title := Title;
  576. end;
  577. constructor TCustomWebApplication.Create(AOwner: TComponent);
  578. begin
  579. Inherited Create(AOwner);
  580. FWebHandler := InitializeWebHandler;
  581. FWebHandler.FOnTerminate:=@DoOnTerminate;
  582. FWebHandler.FOnLog:=@Log;
  583. end;
  584. procedure TCustomWebApplication.DoOnTerminate(Sender : TObject);
  585. begin
  586. If Not Terminated then
  587. Terminate;
  588. end;
  589. destructor TCustomWebApplication.Destroy;
  590. begin
  591. FWebHandler.Free;
  592. if assigned(FEventLog) then
  593. FEventLog.Free;
  594. end;
  595. procedure TCustomWebApplication.CreateForm(AClass: TComponentClass; out Reference);
  596. begin
  597. TComponent(Reference):=AClass.Create(FWebHandler);
  598. end;
  599. procedure TCustomWebApplication.Initialize;
  600. begin
  601. StopOnException:=True;
  602. Inherited;
  603. end;
  604. procedure TCustomWebApplication.DoLog(EventType: TEventType; const Msg: String);
  605. begin
  606. EventLog.log(EventType,Msg);
  607. end;
  608. Procedure TCustomWebApplication.Terminate;
  609. begin
  610. Inherited;
  611. If Not Webhandler.FTerminated then
  612. WebHandler.Terminate;
  613. end;
  614. end.