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 
49  public:
50  static const std::string maxAge;
51  static const std::string comment;
52  static const std::string domain;
53  static const std::string path;
54  static const std::string secure;
55  static const std::string version;
56  static const std::string expires;
57 
58  private:
59  typedef std::map<std::string, std::string, StringLessIgnoreCase<std::string> > attrs_type;
60 
61  std::string _value;
62  attrs_type _attrs;
63  bool _secureFlag;
64 
65  void write(std::ostream& out, const std::string& name) const;
66 
67  public:
69  : _secureFlag(false)
70  { }
71 
72  Cookie(const std::string& v, unsigned maxAge = 0)
73  : _value(v),
74  _secureFlag(false)
75  {
76  if (maxAge)
78  }
79  Cookie(const char* v, unsigned maxAge = 0)
80  : _value(v),
81  _secureFlag(false)
82  {
83  if (maxAge)
85  }
86 
87  const std::string& getValue() const { return _value; }
88 
89  std::string getAttr(const std::string& name) const
90  {
91  attrs_type::const_iterator it = _attrs.find(name);
92  return it == _attrs.end() ? std::string() : it->second;
93  }
94 
95  operator const std::string& () const { return _value; }
96 
97  void setAttr(const std::string& name, const std::string& value)
98  { _attrs[name] = value; }
99  bool hasAttr(const std::string& name) const
100  { return _attrs.find(name) != _attrs.end(); }
101 
102  unsigned getMaxAge() const;
103  std::string getComment() const { return getAttr(comment); }
104  std::string getDomain() const { return getAttr(domain); }
105  std::string getPath() const { return getAttr(path); }
106  std::string getVersion() const { return getAttr(version); }
107  std::string getExpires() const { return getAttr(expires); }
108  bool isSecure() const { return _secureFlag; }
109 
110  void setMaxAge(unsigned seconds);
111  void setComment(const std::string& value) { setAttr(comment, value); }
112  void setDomain(const std::string& value) { setAttr(domain, value); }
113  void setPath(const std::string& value) { setAttr(path, value); }
114  void setVersion(const std::string& value) { setAttr(version, value); }
115  void setExpires(const std::string& value) { setAttr(expires, value); }
116  void setSecure(bool f = true) { _secureFlag = f; }
117 
118  bool hasMaxAge() const { return hasAttr(maxAge); }
119  bool hasComment() const { return hasAttr(comment); }
120  bool hasDomain() const { return hasAttr(domain); }
121  bool hasPath() const { return hasAttr(path); }
122  bool hasVersion() const { return hasAttr(version); }
123  bool hasExpires() const { return hasAttr(expires); }
124 
125  };
126 
127  class Cookies
128  {
129  friend std::ostream& operator<< (std::ostream& out, const Cookies& c);
130  friend class HttpReply;
131 
132  typedef std::map<std::string, Cookie, StringLessIgnoreCase<std::string> > cookies_type;
133  cookies_type _data;
134 
135  static const Cookie _emptyCookie;
136 
137  public:
138  void set(const std::string& header);
139  void clear() { _data.clear(); }
140 
141  const Cookie& getCookie(const std::string& name) const
142  {
143  cookies_type::const_iterator it = _data.find(name);
144  return it == _data.end() ? _emptyCookie : it->second;
145  }
146 
147  void setCookie(const std::string& name, const Cookie& value)
148  { _data[name] = value; }
149 
150  void clearCookie(const std::string& name);
151  void clearCookie(const std::string& name, const Cookie& c);
152 
153  bool hasCookie(const std::string& name) const
154  { return _data.find(name) != _data.end(); }
155  bool hasCookies() const
156  { return !_data.empty(); }
157  };
158 
159  std::ostream& operator<< (std::ostream& out, const Cookies& c);
160 }
161 
162 #endif // TNT_COOKIE_H
163