cookie.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library 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 GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_COOKIE_H
31 #define TNT_COOKIE_H
32 
33 #include <string>
34 #include <map>
35 #include <iosfwd>
37 
38 namespace tnt
39 {
40  class Cookies;
41  class CookieParser;
42 
43  class Cookie
44  {
45  friend std::ostream& operator<< (std::ostream& out, const Cookies& c);
46  friend class CookieParser;
47  friend class HttpReply;
48  void write(std::ostream& out, const std::string& name) const;
49 
50  public:
51  static const std::string maxAge;
52  static const std::string comment;
53  static const std::string domain;
54  static const std::string path;
55  static const std::string secure;
56  static const std::string version;
57  static const std::string expires;
58 
59  private:
60  typedef std::map<std::string, std::string, StringLessIgnoreCase<std::string> > attrs_type;
61  std::string value;
62  attrs_type attrs;
63  bool secureFlag;
64 
65  public:
67  : secureFlag(false)
68  { }
69 
70  Cookie(const std::string& v, unsigned maxAge_ = 0)
71  : value(v),
72  secureFlag(false)
73  {
74  if (maxAge_)
75  setMaxAge(maxAge_);
76  }
77  Cookie(const char* v, unsigned maxAge_ = 0)
78  : value(v),
79  secureFlag(false)
80  {
81  if (maxAge_)
82  setMaxAge(maxAge_);
83  }
84 
85  const std::string& getValue() const { return value; }
86 
87  std::string getAttr(const std::string& name) const
88  {
89  attrs_type::const_iterator it = attrs.find(name);
90  return it == attrs.end() ? std::string() : it->second;
91  }
92 
93  operator const std::string& () const { return value; }
94 
95  void setAttr(const std::string& name, const std::string& value_)
96  { attrs[name] = value_; }
97  bool hasAttr(const std::string& name) const
98  { return attrs.find(name) != attrs.end(); }
99 
100  unsigned getMaxAge() const;
101  std::string getComment() const { return getAttr(comment); }
102  std::string getDomain() const { return getAttr(domain); }
103  std::string getPath() const { return getAttr(path); }
104  std::string getVersion() const { return getAttr(version); }
105  std::string getExpires() const { return getAttr(expires); }
106  bool isSecure() const { return secureFlag; }
107 
108  void setMaxAge(unsigned seconds);
109  void setComment(const std::string& value_) { setAttr(comment, value_); }
110  void setDomain(const std::string& value_) { setAttr(domain, value_); }
111  void setPath(const std::string& value_) { setAttr(path, value_); }
112  void setVersion(const std::string& value_) { setAttr(version, value_); }
113  void setExpires(const std::string& value_) { setAttr(expires, value_); }
114  void setSecure(bool f = true) { secureFlag = f; }
115 
116  bool hasMaxAge() const { return hasAttr(maxAge); }
117  bool hasComment() const { return hasAttr(comment); }
118  bool hasDomain() const { return hasAttr(domain); }
119  bool hasPath() const { return hasAttr(path); }
120  bool hasVersion() const { return hasAttr(version); }
121  bool hasExpires() const { return hasAttr(expires); }
122 
123  };
124 
125  class Cookies
126  {
127  friend std::ostream& operator<< (std::ostream& out, const Cookies& c);
128  friend class HttpReply;
129 
130  typedef std::map<std::string, Cookie, StringLessIgnoreCase<std::string> > cookies_type;
131  cookies_type data;
132 
133  static const Cookie emptyCookie;
134 
135  public:
136  void set(const std::string& header);
137  void clear() { data.clear(); }
138 
139  const Cookie& getCookie(const std::string& name) const
140  {
141  cookies_type::const_iterator it = data.find(name);
142  return it == data.end() ? emptyCookie : it->second;
143  }
144 
145  void setCookie(const std::string& name, const Cookie& value_)
146  { data[name] = value_; }
147 
148  void clearCookie(const std::string& name);
149  void clearCookie(const std::string& name, const Cookie& c);
150 
151  bool hasCookie(const std::string& name) const
152  { return data.find(name) != data.end(); }
153  bool hasCookies() const
154  { return !data.empty(); }
155  };
156 
157  std::ostream& operator<< (std::ostream& out, const Cookies& c);
158 }
159 
160 #endif // TNT_COOKIE_H
161