big svn cleanup
[anytun.git] / src / Sockets / HttpdCookies.cpp
blob9662f0d370b72a9b5bbc676f2e9df7c044fcd198
1 /** \file HttpdCookies.cpp
2 */
3 /*
4 Copyright (C) 2003-2007 Anders Hedstrom
6 This library is made available under the terms of the GNU GPL.
8 If you would like to use this library in a closed-source application,
9 a separate license agreement is available. For information about
10 the closed-source license agreement for the C++ sockets library,
11 please visit http://www.alhem.net/Sockets/license.html and/or
12 email license@alhem.net.
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #ifdef _MSC_VER
30 #pragma warning(disable:4786)
31 #endif
32 #include "Parse.h"
33 #include "Utility.h"
34 #include "HTTPSocket.h"
35 #include "HttpdCookies.h"
37 #ifdef SOCKETS_NAMESPACE
38 namespace SOCKETS_NAMESPACE {
39 #endif
41 #ifdef _DEBUG
42 #define DEB(x) x; fflush(stderr);
43 #else
44 #define DEB(x)
45 #endif
48 HttpdCookies::HttpdCookies()
52 HttpdCookies::HttpdCookies(const std::string& s)
54 Parse *pa = new Parse(s,";");
56 std::string slask = pa -> getword();
57 while (slask.size())
59 Parse *pa2 = new Parse(slask,"=");
60 std::string name = pa2 -> getword();
61 std::string value = pa2 -> getword();
62 delete pa2;
63 m_cookies.push_back(std::pair<std::string, std::string>(name, value));
65 slask = pa -> getword();
67 delete pa;
70 void HttpdCookies::add(const std::string& s)
72 Parse *pa = new Parse(s,";");
73 DEB(fprintf(stderr, "Parse cookie: %s\n", s.c_str());)
74 std::string slask = pa -> getword();
75 while (slask.size())
77 Parse *pa2 = new Parse(slask,"=");
78 std::string name = pa2 -> getword();
79 std::string value = pa2 -> getword();
80 delete pa2;
81 m_cookies.push_back(std::pair<std::string, std::string>(name, value));
83 slask = pa -> getword();
85 delete pa;
88 HttpdCookies::~HttpdCookies()
92 bool HttpdCookies::getvalue(const std::string& name,std::string& buffer) const
94 for (cookie_v::const_iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
96 const std::pair<std::string, std::string>& ref = *it;
97 if (!strcasecmp(ref.first.c_str(),name.c_str()))
99 buffer = ref.second;
100 return true;
103 buffer = "";
104 return false;
107 void HttpdCookies::replacevalue(const std::string& name,const std::string& value)
109 for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
111 std::pair<std::string, std::string>& ref = *it;
112 if (!strcasecmp(ref.first.c_str(),name.c_str()))
114 ref.second = value;
115 return;
118 m_cookies.push_back(std::pair<std::string, std::string>(name, value));
122 void HttpdCookies::replacevalue(const std::string& name,long l)
124 replacevalue(name, Utility::l2string(l));
127 void HttpdCookies::replacevalue(const std::string& name,int i)
129 replacevalue(name, Utility::l2string(i));
132 size_t HttpdCookies::getlength(const std::string& name) const
134 for (cookie_v::const_iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
136 const std::pair<std::string, std::string>& ref = *it;
137 if (!strcasecmp(ref.first.c_str(),name.c_str()))
139 return ref.second.size();
142 return 0;
145 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, const std::string& value)
147 char *str = new char[name.size() + value.size() + domain.size() + path.size() + 100];
149 // set-cookie response
150 if (domain.size())
152 sprintf(str, "%s=%s; domain=%s; path=%s; expires=%s",
153 name.c_str(), value.c_str(),
154 domain.c_str(),
155 path.c_str(),
156 expiredatetime().c_str());
158 else
160 sprintf(str, "%s=%s; path=%s; expires=%s",
161 name.c_str(), value.c_str(),
162 path.c_str(),
163 expiredatetime().c_str());
165 sock -> AddResponseHeader("Set-cookie", str);
166 delete[] str;
168 replacevalue(name, value);
171 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, long value)
173 char *str = new char[name.size() + domain.size() + path.size() + 100];
174 char dt[80];
176 // set-cookie response
177 if (domain.size())
179 sprintf(str, "%s=%ld; domain=%s; path=%s; expires=%s",
180 name.c_str(), value,
181 domain.c_str(),
182 path.c_str(),
183 expiredatetime().c_str());
185 else
187 sprintf(str, "%s=%ld; path=%s; expires=%s",
188 name.c_str(), value,
189 path.c_str(),
190 expiredatetime().c_str());
192 sock -> AddResponseHeader("Set-cookie", str);
193 delete[] str;
195 sprintf(dt, "%ld", value);
196 replacevalue(name, dt);
199 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, int value)
201 char *str = new char[name.size() + domain.size() + path.size() + 100];
202 char dt[80];
204 // set-cookie response
205 if (domain.size())
207 sprintf(str, "%s=%d; domain=%s; path=%s; expires=%s",
208 name.c_str(), value,
209 domain.c_str(),
210 path.c_str(),
211 expiredatetime().c_str());
213 else
215 sprintf(str, "%s=%d; path=%s; expires=%s",
216 name.c_str(), value,
217 path.c_str(),
218 expiredatetime().c_str());
220 sock -> AddResponseHeader("Set-cookie", str);
221 delete[] str;
223 sprintf(dt, "%d", value);
224 replacevalue(name, dt);
228 const std::string& HttpdCookies::expiredatetime() const
230 time_t t = time(NULL);
231 struct tm tp;
232 #ifdef _WIN32
233 memcpy(&tp, gmtime(&t), sizeof(tp));
234 #else
235 gmtime_r(&t, &tp);
236 #endif
237 const char *days[7] = {"Sunday", "Monday",
238 "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
239 const char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May",
240 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
241 char dt[100];
243 sprintf(dt, "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
244 days[tp.tm_wday],
245 tp.tm_mday,
246 months[tp.tm_mon],
247 tp.tm_year + 1910,
248 tp.tm_hour,
249 tp.tm_min,
250 tp.tm_sec);
251 m_date = dt;
252 return m_date;
256 void HttpdCookies::Reset()
258 while (!m_cookies.empty())
260 m_cookies.erase(m_cookies.begin());
262 m_date = "";
266 #ifdef SOCKETS_NAMESPACE
268 #endif