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 #ifdef _MSC_VER
00030 #pragma warning(disable:4786)
00031 #endif
00032 #include "Parse.h"
00033 #include "Utility.h"
00034 #include "HTTPSocket.h"
00035 #include "HttpdCookies.h"
00036
00037 #ifdef SOCKETS_NAMESPACE
00038 namespace SOCKETS_NAMESPACE {
00039 #endif
00040
00041 #ifdef _DEBUG
00042 #define DEB(x) x; fflush(stderr);
00043 #else
00044 #define DEB(x)
00045 #endif
00046
00047
00048 HttpdCookies::HttpdCookies()
00049 {
00050 }
00051
00052 HttpdCookies::HttpdCookies(const std::string& s)
00053 {
00054 Parse *pa = new Parse(s,";");
00055
00056 std::string slask = pa -> getword();
00057 while (slask.size())
00058 {
00059 Parse *pa2 = new Parse(slask,"=");
00060 std::string name = pa2 -> getword();
00061 std::string value = pa2 -> getword();
00062 delete pa2;
00063 m_cookies.push_back(std::pair<std::string, std::string>(name, value));
00064
00065 slask = pa -> getword();
00066 }
00067 delete pa;
00068 }
00069
00070 void HttpdCookies::add(const std::string& s)
00071 {
00072 Parse *pa = new Parse(s,";");
00073 DEB(fprintf(stderr, "Parse cookie: %s\n", s.c_str());)
00074 std::string slask = pa -> getword();
00075 while (slask.size())
00076 {
00077 Parse *pa2 = new Parse(slask,"=");
00078 std::string name = pa2 -> getword();
00079 std::string value = pa2 -> getword();
00080 delete pa2;
00081 m_cookies.push_back(std::pair<std::string, std::string>(name, value));
00082
00083 slask = pa -> getword();
00084 }
00085 delete pa;
00086 }
00087
00088 HttpdCookies::~HttpdCookies()
00089 {
00090 }
00091
00092 bool HttpdCookies::getvalue(const std::string& name,std::string& buffer) const
00093 {
00094 for (cookie_v::const_iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00095 {
00096 const std::pair<std::string, std::string>& ref = *it;
00097 if (!strcasecmp(ref.first.c_str(),name.c_str()))
00098 {
00099 buffer = ref.second;
00100 return true;
00101 }
00102 }
00103 buffer = "";
00104 return false;
00105 }
00106
00107 void HttpdCookies::replacevalue(const std::string& name,const std::string& value)
00108 {
00109 for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00110 {
00111 std::pair<std::string, std::string>& ref = *it;
00112 if (!strcasecmp(ref.first.c_str(),name.c_str()))
00113 {
00114 ref.second = value;
00115 return;
00116 }
00117 }
00118 m_cookies.push_back(std::pair<std::string, std::string>(name, value));
00119
00120 }
00121
00122 void HttpdCookies::replacevalue(const std::string& name,long l)
00123 {
00124 replacevalue(name, Utility::l2string(l));
00125 }
00126
00127 void HttpdCookies::replacevalue(const std::string& name,int i)
00128 {
00129 replacevalue(name, Utility::l2string(i));
00130 }
00131
00132 size_t HttpdCookies::getlength(const std::string& name) const
00133 {
00134 for (cookie_v::const_iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00135 {
00136 const std::pair<std::string, std::string>& ref = *it;
00137 if (!strcasecmp(ref.first.c_str(),name.c_str()))
00138 {
00139 return ref.second.size();
00140 }
00141 }
00142 return 0;
00143 }
00144
00145 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, const std::string& value)
00146 {
00147 char *str = new char[name.size() + value.size() + domain.size() + path.size() + 100];
00148
00149
00150 if (domain.size())
00151 {
00152 sprintf(str, "%s=%s; domain=%s; path=%s; expires=%s",
00153 name.c_str(), value.c_str(),
00154 domain.c_str(),
00155 path.c_str(),
00156 expiredatetime().c_str());
00157 }
00158 else
00159 {
00160 sprintf(str, "%s=%s; path=%s; expires=%s",
00161 name.c_str(), value.c_str(),
00162 path.c_str(),
00163 expiredatetime().c_str());
00164 }
00165 sock -> AddResponseHeader("Set-cookie", str);
00166 delete[] str;
00167
00168 replacevalue(name, value);
00169 }
00170
00171 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, long value)
00172 {
00173 char *str = new char[name.size() + domain.size() + path.size() + 100];
00174 char dt[80];
00175
00176
00177 if (domain.size())
00178 {
00179 sprintf(str, "%s=%ld; domain=%s; path=%s; expires=%s",
00180 name.c_str(), value,
00181 domain.c_str(),
00182 path.c_str(),
00183 expiredatetime().c_str());
00184 }
00185 else
00186 {
00187 sprintf(str, "%s=%ld; path=%s; expires=%s",
00188 name.c_str(), value,
00189 path.c_str(),
00190 expiredatetime().c_str());
00191 }
00192 sock -> AddResponseHeader("Set-cookie", str);
00193 delete[] str;
00194
00195 sprintf(dt, "%ld", value);
00196 replacevalue(name, dt);
00197 }
00198
00199 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, int value)
00200 {
00201 char *str = new char[name.size() + domain.size() + path.size() + 100];
00202 char dt[80];
00203
00204
00205 if (domain.size())
00206 {
00207 sprintf(str, "%s=%d; domain=%s; path=%s; expires=%s",
00208 name.c_str(), value,
00209 domain.c_str(),
00210 path.c_str(),
00211 expiredatetime().c_str());
00212 }
00213 else
00214 {
00215 sprintf(str, "%s=%d; path=%s; expires=%s",
00216 name.c_str(), value,
00217 path.c_str(),
00218 expiredatetime().c_str());
00219 }
00220 sock -> AddResponseHeader("Set-cookie", str);
00221 delete[] str;
00222
00223 sprintf(dt, "%d", value);
00224 replacevalue(name, dt);
00225 }
00226
00227
00228 const std::string& HttpdCookies::expiredatetime() const
00229 {
00230 time_t t = time(NULL);
00231 struct tm tp;
00232 #ifdef _WIN32
00233 memcpy(&tp, gmtime(&t), sizeof(tp));
00234 #else
00235 gmtime_r(&t, &tp);
00236 #endif
00237 const char *days[7] = {"Sunday", "Monday",
00238 "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
00239 const char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May",
00240 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
00241 char dt[100];
00242
00243 sprintf(dt, "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
00244 days[tp.tm_wday],
00245 tp.tm_mday,
00246 months[tp.tm_mon],
00247 tp.tm_year + 1910,
00248 tp.tm_hour,
00249 tp.tm_min,
00250 tp.tm_sec);
00251 m_date = dt;
00252 return m_date;
00253 }
00254
00255
00256 void HttpdCookies::Reset()
00257 {
00258 while (!m_cookies.empty())
00259 {
00260 m_cookies.erase(m_cookies.begin());
00261 }
00262 m_date = "";
00263 }
00264
00265
00266 #ifdef SOCKETS_NAMESPACE
00267 }
00268 #endif
00269
00270