Преглед на файлове

Add HFile remove rename

ithewei преди 3 години
родител
ревизия
abf04b2eac
променени са 4 файла, в които са добавени 36 реда и са изтрити 12 реда
  1. 10 0
      cpputil/hfile.h
  2. 13 9
      examples/httpd/handler.cpp
  3. 2 2
      examples/wget.cpp
  4. 11 1
      http/server/HttpContext.h

+ 10 - 0
cpputil/hfile.h

@@ -34,6 +34,16 @@ public:
         return fp != NULL;
     }
 
+    int remove() {
+        close();
+        return ::remove(filepath);
+    }
+
+    int rename(const char* newpath) {
+        close();
+        return ::rename(filepath, newpath);
+    }
+
     size_t read(void* ptr, size_t len) {
         return fread(ptr, 1, len, fp);
     }

+ 13 - 9
examples/httpd/handler.cpp

@@ -215,44 +215,48 @@ int Handler::upload(const HttpContextPtr& ctx) {
 }
 
 int Handler::recvLargeFile(const HttpContextPtr& ctx, http_parser_state state, const char* data, size_t size) {
+    // printf("recvLargeFile state=%d\n", (int)state);
+    int status_code = HTTP_STATUS_UNFINISHED;
+    HFile* file = (HFile*)ctx->userdata;
     switch (state) {
     case HP_HEADERS_COMPLETE:
         {
-            ctx->setContentType(APPLICATION_JSON);
             std::string save_path = "html/uploads/";
             std::string filename = ctx->param("filename", "unnamed.txt");
             std::string filepath = save_path + filename;
-            HFile* file = new HFile;
+            file = new HFile;
             if (file->open(filepath.c_str(), "wb") != 0) {
-                return response_status(ctx, HTTP_STATUS_INTERNAL_SERVER_ERROR);
+                ctx->close();
+                return HTTP_STATUS_INTERNAL_SERVER_ERROR;
             }
             ctx->userdata = file;
         }
         break;
     case HP_BODY:
         {
-            HFile* file = (HFile*)ctx->userdata;
             if (file && data && size) {
                 if (file->write(data, size) != size) {
-                    return response_status(ctx, HTTP_STATUS_INTERNAL_SERVER_ERROR);
+                    ctx->close();
+                    return HTTP_STATUS_INTERNAL_SERVER_ERROR;
                 }
             }
         }
         break;
     case HP_MESSAGE_COMPLETE:
         {
-            HFile* file = (HFile*)ctx->userdata;
+            status_code = HTTP_STATUS_OK;
+            ctx->setContentType(APPLICATION_JSON);
+            response_status(ctx, status_code);
             if (file) {
                 delete file;
                 ctx->userdata = NULL;
             }
-            return response_status(ctx, HTTP_STATUS_OK);
         }
         break;
     case HP_ERROR:
         {
-            HFile* file = (HFile*)ctx->userdata;
             if (file) {
+                file->remove();
                 delete file;
                 ctx->userdata = NULL;
             }
@@ -261,7 +265,7 @@ int Handler::recvLargeFile(const HttpContextPtr& ctx, http_parser_state state, c
     default:
         break;
     }
-    return HTTP_STATUS_UNFINISHED;
+    return status_code;
 }
 
 int Handler::sendLargeFile(const HttpContextPtr& ctx) {

+ 2 - 2
examples/wget.cpp

@@ -112,14 +112,14 @@ static int wget(const char* url, const char* filepath, wget_progress_cb progress
 
 success:
     file.close();
-    ret = rename(filepath_download.c_str(), filepath);
+    ret = file.rename(filepath);
     if (ret != 0) {
         fprintf(stderr, "mv %s => %s failed: %s:%d\n", filepath_download.c_str(), filepath, strerror(ret), ret);
     }
     return ret;
 error:
     file.close();
-    // remove(filepath_download.c_str());
+    // file.remove();
     return ret;
 }
 

+ 11 - 1
http/server/HttpContext.h

@@ -15,6 +15,11 @@ struct HV_EXPORT HttpContext {
     HttpResponseWriterPtr   writer;
     void*                   userdata;
 
+    HttpContext() {
+        service = NULL;
+        userdata = NULL;
+    }
+
     // HttpRequest aliases
     // return request->xxx
     std::string ip() {
@@ -144,7 +149,9 @@ struct HV_EXPORT HttpContext {
 
     // response->sendXxx
     int send() {
-        writer->End();
+        if (writer) {
+            writer->End();
+        }
         return response->status_code;
     }
 
@@ -185,6 +192,9 @@ struct HV_EXPORT HttpContext {
     }
 #endif
 
+    int close() {
+        return writer ? writer->close(true) : -1;
+    }
 };
 
 } // end namespace hv