summaryrefslogtreecommitdiffstats
path: root/ext/yahttp/yahttp/cookie.hpp
blob: aa5359b1a720de3de1522fba0e3c9335fdadd02c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace YaHTTP {
  /*! Implements a single cookie */
  class Cookie {
  public:
     Cookie() {
       secure = false;
       httponly = false;
       name = value = "";
       expires = DateTime();
     }; //!< Set the cookie to empty value

     Cookie(const Cookie &rhs) {
       name = rhs.name;
       value = rhs.value;
       domain = rhs.domain;
       path = rhs.path;
       secure = rhs.secure;
       httponly = rhs.httponly;
       expires = rhs.expires;
     }; //<! Copy cookie values

     Cookie& operator=(const Cookie &rhs) {
       name = rhs.name;
       value = rhs.value;
       domain = rhs.domain;
       path = rhs.path;
       secure = rhs.secure;
       httponly = rhs.httponly;
       expires = rhs.expires;
       return *this;
     }

     DateTime expires; /*!< Expiration date */
     std::string domain; /*!< Domain where cookie is valid */
     std::string path; /*!< Path where the cookie is valid */
     bool httponly; /*!< Whether the cookie is for server use only */
     bool secure; /*!< Whether the cookie is for HTTPS only */
 
     std::string name; /*!< Cookie name */
     std::string value; /*!< Cookie value */

     std::string str() const {
       std::ostringstream oss;
       oss << YaHTTP::Utility::encodeURL(name) << "=" << YaHTTP::Utility::encodeURL(value);

       if (expires.isSet) 
         oss << "; expires=" << expires.cookie_str();
       if (domain.size()>0)
         oss << "; domain=" << domain;
       if (path.size()>0)
         oss << "; path=" << path;
       if (secure)
         oss << "; secure";
       if (httponly)
         oss << "; httpOnly";
       return oss.str();
     }; //!< Stringify the cookie
  };

  /*! Implements a Cookie jar for storing multiple cookies */
  class CookieJar {
    public:
    std::map<std::string, Cookie, ASCIICINullSafeComparator> cookies;  //<! cookie container
  
    CookieJar() {}; //<! constructs empty cookie jar
    CookieJar(const CookieJar & rhs) {
      this->cookies = rhs.cookies;
    } //<! copy cookies from another cookie jar
    CookieJar& operator=(const CookieJar & rhs) = default;
  
    void clear() {
      this->cookies.clear();
    }

    void keyValuePair(const std::string &keyvalue, std::string &key, std::string &value) {
      size_t pos;
      pos = keyvalue.find("=");
      if (pos == std::string::npos) throw ParseError("Not a Key-Value pair (cookie)");
      key = std::string(keyvalue.begin(), keyvalue.begin()+pos);
      value = std::string(keyvalue.begin()+pos+1, keyvalue.end());
    } //<! key value pair parser
  
    void parseCookieHeader(const std::string &cookiestr) {
      size_t pos, npos;
      std::list<Cookie> lcookies;
      Cookie c;
      pos = 0;
      while(pos < cookiestr.size()) {
        if ((npos = cookiestr.find("; ", pos)) == std::string::npos)
          npos = cookiestr.size();
        keyValuePair(cookiestr.substr(pos, npos-pos), c.name, c.value);
        c.name = YaHTTP::Utility::decodeURL(c.name);
        c.value = YaHTTP::Utility::decodeURL(c.value);
        lcookies.push_back(c);
        pos = npos+2;
      }
      for(std::list<Cookie>::iterator i = lcookies.begin(); i != lcookies.end(); i++) {
        this->cookies[i->name] = *i;
      }
    }

    void parseSetCookieHeader(const std::string &cookiestr) {
      Cookie c;
      size_t pos,npos;
      std::string k, v;

      if ((pos = cookiestr.find("; ", 0)) == std::string::npos)
        pos = cookiestr.size();
      keyValuePair(cookiestr.substr(0, pos), c.name, c.value);
      c.name = YaHTTP::Utility::decodeURL(c.name);
      c.value = YaHTTP::Utility::decodeURL(c.value);
      if (pos < cookiestr.size()) pos+=2;

      while(pos < cookiestr.size()) {
        if ((npos = cookiestr.find("; ", pos)) == std::string::npos)
          npos = cookiestr.size();
        std::string s = cookiestr.substr(pos, npos-pos);
        if (s.find("=") != std::string::npos)
          keyValuePair(s, k, v);
        else
          k = s;
        if (k == "expires") {
          DateTime dt;
          dt.parseCookie(v);
          c.expires = dt;
        } else if (k == "domain") {
          c.domain = v;
        } else if (k == "path") {
          c.path = v;
        } else if (k == "httpOnly") {
          c.httponly = true;
        } else if (k == "secure") {
          c.secure = true;
        } else {
          // ignore crap
          break;
        }
        pos = npos+2;
      }
  
      this->cookies[c.name] = c;
    }; //<! Parse multiple cookies from header 
  };
};