testez.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. program CGITest;
  2. {$mode delphi}
  3. uses classes, ezcgi;
  4. // In the following class you only need to use either DoPost or DoGet
  5. // depending on what type of CGI request you want to use. If you only
  6. // are going to be making GET requests that you only need to define
  7. // and override DoGet and like wise for POST requests.
  8. type
  9. TCGIData = class(TEZcgi)
  10. procedure DoPost; override;
  11. procedure DoGet; override;
  12. procedure ShowStuff;
  13. end;
  14. var
  15. cgiStuff : TCGIData;
  16. // All output from the CGI must first begin with the WriteContent
  17. // call. This places the passed information and the correct CR's
  18. // required. In most cases you will always use it as shown below.
  19. // However if you ever need to return content that isn't standard
  20. // text/html this allows you to set what that content type is.
  21. //
  22. // The PutLine call just passes the information indicated out into
  23. // the stream for return to the client browser.
  24. procedure TCGIData.DoGet;
  25. begin
  26. WriteContent('text/html');
  27. PutLine('++++++++++ Using GET +++++++++');
  28. ShowStuff;
  29. end;
  30. procedure TCGIData.DoPost;
  31. begin
  32. WriteContent('text/html');
  33. PutLine('++++++++++ Using POST +++++++++');
  34. ShowStuff;
  35. end;
  36. // I wrote this method so that you can see everything that is passed to
  37. // and stored by the TEZcgi class. It currently stores all normal CGI
  38. // as well as everything passed to the CGI by the client. These
  39. // variables can be accessed via the following properties.
  40. //
  41. // Values[Index : string];
  42. // The Index data can be something like AUTH_TYPE, SCRIPT_NAME, or any
  43. // standard HTML or CGI environment variable. This can also be the
  44. // field name of the content being passed in a query string such as
  45. // "name" found in //URL/CGI/cgiapp?name=bob
  46. //
  47. // Another routine is availble GetValue(Index, defaultValue : string);
  48. // This routine does the same as the property Values except it allows
  49. // you to set a default value to be returned if no variable of type
  50. // name index is found.
  51. // This data is stored in a TStringList so you can retreive it by index
  52. // as well if you know the index location of the information.
  53. //
  54. // The properties for doing this are Names[index : integer] and
  55. // Variables[index : integer]. Names returns the name of the variable,
  56. // this would be the data passed in the values or GetValue calls.
  57. // Instead of returning the value "bob" it returns the value "name". The
  58. // variables property returns the whole string so using the name example
  59. // above it would return "name=bob".
  60. //
  61. // To determine how many environment variables have been stored you can
  62. // use the VariableCount property.
  63. //
  64. // The following procedure loops through all of the environment variables
  65. // and prints them back to the client. This is a good CGI example to
  66. // show you exactly what information you have to work with.
  67. procedure TCGIData.ShowStuff;
  68. var
  69. loop : integer;
  70. begin
  71. for loop := 0 to VariableCount - 1 do
  72. PutLine(Variables[loop] + '<BR>');
  73. end;
  74. // what follows is the actual program begin..end.
  75. // The Create call for the class does all the work of the CGI loading
  76. // the environment variables.
  77. // The FName and FEmail data is used by the Error report. If the CGI
  78. // doesn't work correctly for the user this data is included in an
  79. // error report that is returned to the user which tells them who to
  80. // inform and how.
  81. // The Run command starts processing the CGI request and eventually
  82. // calls either the DoGet or DoPost depending on the request type.
  83. // I don't try and trap it but the class does raise an exception called
  84. // ECGIException when some error is generated.
  85. begin
  86. try
  87. cgiStuff := TCGIData.Create;
  88. cgiStuff.Name := 'Michael A. Hess'; // replace with your name
  89. cgiStuff.Email := '[email protected]';// replace with your email
  90. cgiStuff.Run;
  91. finally
  92. cgiStuff.Free;
  93. end;
  94. end.