fpcgi.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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];
  62. If (Result='') then
  63. Result:=ARequest.GetNextPathInfo;
  64. end;
  65. function TCGIApplication.FindModule(ModuleClass : TCustomHTTPModuleClass): TCustomHTTPModule;
  66. Var
  67. I : Integer;
  68. begin
  69. I:=ComponentCount-1;
  70. While (I>=0) and (Not (Components[i] is ModuleClass)) do
  71. Dec(i);
  72. if (I>=0) then
  73. Result:=Components[i] as TCustomHTTPModule
  74. else
  75. Result:=Nil;
  76. end;
  77. constructor TCGIApplication.Create(AOwner: TComponent);
  78. begin
  79. inherited Create(AOwner);
  80. FModuleVar:='Module'; // Do not localize
  81. FAllowDefaultModule:=True;
  82. end;
  83. procedure TCGIApplication.CreateForm(AClass: TComponentClass;
  84. var Reference: TComponent);
  85. begin
  86. Reference:=AClass.Create(Self);
  87. end;
  88. procedure TCGIApplication.HandleRequest(ARequest: TRequest; AResponse: TResponse);
  89. Var
  90. MC : TCustomHTTPModuleClass;
  91. M : TCustomHTTPModule;
  92. MN : String;
  93. MI : TModuleItem;
  94. begin
  95. MC:=Nil;
  96. If (OnGetModule<>Nil) then
  97. OnGetModule(Self,ARequest,MC);
  98. If (MC=Nil) then
  99. begin
  100. MN:=GetModuleName(ARequest);
  101. If (MN='') and Not AllowDefaultModule then
  102. Raise EFPCGIError.Create(SErrNoModuleNameForRequest);
  103. MI:=ModuleFactory.FindModule(MN);
  104. If (MI=Nil) and (ModuleFactory.Count=1) then
  105. MI:=ModuleFactory[0];
  106. if (MI=Nil) then
  107. begin
  108. Raise EFPCGIError.CreateFmt(SErrNoModuleForRequest,[MN]);
  109. end;
  110. MC:=MI.ModuleClass;
  111. M:=FindModule(MC); // Check if a module exists already
  112. end;
  113. If (M=Nil) then
  114. M:=MC.Create(Self);
  115. M.HandleRequest(ARequest,AResponse);
  116. end;
  117. Initialization
  118. InitCGI;
  119. Finalization
  120. DoneCGI;
  121. end.