1 /** \file HttpdCookies.cpp
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.
30 #pragma warning(disable:4786)
34 #include "HTTPSocket.h"
35 #include "HttpdCookies.h"
37 #ifdef SOCKETS_NAMESPACE
38 namespace SOCKETS_NAMESPACE
{
42 #define DEB(x) x; fflush(stderr);
48 HttpdCookies::HttpdCookies()
52 HttpdCookies::HttpdCookies(const std::string
& s
)
54 Parse
*pa
= new Parse(s
,";");
56 std::string slask
= pa
-> getword();
59 Parse
*pa2
= new Parse(slask
,"=");
60 std::string name
= pa2
-> getword();
61 std::string value
= pa2
-> getword();
63 m_cookies
.push_back(std::pair
<std::string
, std::string
>(name
, value
));
65 slask
= pa
-> getword();
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();
77 Parse
*pa2
= new Parse(slask
,"=");
78 std::string name
= pa2
-> getword();
79 std::string value
= pa2
-> getword();
81 m_cookies
.push_back(std::pair
<std::string
, std::string
>(name
, value
));
83 slask
= pa
-> getword();
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()))
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()))
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();
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
152 sprintf(str
, "%s=%s; domain=%s; path=%s; expires=%s",
153 name
.c_str(), value
.c_str(),
156 expiredatetime().c_str());
160 sprintf(str
, "%s=%s; path=%s; expires=%s",
161 name
.c_str(), value
.c_str(),
163 expiredatetime().c_str());
165 sock
-> AddResponseHeader("Set-cookie", 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];
176 // set-cookie response
179 sprintf(str
, "%s=%ld; domain=%s; path=%s; expires=%s",
183 expiredatetime().c_str());
187 sprintf(str
, "%s=%ld; path=%s; expires=%s",
190 expiredatetime().c_str());
192 sock
-> AddResponseHeader("Set-cookie", 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];
204 // set-cookie response
207 sprintf(str
, "%s=%d; domain=%s; path=%s; expires=%s",
211 expiredatetime().c_str());
215 sprintf(str
, "%s=%d; path=%s; expires=%s",
218 expiredatetime().c_str());
220 sock
-> AddResponseHeader("Set-cookie", str
);
223 sprintf(dt
, "%d", value
);
224 replacevalue(name
, dt
);
228 const std::string
& HttpdCookies::expiredatetime() const
230 time_t t
= time(NULL
);
233 memcpy(&tp
, gmtime(&t
), sizeof(tp
));
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"};
243 sprintf(dt
, "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
256 void HttpdCookies::Reset()
258 while (!m_cookies
.empty())
260 m_cookies
.erase(m_cookies
.begin());
266 #ifdef SOCKETS_NAMESPACE