testcgiapp.pp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. If (Method<>'POST') and (Method<>'GET') then
  99. Raise Exception.CreateFmt(SerrUnsupportedMethod,['METHOD']);
  100. end;
  101. end;
  102. (*
  103. ({ 1: 'AUTH_TYPE' } fieldWWWAuthenticate, // ?
  104. { 2: 'CONTENT_LENGTH' } FieldContentLength,
  105. { 3: 'CONTENT_TYPE' } FieldContentType,
  106. { 4: 'GATEWAY_INTERFACE' } '',
  107. { 5: 'PATH_INFO' } '',
  108. { 6: 'PATH_TRANSLATED' } '',
  109. { 7: 'QUERY_STRING' } '',
  110. { 8: 'REMOTE_ADDR' } '',
  111. { 9: 'REMOTE_HOST' } '',
  112. { 10: 'REMOTE_IDENT' } '',
  113. { 11: 'REMOTE_USER' } '',
  114. { 12: 'REQUEST_METHOD' } '',
  115. { 13: 'SCRIPT_NAME' } '',
  116. { 14: 'SERVER_NAME' } '',
  117. { 15: 'SERVER_PORT' } '',
  118. { 16: 'SERVER_PROTOCOL' } '',
  119. { 17: 'SERVER_SOFTWARE' } '',
  120. { 18: 'HTTP_ACCEPT' } FieldAccept,
  121. { 19: 'HTTP_ACCEPT_CHARSET' } FieldAcceptCharset,
  122. { 20: 'HTTP_ACCEPT_ENCODING' } FieldAcceptEncoding,
  123. { 21: 'HTTP_IF_MODIFIED_SINCE' } FieldIfModifiedSince,
  124. { 22: 'HTTP_REFERER' } FieldReferer,
  125. { 23: 'HTTP_USER_AGENT' } FieldUserAgent,
  126. { 24: 'HTTP_COOKIE' } FieldCookie,
  127. // Additional Apache vars
  128. { 25: 'HTTP_CONNECTION' } FieldConnection,
  129. { 26: 'HTTP_ACCEPT_LANGUAGE' } FieldAcceptLanguage,
  130. { 27: 'HTTP_HOST' } '',
  131. { 28: 'SERVER_SIGNATURE' } '',
  132. { 29: 'SERVER_ADDR' } '',
  133. { 30: 'DOCUMENT_ROOT' } '',
  134. { 31: 'SERVER_ADMIN' } '',
  135. { 32: 'SCRIPT_FILENAME' } '',
  136. { 33: 'REMOTE_PORT' } '',
  137. { 34: 'REQUEST_URI' } '',
  138. { 35: 'CONTENT' } '',
  139. { 36: 'XHTTPREQUESTEDWITH' } ''
  140. *)
  141. procedure TTestCGIApplication.CheckEnvironment;
  142. Var
  143. L : TStrings;
  144. S,N,V : String;
  145. I : Integer;
  146. begin
  147. L:=CGIEnvironment;
  148. If L.IndexOfName('REQUEST_METHOD')=-1 then
  149. L.Values['REQUEST_METHOD']:=Method;
  150. S:=ScriptName;
  151. If (S='') then
  152. S:=CGIBinary;
  153. If L.IndexOfName('SCRIPT_NAME')=-1 then
  154. L.Values['SCRIPT_NAME']:=S;
  155. If L.IndexOfName('SCRIPT_FILENAME')=-1 then
  156. L.Values['SCRIPT_FILENAME']:=S;
  157. If (PathInfo<>'') then
  158. L.Values['PATH_INFO']:=PathInfo;
  159. If (Method='GET') then
  160. begin
  161. If L.IndexOfName('QUERY_STRING')=-1 then
  162. begin
  163. S:='';
  164. If (CGIVariables.Count>0) then
  165. For I:=0 to CGIVariables.Count-1 do
  166. begin
  167. CGIVariables.GetNameValue(I,N,V);
  168. If (S<>'') then
  169. S:=S+'&';
  170. S:=S+N+'='+HTTPEncode(V);
  171. end;
  172. L.Add('QUERY_STRING='+S)
  173. end;
  174. end
  175. end;
  176. procedure TTestCGIApplication.DoRun;
  177. var
  178. ErrorMsg: String;
  179. begin
  180. // parse parameters
  181. if HasOption('h','help') then begin
  182. WriteHelp;
  183. Terminate;
  184. Exit;
  185. end;
  186. if HasOption('c','config') then
  187. ProcessConfig;
  188. If HasOption('u','url') then
  189. URL:=GetOptionValue('u','url');
  190. If HasOption('e','environment') then
  191. CGIEnvironment.LoadFromFile(GetOptionValue('e','environment'));
  192. If HasOption('o','output') then
  193. CGIOutput:=GetOptionValue('o','output');
  194. If HasOption('m','method') then
  195. Method:=GetOptionValue('m','method');
  196. If HasOption('p','pathinfo') then
  197. PathInfo:=GetOptionValue('p','pathinfo');
  198. If HasOption('s','scriptname') then
  199. ScriptName:=GetOptionValue('s','scriptname');
  200. If HasOption('r','variables') then
  201. CGIOutput:=GetOptionValue('v','variables');
  202. If HasOption('i','input') then
  203. CGIBinary:=GetOptionValue('i','input');
  204. CheckMethod;
  205. CheckEnvironment;
  206. RunCGI;
  207. { add your program here }
  208. // stop program loop
  209. Terminate;
  210. end;
  211. constructor TTestCGIApplication.Create(TheOwner: TComponent);
  212. begin
  213. inherited Create(TheOwner);
  214. StopOnException:=True;
  215. FCGIE:=TStringList.Create;
  216. FCGV:=TStringList.Create;
  217. end;
  218. destructor TTestCGIApplication.Destroy;
  219. begin
  220. FreeAndNil(FCGIE);
  221. FreeAndNil(FCGV);
  222. inherited Destroy;
  223. end;
  224. procedure TTestCGIApplication.WriteHelp;
  225. begin
  226. Writeln('Usage: ',ExeName,' [options]');
  227. Writeln('Where options is one of : ');
  228. Writeln(' -h this help');
  229. Writeln(' -c|--config=file use file for configuration');
  230. Writeln(' -e|--environment=file use file for CGI environment (overrides config).');
  231. Writeln(' -i|--input=file use file as CGI binary.');
  232. Writeln(' -m|--method=method use method to invoke CGI (overrides config, default is GET).');
  233. Writeln(' -o|--output=file use file for CGI output (overrides config).');
  234. Writeln(' -p|--pathinfo=path use path for PATH_INFO environment variable (overrides config).');
  235. Writeln(' -r|--variables=file read query variables from file (overrides config).');
  236. Writeln(' -u|--url=URL use URL as the URL (overrides config).');
  237. end;
  238. var
  239. Application: TTestCGIApplication;
  240. begin
  241. Application:=TTestCGIApplication.Create(nil);
  242. Application.Title:='Test CGI application';
  243. Application.Run;
  244. Application.Free;
  245. end.