Prechádzať zdrojové kódy

add timer to update date every 1s

hewei.it 4 rokov pred
rodič
commit
3ba6647df1
3 zmenil súbory, kde vykonal 12 pridanie a 1 odobranie
  1. 7 1
      http/HttpMessage.cpp
  2. 1 0
      http/HttpMessage.h
  3. 4 0
      http/server/HttpServer.cpp

+ 7 - 1
http/HttpMessage.cpp

@@ -6,6 +6,8 @@
 #include "hlog.h"
 #include "http_parser.h" // for http_parser_url
 
+char HttpMessage::s_date[32] = {0};
+
 #ifndef WITHOUT_HTTP_CONTENT
 // NOTE: json ignore number/string, 123/"123"
 
@@ -426,7 +428,11 @@ std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
     snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
     str += c_str;
     if (is_dump_headers) {
-        headers["Date"] = gmtime_fmt(time(NULL), c_str);
+        if (*s_date) {
+            headers["Date"] = s_date;
+        } else {
+            headers["Date"] = gmtime_fmt(time(NULL), c_str);
+        }
         DumpHeaders(str);
     }
     str += "\r\n";

+ 1 - 0
http/HttpMessage.h

@@ -53,6 +53,7 @@ struct HNetAddr {
 
 class HV_EXPORT HttpMessage {
 public:
+    static char         s_date[32];
     int                 type;
     unsigned short      http_major;
     unsigned short      http_minor;

+ 4 - 0
http/server/HttpServer.cpp

@@ -340,6 +340,10 @@ static void loop_thread(void* userdata) {
             FileCache* filecache = default_filecache();
             filecache->RemoveExpiredFileCache();
         }, DEFAULT_FILE_EXPIRED_TIME * 1000);
+        // NOTE: add timer to update date every 1s
+        htimer_add(hloop, [](htimer_t* timer) {
+            gmtime_fmt(hloop_now(hevent_loop(timer)), HttpMessage::s_date);
+        }, 1000);
     }
     privdata->loops.push_back(loop);
     privdata->mutex_.unlock();