#include <Web.h>
Inheritance diagram for Cgi::Web:
Public Member Functions | ||||
Web () | ||||
Web (const std::string &cd, const std::string &cp, const std::string &id="") | ||||
| ||||
Web (int argc, char *argv[], const std::string &cd, const std::string &cp, const std::string &id="") | ||||
| ||||
virtual | ~Web () | |||
void | Init (const std::string &id) | |||
| ||||
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 () | |||
Form * | GetForm () | |||
Cookies * | GetCookies () | |||
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 | ||||
Form * | m_pclForm | |||
Cookies * | m_pclCookies | |||
Private Member Functions | ||||
Web (const Web &) | ||||
Web & | operator= (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 |
Definition at line 41 of file Web.h.
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 = "" | |||
) |
cd | cookie domain |
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 = "" | |||
) |
cd | cookie domain |
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 }
void Cgi::Web::Init | ( | const std::string & | id | ) |
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 * | ) |
void Cgi::Web::GetEnvironment | ( | ) | [virtual] |
void Cgi::Web::ReadCookies | ( | ) | [virtual] |
Reimplemented in Cgi::MenuWeb.
Definition at line 246 of file Web.cpp.
Referenced by Execute(), and Cgi::MenuWeb::ReadCookies().
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] |
std::string Cgi::Web::GetDate | ( | ) |
std::string Cgi::Web::GetRequestMethod | ( | ) |
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.
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] |
const std::string& Cgi::Web::GetQueryString | ( | ) | [inline] |
void Cgi::Web::SetRealm | ( | const std::string & | realm_id | ) |
const std::string& Cgi::Web::GetRealm | ( | ) | [inline] |
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().
const std::string & Cgi::Web::GetUser | ( | ) |
bool Cgi::Web::Valid | ( | ) |
void Cgi::Web::SetContentType | ( | const std::string & | x | ) | [inline] |
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] |
Form* Cgi::Web::m_pclForm [protected] |
Cookies* Cgi::Web::m_pclCookies [protected] |
std::string Cgi::Web::m_strCookieDomain [private] |
std::string Cgi::Web::m_strCookiePath [private] |
std::string Cgi::Web::m_strCgiName [private] |
std::list<WebForm *> Cgi::Web::m_forms [private] |
std::string Cgi::Web::m_date [private] |
std::string Cgi::Web::m_method [private] |
std::string Cgi::Web::m_lastmodified [private] |
std::string Cgi::Web::m_query_string [private] |
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] |
bool Cgi::Web::m_bValid [private] |
std::string Cgi::Web::m_contentType [private] |
std::string Cgi::Web::m_contentDisposition [private] |
std::string Cgi::Web::m_location [private] |