Main Page   Class Hierarchy   File List  

cgiCookie.cpp

00001 #include "cgiCookie.h"
00002 
00003 cgiCookie::cgiCookie():cgiBase()
00004 {
00005   m_strCookies = getenv("HTTP_COOKIE");
00006   char * pchTok = m_strCookies.token( ";" );
00007 
00008   while ( pchTok && strlen( pchTok ) )
00009   {
00010     // a string we can modify
00011     aString subToken = pchTok;
00012 
00013     // get the name first.
00014     aString tmpRawName = subToken.token( "=" );
00015     string tmpName = tmpRawName.trim(aString::both).str();
00016 
00017     // get the variable(s)
00018     aString tmpVal = subToken.token( "=" );
00019 
00020     // add element to (or modify element of) the collection
00021     queryStringMap::iterator pos = cookieMap.find(tmpName);
00022 
00023     if( pos == cookieMap.end() )
00024     {
00025       cookieMap.insert(make_pair(tmpName,tmpVal));
00026     }
00027     pchTok = m_strCookies.token( ";" );
00028   }
00029 }
00030 
00031 cgiCookie::~cgiCookie(){;}
00032 
00033 string & cgiCookie::setTimeout( int month, int day, int year, int hour, int minute )
00034 {
00035   char res[512];
00036   memset( &m_tm, 0, sizeof(m_tm) );
00037   m_tm.tm_min = minute;
00038   m_tm.tm_hour = hour;
00039   m_tm.tm_mon = month-1;
00040   m_tm.tm_mday = day;
00041   m_tm.tm_year = year-1900;
00042   mktime( & m_tm );
00043   strftime(res, sizeof(res), "%a %d-%b-%Y %H:%M:%S GMT", &m_tm );
00044   m_strExpires = res;
00045   return m_strExpires;
00046 }
00047 
00048 string & cgiCookie::setPath( const char * path )
00049 {
00050   m_strPath  = path;
00051   return m_strPath;
00052 }
00053 
00054 string & cgiCookie::setDomain( const char * domain )
00055 {
00056   m_strDomain = domain;
00057   return  m_strDomain;
00058 }
00059 
00060 bool cgiCookie::set( const char* name, const char * value )
00061 {
00062   //Set-Cookie: foo=; path=/; expires Thu, 01-Jan-1970 00:00:00 GMT
00063   if ( name && value && strlen(name) )
00064   {
00065     *this << "Set-Cookie: " << name << "=" << value;
00066     cookieMap[string(name)] = aString(value);
00067   }
00068   if ( m_strPath.length() )
00069   {
00070     *this << "; path=" << m_strPath;
00071   }
00072   if ( m_strDomain.length() )
00073   {
00074     *this << "; domain=" << m_strDomain;
00075   }
00076   if( m_strExpires.length() )
00077   {
00078     *this << "; expires=" << m_strExpires;
00079   }  
00080   *this << endLine;
00081 
00082   return true;
00083 }
00084 
00085 string & cgiCookie::get( const char * key )
00086 {
00087   m_strGotCookie = "";
00088 
00089   queryStringMap::iterator pos;
00090   pos = cookieMap.find( string(key) );
00091   if( pos != cookieMap.end() )
00092   {
00093     m_strGotCookie = pos->second.str();
00094   }
00095 
00096   return m_strGotCookie;
00097 }
00098 aString & cgiCookie::allCookies( void )
00099 {
00100   return m_strCookies;
00101 }

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