Google
Web alhem.net

Cgi::Web Class Reference

The CGI main class. More...

#include <Web.h>

Inheritance diagram for Cgi::Web:

Inheritance graph
Collaboration diagram for Cgi::Web:

Collaboration graph
List of all members.

Public Member Functions

 Web ()
 Web (const std::string &cd, const std::string &cp, const std::string &id="")
 
Parameters:
cd cookie domain

 Web (int argc, char *argv[], const std::string &cd, const std::string &cp, const std::string &id="")
 
Parameters:
cd cookie domain

virtual ~Web ()
void Init (const std::string &id)
 
Parameters:
id input data filename

void SetCookieDomain (const std::string &)
void SetCookiePath (const std::string &)
void SetCookie (const std::string &name, const std::string &value)
void SetCookie (const std::string &name, long value)
void SetCookie (const std::string &name, int value)
void Execute ()
void SetCgiName (const std::string &)
std::string GetCgiName ()
FormGetForm ()
CookiesGetCookies ()
void RegWebForm (WebForm *)
virtual void GetEnvironment ()
virtual void ReadCookies ()
virtual void FormInput ()
virtual void ValidateAuth ()
virtual void CreateHeader ()
virtual void GenerateDocument ()
std::string GetDate ()
std::string GetRequestMethod ()
void UpdateModified (const std::string &)
std::string datetime2httpdate (const std::string &dt)
const std::string & GetLastModified ()
const std::string & GetQueryString ()
void SetRealm (const std::string &realm_id)
 enable 401 Forbidden response with realm session_id
const std::string & GetRealm ()
virtual bool ValidateUser (const std::string &user, const std::string &pw)
 callback to validate user entered in basic auth dialog.
const std::string & GetUser ()
 which user is validated
bool Valid ()
 validated?
void SetContentType (const std::string &x)
void SetContentDisposition (const std::string &x)
void SetLocation (const std::string &x)

Protected Attributes

Formm_pclForm
Cookiesm_pclCookies

Private Member Functions

 Web (const Web &)
Weboperator= (const Web &)

Private Attributes

std::string m_strCookieDomain
std::string m_strCookiePath
std::string m_strCgiName
std::list< WebForm * > m_forms
std::string m_date
std::string m_method
std::string m_lastmodified
std::string m_query_string
std::string m_realm
 session_id / basic realm name
std::string m_user
bool m_bValid
std::string m_contentType
std::string m_contentDisposition
std::string m_location

Detailed Description

The CGI main class.

Definition at line 41 of file Web.h.


Constructor & Destructor Documentation

Cgi::Web::Web (  ) 

Definition at line 54 of file Web.cpp.

References m_date.

00055 :m_pclForm(NULL)
00056 ,m_pclCookies(NULL)
00057 ,m_strCookieDomain("")
00058 ,m_strCookiePath("")
00059 ,m_lastmodified("")
00060 ,m_bValid(false)
00061 ,m_contentType("text/html; charset=ISO-8859-1")
00062 {
00063         time_t t = time(NULL);
00064         struct tm * tp = localtime(&t);
00065         char date[40];
00066 
00067         sprintf(date,"%d-%02d-%02d %02d:%02d:%02d",
00068                 tp -> tm_year + 1900,
00069                 tp -> tm_mon + 1,
00070                 tp -> tm_mday,
00071                 tp -> tm_hour,
00072                 tp -> tm_min,
00073                 tp -> tm_sec);
00074         m_date = date;
00075 
00076 //      Init();
00077 }

Cgi::Web::Web ( const std::string &  cd,
const std::string &  cp,
const std::string &  id = "" 
)

Parameters:
cd cookie domain

Parameters:
cp cookie path
id input data filename

Definition at line 80 of file Web.cpp.

References Init(), and m_date.

00081 :m_pclForm(NULL)
00082 ,m_pclCookies(NULL)
00083 ,m_strCookieDomain(strd)
00084 ,m_strCookiePath(strp)
00085 ,m_lastmodified("")
00086 ,m_bValid(false)
00087 ,m_contentType("text/html; charset=ISO-8859-1")
00088 {
00089         time_t t = time(NULL);
00090         struct tm * tp = localtime(&t);
00091         char date[40];
00092 
00093         sprintf(date,"%d-%02d-%02d %02d:%02d:%02d",
00094                 tp -> tm_year + 1900,
00095                 tp -> tm_mon + 1,
00096                 tp -> tm_mday,
00097                 tp -> tm_hour,
00098                 tp -> tm_min,
00099                 tp -> tm_sec);
00100         m_date = date;
00101 
00102         Init(id);
00103 }

Cgi::Web::Web ( int  argc,
char *  argv[],
const std::string &  cd,
const std::string &  cp,
const std::string &  id = "" 
)

Parameters:
cd cookie domain

Parameters:
cp cookie path
id input data filename

Definition at line 106 of file Web.cpp.

References Init(), m_date, and m_lastmodified.

00107 :m_pclForm(NULL)
00108 ,m_pclCookies(NULL)
00109 ,m_strCookieDomain(strd)
00110 ,m_strCookiePath(strp)
00111 ,m_lastmodified("")
00112 ,m_bValid(false)
00113 ,m_contentType("text/html; charset=ISO-8859-1")
00114 {
00115         time_t t = time(NULL);
00116         struct tm * tp = localtime(&t);
00117         char date[40];
00118 
00119         sprintf(date,"%d-%02d-%02d %02d:%02d:%02d",
00120                 tp -> tm_year + 1900,
00121                 tp -> tm_mon + 1,
00122                 tp -> tm_mday,
00123                 tp -> tm_hour,
00124                 tp -> tm_min,
00125                 tp -> tm_sec);
00126         m_date = date;
00127 
00128         Init(id);
00129 
00130         struct stat st;
00131         if (stat(argv[0], &st) != -1)
00132         {
00133                 time_t t = st.st_mtime; // st_atime, st_mtime, st_ctime
00134                 struct tm *tp = localtime(&t);
00135                 sprintf(date,"%d-%02d-%02d %02d:%02d:%02d",
00136                         tp -> tm_year + 1900,
00137                         tp -> tm_mon + 1,
00138                         tp -> tm_mday,
00139                         tp -> tm_hour,
00140                         tp -> tm_min,
00141                         tp -> tm_sec);
00142                 m_lastmodified = date;
00143         }
00144 }

Cgi::Web::~Web (  )  [virtual]

Definition at line 217 of file Web.cpp.

References m_pclCookies, and m_pclForm.

00218 {
00219         if (m_pclForm)
00220         {
00221                 delete m_pclForm;
00222         }
00223         if (m_pclCookies)
00224         {
00225                 delete m_pclCookies;
00226         }
00227 }

Cgi::Web::Web ( const Web  )  [inline, private]

Definition at line 106 of file Web.h.

00106 {}


Member Function Documentation

void Cgi::Web::Init ( const std::string &  id  ) 

Parameters:
id input data filename

Definition at line 153 of file Web.cpp.

References m_method, m_pclCookies, m_pclForm, and m_query_string.

Referenced by Web().

00154 {
00155         char *r_m = getenv("REQUEST_METHOD");
00156 //      char *c_l = getenv("CONTENT_LENGTH");
00157         char *q_s = getenv("QUERY_STRING");
00158 //      char *r_a = getenv("REMOTE_ADDR");
00159         char *h_c = getenv("HTTP_COOKIE");
00160 
00161         if (q_s)
00162                 m_query_string = q_s;
00163 
00164 #ifdef WIN32
00165 #else
00166         struct timeval tv;
00167 
00168         gettimeofday(&tv,NULL);
00169         srandom( (tv.tv_sec % 1000000) * 1000 + tv.tv_usec);
00170 #endif
00171 
00172         if (!r_m)
00173         {
00174                 fprintf(stderr,"This is a cgi program\n");
00175                 exit(-1);
00176         }
00177         m_method = r_m;
00178         if (!strcasecmp(r_m,"get"))
00179         {
00180                 if (q_s)
00181                 {
00182                         m_pclForm = new Form(q_s,strlen(q_s));
00183                 }
00184                 else
00185                 {
00186 #ifdef WIN32
00187                         // IIS won't set QUERY_STRING if it's empty
00188                         m_pclForm = new Form("",0);
00189 #else
00190                         fprintf(stderr,"QUERY_STRING undefined\n");
00191                         exit(-1);
00192 #endif
00193                 }
00194         }
00195         else
00196         if (id.size())
00197         {
00198                 FILE *fil = fopen(id.c_str(),"rb");
00199                 m_pclForm = new Form(fil);
00200                 fclose(fil);
00201         }
00202         else
00203         if (!strcasecmp(r_m,"post"))
00204         {
00205                 m_pclForm = new Form;
00206         }
00207         else
00208         {
00209                 fprintf(stderr,"Unknown request method: '%s'\n",r_m);
00210                 exit(-1);
00211         }
00212         // Retrieve cookies
00213         m_pclCookies = new Cookies(h_c ? h_c : (char *)"");
00214 }

void Cgi::Web::SetCookieDomain ( const std::string &   ) 

Definition at line 326 of file Web.cpp.

References m_strCookieDomain.

00327 {
00328         m_strCookieDomain = str;
00329 }

void Cgi::Web::SetCookiePath ( const std::string &   ) 

Definition at line 332 of file Web.cpp.

References m_strCookiePath.

00333 {
00334         m_strCookiePath = str;
00335 }

void Cgi::Web::SetCookie ( const std::string &  name,
const std::string &  value 
)

Definition at line 338 of file Web.cpp.

References m_pclCookies, m_strCookieDomain, and m_strCookiePath.

Referenced by Cgi::MenuWeb::SetTheCookie().

00339 {
00340         m_pclCookies -> setcookie( (char *)m_strCookieDomain.c_str(),
00341                 (char *)m_strCookiePath.c_str(),
00342                 (char *)name.c_str(),
00343                 (char *)value.c_str());
00344 }

void Cgi::Web::SetCookie ( const std::string &  name,
long  value 
)

Definition at line 347 of file Web.cpp.

References m_pclCookies, m_strCookieDomain, and m_strCookiePath.

00348 {
00349         m_pclCookies -> setcookie( (char *)m_strCookieDomain.c_str(),
00350                 (char *)m_strCookiePath.c_str(),
00351                 (char *)name.c_str(),
00352                 value);
00353 }

void Cgi::Web::SetCookie ( const std::string &  name,
int  value 
)

Definition at line 356 of file Web.cpp.

References m_pclCookies, m_strCookieDomain, and m_strCookiePath.

00357 {
00358         m_pclCookies -> setcookie( (char *)m_strCookieDomain.c_str(),
00359                 (char *)m_strCookiePath.c_str(),
00360                 (char *)name.c_str(),
00361                 value);
00362 }

void Cgi::Web::Execute (  ) 

Definition at line 230 of file Web.cpp.

References CreateHeader(), FormInput(), GenerateDocument(), GetEnvironment(), ReadCookies(), and ValidateAuth().

00231 {
00232         GetEnvironment();
00233         ReadCookies();
00234         FormInput();
00235         ValidateAuth();
00236         CreateHeader();
00237         GenerateDocument();
00238 }

void Cgi::Web::SetCgiName ( const std::string &   ) 

Definition at line 365 of file Web.cpp.

References m_strCgiName.

00366 {
00367         m_strCgiName = name;
00368 }

std::string Cgi::Web::GetCgiName (  ) 

Definition at line 371 of file Web.cpp.

References m_strCgiName.

Referenced by Cgi::MenuWeb::GenerateDocument(), and Cgi::WebForm::GetCgiName().

00372 {
00373         return m_strCgiName;
00374 }

Form * Cgi::Web::GetForm (  ) 

Definition at line 377 of file Web.cpp.

References m_pclForm.

Referenced by Cgi::MenuWeb::FormInput(), and Cgi::WebForm::GetForm().

00378 {
00379         return m_pclForm;
00380 }

Cookies * Cgi::Web::GetCookies (  ) 

Definition at line 383 of file Web.cpp.

References m_pclCookies.

Referenced by Cgi::WebForm::GetCookies(), and Cgi::MenuWeb::ReadCookies().

00384 {
00385         return m_pclCookies;
00386 }

void Cgi::Web::RegWebForm ( WebForm  ) 

Definition at line 147 of file Web.cpp.

References m_forms.

00148 {
00149         m_forms.insert(m_forms.end(), pclForm);
00150 }

void Cgi::Web::GetEnvironment (  )  [virtual]

Definition at line 241 of file Web.cpp.

Referenced by Execute().

00242 {
00243 }

void Cgi::Web::ReadCookies (  )  [virtual]

Reimplemented in Cgi::MenuWeb.

Definition at line 246 of file Web.cpp.

Referenced by Execute(), and Cgi::MenuWeb::ReadCookies().

00247 {
00248 }

void Cgi::Web::FormInput (  )  [virtual]

Reimplemented in Cgi::MenuWeb.

Definition at line 251 of file Web.cpp.

References m_forms.

Referenced by Execute(), and Cgi::MenuWeb::FormInput().

00252 {
00253         std::list<WebForm *>::iterator it;
00254         
00255         for (it = m_forms.begin(); it != m_forms.end(); it++)
00256         {
00257                 WebForm *pclForm = static_cast<WebForm *>(*it);
00258                 pclForm -> Process();
00259         }
00260 }

void Cgi::Web::ValidateAuth (  )  [virtual]

Definition at line 263 of file Web.cpp.

References Cgi::Base64::decode_to_buffer(), Cgi::Parse::getword(), m_bValid, m_realm, m_user, and ValidateUser().

Referenced by Execute().

00264 {
00265         if (m_realm.size())
00266         {
00267                 char *auth = getenv("HTTP_AUTHORIZATION");
00268                 if (auth)
00269                 {
00270                         Parse pa(auth);
00271                         pa.getword(); // 'basic'
00272                         char slask[1000];
00273                         pa.getword(slask); // base64(user:pw)
00274                         Base64 b;
00275                         std::string tmp = slask;
00276                         b.decode_to_buffer(tmp, slask, 1000);
00277                         Parse pa2(slask, ":");
00278                         pa2.getword(slask);
00279                         m_user = slask;
00280                         pa2.getword(slask);
00281                         if (ValidateUser(m_user, slask))
00282                         {
00283                                 m_bValid = true;
00284                         }
00285                 }
00286         }
00287 }

void Cgi::Web::CreateHeader (  )  [virtual]

Reimplemented in Cgi::MenuWeb.

Definition at line 290 of file Web.cpp.

References m_bValid, m_contentDisposition, m_contentType, m_location, and m_realm.

Referenced by Cgi::MenuWeb::CreateHeader(), and Execute().

00291 {
00292         // character set
00293         printf("Content-type: %s\n", m_contentType.c_str());
00294         if (m_contentDisposition.size())
00295         {
00296                 printf("Content-disposition: %s\n", m_contentDisposition.c_str());
00297         }
00298         if (m_realm.size())
00299         {
00300                 printf("WWW-Authenticate: Basic realm=\"%s\"\n", m_realm.c_str());
00301                 if (!m_bValid)
00302                 {
00303                         printf("Status: 401 Forbidden\n");
00304                 }
00305         }
00306         if (m_location.size())
00307         {
00308                 printf("Location: %s\n", m_location.c_str());
00309         }
00310         printf("\n");
00311         fflush(stdout);
00312 
00313         // 4.01 strict
00314 //      printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
00315         // 4.01 transitional
00316 //      printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">\n");
00317         fflush(stdout);
00318 }

void Cgi::Web::GenerateDocument (  )  [virtual]

Reimplemented in Cgi::MenuWeb.

Definition at line 321 of file Web.cpp.

Referenced by Execute().

00322 {
00323 }

std::string Cgi::Web::GetDate (  ) 

Definition at line 389 of file Web.cpp.

References m_date.

00390 { 
00391         return m_date; 
00392 }

std::string Cgi::Web::GetRequestMethod (  ) 

Definition at line 395 of file Web.cpp.

References m_method.

00396 { 
00397         return m_method; 
00398 }

void Cgi::Web::UpdateModified ( const std::string &   ) 

Definition at line 401 of file Web.cpp.

References m_lastmodified.

00402 {
00403         if (!m_lastmodified.size() || strcmp(str.c_str(),m_lastmodified.c_str()) > 0)
00404         {
00405                 if (str.size())
00406                         m_lastmodified = str;
00407         }
00408 }

std::string Cgi::Web::datetime2httpdate ( const std::string &  dt  ) 

Definition at line 411 of file Web.cpp.

References days, and months.

00412 {
00413 /* time_t mktime(struct tm *) */
00414         char slask[8];
00415         struct tm tp;
00416         time_t t;
00417         char *days[] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
00418 /*
00419         char *months[] = { "Jan","Feb","Mar","Apr","May","Jun",
00420                            "Jul","Aug","Sep","Oct","Nov","Dec" };
00421 */
00422         char *months[] = { "Jan","Feb","Mar","Apr","May","Jun",
00423                            "Jul","Aug","Sep","Oct","Nov","Dec" };
00424         int i;
00425         char s[40];
00426 
00427 /* 1997-12-16 09:50:40 */
00428 
00429         if (dt.size() == 19) // && (int)strlen(dt) == 19)
00430         {
00431                 slask[4] = 0;
00432                 tp.tm_year = atoi(strncpy(slask,dt.c_str(),4)) - 1900;
00433                 slask[2] = 0;
00434                 i = atoi(strncpy(slask,dt.c_str() + 5,2)) - 1;
00435                 tp.tm_mon = i >= 0 ? i : 0;
00436                 tp.tm_mday = atoi(strncpy(slask,dt.c_str() + 8,2));
00437                 tp.tm_hour = atoi(strncpy(slask,dt.c_str() + 11,2));
00438                 tp.tm_min = atoi(strncpy(slask,dt.c_str() + 14,2));
00439                 tp.tm_sec = atoi(strncpy(slask,dt.c_str() + 17,2));
00440                 tp.tm_wday = 0;
00441                 tp.tm_yday = 0;
00442                 tp.tm_isdst = 0;
00443                 t = mktime(&tp);
00444                 if (t == -1)
00445                 {
00446                         printf("datetime2httpdate() failed\n");
00447                 }
00448 
00449                 sprintf(s,"%s, %02d %s %d %02d:%02d:%02d GMT",
00450                  days[tp.tm_wday],
00451                  tp.tm_mday,
00452                  months[tp.tm_mon],
00453                  tp.tm_year + 1900,
00454                  tp.tm_hour,tp.tm_min,tp.tm_sec);
00455         } 
00456         else
00457         {
00458                 *s = 0;
00459         }
00460         return s;
00461 }

const std::string& Cgi::Web::GetLastModified (  )  [inline]

Definition at line 84 of file Web.h.

References m_lastmodified.

00084 { return m_lastmodified; }

const std::string& Cgi::Web::GetQueryString (  )  [inline]

Definition at line 85 of file Web.h.

References m_query_string.

00085 { return m_query_string; }

void Cgi::Web::SetRealm ( const std::string &  realm_id  ) 

enable 401 Forbidden response with realm session_id

Definition at line 464 of file Web.cpp.

References m_realm.

00465 {
00466         m_realm = session_id;
00467 }

const std::string& Cgi::Web::GetRealm (  )  [inline]

Definition at line 89 of file Web.h.

References m_realm.

00089 { return m_realm; }

bool Cgi::Web::ValidateUser ( const std::string &  user,
const std::string &  pw 
) [virtual]

callback to validate user entered in basic auth dialog.

Definition at line 470 of file Web.cpp.

Referenced by ValidateAuth().

00471 {
00472         return false;
00473 }

const std::string & Cgi::Web::GetUser (  ) 

which user is validated

Definition at line 476 of file Web.cpp.

References m_user.

00477 {
00478         return m_user;
00479 }

bool Cgi::Web::Valid (  ) 

validated?

Definition at line 482 of file Web.cpp.

References m_bValid.

00483 {
00484         return m_bValid;
00485 }

void Cgi::Web::SetContentType ( const std::string &  x  )  [inline]

Definition at line 97 of file Web.h.

References m_contentType.

00097 { m_contentType = x; }

void Cgi::Web::SetContentDisposition ( const std::string &  x  )  [inline]

Definition at line 98 of file Web.h.

References m_contentDisposition.

00098 { m_contentDisposition = x; }

void Cgi::Web::SetLocation ( const std::string &  x  )  [inline]

Definition at line 99 of file Web.h.

References m_location.

00099 { m_location = x; }

Web& Cgi::Web::operator= ( const Web  )  [inline, private]

Definition at line 107 of file Web.h.

00107 { return *this; }


Member Data Documentation

Form* Cgi::Web::m_pclForm [protected]

Definition at line 102 of file Web.h.

Referenced by GetForm(), Init(), and ~Web().

Cookies* Cgi::Web::m_pclCookies [protected]

Definition at line 103 of file Web.h.

Referenced by GetCookies(), Init(), SetCookie(), and ~Web().

std::string Cgi::Web::m_strCookieDomain [private]

Definition at line 108 of file Web.h.

Referenced by SetCookie(), and SetCookieDomain().

std::string Cgi::Web::m_strCookiePath [private]

Definition at line 109 of file Web.h.

Referenced by SetCookie(), and SetCookiePath().

std::string Cgi::Web::m_strCgiName [private]

Definition at line 110 of file Web.h.

Referenced by GetCgiName(), and SetCgiName().

std::list<WebForm *> Cgi::Web::m_forms [private]

Definition at line 111 of file Web.h.

Referenced by FormInput(), and RegWebForm().

std::string Cgi::Web::m_date [private]

Definition at line 112 of file Web.h.

Referenced by GetDate(), and Web().

std::string Cgi::Web::m_method [private]

Definition at line 113 of file Web.h.

Referenced by GetRequestMethod(), and Init().

std::string Cgi::Web::m_lastmodified [private]

Definition at line 114 of file Web.h.

Referenced by GetLastModified(), UpdateModified(), and Web().

std::string Cgi::Web::m_query_string [private]

Definition at line 115 of file Web.h.

Referenced by GetQueryString(), and Init().

std::string Cgi::Web::m_realm [private]

session_id / basic realm name

Definition at line 116 of file Web.h.

Referenced by CreateHeader(), GetRealm(), SetRealm(), and ValidateAuth().

std::string Cgi::Web::m_user [private]

Definition at line 117 of file Web.h.

Referenced by GetUser(), and ValidateAuth().

bool Cgi::Web::m_bValid [private]

Definition at line 118 of file Web.h.

Referenced by CreateHeader(), Valid(), and ValidateAuth().

std::string Cgi::Web::m_contentType [private]

Definition at line 119 of file Web.h.

Referenced by CreateHeader(), and SetContentType().

std::string Cgi::Web::m_contentDisposition [private]

Definition at line 120 of file Web.h.

Referenced by CreateHeader(), and SetContentDisposition().

std::string Cgi::Web::m_location [private]

Definition at line 121 of file Web.h.

Referenced by CreateHeader(), and SetLocation().


The documentation for this class was generated from the following files:
Generated for cgi++ by doxygen 1.3.7

Page, code, and content Copyright (C) 2004 by Anders Hedström