Logo
~Sockets~
~Examples~
~Contact~


HttpdCookies Class Reference
[Webserver framework]

HTTP Cookie parse/container class. More...

#include <HttpdCookies.h>

List of all members.


Public Member Functions

 HttpdCookies ()
 HttpdCookies (const std::string &query_string)
 ~HttpdCookies ()
bool getvalue (const std::string &, std::string &)
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 &)
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 ()
cookie_vGetHttpdCookies ()

Private Types

typedef std::list< COOKIE * > cookie_v
 list of key/value structs.

Private Attributes

cookie_v m_cookies
std::string m_date

Classes

struct  COOKIE
 Name/value pair store struct. More...

Detailed Description

HTTP Cookie parse/container class.

See also:
HttpdSocket

HttpdForm

Definition at line 52 of file HttpdCookies.h.


Member Typedef Documentation

typedef std::list<COOKIE *> HttpdCookies::cookie_v [private]

list of key/value structs.

Definition at line 63 of file HttpdCookies.h.


Constructor & Destructor Documentation

HttpdCookies::HttpdCookies (  ) 

Definition at line 42 of file HttpdCookies.cpp.

00043 {
00044 }

HttpdCookies::HttpdCookies ( const std::string &  query_string  ) 

Definition at line 46 of file HttpdCookies.cpp.

References m_cookies.

00047 {
00048         Parse *pa = new Parse(s,";");
00049 
00050         std::string slask = pa -> getword();
00051         while (slask.size())
00052         {
00053                 Parse *pa2 = new Parse(slask,"=");
00054                 std::string name = pa2 -> getword();
00055                 std::string value = pa2 -> getword();
00056                 delete pa2;
00057                 COOKIE *c = new COOKIE(name,value);
00058                 m_cookies.push_back(c);
00059                 //
00060                 slask = pa -> getword();
00061         }
00062         delete pa;
00063 }

HttpdCookies::~HttpdCookies (  ) 

Definition at line 65 of file HttpdCookies.cpp.

References m_cookies.

00066 {
00067         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00068         {
00069                 COOKIE *c = *it;
00070                 delete c;
00071         }
00072 }


Member Function Documentation

bool HttpdCookies::getvalue ( const std::string &  ,
std::string &   
)

Definition at line 74 of file HttpdCookies.cpp.

References m_cookies.

00075 {
00076         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00077         {
00078                 COOKIE *c = *it;
00079                 if (!strcasecmp(c -> name.c_str(),name.c_str()))
00080                 {
00081                         buffer = c -> value;
00082                         return true;
00083                 }
00084         }
00085         buffer = "";
00086         return false;
00087 }

void HttpdCookies::replacevalue ( const std::string &  ,
const std::string &   
)

Definition at line 89 of file HttpdCookies.cpp.

References m_cookies.

Referenced by replacevalue(), and setcookie().

00090 {
00091         COOKIE *c = NULL;
00092         
00093         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00094         {
00095                 c = *it;
00096                 if (!strcasecmp(c -> name.c_str(),name.c_str()))
00097                         break;
00098                 c = NULL;
00099         }
00100 
00101         if (c)
00102         {
00103                 c -> value = value;
00104         }
00105         else
00106         {
00107                 c = new COOKIE(name,value);
00108                 m_cookies.push_back(c);
00109         }
00110 }

void HttpdCookies::replacevalue ( const std::string &  ,
long   
)

Definition at line 112 of file HttpdCookies.cpp.

References Utility::l2string(), and replacevalue().

00113 {
00114         replacevalue(name, Utility::l2string(l));
00115 }

void HttpdCookies::replacevalue ( const std::string &  ,
int   
)

Definition at line 117 of file HttpdCookies.cpp.

References Utility::l2string(), and replacevalue().

00118 {
00119         replacevalue(name, Utility::l2string(i));
00120 }

size_t HttpdCookies::getlength ( const std::string &   ) 

Definition at line 122 of file HttpdCookies.cpp.

References m_cookies.

00123 {
00124         COOKIE *c = NULL;
00125 
00126         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00127         {
00128                 c = *it;
00129                 if (!strcasecmp(c -> name.c_str(),name.c_str()))
00130                         break;
00131                 c = NULL;
00132         }
00133         return c ? c -> value.size() : 0;
00134 }

void HttpdCookies::setcookie ( HTTPSocket ,
const std::string &  d,
const std::string &  p,
const std::string &  c,
const std::string &  v 
)

Definition at line 136 of file HttpdCookies.cpp.

References expiredatetime(), and replacevalue().

00137 {
00138         char *str = new char[name.size() + value.size() + domain.size() + path.size() + 100];
00139 
00140         // set-cookie response
00141         if (domain.size())
00142         {
00143                 sprintf(str, "%s=%s; domain=%s; path=%s; expires=%s",
00144                  name.c_str(), value.c_str(),
00145                  domain.c_str(),
00146                  path.c_str(),
00147                  expiredatetime().c_str());
00148         }
00149         else
00150         {
00151                 sprintf(str, "%s=%s; path=%s; expires=%s",
00152                  name.c_str(), value.c_str(),
00153                  path.c_str(),
00154                  expiredatetime().c_str());
00155         }
00156         sock -> AddResponseHeader("Set-cookie", str);
00157         delete[] str;
00158 
00159         replacevalue(name, value);
00160 }

void HttpdCookies::setcookie ( HTTPSocket ,
const std::string &  d,
const std::string &  p,
const std::string &  c,
long  v 
)

Definition at line 162 of file HttpdCookies.cpp.

References expiredatetime(), and replacevalue().

00163 {
00164         char *str = new char[name.size() + domain.size() + path.size() + 100];
00165         char dt[80];
00166 
00167         // set-cookie response
00168         if (domain.size())
00169         {
00170                 sprintf(str, "%s=%ld; domain=%s; path=%s; expires=%s",
00171                  name.c_str(), value,
00172                  domain.c_str(),
00173                  path.c_str(),
00174                  expiredatetime().c_str());
00175         }
00176         else
00177         {
00178                 sprintf(str, "%s=%ld; path=%s; expires=%s",
00179                  name.c_str(), value,
00180                  path.c_str(),
00181                  expiredatetime().c_str());
00182         }
00183         sock -> AddResponseHeader("Set-cookie", str);
00184         delete[] str;
00185 
00186         sprintf(dt, "%ld", value);
00187         replacevalue(name, dt);
00188 }

void HttpdCookies::setcookie ( HTTPSocket ,
const std::string &  d,
const std::string &  p,
const std::string &  c,
int  v 
)

Definition at line 190 of file HttpdCookies.cpp.

References expiredatetime(), and replacevalue().

00191 {
00192         char *str = new char[name.size() + domain.size() + path.size() + 100];
00193         char dt[80];
00194 
00195         // set-cookie response
00196         if (domain.size())
00197         {
00198                 sprintf(str, "%s=%d; domain=%s; path=%s; expires=%s",
00199                  name.c_str(), value,
00200                  domain.c_str(),
00201                  path.c_str(),
00202                  expiredatetime().c_str());
00203         }
00204         else
00205         {
00206                 sprintf(str, "%s=%d; path=%s; expires=%s",
00207                  name.c_str(), value,
00208                  path.c_str(),
00209                  expiredatetime().c_str());
00210         }
00211         sock -> AddResponseHeader("Set-cookie", str);
00212         delete[] str;
00213 
00214         sprintf(dt, "%d", value);
00215         replacevalue(name, dt);
00216 }

const std::string & HttpdCookies::expiredatetime (  ) 

Definition at line 219 of file HttpdCookies.cpp.

References m_date.

Referenced by setcookie().

00220 {
00221         time_t t = time(NULL);
00222         struct tm tp;
00223 #ifdef _WIN32
00224         memcpy(&tp, gmtime(&t), sizeof(tp));
00225 #else
00226         gmtime_r(&t, &tp);
00227 #endif
00228         const char *days[7] = {"Sunday", "Monday",
00229          "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
00230         const char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May",
00231          "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
00232         char dt[100];
00233 
00234         sprintf(dt, "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
00235          days[tp.tm_wday],
00236          tp.tm_mday,
00237          months[tp.tm_mon],
00238          tp.tm_year + 1910,
00239          tp.tm_hour,
00240          tp.tm_min,
00241          tp.tm_sec);
00242         m_date = dt;
00243         return m_date;
00244 }

cookie_v& HttpdCookies::GetHttpdCookies (  )  [inline]

Definition at line 80 of file HttpdCookies.h.

00080 { return m_cookies; }


Member Data Documentation

std::string HttpdCookies::m_date [private]

Definition at line 84 of file HttpdCookies.h.

Referenced by expiredatetime().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4