00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef restcgi_cookie_h
00023 #define restcgi_cookie_h
00024 #include "apidefs.h"
00025 #include "httpsyn.h"
00026 #include "utils.h"
00027 #include <uripp/utils.h>
00028 #include <uripp/path.h>
00029 #include <string>
00030 #include <map>
00031 #include <iostream>
00032 #ifdef _WIN32
00033 #pragma warning (disable: 4251)
00034 #endif
00035 namespace restcgi {
00038 struct RESTCGI_API cookie_attrs {
00039 typedef uripp::path uri_path_type;
00040 cookie_attrs();
00041
00042
00043
00044
00045
00046
00047
00048 const std::string& domain() const {return domain_;}
00051 void domain(const std::string& v);
00057 const uri_path_type& path() const {return path_;}
00058 void path(const uri_path_type& v);
00059
00060
00061
00062 size_t max_age() const {return max_age_;}
00064 void max_age(size_t v);
00072 bool discard() const {return discard_;}
00074 void discard(bool v);
00077 bool secure() const {return secure_;}
00078 void secure(bool v);
00079
00080
00081
00082
00083 bool http_only() const {return http_only_;}
00084 void http_only(bool v);
00085
00086 const http_word& comment() const {return comment_;}
00087 void comment(const http_word& v) {comment_ = v;}
00088
00089 size_t version() const {return version_;}
00090 void version(size_t v);
00091
00092
00093
00094
00095 bool set(const http_token& name, const http_word& value);
00097 std::ostream& operator <<(std::ostream& os) const;
00098 static const char SEPARATOR_CSTR[3];
00099 static size_t VERSION;
00100 private:
00101 std::string domain_;
00102 uri_path_type path_;
00103 size_t max_age_;
00104 bool discard_;
00105 bool secure_;
00106 bool http_only_;
00107 http_word comment_;
00108 size_t version_;
00109 };
00111 inline std::ostream& operator <<(std::ostream& os, const cookie_attrs& v) {return v.operator <<(os);}
00135 class RESTCGI_API cookie {
00136 public:
00137 typedef restcgi::cookie_attrs attrs_type;
00138 cookie();
00139
00140 cookie(const http_token& name, const http_word& value, const attrs_type& attrs = attrs_type());
00143 cookie(const std::string& name, const std::string& value, const attrs_type& attrs = attrs_type());
00144 bool is_null() const {return name_.empty();}
00145 const std::string& name() const {return name_;}
00146
00147
00148 template <typename T> T value() const {
00149 T v;
00150 try {uripp::convert(value_.string(), v);}
00151 catch (const std::exception& e) {throw_bad_request(e.what());}
00152 return v;
00153 }
00154 const attrs_type& attrs() const {return attrs_;}
00155 const std::string& id() const {return id_;}
00156
00157 std::ostream& operator <<(std::ostream& os) const;
00158 private:
00159 void throw_bad_request(const char* what) const;
00160 static std::string make_id(const std::string& name, const attrs_type& attrs);
00161 std::string name_;
00162 http_word value_;
00163 attrs_type attrs_;
00164 std::string id_;
00165 };
00167 inline std::ostream& operator <<(std::ostream& os, const cookie& v) {return v.operator <<(os);}
00181 class RESTCGI_API cookies {
00182 public:
00183 typedef std::multimap<std::string, cookie> multimap_type;
00184 typedef multimap_type::const_iterator const_iterator;
00185 cookies(bool is_request);
00186 bool empty() const {return multimap_.empty();}
00187 void clear();
00188
00189 const_iterator find(const std::string& name) const;
00191 bool insert(const cookie& c);
00209 bool insert(const std::string& v);
00211 std::ostream& operator <<(std::ostream& os) const;
00212 const_iterator begin() const {return multimap_.begin();}
00213 const_iterator end() const {return multimap_.end();}
00214
00217 struct observer {virtual ~observer() {} virtual void on_updated(const cookies& v) = 0;};
00218 void attach(observer* o);
00219 void detach(observer* o);
00220 static const char SEPARATOR_CSTR[3];
00221 private:
00222 static bool parse_version(std::string::const_iterator& first, std::string::const_iterator last, size_t& v);
00223 static bool parse_value(std::string::const_iterator& first, std::string::const_iterator last, http_word& value);
00224 static bool parse_attributes(std::string::const_iterator& first, std::string::const_iterator last, cookie_attrs& attrs);
00225 multimap_type multimap_;
00226 bool is_request_;
00227 observer* observer_;
00228 };
00230 inline std::ostream& operator <<(std::ostream& os, const cookies& v) {return v.operator <<(os);}
00231 }
00232 #endif