Logo
~Sockets~
~Examples~
~Contact~


HttpdCookies.cpp

Go to the documentation of this file.
00001 
00003 /*
00004 Copyright (C) 2003-2007  Anders Hedstrom
00005 
00006 This library is made available under the terms of the GNU GPL.
00007 
00008 If you would like to use this library in a closed-source application,
00009 a separate license agreement is available. For information about 
00010 the closed-source license agreement for the C++ sockets library,
00011 please visit http://www.alhem.net/Sockets/license.html and/or
00012 email license@alhem.net.
00013 
00014 This program is free software; you can redistribute it and/or
00015 modify it under the terms of the GNU General Public License
00016 as published by the Free Software Foundation; either version 2
00017 of the License, or (at your option) any later version.
00018 
00019 This program is distributed in the hope that it will be useful,
00020 but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License
00025 along with this program; if not, write to the Free Software
00026 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 */
00028 
00029 #ifdef _WIN32
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 
00042 HttpdCookies::HttpdCookies()
00043 {
00044 }
00045 
00046 HttpdCookies::HttpdCookies(const std::string& s)
00047 {
00048         Parse *pa = new Parse(s,";");
00049 
00050         std::string slask = pa -> getword();
00051         while (slask.size())
00052         {
00053                 Parse *pa2 = new Parse(slask,"=");
00054                 std::string name = pa2 -> getword();
00055                 std::string value = pa2 -> getword();
00056                 delete pa2;
00057                 COOKIE *c = new COOKIE(name,value);
00058                 m_cookies.push_back(c);
00059                 //
00060                 slask = pa -> getword();
00061         }
00062         delete pa;
00063 }
00064 
00065 HttpdCookies::~HttpdCookies()
00066 {
00067         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00068         {
00069                 COOKIE *c = *it;
00070                 delete c;
00071         }
00072 }
00073 
00074 bool HttpdCookies::getvalue(const std::string& name,std::string& buffer) //char *buffer,size_t length)
00075 {
00076         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00077         {
00078                 COOKIE *c = *it;
00079                 if (!strcasecmp(c -> name.c_str(),name.c_str()))
00080                 {
00081                         buffer = c -> value;
00082                         return true;
00083                 }
00084         }
00085         buffer = "";
00086         return false;
00087 }
00088 
00089 void HttpdCookies::replacevalue(const std::string& name,const std::string& value)
00090 {
00091         COOKIE *c = NULL;
00092         
00093         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00094         {
00095                 c = *it;
00096                 if (!strcasecmp(c -> name.c_str(),name.c_str()))
00097                         break;
00098                 c = NULL;
00099         }
00100 
00101         if (c)
00102         {
00103                 c -> value = value;
00104         }
00105         else
00106         {
00107                 c = new COOKIE(name,value);
00108                 m_cookies.push_back(c);
00109         }
00110 }
00111 
00112 void HttpdCookies::replacevalue(const std::string& name,long l)
00113 {
00114         replacevalue(name, Utility::l2string(l));
00115 }
00116 
00117 void HttpdCookies::replacevalue(const std::string& name,int i)
00118 {
00119         replacevalue(name, Utility::l2string(i));
00120 }
00121 
00122 size_t HttpdCookies::getlength(const std::string& name)
00123 {
00124         COOKIE *c = NULL;
00125 
00126         for (cookie_v::iterator it = m_cookies.begin(); it != m_cookies.end(); it++)
00127         {
00128                 c = *it;
00129                 if (!strcasecmp(c -> name.c_str(),name.c_str()))
00130                         break;
00131                 c = NULL;
00132         }
00133         return c ? c -> value.size() : 0;
00134 }
00135 
00136 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, const std::string& value)
00137 {
00138         char *str = new char[name.size() + value.size() + domain.size() + path.size() + 100];
00139 
00140         // set-cookie response
00141         if (domain.size())
00142         {
00143                 sprintf(str, "%s=%s; domain=%s; path=%s; expires=%s",
00144                  name.c_str(), value.c_str(),
00145                  domain.c_str(),
00146                  path.c_str(),
00147                  expiredatetime().c_str());
00148         }
00149         else
00150         {
00151                 sprintf(str, "%s=%s; path=%s; expires=%s",
00152                  name.c_str(), value.c_str(),
00153                  path.c_str(),
00154                  expiredatetime().c_str());
00155         }
00156         sock -> AddResponseHeader("Set-cookie", str);
00157         delete[] str;
00158 
00159         replacevalue(name, value);
00160 }
00161 
00162 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, long value)
00163 {
00164         char *str = new char[name.size() + domain.size() + path.size() + 100];
00165         char dt[80];
00166 
00167         // set-cookie response
00168         if (domain.size())
00169         {
00170                 sprintf(str, "%s=%ld; domain=%s; path=%s; expires=%s",
00171                  name.c_str(), value,
00172                  domain.c_str(),
00173                  path.c_str(),
00174                  expiredatetime().c_str());
00175         }
00176         else
00177         {
00178                 sprintf(str, "%s=%ld; path=%s; expires=%s",
00179                  name.c_str(), value,
00180                  path.c_str(),
00181                  expiredatetime().c_str());
00182         }
00183         sock -> AddResponseHeader("Set-cookie", str);
00184         delete[] str;
00185 
00186         sprintf(dt, "%ld", value);
00187         replacevalue(name, dt);
00188 }
00189 
00190 void HttpdCookies::setcookie(HTTPSocket *sock, const std::string& domain, const std::string& path, const std::string& name, int value)
00191 {
00192         char *str = new char[name.size() + domain.size() + path.size() + 100];
00193         char dt[80];
00194 
00195         // set-cookie response
00196         if (domain.size())
00197         {
00198                 sprintf(str, "%s=%d; domain=%s; path=%s; expires=%s",
00199                  name.c_str(), value,
00200                  domain.c_str(),
00201                  path.c_str(),
00202                  expiredatetime().c_str());
00203         }
00204         else
00205         {
00206                 sprintf(str, "%s=%d; path=%s; expires=%s",
00207                  name.c_str(), value,
00208                  path.c_str(),
00209                  expiredatetime().c_str());
00210         }
00211         sock -> AddResponseHeader("Set-cookie", str);
00212         delete[] str;
00213 
00214         sprintf(dt, "%d", value);
00215         replacevalue(name, dt);
00216 }
00217 
00218 
00219 const std::string& HttpdCookies::expiredatetime()
00220 {
00221         time_t t = time(NULL);
00222         struct tm tp;
00223 #ifdef _WIN32
00224         memcpy(&tp, gmtime(&t), sizeof(tp));
00225 #else
00226         gmtime_r(&t, &tp);
00227 #endif
00228         const char *days[7] = {"Sunday", "Monday",
00229          "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
00230         const char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May",
00231          "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
00232         char dt[100];
00233 
00234         sprintf(dt, "%s, %02d-%s-%04d %02d:%02d:%02d GMT",
00235          days[tp.tm_wday],
00236          tp.tm_mday,
00237          months[tp.tm_mon],
00238          tp.tm_year + 1910,
00239          tp.tm_hour,
00240          tp.tm_min,
00241          tp.tm_sec);
00242         m_date = dt;
00243         return m_date;
00244 }
00245 
00246 
00247 #ifdef SOCKETS_NAMESPACE
00248 }
00249 #endif
00250 
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4