Selaa lähdekoodia

move some code from header file to source file

ithewei 2 vuotta sitten
vanhempi
commit
b2fb3b1419
4 muutettua tiedostoa jossa 158 lisäystä ja 118 poistoa
  1. 131 0
      http/HttpMessage.cpp
  2. 23 114
      http/HttpMessage.h
  3. 1 1
      http/server/HttpContext.h
  4. 3 3
      http/server/HttpResponseWriter.h

+ 131 - 0
http/HttpMessage.cpp

@@ -138,6 +138,35 @@ std::string HttpCookie::dump() const {
     return res;
 }
 
+HttpMessage::HttpMessage() {
+    type = HTTP_BOTH;
+    Init();
+}
+
+HttpMessage::~HttpMessage() {
+
+}
+
+void HttpMessage::Init() {
+    http_major = 1;
+    http_minor = 1;
+    content = NULL;
+    content_length = 0;
+    content_type = CONTENT_TYPE_NONE;
+}
+
+void HttpMessage::Reset() {
+    Init();
+    headers.clear();
+    cookies.clear();
+    body.clear();
+#ifndef WITHOUT_HTTP_CONTENT
+    json.clear();
+    form.clear();
+    kv.clear();
+#endif
+}
+
 #ifndef WITHOUT_HTTP_CONTENT
 // NOTE: json ignore number/string, 123/"123"
 
@@ -403,6 +432,37 @@ bool HttpMessage::IsKeepAlive() {
     return keepalive;
 }
 
+
+// headers
+void HttpMessage::SetHeader(const char* key, const std::string& value) {
+    headers[key] = value;
+}
+std::string HttpMessage::GetHeader(const char* key, const std::string& defvalue) {
+    auto iter = headers.find(key);
+    return iter == headers.end() ? defvalue : iter->second;
+}
+
+// cookies
+void HttpMessage::AddCookie(const HttpCookie& cookie) {
+    cookies.push_back(cookie);
+}
+const HttpCookie& HttpMessage::GetCookie(const std::string& name) {
+    for (auto iter = cookies.begin(); iter != cookies.end(); ++iter) {
+        if (iter->name == name) {
+            return *iter;
+        }
+    }
+    return NoCookie;
+}
+
+// body
+void HttpMessage::SetBody(const std::string& body) {
+    this->body = body;
+}
+const std::string& HttpMessage::Body() {
+    return this->body;
+}
+
 void HttpMessage::DumpHeaders(std::string& str) {
     FillContentType();
     FillContentLength();
@@ -528,6 +588,35 @@ std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
     return str;
 }
 
+
+HttpRequest::HttpRequest() : HttpMessage() {
+    type = HTTP_REQUEST;
+    Init();
+}
+
+void HttpRequest::Init() {
+    headers["User-Agent"] = DEFAULT_HTTP_USER_AGENT;
+    headers["Accept"] = "*/*";
+    method = HTTP_GET;
+    scheme = "http";
+    host = "127.0.0.1";
+    port = DEFAULT_HTTP_PORT;
+    path = "/";
+    timeout = DEFAULT_HTTP_TIMEOUT;
+    connect_timeout = DEFAULT_HTTP_CONNECT_TIMEOUT;
+    retry_count = DEFAULT_HTTP_FAIL_RETRY_COUNT;
+    retry_delay = DEFAULT_HTTP_FAIL_RETRY_DELAY;
+    redirect = 1;
+    proxy = 0;
+}
+
+void HttpRequest::Reset() {
+    HttpMessage::Reset();
+    Init();
+    url.clear();
+    query_params.clear();
+}
+
 void HttpRequest::DumpUrl() {
     std::string str;
     if (url.size() != 0 &&
@@ -656,6 +745,34 @@ std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
     return str;
 }
 
+void HttpRequest::SetRange(long from, long to) {
+    SetHeader("Range", hv::asprintf("bytes=%ld-%ld", from, to));
+}
+
+bool HttpRequest::GetRange(long& from, long& to) {
+    auto iter = headers.find("Range");
+    if (iter != headers.end()) {
+        sscanf(iter->second.c_str(), "bytes=%ld-%ld", &from, &to);
+        return true;
+    }
+    from = to = 0;
+    return false;
+}
+
+HttpResponse::HttpResponse() : HttpMessage() {
+    type = HTTP_RESPONSE;
+    Init();
+}
+
+void HttpResponse::Init() {
+    status_code = HTTP_STATUS_OK;
+}
+
+void HttpResponse::Reset() {
+    HttpMessage::Reset();
+    Init();
+}
+
 std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
     char c_str[256] = {0};
     std::string str;
@@ -679,3 +796,17 @@ std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
     }
     return str;
 }
+
+void HttpResponse::SetRange(long from, long to, long total) {
+    SetHeader("Content-Range", hv::asprintf("bytes %ld-%ld/%ld", from, to, total));
+}
+
+bool HttpResponse::GetRange(long& from, long& to, long& total) {
+    auto iter = headers.find("Content-Range");
+    if (iter != headers.end()) {
+        sscanf(iter->second.c_str(), "bytes %ld-%ld/%ld", &from, &to, &total);
+        return true;
+    }
+    from = to = total = 0;
+    return false;
+}

+ 23 - 114
http/HttpMessage.h

@@ -273,32 +273,11 @@ public:
     }
 #endif
 
-    HttpMessage() {
-        type = HTTP_BOTH;
-        Init();
-    }
-
-    virtual ~HttpMessage() {}
-
-    void Init() {
-        http_major = 1;
-        http_minor = 1;
-        content = NULL;
-        content_length = 0;
-        content_type = CONTENT_TYPE_NONE;
-    }
+    HttpMessage();
+    virtual ~HttpMessage();
 
-    virtual void Reset() {
-        Init();
-        headers.clear();
-        cookies.clear();
-        body.clear();
-#ifndef WITHOUT_HTTP_CONTENT
-        json.clear();
-        form.clear();
-        kv.clear();
-#endif
-    }
+    void Init();
+    virtual void Reset();
 
     // structured-content -> content_type <-> headers["Content-Type"]
     void FillContentType();
@@ -309,21 +288,16 @@ public:
     bool IsKeepAlive();
 
     // headers
-    void SetHeader(const char* key, const std::string& value) {
-        headers[key] = value;
-    }
-    std::string GetHeader(const char* key, const std::string& defvalue = hv::empty_string) {
-        auto iter = headers.find(key);
-        return iter == headers.end() ? defvalue : iter->second;
-    }
+    void SetHeader(const char* key, const std::string& value);
+    std::string GetHeader(const char* key, const std::string& defvalue = hv::empty_string);
+
+    // cookies
+    void AddCookie(const HttpCookie& cookie);
+    const HttpCookie& GetCookie(const std::string& name);
 
     // body
-    void SetBody(const std::string& body) {
-        this->body = body;
-    }
-    const std::string& Body() {
-        return this->body;
-    }
+    void SetBody(const std::string& body);
+    const std::string& Body();
 
     // headers -> string
     void DumpHeaders(std::string& str);
@@ -372,19 +346,6 @@ public:
         }
     }
 
-    void AddCookie(const HttpCookie& cookie) {
-        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;
@@ -452,33 +413,10 @@ public:
     unsigned            redirect: 1;
     unsigned            proxy   : 1;
 
-    HttpRequest() : HttpMessage() {
-        type = HTTP_REQUEST;
-        Init();
-    }
-
-    void Init() {
-        headers["User-Agent"] = DEFAULT_HTTP_USER_AGENT;
-        headers["Accept"] = "*/*";
-        method = HTTP_GET;
-        scheme = "http";
-        host = "127.0.0.1";
-        port = DEFAULT_HTTP_PORT;
-        path = "/";
-        timeout = DEFAULT_HTTP_TIMEOUT;
-        connect_timeout = DEFAULT_HTTP_CONNECT_TIMEOUT;
-        retry_count = DEFAULT_HTTP_FAIL_RETRY_COUNT;
-        retry_delay = DEFAULT_HTTP_FAIL_RETRY_DELAY;
-        redirect = 1;
-        proxy = 0;
-    }
+    HttpRequest();
 
-    virtual void Reset() {
-        HttpMessage::Reset();
-        Init();
-        url.clear();
-        query_params.clear();
-    }
+    void Init();
+    virtual void Reset();
 
     virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
 
@@ -547,18 +485,8 @@ public:
     }
 
     // Range: bytes=0-4095
-    void SetRange(long from = 0, long to = -1) {
-        headers["Range"] = hv::asprintf("bytes=%ld-%ld", from, to);
-    }
-    bool GetRange(long& from, long& to) {
-        auto iter = headers.find("Range");
-        if (iter != headers.end()) {
-            sscanf(iter->second.c_str(), "bytes=%ld-%ld", &from, &to);
-            return true;
-        }
-        from = to = 0;
-        return false;
-    }
+    void SetRange(long from = 0, long to = -1);
+    bool GetRange(long& from, long& to);
 };
 
 class HV_EXPORT HttpResponse : public HttpMessage {
@@ -568,39 +496,20 @@ public:
         return http_status_str(status_code);
     }
 
-    HttpResponse() : HttpMessage() {
-        type = HTTP_RESPONSE;
-        Init();
-    }
+    HttpResponse();
 
-    void Init() {
-        status_code = HTTP_STATUS_OK;
-    }
-
-    virtual void Reset() {
-        HttpMessage::Reset();
-        Init();
-    }
+    void Init();
+    virtual void Reset();
 
     virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
 
     // Content-Range: bytes 0-4095/10240000
-    void SetRange(long from, long to, long total) {
-        headers["Content-Range"] = hv::asprintf("bytes %ld-%ld/%ld", from, to, total);
-    }
-    bool GetRange(long& from, long& to, long& total) {
-        auto iter = headers.find("Content-Range");
-        if (iter != headers.end()) {
-            sscanf(iter->second.c_str(), "bytes %ld-%ld/%ld", &from, &to, &total);
-            return true;
-        }
-        from = to = total = 0;
-        return false;
-    }
+    void SetRange(long from, long to, long total);
+    bool GetRange(long& from, long& to, long& total);
 
     int Redirect(const std::string& location, http_status status = HTTP_STATUS_FOUND) {
         status_code = status;
-        headers["Location"] = location;
+        SetHeader("Location", location);
         return status_code;
     }
 };

+ 1 - 1
http/server/HttpContext.h

@@ -137,7 +137,7 @@ struct HV_EXPORT HttpContext {
     }
 
     void setHeader(const char* key, const std::string& value) {
-        response->headers[key] = value;
+        response->SetHeader(key, value);
         if (stricmp(key, "Content-Type") == 0) {
             setContentType(value.c_str());
         }

+ 3 - 3
http/server/HttpResponseWriter.h

@@ -42,13 +42,13 @@ public:
     }
 
     int WriteHeader(const char* key, const char* value) {
-        response->headers[key] = value;
+        response->SetHeader(key, value);
         return 0;
     }
 
     template<typename T>
     int WriteHeader(const char* key, T num) {
-        response->headers[key] = hv::to_string(num);
+        response->SetHeader(key, hv::to_string(num));
         return 0;
     }
 
@@ -60,7 +60,7 @@ public:
     int EndHeaders(const char* key = NULL, const char* value = NULL) {
         if (state != SEND_BEGIN) return -1;
         if (key && value) {
-            response->headers[key] = value;
+            response->SetHeader(key, value);
         }
         std::string headers = response->Dump(true, false);
         state = SEND_HEADER;