|
|
@@ -9,13 +9,14 @@ |
|
|
|
#include <cppcms/http_cookie.h> |
|
|
|
#include "http_protocol.h" |
|
|
|
#include <cppcms/cppcms_error.h> |
|
|
|
#include <booster/posix_time.h> |
|
|
|
|
|
|
|
#include <sstream> |
|
|
|
#include <locale> |
|
|
|
|
|
|
|
namespace cppcms { namespace http { |
|
|
|
|
|
|
|
struct cookie::_data { }; |
|
|
|
struct cookie::_data { time_t expires; }; |
|
|
|
bool cookie::empty() const |
|
|
|
{ |
|
|
|
return name_.empty() && value_.empty(); |
|
|
@@ -31,6 +32,15 @@ std::string cookie::domain() const { return domain_; } |
|
|
|
void cookie::domain(std::string v) { domain_=v; } |
|
|
|
std::string cookie::comment() const { return comment_; } |
|
|
|
void cookie::comment(std::string v) { comment_=v; } |
|
|
|
|
|
|
|
void cookie::expires(time_t when) |
|
|
|
{ |
|
|
|
if(!d.get()) { |
|
|
|
d.reset(new _data()); |
|
|
|
} |
|
|
|
has_expiration_=1; |
|
|
|
d->expires = when; |
|
|
|
} |
|
|
|
void cookie::max_age(unsigned age) |
|
|
|
{ |
|
|
|
has_age_=1; |
|
|
@@ -39,6 +49,7 @@ void cookie::max_age(unsigned age) |
|
|
|
void cookie::browser_age() |
|
|
|
{ |
|
|
|
has_age_=0; |
|
|
|
has_expiration_=0; |
|
|
|
} |
|
|
|
bool cookie::secure() const { return secure_; } |
|
|
|
void cookie::secure(bool secure) { secure_ = secure ? 1: 0; } |
|
|
@@ -60,11 +71,23 @@ void cookie::write(std::ostream &out) const |
|
|
|
out<<"; Comment="<<protocol::quote(comment_); |
|
|
|
if(!domain_.empty()) |
|
|
|
out<<"; Domain="<<domain_; |
|
|
|
if(has_age_) { |
|
|
|
std::ostringstream ss; |
|
|
|
ss.imbue(std::locale("C")); |
|
|
|
ss<<max_age_; |
|
|
|
out<<"; Max-Age="<<ss.str(); |
|
|
|
if(has_age_ || has_expiration_) { |
|
|
|
std::locale l=std::locale::classic(); |
|
|
|
std::stringstream ss; |
|
|
|
ss.imbue(l); |
|
|
|
if(has_age_) |
|
|
|
ss<<"; Max-Age="<<max_age_; |
|
|
|
|
|
|
|
if(has_expiration_ && d.get()) { |
|
|
|
ss<<"; Expires="; |
|
|
|
|
|
|
|
std::tm splitted = booster::ptime::universal_time(booster::ptime(d->expires)); |
|
|
|
static char const format[]="%a, %d %b %Y %H:%M:%S GMT"; |
|
|
|
char const *b=format; |
|
|
|
char const *e=b+sizeof(format)-1; |
|
|
|
std::use_facet<std::time_put<char> >(l).put(ss,ss,' ',&splitted,b,e); |
|
|
|
} |
|
|
|
out << ss.rdbuf(); |
|
|
|
} |
|
|
|
if(!path_.empty()) |
|
|
|
out<<"; Path="<<path_; |
|
|
@@ -80,25 +103,26 @@ std::ostream &operator<<(std::ostream &out,cookie const &c) |
|
|
|
} |
|
|
|
|
|
|
|
cookie::cookie(std::string name,std::string value) : |
|
|
|
name_(name), value_(value), secure_(0), has_age_(0) |
|
|
|
name_(name), value_(value), secure_(0), has_age_(0), has_expiration_(0) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
cookie::cookie(std::string name,std::string value,unsigned age) : |
|
|
|
name_(name), value_(value), max_age_(age), secure_(0), has_age_(1) |
|
|
|
name_(name), value_(value), max_age_(age), secure_(0), has_age_(1), has_expiration_(0) |
|
|
|
{ |
|
|
|
} |
|
|
|
cookie::cookie(std::string name,std::string value,unsigned age,std::string path,std::string domain,std::string comment) : |
|
|
|
name_(name), value_(value), path_(path),domain_(domain),comment_(comment),max_age_(age), secure_(0), has_age_(1) |
|
|
|
name_(name), value_(value), path_(path),domain_(domain),comment_(comment),max_age_(age), secure_(0), has_age_(1), has_expiration_(0) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
cookie::cookie(std::string name,std::string value,std::string path,std::string domain,std::string comment) : |
|
|
|
name_(name), value_(value), path_(path),domain_(domain),comment_(comment), secure_(0), has_age_(0) |
|
|
|
name_(name), value_(value), path_(path),domain_(domain),comment_(comment), secure_(0), has_age_(0), has_expiration_(0) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
cookie::cookie(cookie const &other) : |
|
|
|
d(other.d), |
|
|
|
name_(other.name_), |
|
|
|
value_(other.value_), |
|
|
|
path_(other.path_), |
|
|
@@ -106,12 +130,14 @@ cookie::cookie(cookie const &other) : |
|
|
|
comment_(other.comment_), |
|
|
|
max_age_(other.max_age_), |
|
|
|
secure_(other.secure_), |
|
|
|
has_age_(other.has_age_) |
|
|
|
has_age_(other.has_age_), |
|
|
|
has_expiration_(other.has_expiration_) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
cookie const &cookie::operator=(cookie const &other) |
|
|
|
{ |
|
|
|
d=other.d; |
|
|
|
name_=other.name_; |
|
|
|
value_=other.value_; |
|
|
|
path_=other.path_; |
|
|
@@ -120,10 +146,11 @@ cookie const &cookie::operator=(cookie const &other) |
|
|
|
max_age_=other.max_age_; |
|
|
|
secure_=other.secure_; |
|
|
|
has_age_=other.has_age_; |
|
|
|
has_expiration_ = other.has_expiration_; |
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
cookie::cookie() : secure_(0), has_age_(0) {} |
|
|
|
cookie::cookie() : secure_(0), has_age_(0), has_expiration_(0) {} |
|
|
|
cookie::~cookie() {} |
|
|
|
|
|
|
|
|
|
|
|