fpcgi.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. {
  2. $Id: header,v 1.1 2000/07/13 06:33:45 michael Exp $
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  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. {$mode objfpc}
  12. {$H+}
  13. unit fpcgi;
  14. interface
  15. uses SysUtils,Classes,CustCgi,httpDefs,fpHTTP;
  16. Type
  17. { TCGIApplication }
  18. TGetModuleEvent = Procedure (Sender : TObject; ARequest : TRequest;
  19. Var ModuleClass : TCustomHTTPModuleClass) of object;
  20. TCGIApplication = Class(TCustomCGIApplication)
  21. private
  22. FModuleVar: String;
  23. FOnGetModule: TGetModuleEvent;
  24. FAllowDefaultModule: Boolean;
  25. Protected
  26. Function GetModuleName(Arequest : TRequest) : string;
  27. function FindModule(ModuleClass : TCustomHTTPModuleClass): TCustomHTTPModule;
  28. Public
  29. Constructor Create(AOwner : TComponent); override;
  30. Procedure CreateForm(AClass : TComponentClass; Var Reference : TComponent);
  31. Procedure handleRequest(ARequest : TRequest; AResponse : TResponse); override;
  32. Property OnGetModule : TGetModuleEvent Read FOnGetModule Write FOnGetModule;
  33. Property ModuleVariable : String Read FModuleVar Write FModuleVar;
  34. Property AllowDefaultModule : Boolean Read FAllowDefaultModule Write FAllowDefaultModule;
  35. end;
  36. EFPCGIError = Class(Exception);
  37. Var
  38. Application : TCGIApplication;
  39. ShowCleanUpErrors : Boolean = False;
  40. Implementation
  41. resourcestring
  42. SErrNoModuleNameForRequest = 'Could not determine HTTP module name for request';
  43. SErrNoModuleForRequest = 'Could not determine HTTP module for request "%s"';
  44. Procedure InitCGI;
  45. begin
  46. Application:=TCGIApplication.Create(Nil);
  47. end;
  48. Procedure DoneCGI;
  49. begin
  50. Try
  51. FreeAndNil(Application);
  52. except
  53. if ShowCleanUpErrors then
  54. Raise;
  55. end;
  56. end;
  57. { TCGIApplication }
  58. function TCGIApplication.GetModuleName(Arequest: TRequest): string;
  59. begin
  60. If (FModuleVar<>'') then
  61. Result:=ARequest.QueryFields.Values[FModuleVar];//Module name from query parameter using the FModuleVar as parameter name (default is 'Module')
  62. If (Result='') then
  63. begin
  64. if (Pos('/', pchar(@ARequest.PathInfo[2])) <= 0) and AllowDefaultModule then Exit;//There is only 1 '/' in ARequest.PathInfo -> only ActionName is there -> use default module
  65. Result:=ARequest.GetNextPathInfo;
  66. end;
  67. end;
  68. function TCGIApplication.FindModule(ModuleClass : TCustomHTTPModuleClass): TCustomHTTPModule;
  69. Var
  70. I : Integer;
  71. begin
  72. I:=ComponentCount-1;
  73. While (I>=0) and (Not (Components[i] is ModuleClass)) do
  74. Dec(i);
  75. if (I>=0) then
  76. Result:=Components[i] as TCustomHTTPModule
  77. else
  78. Result:=Nil;
  79. end;
  80. constructor TCGIApplication.Create(AOwner: TComponent);
  81. begin
  82. inherited Create(AOwner);
  83. FModuleVar:='Module'; // Do not localize
  84. FAllowDefaultModule:=True;
  85. end;
  86. procedure TCGIApplication.CreateForm(AClass: TComponentClass;
  87. var Reference: TComponent);
  88. begin
  89. Reference:=AClass.Create(Self);
  90. end;
  91. procedure TCGIApplication.HandleRequest(ARequest: TRequest; AResponse: TResponse);
  92. Var
  93. MC : TCustomHTTPModuleClass;
  94. M : TCustomHTTPModule;
  95. MN : String;
  96. MI : TModuleItem;
  97. begin
  98. MC:=Nil;
  99. M:=NIL;
  100. If (OnGetModule<>Nil) then
  101. OnGetModule(Self,ARequest,MC);
  102. If (MC=Nil) then
  103. begin
  104. MN:=GetModuleName(ARequest);
  105. If (MN='') and Not AllowDefaultModule then
  106. Raise EFPCGIError.Create(SErrNoModuleNameForRequest);
  107. MI:=ModuleFactory.FindModule(MN);
  108. If (MI=Nil) and (ModuleFactory.Count=1) then
  109. MI:=ModuleFactory[0];
  110. if (MI=Nil) then
  111. begin
  112. Raise EFPCGIError.CreateFmt(SErrNoModuleForRequest,[MN]);
  113. end;
  114. MC:=MI.ModuleClass;
  115. end;
  116. M:=FindModule(MC); // Check if a module exists already
  117. If (M=Nil) then
  118. M:=MC.Create(Self);
  119. M.HandleRequest(ARequest,AResponse);
  120. end;
  121. Initialization
  122. InitCGI;
  123. Finalization
  124. DoneCGI;
  125. end.