hewei.it vor 4 Jahren
Ursprung
Commit
8d74d77879
2 geänderte Dateien mit 116 neuen und 0 gelöschten Zeilen
  1. 76 0
      http/HttpMessage.cpp
  2. 40 0
      http/HttpMessage.h

+ 76 - 0
http/HttpMessage.cpp

@@ -8,6 +8,82 @@
 
 char HttpMessage::s_date[32] = {0};
 
+bool HttpCookie::parse(const std::string& str) {
+    std::stringstream ss;
+    ss << str;
+    std::string kv;
+    std::string::size_type pos;
+    std::string key;
+    std::string val;
+    while (std::getline(ss, kv, ';')) {
+        pos = kv.find_first_of('=');
+        if (pos != std::string::npos) {
+            key = trim(kv.substr(0, pos));
+            val = trim(kv.substr(pos+1));
+        } else {
+            key = trim(kv);
+        }
+
+        const char* pkey = key.c_str();
+        if (stricmp(pkey, "domain") == 0) {
+            domain = val;
+        }
+        else if (stricmp(pkey, "path") == 0) {
+            path = val;
+        }
+        else if (stricmp(pkey, "max-age") == 0) {
+            max_age = atoi(val.c_str());
+        }
+        else if (stricmp(pkey, "secure") == 0) {
+            secure = true;
+        }
+        else if (stricmp(pkey, "httponly") == 0) {
+            httponly = true;
+        }
+        else if (val.size() > 0) {
+            name = key;
+            value = val;
+        }
+        else {
+            hlogw("Unrecognized key '%s'", key.c_str());
+        }
+    }
+    return !name.empty() && !value.empty();
+}
+
+std::string HttpCookie::dump() const {
+    assert(!name.empty() && !value.empty());
+    std::string res;
+    res = name;
+    res += "=";
+    res += value;
+
+    if (!domain.empty()) {
+        res += "; domain=";
+        res += domain;
+    }
+
+    if (!path.empty()) {
+        res += "; path=";
+        res += path;
+    }
+
+    if (max_age > 0) {
+        res += "; max-age=";
+        res += hv::to_string(max_age);
+    }
+
+    if (secure) {
+        res += "; secure";
+    }
+
+    if (httponly) {
+        res += "; httponly";
+    }
+
+    return res;
+}
+
 #ifndef WITHOUT_HTTP_CONTENT
 // NOTE: json ignore number/string, 123/"123"
 

+ 40 - 0
http/HttpMessage.h

@@ -51,6 +51,26 @@ struct HNetAddr {
     }
 };
 
+// Cookie: sessionid=1; domain=.example.com; path=/; max-age=86400; secure; httponly
+struct HttpCookie {
+    std::string name;
+    std::string value;
+    std::string domain;
+    std::string path;
+    int         max_age;
+    bool        secure;
+    bool        httponly;
+
+    HttpCookie() {
+        max_age = 0;
+        secure = false;
+        httponly = false;
+    }
+
+    bool parse(const std::string& str);
+    std::string dump() const;
+};
+
 class HV_EXPORT HttpMessage {
 public:
     static char         s_date[32];
@@ -308,6 +328,16 @@ public:
         from = to = 0;
         return false;
     }
+
+    // Cookie
+    void SetCookie(const HttpCookie& cookie) {
+        headers["Cookie"] = cookie.dump();
+    }
+    bool GetCookie(HttpCookie& cookie) {
+        std::string str = GetHeader("Cookie");
+        if (str.empty()) return false;
+        return cookie.parse(str);
+    }
 };
 
 class HV_EXPORT HttpResponse : public HttpMessage {
@@ -345,6 +375,16 @@ public:
         from = to = total = 0;
         return false;
     }
+
+    // Set-Cookie
+    void SetCookie(const HttpCookie& cookie) {
+        headers["Set-Cookie"] = cookie.dump();
+    }
+    bool GetCookie(HttpCookie& cookie) {
+        std::string str = GetHeader("Set-Cookie");
+        if (str.empty()) return false;
+        return cookie.parse(str);
+    }
 };
 
 typedef std::shared_ptr<HttpRequest>    HttpRequestPtr;