Main Page   Class Hierarchy   File List  

cgiClass.h

00001 /*
00002   cgiClass.h
00003   
00004   Common Gateway Interface Class Definitions.
00005   
00006   Copyright (c) 1996 - 2002
00007   
00008   David McCombs davidmc@w3sys.com
00009   
00010   Open Core Class Library
00011   
00012 */
00013 
00014 #ifndef cgiCLASS_H
00015 #define cgiCLASS_H
00016 
00017 #include "Astring.h"
00018 #include <map>
00019 #include <string>
00020 #include <sstream>
00021 #include <fstream>
00022 #include <iomanip>
00023 #include "ocCommon.h"
00024 
00025 using namespace std;
00026 
00027 /*
00028   enumerated id's of classes
00029   enum cgiObjectId
00030    cgi html head body generic
00031   
00032 */
00033 enum cgiObjectId { cgi, html, head, body, generic};
00034 
00035 // forward declaration so as to declare as friend.
00036 class cgiEnvironment;
00037 /*
00038   typedef map < string, aString > queryStringMap
00039 */
00040 typedef map < string, aString > queryStringMap;
00041 
00042 /*
00043   class cgiInput
00044   This class is used to hold form (or querystring)
00045      data from the caller.
00046      cgiEnvironment HAS-A cgiInput member variable called clientArguments
00047      clientArguments is obtained by calling cgiInput::ClientArguments()
00048   
00049 */
00050 class cgiInput
00051 {
00052 /*
00053   private member variables:
00054    queryStringMap theMap aString safe
00055 */
00056 private:
00057   queryStringMap theMap;
00058   ocFiles        fileMap;
00059   aString safe;
00060 protected:
00061   aString uploadPath;
00062 /*
00063   public methods:
00064   
00065    cgiInput() Constructor
00066    ~cgiInput() Destructor
00067 */
00068 public:
00069 
00070   cgiInput();
00071   ~cgiInput();
00072   /* void set( const char * queryString, size_t size = 0 );
00073      Parses the input string into name value pairs,
00074         placing them in an associative array.*/
00075   void set( const char * boundaryString, size_t size = 0 );
00076   /* void setMultipart( aString boundary );
00077      Parses stdin name value pairs,
00078         placing them in an associative array.
00079         Writes any files out to upload directory.
00080         Note: upload directory must already exist!
00081         */
00082   void setMultipart( aString boundary );
00083   /* aString & Safe(void)
00084      Returns an untouched copy of the
00085         buffer passed to the set method*/
00086   aString & Safe(void);
00087   /* queryStringMap & TheMap(void);
00088      Returns an associative array of name value pairs*/
00089   queryStringMap & TheMap(void);
00090   /* ocFiles & FileMap(void);
00091      Returns an associative array of name ocFile pairs*/
00092   ocFiles & FileMap(void);
00093 
00094   /* count of input arguments */
00095   int count( const char * key );
00096   /* default [const char * key] array operator returns found cgi query value by key*/
00097   aString & operator [] ( const char * key );
00098 
00099   friend class cgiEnvironment;
00100 };
00101 
00102 /*
00103   class cgiEnvironment
00104   
00105   class that holds the information
00106   coming from the user, as well as information about the
00107   browser and server.
00108   You would not use this directly, but should keep in mind that
00109   cgiScript IS-A cgiEnvironment through public inheritance.
00110   
00111 */
00112 class cgiEnvironment
00113 {
00114 /*
00115   private member variables:
00116    aString contentLength aString contentType
00117        aString gatewayInterface aString httpAccept
00118        aString httpUserAgent aString pathInfo
00119        aString pathTranslated aString queryString
00120        aString remoteAddr aString remoteHost
00121        aString remoteIdent; aString requestMethod;
00122        aString remoteUser; aString scriptName;
00123        aString serverName; aString serverPort;
00124        aString serverProtocol; aString serverSoftware;
00125        cgiInput clientArguments; int contentSize;
00126   
00127 */
00128 private:
00129 
00130   aString contentLength;
00131   aString contentType;
00132   aString contentBoundary;
00133   aString gatewayInterface;
00134   aString httpAccept;
00135   aString httpUserAgent;
00136   aString pathInfo;
00137   aString pathTranslated;
00138   aString queryString;
00139   aString remoteAddr;
00140   aString remoteHost;
00141   aString remoteIdent;
00142   aString requestMethod;
00143   aString remoteUser;
00144   aString scriptName;
00145   aString serverName;
00146   aString serverPort;
00147   aString serverProtocol;
00148   aString serverSoftware;
00149   cgiInput clientArguments;
00150   int contentSize;
00151 
00152 public:
00153 /*
00154   public methods:
00155   
00156    cgiEnvironment(uploadPath) Constructor (upload path is for file uploads, defaults to null)
00157    ~cgiEnvironment() Destructor
00158 */
00159   cgiEnvironment(const char * uploadPath = NULL);
00160   ~cgiEnvironment();
00161 
00162   // cgiEnvironment & setUploadPath( string path ); sets the path for uploaded files.
00163   aString & ContentLength(void);
00164   // aString & ContentType(void);  Indicates the MIME type of user supplied info.
00165   aString & ContentType(void);
00166   // aString & GatewayInterface(void);  Indicates the name of the gateway interface.
00167   aString & GatewayInterface(void);
00168   // aString & HttpAccept(void);  Provides a list of browser acceptible mime types.
00169   aString & HttpAccept(void);
00170   // aString & HttpUserAgent(void);  Indicates the name of the browser.
00171   aString & HttpUserAgent(void);
00172   // aString & PathInfo(void);  holds additional path passed beyond this cgi location.
00173   aString & PathInfo(void);
00174   // aString & PathTranslated(void);  holds the same info as the path info, but translated to
00175   // a whole path from the sever document root.
00176   aString & PathTranslated(void);
00177   // aString & QueryString(void);  The user passed info, used only if the method is GET.<d/d>
00178   aString & QueryString(void);
00179   // aString & RemoteAddr(void);  indicates the IP address of the browser.
00180   aString & RemoteAddr(void);
00181   // aString & RemoteHost(void);  indicates the hostname of the browser.
00182   aString & RemoteHost(void);
00183   // aString & RemoteIdent(void);  Indicates the users name if set in the users
00184   // browsers configuration.
00185   aString & RemoteIdent(void);
00186   // aString & RequestMethod(void);  Indicates the method used in the request,
00187   // usually GET or POST.
00188   aString & RequestMethod(void);
00189   // aString & RemoteUser(void);  Indicates the authentication name of the user.
00190   aString & RemoteUser(void);
00191   // aString & ScriptName(void);  Indicates the cgi filename. 
00192   aString & ScriptName(void);
00193   // aString & ServerName(void);  indicates the host server name 
00194   aString & ServerName(void);
00195   // aString & ServerPort(void);  indicates the server port - typically 80.
00196   aString & ServerPort(void);
00197   // aString & ServerProtocol(void);  Indicates the server protocol 
00198   aString & ServerProtocol(void);
00199   // aString & ServerSoftware(void);  Indicates the name and version of the sever software
00200   aString & ServerSoftware(void);
00201   // ContentBoundary  returns the boundary demarcing multipart 
00202   aString & ContentBoundary(void);
00203   // ClientArguments  returns the associateive arra of the client input.
00204   cgiInput & ClientArguments( void );
00205   // 
00206 };
00207 
00208 
00209 /*
00210   class cgiBase : public ostream
00211   
00212   The base class for all cgi classes that follow
00213 */
00214 class cgiBase : public ostream
00215 {
00216 
00217 protected:
00218   cgiObjectId id;
00219   aString opening;
00220   aString close;
00221   aString endLine;
00222 
00223 public:
00224 
00225   cgiBase();
00226   cgiBase(cgiBase& input);
00227   const char * tag( void );
00228   virtual ~cgiBase();
00229 };
00230 
00231 
00232 
00233 /*
00234   class cgiScriptLite: public cgiBase
00235   
00236   The lightweight cgi script class
00237   Intended for scalable b to b server interaction.
00238   (Ie, users get input by querying the environment
00239     variables and stdin themselves.)
00240 */
00241 class cgiScriptLite: public cgiBase
00242 {
00243 protected:
00244   aString mimeType;
00245 
00246 public:
00247   cgiScriptLite( const char * mimeString = "text/html", bool bCloseHeader=true );
00248   void closeHeader( void );
00249   ~cgiScriptLite();
00250   void Redirect( const char * location );
00251 };
00252 
00253 
00254 /*
00255   class cgiScript: public cgiEnvironment, public cgiScriptLite
00256   
00257   The cgi script class, input is parsed and ready in environment methods and
00258   a client argument vector of name value pairs.
00259 */
00260 class cgiScript: public cgiEnvironment, public cgiScriptLite
00261 {
00262 public:
00263   cgiScript( const char * mimeString = "text/html",
00264              bool bCloseHeader = true,
00265              const char * uploadPath = NULL );
00266   ~cgiScript();
00267    cgiScript & DebugString( void );
00268 };
00269 
00270 /*
00271   class cgiHtml : public cgiBase
00272   
00273   The html tag container
00274 */
00275 class cgiHtml : public cgiBase
00276 {
00277 
00278 public:
00279 
00280   cgiHtml( char * attr = "");
00281   virtual ~cgiHtml();
00282 
00283 };
00284 
00285 /*
00286   class cgiHead : public cgiBase
00287   
00288   the head tag container
00289 */
00290 class cgiHead : public cgiBase
00291 {
00292 
00293 public:
00294 
00295   cgiHead( char * attr = "");
00296   virtual ~cgiHead();
00297 
00298 };
00299 
00300 /*
00301   class cgiBody : public cgiBase
00302   
00303   the body tag container
00304 */
00305 class cgiBody : public cgiBase {
00306 
00307 public:
00308 
00309   cgiBody(char * attr = "");
00310   virtual ~cgiBody();
00311 
00312 };
00313 
00314 #endif // sentry
00315 
00316 #ifdef IN_T2_TESTHARNESS
00317 /* testing and example code here */
00318 
00319   // script
00320   cgiScript script;   
00321   {
00322     // create a new html instance in its own scope 
00323     cgiHtml html;
00324     {
00325       // create a new head tag
00326       cgiHead head;
00327       {
00328         cgiCan title("title");
00329         title << "T2 test code";
00330       }
00331       {
00332         cgiCan style("style");
00333         style << ".odd{ color: navy; background: white;}\n"
00334               << ".even{ color: white; background: navy;}\n"
00335               << ".cookie{ color: green; background: tan; }\n";
00336       }
00337     }  
00338     {
00339       // create a body tag
00340       cgiBody body;
00341       body << "<h1>The test harness</h1>" << endl;
00342       {
00343         cgiCan div("div"," class='odd'" );
00344         div << "This is an odd div block" << endl;
00345       }
00346       {
00347         cgiCan div("div", " class='even'" );
00348         div << "This is an even div block.\n";
00349       }
00350       {
00351         cgiCan div("div"," class='odd'" );
00352         div << "<h4>See if anybody typed:?question=...</h4>" << endl;
00353         div << "count = " << script.ClientArguments().count("question") << "<br>" << endl;
00354         div << "variable = " << script.ClientArguments()["question"].str() << "<br>" << endl;
00355 
00356       }    
00357     }  // body scope
00358   }  // html scope
00359 /* end test harness */
00360 #endif
00361 
00362 

Generated on Tue Jan 20 09:03:27 2004 for OpenTools by doxygen1.2.18