testcgiapp.pp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. program testcgiapp;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, inifiles, process, httpdefs,custcgi
  5. { you can add units after this };
  6. type
  7. { TTestCGIApplication }
  8. TTestCGIApplication = class(TCustomApplication)
  9. private
  10. FCGB: String;
  11. FCGIE: TStrings;
  12. FCGV: TStrings;
  13. FMethod: String;
  14. Foutput: String;
  15. FPostData: String;
  16. FPathInfo : String;
  17. FScriptName: String;
  18. FURL: String;
  19. procedure CheckEnvironment;
  20. procedure CheckMethod;
  21. procedure ProcessConfig;
  22. procedure RunCGI;
  23. protected
  24. Property CGIEnvironment : TStrings Read FCGIE Write FCGIE;
  25. Property URL : String Read FURL Write FURL;
  26. Property PostData : String Read FPostData Write FPostData;
  27. Property Method : String Read FMethod Write FMethod;
  28. Property CGIOutput : String Read Foutput Write FOutput;
  29. Property CGIBinary : String Read FCGB Write FCGB;
  30. Property CGIVariables : TStrings Read FCGV Write FCGV;
  31. Property PathInfo : String Read FPathInfo Write FPathInfo;
  32. Property ScriptName : String Read FScriptName Write FScriptName;
  33. procedure DoRun; override;
  34. public
  35. constructor Create(TheOwner: TComponent); override;
  36. Destructor Destroy; override;
  37. procedure WriteHelp; virtual;
  38. end;
  39. { TTestCGIApplication }
  40. Resourcestring
  41. SErrUnsupportedMethod = 'Unsupported method: "%s"';
  42. SErrNoCGIBinary = 'No CGI binary specified';
  43. Const
  44. SConfig = 'Config';
  45. KeyURL = 'URL';
  46. KeyEnvironment = 'Environment';
  47. KeyMethod = 'Method';
  48. KeyPost = 'PostData';
  49. SEnvironment = KeyEnvironment;
  50. SVariables = 'Variables';
  51. procedure TTestCGIApplication.ProcessConfig;
  52. Var
  53. Ini : TInifile;
  54. S : String;
  55. begin
  56. Ini:=TIniFile.Create(GetOptionValue('c','config'));
  57. try
  58. With Ini do
  59. begin
  60. URL:=ReadString(SConfig,KeyURL,'');
  61. S:=ReadString(SConfig,KeyEnvironment,'');
  62. If (S<>'') and FileExists(S) then
  63. CGIEnvironment.LoadFromFile(S);
  64. If SectionExists(SEnvironment) then
  65. ReadSectionValues(SEnvironment,CGIEnvironment);
  66. If SectionExists(SVariables) then
  67. ReadSectionValues(SVariables,CGIVariables);
  68. If (Method='') then
  69. Method:=ReadString(SConfig,KeyMethod,'GET');
  70. PostData:=ReadString(SConfig,KeyPost,'');
  71. end;
  72. finally
  73. Ini.Free;
  74. end;
  75. end;
  76. procedure TTestCGIApplication.RunCGI;
  77. Var
  78. Proc : TProcess;
  79. begin
  80. If (CGIBinary='') then
  81. Raise Exception.Create(SerrNoCGIBinary);
  82. Proc:=TProcess.Create(Self);
  83. try
  84. Proc.CommandLine:=CGIBinary;
  85. Proc.Environment:=CGIEnvironment;
  86. Proc.Execute;
  87. finally
  88. Proc.Free;
  89. end;
  90. end;
  91. procedure TTestCGIApplication.CheckMethod;
  92. begin
  93. If (Method='') then
  94. Method:='GET'
  95. else
  96. begin
  97. Method:=Uppercase(Method);
  98. end;
  99. end;
  100. (*
  101. ({ 1: 'AUTH_TYPE' } fieldWWWAuthenticate, // ?
  102. { 2: 'CONTENT_LENGTH' } FieldContentLength,
  103. { 3: 'CONTENT_TYPE' } FieldContentType,
  104. { 4: 'GATEWAY_INTERFACE' } '',
  105. { 5: 'PATH_INFO' } '',
  106. { 6: 'PATH_TRANSLATED' } '',
  107. { 7: 'QUERY_STRING' } '',
  108. { 8: 'REMOTE_ADDR' } '',
  109. { 9: 'REMOTE_HOST' } '',
  110. { 10: 'REMOTE_IDENT' } '',
  111. { 11: 'REMOTE_USER' } '',
  112. { 12: 'REQUEST_METHOD' } '',
  113. { 13: 'SCRIPT_NAME' } '',
  114. { 14: 'SERVER_NAME' } '',
  115. { 15: 'SERVER_PORT' } '',
  116. { 16: 'SERVER_PROTOCOL' } '',
  117. { 17: 'SERVER_SOFTWARE' } '',
  118. { 18: 'HTTP_ACCEPT' } FieldAccept,
  119. { 19: 'HTTP_ACCEPT_CHARSET' } FieldAcceptCharset,
  120. { 20: 'HTTP_ACCEPT_ENCODING' } FieldAcceptEncoding,
  121. { 21: 'HTTP_IF_MODIFIED_SINCE' } FieldIfModifiedSince,
  122. { 22: 'HTTP_REFERER' } FieldReferer,
  123. { 23: 'HTTP_USER_AGENT' } FieldUserAgent,
  124. { 24: 'HTTP_COOKIE' } FieldCookie,
  125. // Additional Apache vars
  126. { 25: 'HTTP_CONNECTION' } FieldConnection,
  127. { 26: 'HTTP_ACCEPT_LANGUAGE' } FieldAcceptLanguage,
  128. { 27: 'HTTP_HOST' } '',
  129. { 28: 'SERVER_SIGNATURE' } '',
  130. { 29: 'SERVER_ADDR' } '',
  131. { 30: 'DOCUMENT_ROOT' } '',
  132. { 31: 'SERVER_ADMIN' } '',
  133. { 32: 'SCRIPT_FILENAME' } '',
  134. { 33: 'REMOTE_PORT' } '',
  135. { 34: 'REQUEST_URI' } '',
  136. { 35: 'CONTENT' } '',
  137. { 36: 'XHTTPREQUESTEDWITH' } ''
  138. *)
  139. procedure TTestCGIApplication.CheckEnvironment;
  140. Var
  141. L : TStrings;
  142. S,N,V : String;
  143. I : Integer;
  144. begin
  145. L:=CGIEnvironment;
  146. If L.IndexOfName('REQUEST_METHOD')=-1 then
  147. L.Values['REQUEST_METHOD']:=Method;
  148. S:=ScriptName;
  149. If (S='') then
  150. S:=CGIBinary;
  151. If L.IndexOfName('SCRIPT_NAME')=-1 then
  152. L.Values['SCRIPT_NAME']:=S;
  153. If L.IndexOfName('SCRIPT_FILENAME')=-1 then
  154. L.Values['SCRIPT_FILENAME']:=S;
  155. If (PathInfo<>'') then
  156. L.Values['PATH_INFO']:=PathInfo;
  157. If (Method='GET') then
  158. begin
  159. If L.IndexOfName('QUERY_STRING')=-1 then
  160. begin
  161. S:='';
  162. If (CGIVariables.Count>0) then
  163. For I:=0 to CGIVariables.Count-1 do
  164. begin
  165. CGIVariables.GetNameValue(I,N,V);
  166. If (S<>'') then
  167. S:=S+'&';
  168. S:=S+N+'='+HTTPEncode(V);
  169. end;
  170. L.Add('QUERY_STRING='+S)
  171. end;
  172. end
  173. end;
  174. procedure TTestCGIApplication.DoRun;
  175. var
  176. ErrorMsg: String;
  177. begin
  178. // parse parameters
  179. if HasOption('h','help') then begin
  180. WriteHelp;
  181. Terminate;
  182. Exit;
  183. end;
  184. if HasOption('c','config') then
  185. ProcessConfig;
  186. If HasOption('u','url') then
  187. URL:=GetOptionValue('u','url');
  188. If HasOption('e','environment') then
  189. CGIEnvironment.LoadFromFile(GetOptionValue('e','environment'));
  190. If HasOption('o','output') then
  191. CGIOutput:=GetOptionValue('o','output');
  192. If HasOption('m','method') then
  193. Method:=GetOptionValue('m','method');
  194. If HasOption('p','pathinfo') then
  195. PathInfo:=GetOptionValue('p','pathinfo');
  196. If HasOption('s','scriptname') then
  197. ScriptName:=GetOptionValue('s','scriptname');
  198. If HasOption('r','variables') then
  199. CGIOutput:=GetOptionValue('v','variables');
  200. If HasOption('i','input') then
  201. CGIBinary:=GetOptionValue('i','input');
  202. CheckMethod;
  203. CheckEnvironment;
  204. RunCGI;
  205. { add your program here }
  206. // stop program loop
  207. Terminate;
  208. end;
  209. constructor TTestCGIApplication.Create(TheOwner: TComponent);
  210. begin
  211. inherited Create(TheOwner);
  212. StopOnException:=True;
  213. FCGIE:=TStringList.Create;
  214. FCGV:=TStringList.Create;
  215. end;
  216. destructor TTestCGIApplication.Destroy;
  217. begin
  218. FreeAndNil(FCGIE);
  219. FreeAndNil(FCGV);
  220. inherited Destroy;
  221. end;
  222. procedure TTestCGIApplication.WriteHelp;
  223. begin
  224. Writeln('Usage: ',ExeName,' [options]');
  225. Writeln('Where options is one of : ');
  226. Writeln(' -h this help');
  227. Writeln(' -c|--config=file use file for configuration');
  228. Writeln(' -e|--environment=file use file for CGI environment (overrides config).');
  229. Writeln(' -i|--input=file use file as CGI binary.');
  230. Writeln(' -m|--method=method use method to invoke CGI (overrides config, default is GET).');
  231. Writeln(' -o|--output=file use file for CGI output (overrides config).');
  232. Writeln(' -p|--pathinfo=path use path for PATH_INFO environment variable (overrides config).');
  233. Writeln(' -r|--variables=file read query variables from file (overrides config).');
  234. Writeln(' -u|--url=URL use URL as the URL (overrides config).');
  235. end;
  236. var
  237. Application: TTestCGIApplication;
  238. begin
  239. Application:=TTestCGIApplication.Create(nil);
  240. Application.Title:='Test CGI application';
  241. Application.Run;
  242. Application.Free;
  243. end.