ithewei 5 vuotta sitten
vanhempi
commit
a7ccdece98
4 muutettua tiedostoa jossa 33 lisäystä ja 4 poistoa
  1. 1 0
      .travis.yml
  2. 5 3
      base/hthread.h
  3. 6 1
      examples/httpd/handler.h
  4. 21 0
      http/HttpMessage.h

+ 1 - 0
.travis.yml

@@ -20,6 +20,7 @@ jobs:
         - make libhv examples unittest
 
     - os: windows
+      compiler: msvc
       env: COVERALLS=no
       script:
         - mkdir win64

+ 5 - 3
base/hthread.h

@@ -105,6 +105,7 @@ public:
     HThread() {
         status = STOP;
         status_changed = false;
+        dotask_cnt = 0;
         sleep_policy = YIELD;
         sleep_ms = 0;
     }
@@ -124,12 +125,11 @@ public:
 
     virtual int start() {
         if (status == STOP) {
-            setStatus(RUNNING);
-            dotask_cnt = 0;
-
             thread = std::thread([this] {
                 if (!doPrepare()) return;
+                setStatus(RUNNING);
                 run();
+                setStatus(STOP);
                 if (!doFinish()) return;
             });
         }
@@ -139,6 +139,8 @@ public:
     virtual int stop() {
         if (status != STOP) {
             setStatus(STOP);
+        }
+        if (thread.joinable()) {
             thread.join();  // wait thread exit
         }
         return 0;

+ 6 - 1
examples/httpd/handler.h

@@ -96,7 +96,8 @@ public:
         resp->form["int"] = 123;
         resp->form["float"] = 3.14;
         resp->form["float"] = "hello";
-        // resp->form["file"] = FormData("test.jpg");
+        // resp->form["file"] = FormData(NULL, "test.jpg");
+        // resp->UploadFormFile("file", "test.jpg");
         return 200;
     }
 
@@ -166,10 +167,14 @@ public:
     }
 
     static int upload(HttpRequest* req, HttpResponse* resp) {
+        // return resp->SaveFormFile("file", "html/uploads/test.jpg");
         if (req->content_type != MULTIPART_FORM_DATA) {
             return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }
         FormData file = req->form["file"];
+        if (file.content.empty()) {
+            return response_status(resp, HTTP_STATUS_BAD_REQUEST);
+        }
         string filepath("html/uploads/");
         filepath += file.filename;
         FILE* fp = fopen(filepath.c_str(), "w");

+ 21 - 0
http/HttpMessage.h

@@ -221,6 +221,27 @@ public:
         file.readall(body);
         return 200;
     }
+
+    void UploadFormFile(const char* name, const char* filepath) {
+        content_type = MULTIPART_FORM_DATA;
+        form[name] = FormData(NULL, filepath);
+    }
+
+    int SaveFormFile(const char* name, const char* filepath) {
+        if (content_type != MULTIPART_FORM_DATA) {
+            return HTTP_STATUS_BAD_REQUEST;
+        }
+        FormData formdata = form[name];
+        if (formdata.content.empty()) {
+            return HTTP_STATUS_BAD_REQUEST;
+        }
+        HFile file;
+        if (file.open(filepath, "w") != 0) {
+            return HTTP_STATUS_INTERNAL_SERVER_ERROR;
+        }
+        file.write(formdata.content.data(), formdata.content.size());
+        return 200;
+    }
 };
 
 #define DEFAULT_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"