Explorar o código

Change cookie interfaces

ithewei %!s(int64=3) %!d(string=hai) anos
pai
achega
4bb419fc2f
Modificáronse 3 ficheiros con 19 adicións e 20 borrados
  1. 1 0
      http/HttpMessage.cpp
  2. 10 20
      http/HttpMessage.h
  3. 8 0
      http/server/HttpContext.h

+ 1 - 0
http/HttpMessage.cpp

@@ -11,6 +11,7 @@ using namespace hv;
 
 http_headers DefaultHeaders;
 http_body    NoBody;
+HttpCookie   NoCookie;
 char HttpMessage::s_date[32] = {0};
 
 bool HttpCookie::parse(const std::string& str) {

+ 10 - 20
http/HttpMessage.h

@@ -94,6 +94,7 @@ typedef std::string                                             http_body;
 
 HV_EXPORT extern http_headers DefaultHeaders;
 HV_EXPORT extern http_body    NoBody;
+HV_EXPORT extern HttpCookie   NoCookie;
 
 class HV_EXPORT HttpMessage {
 public:
@@ -343,6 +344,15 @@ public:
         cookies.push_back(cookie);
     }
 
+    const HttpCookie& GetCookie(const std::string& name) {
+        for (auto iter = cookies.begin(); iter != cookies.end(); ++iter) {
+            if (iter->name == name) {
+                return *iter;
+            }
+        }
+        return NoCookie;
+    }
+
     int String(const std::string& str) {
         content_type = TEXT_PLAIN;
         body = str;
@@ -501,16 +511,6 @@ 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 {
@@ -549,16 +549,6 @@ 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;

+ 8 - 0
http/server/HttpContext.h

@@ -52,6 +52,10 @@ struct HV_EXPORT HttpContext {
         return request->GetParam(key, defvalue);
     }
 
+    const HttpCookie& cookie(const char* name) {
+        return request->GetCookie(name);
+    }
+
     int length() {
         return request->ContentLength();
     }
@@ -125,6 +129,10 @@ struct HV_EXPORT HttpContext {
         }
     }
 
+    void setCookie(const HttpCookie& cookie) {
+        response->AddCookie(cookie);
+    }
+
     void setBody(const std::string& body) {
         response->body = body;
     }