![]() |
HttpdCookies Class ReferenceHTTP Cookie parse/container class.
More...
|
Public Member Functions | |
HttpdCookies () | |
HttpdCookies (const std::string &query_string) | |
~HttpdCookies () | |
void | add (const std::string &s) |
bool | getvalue (const std::string &, std::string &) const |
void | replacevalue (const std::string &, const std::string &) |
void | replacevalue (const std::string &, long) |
void | replacevalue (const std::string &, int) |
size_t | getlength (const std::string &) const |
void | setcookie (HTTPSocket *, const std::string &d, const std::string &p, const std::string &c, const std::string &v) |
void | setcookie (HTTPSocket *, const std::string &d, const std::string &p, const std::string &c, long v) |
void | setcookie (HTTPSocket *, const std::string &d, const std::string &p, const std::string &c, int v) |
const std::string & | expiredatetime () const |
cookie_v & | GetHttpdCookies () |
void | Reset () |
Private Types | |
typedef std::list< std::pair < std::string, std::string > > | cookie_v |
list of key/value structs. | |
Private Attributes | |
cookie_v | m_cookies |
std::string | m_date |
Definition at line 54 of file HttpdCookies.h.
typedef std::list<std::pair<std::string, std::string> > HttpdCookies::cookie_v [private] |
HttpdCookies::HttpdCookies | ( | ) |
HttpdCookies::HttpdCookies | ( | const std::string & | query_string | ) |
Definition at line 54 of file HttpdCookies.cpp.
References m_cookies.
00055 { 00056 Parse *pa = new Parse(s,";"); 00057 00058 std::string slask = pa -> getword(); 00059 while (slask.size()) 00060 { 00061 Parse *pa2 = new Parse(slask,"="); 00062 std::string name = pa2 -> getword(); 00063 std::string value = pa2 -> getword(); 00064 delete pa2; 00065 m_cookies.push_back(std::pair<std::string, std::string>(name, value)); 00066 // 00067 slask = pa -> getword(); 00068 } 00069 delete pa; 00070 }
HttpdCookies::~HttpdCookies | ( | ) |
void HttpdCookies::add | ( | const std::string & | s | ) |
Definition at line 72 of file HttpdCookies.cpp.
References DEB, and m_cookies.
Referenced by HttpRequest::AddCookie().
00073 { 00074 Parse *pa = new Parse(s,";"); 00075 DEB(fprintf(stderr, "Parse cookie: %s\n", s.c_str());) 00076 std::string slask = pa -> getword(); 00077 while (slask.size()) 00078 { 00079 Parse *pa2 = new Parse(slask,"="); 00080 std::string name = pa2 -> getword(); 00081 std::string value = pa2 -> getword(); 00082 delete pa2; 00083 m_cookies.push_back(std::pair<std::string, std::string>(name, value)); 00084 // 00085 slask = pa -> getword(); 00086 } 00087 delete pa; 00088 }
bool HttpdCookies::getvalue | ( | const std::string & | name, | |
std::string & | buffer | |||
) | const |
Definition at line 94 of file HttpdCookies.cpp.
References m_cookies.
00095 { 00096 for (cookie_v::const_iterator it = m_cookies.begin(); it != m_cookies.end(); ++it) 00097 { 00098 const std::pair<std::string, std::string>& ref = *it; 00099 if (!strcasecmp(ref.first.c_str(),name.c_str())) 00100 { 00101 buffer = ref.second; 00102 return true; 00103 } 00104 } 00105 buffer = ""; 00106 return false; 00107 }
void HttpdCookies::replacevalue | ( | const std::string & | name, | |
const std::string & | value | |||
) |
Definition at line 109 of file HttpdCookies.cpp.
References m_cookies.
Referenced by replacevalue(), and setcookie().
00110 { 00111 for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); ++it) 00112 { 00113 std::pair<std::string, std::string>& ref = *it; 00114 if (!strcasecmp(ref.first.c_str(),name.c_str())) 00115 { 00116 ref.second = value; 00117 return; 00118 } 00119 } 00120 m_cookies.push_back(std::pair<std::string, std::string>(name, value)); 00121 00122 }
void HttpdCookies::replacevalue | ( | const std::string & | name, | |
long | l | |||
) |
Definition at line 124 of file HttpdCookies.cpp.
References Utility::l2string(), and replacevalue().
00125 { 00126 replacevalue(name, Utility::l2string(l)); 00127 }
void HttpdCookies::replacevalue | ( | const std::string & | name, | |
int | i | |||
) |
Definition at line 129 of file HttpdCookies.cpp.
References Utility::l2string(), and replacevalue().
00130 { 00131 replacevalue(name, Utility::l2string(i)); 00132 }
size_t HttpdCookies::getlength | ( | const std::string & | name | ) | const |
Definition at line 134 of file HttpdCookies.cpp.
References m_cookies.
00135 { 00136 for (cookie_v::const_iterator it = m_cookies.begin(); it != m_cookies.end(); ++it) 00137 { 00138 const std::pair<std::string, std::string>& ref = *it; 00139 if (!strcasecmp(ref.first.c_str(),name.c_str())) 00140 { 00141 return ref.second.size(); 00142 } 00143 } 00144 return 0; 00145 }
void HttpdCookies::setcookie | ( | HTTPSocket * | sock, | |
const std::string & | d, | |||
const std::string & | p, | |||
const std::string & | c, | |||
const std::string & | v | |||
) |
Definition at line 147 of file HttpdCookies.cpp.
References expiredatetime(), and replacevalue().
00148 { 00149 size_t sz = name.size() + value.size() + domain.size() + path.size() + 100; 00150 char *str = new char[sz]; 00151 00152 // set-cookie response 00153 if (domain.size()) 00154 { 00155 snprintf(str, sz, "%s=%s; domain=%s; path=%s; expires=%s", 00156 name.c_str(), value.c_str(), 00157 domain.c_str(), 00158 path.c_str(), 00159 expiredatetime().c_str()); 00160 } 00161 else 00162 { 00163 snprintf(str, sz, "%s=%s; path=%s; expires=%s", 00164 name.c_str(), value.c_str(), 00165 path.c_str(), 00166 expiredatetime().c_str()); 00167 } 00168 sock -> AddResponseHeader("Set-cookie", str); 00169 delete[] str; 00170 00171 replacevalue(name, value); 00172 }
void HttpdCookies::setcookie | ( | HTTPSocket * | sock, | |
const std::string & | d, | |||
const std::string & | p, | |||
const std::string & | c, | |||
long | v | |||
) |
Definition at line 174 of file HttpdCookies.cpp.
References expiredatetime(), and replacevalue().
00175 { 00176 size_t sz = name.size() + domain.size() + path.size() + 100; 00177 char *str = new char[sz]; 00178 char dt[80]; 00179 00180 // set-cookie response 00181 if (domain.size()) 00182 { 00183 snprintf(str, sz, "%s=%ld; domain=%s; path=%s; expires=%s", 00184 name.c_str(), value, 00185 domain.c_str(), 00186 path.c_str(), 00187 expiredatetime().c_str()); 00188 } 00189 else 00190 { 00191 snprintf(str, sz, "%s=%ld; path=%s; expires=%s", 00192 name.c_str(), value, 00193 path.c_str(), 00194 expiredatetime().c_str()); 00195 } 00196 sock -> AddResponseHeader("Set-cookie", str); 00197 delete[] str; 00198 00199 snprintf(dt, sizeof(dt), "%ld", value); 00200 replacevalue(name, dt); 00201 }
void HttpdCookies::setcookie | ( | HTTPSocket * | sock, | |
const std::string & | d, | |||
const std::string & | p, | |||
const std::string & | c, | |||
int | v | |||
) |
Definition at line 203 of file HttpdCookies.cpp.
References expiredatetime(), and replacevalue().
00204 { 00205 size_t sz = name.size() + domain.size() + path.size() + 100; 00206 char *str = new char[sz]; 00207 char dt[80]; 00208 00209 // set-cookie response 00210 if (domain.size()) 00211 { 00212 snprintf(str, sz, "%s=%d; domain=%s; path=%s; expires=%s", 00213 name.c_str(), value, 00214 domain.c_str(), 00215 path.c_str(), 00216 expiredatetime().c_str()); 00217 } 00218 else 00219 { 00220 snprintf(str, sz, "%s=%d; path=%s; expires=%s", 00221 name.c_str(), value, 00222 path.c_str(), 00223 expiredatetime().c_str()); 00224 } 00225 sock -> AddResponseHeader("Set-cookie", str); 00226 delete[] str; 00227 00228 snprintf(dt, sizeof(dt), "%d", value); 00229 replacevalue(name, dt); 00230 }
const std::string & HttpdCookies::expiredatetime | ( | ) | const |
Definition at line 233 of file HttpdCookies.cpp.
References m_date.
Referenced by setcookie().
00234 { 00235 const char *days[7] = {"Sunday", "Monday", 00236 "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 00237 const char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", 00238 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 00239 char dt[100]; 00240 00241 time_t t = time(NULL); 00242 #ifdef __CYGWIN__ 00243 struct tm *tp = gmtime(&t); 00244 snprintf(dt, sizeof(dt), "%s, %02d-%s-%04d %02d:%02d:%02d GMT", 00245 days[tp -> tm_wday], 00246 tp -> tm_mday, 00247 months[tp -> tm_mon], 00248 tp -> tm_year + 1910, 00249 tp -> tm_hour, 00250 tp -> tm_min, 00251 tp -> tm_sec); 00252 #else 00253 struct tm tp; 00254 #if defined( _WIN32) && !defined(__CYGWIN__) 00255 gmtime_s(&tp, &t); 00256 #else 00257 gmtime_r(&t, &tp); 00258 #endif 00259 snprintf(dt, sizeof(dt), "%s, %02d-%s-%04d %02d:%02d:%02d GMT", 00260 days[tp.tm_wday], 00261 tp.tm_mday, 00262 months[tp.tm_mon], 00263 tp.tm_year + 1910, 00264 tp.tm_hour, 00265 tp.tm_min, 00266 tp.tm_sec); 00267 #endif 00268 m_date = dt; 00269 return m_date; 00270 }
cookie_v& HttpdCookies::GetHttpdCookies | ( | ) | [inline] |
void HttpdCookies::Reset | ( | ) |
Definition at line 273 of file HttpdCookies.cpp.
References m_cookies, and m_date.
Referenced by HttpRequest::Reset().
00274 { 00275 while (!m_cookies.empty()) 00276 { 00277 m_cookies.erase(m_cookies.begin()); 00278 } 00279 m_date = ""; 00280 }
cookie_v HttpdCookies::m_cookies [private] |
Definition at line 81 of file HttpdCookies.h.
Referenced by add(), getlength(), getvalue(), HttpdCookies(), replacevalue(), and Reset().
std::string HttpdCookies::m_date [mutable, private] |