00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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
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
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;
00079
00080
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
00087 delete c;
00088 }
00089 }
00090
00091 int Cookies::getvalue(char *name,char *buffer,short length)
00092 {
00093 COOKIE *c = NULL;
00094
00095
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
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
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
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
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
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
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 }