hewei.it 4 năm trước cách đây
mục cha
commit
fc9bd0fc68
4 tập tin đã thay đổi với 21 bổ sung8 xóa
  1. 10 1
      http/HttpMessage.cpp
  2. 9 2
      http/server/FileCache.cpp
  3. 1 2
      http/server/FileCache.h
  4. 1 3
      http/server/HttpHandler.cpp

+ 10 - 1
http/HttpMessage.cpp

@@ -4,6 +4,7 @@
 
 #include "htime.h"
 #include "hlog.h"
+#include "hurl.h"
 #include "http_parser.h" // for http_parser_url
 
 char HttpMessage::s_date[32] = {0};
@@ -506,7 +507,15 @@ void HttpRequest::ParseUrl() {
     port = parser.port ? parser.port : strcmp(scheme.c_str(), "https") ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT;
     // path
     if (parser.field_set & (1<<UF_PATH)) {
-        path = url.c_str() + parser.field_data[UF_PATH].off;
+        const char* sp = url.c_str() + parser.field_data[UF_PATH].off;
+        char* ep = (char*)(sp + parser.field_data[UF_PATH].len);
+        char ev = *ep;
+        *ep = '\0';
+        path = url_unescape(sp);
+        if (ev != '\0') {
+            *ep = ev;
+            path += ep;
+        }
     }
     // query
     if (parser.field_set & (1<<UF_QUERY)) {

+ 9 - 2
http/server/FileCache.cpp

@@ -70,7 +70,14 @@ file_cache_ptr FileCache::Open(const char* filepath, bool need_read, void* ctx)
             }
             const char* suffix = strrchr(filepath, '.');
             if (suffix) {
-                fc->content_type = http_content_type_str_by_suffix(++suffix);
+                http_content_type content_type = http_content_type_enum_by_suffix(suffix+1);
+                if (content_type == TEXT_HTML) {
+                    fc->content_type = "text/html; charset=utf-8";
+                } else if (content_type == TEXT_PLAIN) {
+                    fc->content_type = "text/plain; charset=utf-8";
+                } else {
+                    fc->content_type = http_content_type_str_by_suffix(suffix+1);
+                }
             }
         }
         else if (S_ISDIR(fc->st.st_mode)) {
@@ -79,7 +86,7 @@ file_cache_ptr FileCache::Open(const char* filepath, bool need_read, void* ctx)
             make_index_of_page(filepath, page, (const char*)ctx);
             fc->resize_buf(page.size());
             memcpy(fc->filebuf.base, page.c_str(), page.size());
-            fc->content_type = http_content_type_str(TEXT_HTML);
+            fc->content_type = "text/html; charset=utf-8";
         }
         gmtime_fmt(fc->st.st_mtime, fc->last_modified);
         snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);

+ 1 - 2
http/server/FileCache.h

@@ -23,11 +23,10 @@ typedef struct file_cache_s {
     hbuf_t      httpbuf;
     char        last_modified[64];
     char        etag[64];
-    const char* content_type;
+    std::string content_type;
 
     file_cache_s() {
         stat_cnt = 0;
-        content_type = NULL;
     }
 
     bool is_modified() {

+ 1 - 3
http/server/HttpHandler.cpp

@@ -118,9 +118,7 @@ make_http_status_page:
     if (fc) {
         pResp->content = fc->filebuf.base;
         pResp->content_length = fc->filebuf.len;
-        if (fc->content_type && *fc->content_type != '\0') {
-            pResp->headers["Content-Type"] = fc->content_type;
-        }
+        pResp->headers["Content-Type"] = fc->content_type;
         pResp->headers["Last-Modified"] = fc->last_modified;
         pResp->headers["Etag"] = fc->etag;
     }