Yet Another HTTP Library: /home/cmouse/projects/libyahttp/yahttp/cookie.hpp Source File

Yet Another HTTP Library yahttp

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cookie.hpp
Go to the documentation of this file.
1 namespace YaHTTP {
3 class Cookie {
4 public:
5 Cookie() {
6 secure = false;
7 httponly = false;
8 name = value = "";
9 };
10
11 Cookie(const Cookie &rhs) {
12 domain = rhs.domain;
13 path = rhs.path;
14 secure = rhs.secure;
15 httponly = rhs.httponly;
16 name = rhs.name;
17 value = rhs.value;
18 }; //<! Copy cookie values
19
20 DateTime expires;
21 std::string domain;
22 std::string path;
23 bool httponly;
24 bool secure;
26 std::string name;
27 std::string value;
29 std::string str() const {
30 std::ostringstream oss;
31 oss << YaHTTP::Utility::encodeURL(name) << "=" << YaHTTP::Utility::encodeURL(value);
32 if (expires.isSet)
33 oss << "; expires=" << expires.cookie_str();
34 if (domain.size()>0)
35 oss << "; domain=" << domain;
36 if (path.size()>0)
37 oss << "; path=" << path;
38 if (secure)
39 oss << "; secure";
40 if (httponly)
41 oss << "; httpOnly";
42 return oss.str();
43 };
44 };
45
47 class CookieJar {
48 public:
49 std::map<std::string, Cookie, ASCIICINullSafeComparator> cookies; //<! cookie container
50
51 CookieJar() {}; //<! constructs empty cookie jar
52 CookieJar(const CookieJar & rhs) {
53 this->cookies = rhs.cookies;
54 } //<! copy cookies from another cookie jar
55
56 void clear() {
57 this->cookies.clear();
58 }
59
60 void keyValuePair(const std::string &keyvalue, std::string &key, std::string &value) {
61 size_t pos;
62 pos = keyvalue.find("=");
63 if (pos == std::string::npos) throw "Not a Key-Value pair (cookie)";
64 key = std::string(keyvalue.begin(), keyvalue.begin()+pos);
65 value = std::string(keyvalue.begin()+pos+1, keyvalue.end());
66 } //<! key value pair parser
67
68 void parseCookieHeader(const std::string &cookiestr) {
69 std::list<Cookie> cookies;
70 int cstate = 0; //cookiestate
71 size_t pos,npos;
72 pos = 0;
73 cstate = 0;
74 while(pos < cookiestr.size()) {
75 if (cookiestr.compare(pos, 7, "expires") ==0 ||
76 cookiestr.compare(pos, 6, "domain") ==0 ||
77 cookiestr.compare(pos, 4, "path") ==0) {
78 cstate = 1;
79 // get the date
80 std::string key, value, s;
81 npos = cookiestr.find("; ", pos);
82 if (npos == std::string::npos) {
83 // last value
84 s = std::string(cookiestr.begin() + pos + 1, cookiestr.end());
85 pos = cookiestr.size();
86 } else {
87 s = std::string(cookiestr.begin() + pos + 1, cookiestr.begin() + npos - 1);
88 pos = npos+2;
89 }
90 keyValuePair(s, key, value);
91 if (s == "expires") {
92 DateTime dt;
93 dt.parseCookie(value);
94 for(std::list<Cookie>::iterator i = cookies.begin(); i != cookies.end(); i++)
95 i->expires = dt;
96 } else if (s == "domain") {
97 for(std::list<Cookie>::iterator i = cookies.begin(); i != cookies.end(); i++)
98 i->domain = value;
99 } else if (s == "path") {
100 for(std::list<Cookie>::iterator i = cookies.begin(); i != cookies.end(); i++)
101 i->path = value;
102 }
103 } else if (cookiestr.compare(pos, 8, "httpOnly")==0) {
104 cstate = 1;
105 for(std::list<Cookie>::iterator i = cookies.begin(); i != cookies.end(); i++)
106 i->httponly = true;
107 } else if (cookiestr.compare(pos, 6, "secure") ==0) {
108 cstate = 1;
109 for(std::list<Cookie>::iterator i = cookies.begin(); i != cookies.end(); i++)
110 i->secure = true;
111 } else if (cstate == 0) { // expect cookie
112 Cookie c;
113 std::string s;
114 npos = cookiestr.find("; ", pos);
115 if (npos == std::string::npos) {
116 // last value
117 s = std::string(cookiestr.begin() + pos, cookiestr.end());
118 pos = cookiestr.size();
119 } else {
120 s = std::string(cookiestr.begin() + pos, cookiestr.begin() + npos);
121 pos = npos+2;
122 }
123 keyValuePair(s, c.name, c.value);
124 c.name = YaHTTP::Utility::decodeURL(c.name);
125 c.value = YaHTTP::Utility::decodeURL(c.value);
126 cookies.push_back(c);
127 } else if (cstate == 1) {
128 // ignore crap
129 break;
130 }
131 }
132
133 // store cookies
134 for(std::list<Cookie>::iterator i = cookies.begin(); i != cookies.end(); i++) {
135 this->cookies[i->name] = *i;
136 }
137 }; //<! Parse multiple cookies from header
138 };
139 };
YaHTTP::Cookie::Cookie
Cookie(const Cookie &rhs)
Set the cookie to empty value.
Definition: cookie.hpp:11
YaHTTP::Cookie::httponly
bool httponly
Definition: cookie.hpp:23
YaHTTP::CookieJar::cookies
std::map< std::string, Cookie, ASCIICINullSafeComparator > cookies
Definition: cookie.hpp:49
YaHTTP::Cookie::name
std::string name
Definition: cookie.hpp:26
YaHTTP::Utility::encodeURL
static std::string encodeURL(const std::string &component, bool asUrl=true)
Definition: utility.hpp:233
YaHTTP::CookieJar::CookieJar
CookieJar()
Definition: cookie.hpp:51
YaHTTP::Cookie::value
std::string value
Definition: cookie.hpp:27
YaHTTP::CookieJar::keyValuePair
void keyValuePair(const std::string &keyvalue, std::string &key, std::string &value)
Definition: cookie.hpp:60
YaHTTP::Cookie::Cookie
Cookie()
Definition: cookie.hpp:5
YaHTTP::Utility::decodeURL
static std::string decodeURL(const std::string &component)
Definition: utility.hpp:204
YaHTTP::CookieJar::clear
void clear()
Definition: cookie.hpp:56
YaHTTP::DateTime
Definition: utility.hpp:25
YaHTTP::CookieJar
Definition: cookie.hpp:47
YaHTTP::Cookie::secure
bool secure
Definition: cookie.hpp:24
YaHTTP::DateTime::parseCookie
void parseCookie(const std::string &cookie_date)
Definition: utility.hpp:172
YaHTTP::Cookie::path
std::string path
Definition: cookie.hpp:22
YaHTTP::DateTime::isSet
bool isSet
Definition: utility.hpp:27
YaHTTP::Cookie::expires
DateTime expires
Definition: cookie.hpp:18
YaHTTP::CookieJar::CookieJar
CookieJar(const CookieJar &rhs)
Definition: cookie.hpp:52
YaHTTP::Cookie::domain
std::string domain
Definition: cookie.hpp:21
YaHTTP::Cookie::str
std::string str() const
Definition: cookie.hpp:29
YaHTTP::Cookie
Definition: cookie.hpp:3
YaHTTP::DateTime::cookie_str
std::string cookie_str() const
Definition: utility.hpp:137
YaHTTP::CookieJar::parseCookieHeader
void parseCookieHeader(const std::string &cookiestr)
Definition: cookie.hpp:68