Explorar el Código

Impl #172: defaultLargeFileHandler

ithewei hace 3 años
padre
commit
63d57555c5
Se han modificado 3 ficheros con 17 adiciones y 7 borrados
  1. 14 6
      http/server/HttpHandler.cpp
  2. 1 1
      http/server/HttpServer.cpp
  3. 2 0
      unittest/sizeof_test.cpp

+ 14 - 6
http/server/HttpHandler.cpp

@@ -1,4 +1,4 @@
-#include "HttpHandler.h"
+#include "HttpHandler.h"
 
 #include "hbase.h"
 #include "herr.h"
@@ -350,7 +350,7 @@ int HttpHandler::defaultLargeFileHandler() {
     if (service->limit_rate == 0) {
         // forbidden to send large file
         resp->content_length = 0;
-        resp->status_code = HTTP_STATUS_NOT_ACCEPTABLE;
+        resp->status_code = HTTP_STATUS_FORBIDDEN;
     } else {
         size_t bufsize = 40960; // 40K
         file->buf.resize(bufsize);
@@ -368,7 +368,7 @@ int HttpHandler::defaultLargeFileHandler() {
             // limit_rate=40MB/s interval_m=1: 40KB/ms = 40MB/s = 320Mbps
             if (interval_ms == 0) interval_ms = 1;
             // printf("limit_rate=%dKB/s interval_ms=%d\n", service->limit_rate, interval_ms);
-            hv::setInterval(interval_ms, std::bind(&HttpHandler::sendFile, this));
+            file->timer = setInterval(interval_ms, std::bind(&HttpHandler::sendFile, this));
         }
     }
     writer->EndHeaders();
@@ -509,6 +509,7 @@ return_header:
 int HttpHandler::openFile(const char* filepath) {
     closeFile();
     file = new LargeFile;
+    file->timer = INVALID_TIMER_ID;
     return file->open(filepath, "rb");
 }
 
@@ -519,6 +520,7 @@ bool HttpHandler::isFileOpened() {
 int HttpHandler::sendFile() {
     if (!writer || !writer->isWriteComplete() ||
         !isFileOpened() ||
+        file->buf.len == 0 ||
         resp->content_length == 0) {
         return -1;
     }
@@ -528,9 +530,14 @@ int HttpHandler::sendFile() {
     if (nread <= 0) {
         hloge("read file error!");
         writer->close(true);
-        return 0;
+        return nread;
+    }
+    int nwrite = writer->WriteBody(file->buf.base, nread);
+    if (nwrite < 0) {
+        // disconnectd
+        writer->close(true);
+        return nwrite;
     }
-    writer->WriteBody(file->buf.base, nread);
     resp->content_length -= nread;
     if (resp->content_length == 0) {
         writer->End();
@@ -542,7 +549,8 @@ int HttpHandler::sendFile() {
 void HttpHandler::closeFile() {
     if (file) {
         if (file->timer != INVALID_TIMER_ID) {
-            hv::killTimer(file->timer);
+            killTimer(file->timer);
+            file->timer = INVALID_TIMER_ID;
         }
         delete file;
         file = NULL;

+ 1 - 1
http/server/HttpServer.cpp

@@ -177,6 +177,7 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
         }
         // onopen
         handler->WebSocketOnOpen();
+        return;
     }
 
     if (status_code && !keepalive) {
@@ -268,7 +269,6 @@ static void loop_thread(void* userdata) {
         filecache->stat_interval = service->file_cache_stat_interval;
         filecache->expired_time = service->file_cache_expired_time;
         if (filecache->expired_time > 0) {
-            filecache->expired_time = service->file_cache_expired_time;
             // NOTE: add timer to remove expired file cache
             htimer_t* timer = htimer_add(hloop, [](htimer_t* timer) {
                 FileCache* filecache = (FileCache*)hevent_userdata(timer);

+ 2 - 0
unittest/sizeof_test.cpp

@@ -16,6 +16,7 @@
 #include "Http1Parser.h"
 #include "HttpContext.h"
 #include "HttpServer.h"
+#include "HttpHandler.h"
 
 #include "WebSocketChannel.h"
 #include "WebSocketParser.h"
@@ -49,6 +50,7 @@ int main() {
     printf("sizeof(class Http1Parser)=%lu\n", sizeof(Http1Parser));
     printf("sizeof(class HttpContext)=%lu\n", sizeof(HttpContext));
     printf("sizeof(class HttpServer)=%lu\n", sizeof(HttpServer));
+    printf("sizeof(class HttpHandler)=%lu\n", sizeof(HttpHandler));
     // websocket
     printf("sizeof(class WebSocketChannel)=%lu\n", sizeof(WebSocketChannel));
     printf("sizeof(class WebSocketParser)=%lu\n", sizeof(WebSocketParser));