ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

138 lines
4.0 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #define CPPCMS_SOURCE
  20. #include "http_cookie.h"
  21. #include "http_protocol.h"
  22. #include "cppcms_error.h"
  23. #include <sstream>
  24. #include <locale>
  25. namespace cppcms { namespace http {
  26. struct cookie::data { };
  27. std::string cookie::name() const { return name_; }
  28. void cookie::name(std::string v) { name_=v; }
  29. std::string cookie::value() const { return value_; }
  30. void cookie::value(std::string v) { value_=v; }
  31. std::string cookie::path() const { return path_; }
  32. void cookie::path(std::string v) { path_=v; }
  33. std::string cookie::domain() const { return domain_; }
  34. void cookie::domain(std::string v) { domain_=v; }
  35. std::string cookie::comment() const { return comment_; }
  36. void cookie::comment(std::string v) { comment_=v; }
  37. void cookie::max_age(unsigned age)
  38. {
  39. has_age_=1;
  40. max_age_=age;
  41. }
  42. void cookie::browser_age()
  43. {
  44. has_age_=0;
  45. }
  46. bool cookie::secure() const { return secure_; }
  47. void cookie::secure(bool secure) { secure_ = secure ? 1: 0; }
  48. void cookie::write(std::ostream &out) const
  49. {
  50. if(name_.empty())
  51. throw cppcms_error("Cookie's name is not defined");
  52. out<<"Set-Cookie:"<<name_<<'=';
  53. if(value_.empty())
  54. ; // Nothing to do write
  55. if(protocol::tocken(value_.begin(),value_.end())==value_.end())
  56. out<<value_;
  57. else
  58. out<<protocol::quote(value_);
  59. if(!comment_.empty())
  60. out<<"; Comment="<<protocol::quote(comment_);
  61. if(!domain_.empty())
  62. out<<"; Domain="<<domain_;
  63. if(has_age_) {
  64. std::ostringstream ss;
  65. ss.imbue(std::locale("C"));
  66. ss<<max_age_;
  67. out<<"; Max-Age="<<ss.str();
  68. }
  69. if(!path_.empty())
  70. out<<"; Path="<<path_;
  71. if(secure_)
  72. out<<"; Secure";
  73. out<<"; Version=1";
  74. }
  75. std::ostream &operator<<(std::ostream &out,cookie const &c)
  76. {
  77. c.write(out);
  78. return out;
  79. }
  80. cookie::cookie(std::string name,std::string value) :
  81. name_(name), value_(value), secure_(0), has_age_(0)
  82. {
  83. }
  84. cookie::cookie(std::string name,std::string value,unsigned age) :
  85. name_(name), value_(value), max_age_(age), secure_(0), has_age_(1)
  86. {
  87. }
  88. cookie::cookie(std::string name,std::string value,unsigned age,std::string path,std::string domain,std::string comment) :
  89. name_(name), value_(value), path_(path),domain_(domain),comment_(comment),max_age_(age), secure_(0), has_age_(1)
  90. {
  91. }
  92. cookie::cookie(std::string name,std::string value,std::string path,std::string domain,std::string comment) :
  93. name_(name), value_(value), path_(path),domain_(domain),comment_(comment), secure_(0), has_age_(0)
  94. {
  95. }
  96. cookie::cookie(cookie const &other) :
  97. name_(other.name_),
  98. value_(other.value_),
  99. path_(other.path_),
  100. domain_(other.domain_),
  101. comment_(other.comment_),
  102. max_age_(other.max_age_),
  103. secure_(other.secure_),
  104. has_age_(other.has_age_)
  105. {
  106. }
  107. cookie const &cookie::operator=(cookie const &other)
  108. {
  109. name_=other.name_;
  110. value_=other.value_;
  111. path_=other.path_;
  112. domain_=other.domain_;
  113. comment_=other.comment_;
  114. max_age_=other.max_age_;
  115. secure_=other.secure_;
  116. has_age_=other.has_age_;
  117. return *this;
  118. }
  119. cookie::cookie() : secure_(0), has_age_(0) {}
  120. cookie::~cookie() {}
  121. } } // cppcms::http