Cookie.cc

00001 /* -*- c++ -*-
00002  * $Id: Cookie.cc,v 1.11 2006/03/29 05:35:58 brook Exp $
00003  */
00004 
00005 /*
00006  * ClearSilver++ Software License.
00007  *
00008  * Copyright (c) 2005,2006 Brook Milligan <brook@nmsu.edu>
00009  * All rights reserved.
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above
00018  *    copyright notice, this list of conditions and the following
00019  *    disclaimer in the documentation and/or other materials provided
00020  *    with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote
00022  *    products derived from this software without specific prior
00023  *    written permission.
00024  * 
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00026  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00031  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00033  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 
00038 #include "Cookie.h"
00039 
00040 #include <string.h>
00041 #include <boost/date_time/posix_time/posix_time.hpp>
00042 
00043 using namespace std;
00044 using namespace boost;
00045 using namespace ClearSilver;
00046 
00047 static string convert (const posix_time::ptime& t);
00048 
00049 
00050                                 // constructors
00051 Cookie::Cookie () : name_(), value_(), credentials_() {}
00052 Cookie::Cookie (const char * name) : name_(name), value_(), credentials_() {}
00053 Cookie::Cookie (const char * name, const Credentials& credentials)
00054   : name_(name), value_(), credentials_(credentials_) {}
00055 Cookie::Cookie (const char * name, const char * value)
00056   : name_(name), value_(value), credentials_() {}
00057 Cookie::Cookie (const char * name, const char * value,
00058                 const Credentials& credentials)
00059   : name_(name), value_(value), credentials_(credentials) {}
00060 Cookie::Cookie (const string& name) : name_(name), value_(), credentials_() {}
00061 Cookie::Cookie (const string& name, const Credentials& credentials)
00062   : name_(name), value_(), credentials_(credentials_) {}
00063 Cookie::Cookie (const string& name, const string& value)
00064   : name_(name), value_(value), credentials_() {}
00065 Cookie::Cookie (const string& name, const string& value,
00066                 const Credentials& credentials)
00067   : name_(name), value_(value), credentials_(credentials) {}
00068 Cookie::Cookie (const Cookie& c)
00069   : name_(c.name_), value_(c.value_), credentials_(c.credentials_) {}
00070 Cookie::~Cookie () throw() {}
00071 
00072                                 // assignment
00073 Cookie&
00074 Cookie::operator = (const Cookie& c)
00075 {
00076   Cookie c_(c);
00077   swap (c_);
00078   return *this;
00079 }
00080 
00082 void
00083 Cookie::swap (Cookie& c) throw()
00084 {
00085   ::swap(name_, c.name_);
00086   ::swap(value_, c.value_);
00087   ::swap(credentials_, c.credentials_);
00088 }
00089 
00090                                 // data access
00091 string
00092 Cookie::name () const { return name_; }
00093 string
00094 Cookie::value () const { return value_; }
00095 Cookie::Credentials
00096 Cookie::credentials () const { return credentials_; }
00097 Cookie::Credentials&
00098 Cookie::credentials () { return credentials_; }
00099 
00100 
00101                                 // constructors
00102 Cookie::Credentials::Credentials ()
00103   : authority_(), path_(), expires_(), persist_(false), secure_(false) {}
00104 Cookie::Credentials::Credentials (const Authority& a)
00105   : authority_(a), path_(), expires_(), persist_(false), secure_(false) {}
00106 Cookie::Credentials::Credentials (const Path& p)
00107   : authority_(), path_(p), expires_(), persist_(false), secure_(false) {}
00108 Cookie::Credentials::Credentials (const Expires& e)
00109   : authority_(), path_(), expires_(e), persist_(true), secure_(false) {}
00110 Cookie::Credentials::Credentials (const Authority& a, const Path& p)
00111   : authority_(a), path_(p), expires_(), persist_(false), secure_(false) {}
00112 Cookie::Credentials::Credentials (const Authority& a, const Path& p,
00113                                   const Expires& e)
00114   : authority_(a), path_(p), expires_(e), persist_(true), secure_(false) {}
00115 Cookie::Credentials::Credentials (const Credentials& c)
00116   : authority_(c.authority_), path_(c.path_), expires_(c.expires_),
00117     persist_(c.persist_), secure_(c.secure_) {}
00118 Cookie::Credentials::~Credentials () throw() {}
00119 
00120                                 // assignment
00121 Cookie::Credentials&
00122 Cookie::Credentials::operator = (const Credentials& c)
00123 {
00124   Credentials c_(c);
00125   swap (c_);
00126   return *this;
00127 }
00128 
00129                                 // swap contents
00130 void
00131 Cookie::Credentials::swap (Credentials& c) throw()
00132 {
00133   ::swap (authority_, c.authority_);
00134   ::swap (path_, c.path_);
00135   ::swap (expires_, c.expires_);
00136   ::swap (persist_, c.persist_);
00137   ::swap (secure_, c.secure_);
00138 }
00139 
00140 
00141                                 // data access
00142 Cookie::Authority
00143 Cookie::Credentials::authority () const { return authority_; }
00144 
00145 Cookie::Authority&
00146 Cookie::Credentials::authority () { return authority_; }
00147 
00148 Cookie::Path
00149 Cookie::Credentials::path () const { return path_; }
00150 
00151 Cookie::Path&
00152 Cookie::Credentials::path () { return path_; }
00153 
00154 Cookie::Expires
00155 Cookie::Credentials::expires () const { return expires_; }
00156 
00157 Cookie::Expires&
00158 Cookie::Credentials::expires ()
00159 {
00160   persist_ = true;
00161   return expires_;
00162 }
00163 
00164 bool
00165 Cookie::Credentials::persist () const { return persist_; }
00166 
00167 bool
00168 Cookie::Credentials::secure () const { return secure_; }
00169 
00170 bool&
00171 Cookie::Credentials::secure () { return secure_; }
00172 
00173 
00174                                 // constructors
00175 Cookie::Authority::Authority () : authority_() {}
00176 Cookie::Authority::Authority (const char * a) : authority_(a?string(a):"") {}
00177 Cookie::Authority::Authority (const string& a) : authority_(a) {}
00178 Cookie::Authority::Authority (const Authority& a) : authority_(a.authority_) {}
00179 Cookie::Authority::~Authority () throw() {}
00180 
00181                                 // assignment
00182 Cookie::Authority&
00183 Cookie::Authority::operator = (const Authority& a)
00184 {
00185   Authority a_ (a);
00186   swap (a_);
00187   return *this;
00188 }
00189 
00190                                 // swap contents
00191 void
00192 Cookie::Authority::swap (Authority& a) throw()
00193 {
00194   ::swap(authority_, a.authority_);
00195 }
00196 
00197                                 // inspectors
00198 bool
00199 Cookie::Authority::empty () const { return authority_.empty(); }
00200 
00201                                 // conversions
00202 string
00203 Cookie::Authority::operator () () const { return authority_; }
00204 
00205 
00206                                 // constructors
00207 Cookie::Path::Path () : path_() {}
00208 Cookie::Path::Path (const char * p) : path_(p?string(p):"") {}
00209 Cookie::Path::Path (const string& p) : path_(p) {}
00210 Cookie::Path::Path (const Path& p) : path_(p.path_) {}
00211 Cookie::Path::~Path () throw() {}
00212 
00213                                 // assignment
00214 Cookie::Path&
00215 Cookie::Path::operator = (const Path& p)
00216 {
00217   Path p_(p);
00218   swap (p_);
00219   return *this;
00220 }
00221 
00222 
00224 void
00225 Cookie::Path::swap (Path& p) throw()
00226 {
00227   ::swap(path_, p.path_);
00228 }
00229                                 // inspectors
00230 bool
00231 Cookie::Path::empty () const { return path_.empty(); }
00232 
00233                                 // conversions
00234 string
00235 Cookie::Path::operator () () const { return path_; }
00236 
00237 
00238                                 // constructors
00239 Cookie::Expires::Expires () : expires_() {}
00240 Cookie::Expires::Expires (const char* e)
00241   : expires_(convert(posix_time::time_from_string(e))) {}
00242 Cookie::Expires::Expires (const string& e)
00243   : expires_(convert(posix_time::time_from_string(e))) {}
00244 Cookie::Expires::Expires (time_t t)
00245   : expires_(convert(posix_time::from_time_t(t))) {}
00246 Cookie::Expires::Expires (struct tm tm)
00247   : expires_(convert
00248              (posix_time::ptime
00249               (gregorian::date(tm.tm_year, tm.tm_mon, tm.tm_mday),
00250                posix_time::time_duration(tm.tm_hour, tm.tm_min, tm.tm_sec)))) {}
00251 Cookie::Expires::Expires (const boost::posix_time::ptime& expires)
00252   : expires_(convert(expires)) {}
00253 Cookie::Expires::Expires (const Expires& e) : expires_(e.expires_) {}
00254 Cookie::Expires::~Expires () throw() {}
00255 
00256                                 // assignment
00257 Cookie::Expires&
00258 Cookie::Expires::operator = (const Expires& e)
00259 {
00260   expires_ = e.expires_;
00261   return *this;
00262 }
00263 
00264                                 // swap contents
00265 void
00266 Cookie::Expires::swap (Expires& e) throw()
00267 {
00268   std::swap(expires_, e.expires_);
00269 }
00270 
00271                                 // inspectors
00272 Cookie::Expires::operator bool () const { return !expires_.empty(); }
00273 
00274 bool
00275 Cookie::Expires::empty () const { return expires_.empty(); }
00276 
00277                                 // conversions
00278 string
00279 Cookie::Expires::operator () () const { return expires_; }
00280 
00281 string
00282 convert (const posix_time::ptime& t)
00283 {
00284   ostringstream ss;
00285   ss.exceptions(ios_base::failbit);
00286 
00287   date_time::time_facet<posix_time::ptime, char>* facet
00288     = new date_time::time_facet<posix_time::ptime, char>;
00289   ss.imbue(locale(locale::classic(), facet));
00290 
00291   facet->format("%a, %d-%b-%Y %T GMT");
00292   ss.str("");
00293   ss << t;
00294   return ss.str();
00295 }

Generated on Tue May 16 14:50:52 2006 for ClearSilver C++ Library by  doxygen 1.4.5