Google
Web alhem.net

Cookies.cpp

Go to the documentation of this file.
00001 // Cookies.cpp
00002 /*
00003 Copyright (C) 2003  Anders Hedstrom
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 */
00019 
00020 #ifdef _WIN32
00021 #pragma warning(disable:4786)
00022 #define strcasecmp stricmp
00023 #endif
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <time.h>
00027 
00028 #include "Cookies.h"
00029 #include "Parse.h"
00030 //#include "list.h"
00031 
00032 
00033 static  char *days[7] = {"Sunday","Monday",
00034          "Tuesday","Wednesday","Thursday","Friday","Saturday"};
00035 static  char *months[12] = {"Jan","Feb","Mar","Apr","May",
00036          "Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
00037 
00038 namespace Cgi {
00039 
00040 Cookies::Cookies(void)
00041 {
00042 //      cookiebase = NULL;
00043 }
00044 
00045 Cookies::Cookies(char *s)
00046 {
00047         COOKIE *c = NULL;
00048         Cgi::Parse *pa = new Cgi::Parse(s,";");
00049         Cgi::Parse *pa2;
00050         char slask[500];
00051         char name[500];
00052         char value[500];
00053 
00054 //      cookiebase = NULL;
00055 
00056         pa -> getword(slask);
00057         while (*slask)
00058         {
00059                 pa2 = new Cgi::Parse(slask,"=");
00060                 pa2 -> getword(name);
00061                 pa2 -> getword(value);
00062                 delete pa2;
00063                 c = new COOKIE;
00064                 c -> name = new char[strlen(name) + 1];
00065                 strcpy(c -> name,name);
00066                 c -> value = new char[strlen(value) + 1];
00067                 strcpy(c -> value,value);
00068 //              addlist(&cookiebase,c);
00069                 m_cookies.push_back(c);
00070                 //
00071                 pa -> getword(slask);
00072         }
00073         delete pa;
00074 }
00075 
00076 Cookies::~Cookies(void)
00077 {
00078         COOKIE *c = NULL; //,*tmp;
00079 
00080 //      for (c = cookiebase; c; c = tmp)
00081         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00082         {
00083                 c = *it;
00084                 delete[] c -> name;
00085                 delete[] c -> value;
00086 //              tmp = c -> next;
00087                 delete c;
00088         }
00089 }
00090 
00091 int Cookies::getvalue(char *name,char *buffer,short length)
00092 {
00093         COOKIE *c = NULL;
00094 
00095 //      for (c = cookiebase; c; c = c -> next)
00096         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00097         {
00098                 c = *it;
00099                 if (!strcasecmp(c -> name,name))
00100                         break;
00101                 c = NULL;
00102         }
00103         if (c)
00104         {
00105                 if ((short)strlen(c -> value) >= length)
00106                 {
00107                         strncpy(buffer,c -> value,length - 1);
00108                         buffer[length - 1] = 0;
00109                 } else
00110                         strcpy(buffer,c -> value);
00111                 return 1;
00112         } else
00113         {
00114                 *buffer = 0;
00115                 return 0;
00116         }
00117 }
00118 
00119 void Cookies::replacevalue(char *name,char *value)
00120 {
00121         COOKIE *c = NULL;
00122         
00123 //      for (c = cookiebase; c; c = c -> next)
00124         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00125         {
00126                 c = *it;
00127                 if (!strcasecmp(c -> name,name))
00128                         break;
00129                 c = NULL;
00130         }
00131 
00132         if (c)
00133         {
00134                 if (strlen(value) > strlen(c -> value))
00135                 {
00136                         delete[] c -> value;
00137                         c -> value = new char[strlen(value) + 1];
00138                 }
00139                 strcpy(c -> value,value);
00140         } else
00141         {
00142                 c = new COOKIE;
00143                 c -> name = new char[strlen(name) + 1];
00144                 strcpy(c -> name,name);
00145                 c -> value = new char[strlen(value) + 1];
00146                 strcpy(c -> value,value);
00147 //              addlist(&cookiebase,c);
00148                 m_cookies.push_back(c);
00149         }
00150 }
00151 
00152 void Cookies::replacevalue(char *name,long l)
00153 {
00154         char slask[100];
00155 
00156         sprintf(slask,"%ld",l);
00157         replacevalue(name,slask);
00158 }
00159 
00160 void Cookies::replacevalue(char *name,int i)
00161 {
00162         char slask[100];
00163 
00164         sprintf(slask,"%d",i);
00165         replacevalue(name,slask);
00166 }
00167 
00168 int Cookies::getlength(char *name)
00169 {
00170         COOKIE *c = NULL;
00171 
00172 //      for (c = cookiebase; c; c = c -> next)
00173         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00174         {
00175                 c = *it;
00176                 if (!strcasecmp(c -> name,name))
00177                         break;
00178                 c = NULL;
00179         }
00180         return c ? strlen(c -> value) : 0;
00181 }
00182 
00183 void Cookies::setcookie(char *domain,char *path,char *name,char *value)
00184 {
00185         char dt[80];
00186 
00187         Cookies::expiredatetime(dt);
00188 
00189         // set-cookie response
00190         printf("Set-Cookie: %s=%s; domain=%s; path=%s; expires=%s\n",
00191          name,value,
00192          domain,
00193          path,
00194          dt);
00195         Cookies::replacevalue(name,value);
00196 }
00197 
00198 void Cookies::setcookie(char *domain,char *path,char *name,long value)
00199 {
00200         char dt[80];
00201 
00202         Cookies::expiredatetime(dt);
00203 
00204         // set-cookie response
00205         printf("Set-Cookie: %s=%ld; domain=%s; path=%s; expires=%s\n",
00206          name,value,
00207          domain,
00208          path,
00209          dt);
00210         sprintf(dt,"%ld",value);
00211         Cookies::replacevalue(name,dt);
00212 }
00213 
00214 void Cookies::setcookie(char *domain,char *path,char *name,int value)
00215 {
00216         char dt[80];
00217 
00218         Cookies::expiredatetime(dt);
00219 
00220         // set-cookie response
00221         printf("Set-Cookie: %s=%d; domain=%s; path=%s; expires=%s\n",
00222          name,value,
00223          domain,
00224          path,
00225          dt);
00226         sprintf(dt,"%d",value);
00227         Cookies::replacevalue(name,dt);
00228 }
00229 
00230 void Cookies::expiredatetime(char *dt)
00231 {
00232         time_t t = time(NULL);
00233         struct tm * tp = gmtime(&t);
00234 
00235         sprintf(dt,"%s, %02d-%s-%04d %02d:%02d:%02d GMT",
00236          days[tp -> tm_wday],
00237          tp -> tm_mday,
00238          months[tp -> tm_mon],
00239          tp -> tm_year + 1910,
00240          tp -> tm_hour,
00241          tp -> tm_min,
00242          tp -> tm_sec);
00243 }
00244 
00245 } // namespace

Generated for cgi++ by doxygen 1.3.7

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