00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef _MSC_VER
00032 #pragma warning(disable:4786)
00033 #endif
00034 #include "Parse.h"
00035 #include "HTTPSocket.h"
00036 #include "HttpdCookies.h"
00037 #include <cstdio>
00038
00039 #ifdef SOCKETS_NAMESPACE
00040 namespace SOCKETS_NAMESPACE {
00041 #endif
00042
00043 #ifdef _DEBUG
00044 #define DEB(x) x; fflush(stderr);
00045 #else
00046 #define DEB(x)
00047 #endif
00048
00049
00050 HttpdCookies::HttpdCookies()
00051 {
00052 }
00053
00054 HttpdCookies::HttpdCookies(const std::string& s)
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 }
00071
00072 void HttpdCookies::add(const std::string& s)
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 }
00089
00090 HttpdCookies::~HttpdCookies()
00091 {
00092 }
00093
00094 bool HttpdCookies::getvalue(const std::string& name,std::string& buffer) const
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 }
00108
00109 void HttpdCookies::replacevalue(const std::string& name,const std::string& value)
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 }
00123
00124 void HttpdCookies::replacevalue(const std::string& name,long l)
00125 {
00126 replacevalue(name, Utility::l2string(l));
00127 }
00128
00129 void HttpdCookies::replacevalue(const std::string& name,int i)
00130 {
00131 replacevalue(name, Utility::l2string(i));
00132 }
00133
00134 size_t HttpdCookies::getlength(const std::string& name) const
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 }
00146
00147 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, const std::string& value)
00148 {
00149 size_t sz = name.size() + value.size() + domain.size() + path.size() + 100;
00150 char *str = new char[sz];
00151
00152
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 }
00173
00174 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, long value)
00175 {
00176 size_t sz = name.size() + domain.size() + path.size() + 100;
00177 char *str = new char[sz];
00178 char dt[80];
00179
00180
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 }
00202
00203 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, int value)
00204 {
00205 size_t sz = name.size() + domain.size() + path.size() + 100;
00206 char *str = new char[sz];
00207 char dt[80];
00208
00209
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 }
00231
00232
00233 const std::string& HttpdCookies::expiredatetime() const
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 }
00271
00272
00273 void HttpdCookies::Reset()
00274 {
00275 while (!m_cookies.empty())
00276 {
00277 m_cookies.erase(m_cookies.begin());
00278 }
00279 m_date = "";
00280 }
00281
00282
00283 #ifdef SOCKETS_NAMESPACE
00284 }
00285 #endif
00286
00287