changeset 1075:4d203f76b757

undo "client_body_in_file_only any" and introduce "client_body_in_file_only clean" introduce ngx_pool_delete_file() to not break a possible third-party ngx_pool_cleanup_file() usage that may lead to an removal of the useful files delete unnecessary ngx_http_finalize_request_body()
author Igor Sysoev <igor@sysoev.ru>
date Thu, 25 Jan 2007 08:45:04 +0000
parents 8164f479b362
children 2deb6d858c53
files src/core/ngx_file.c src/core/ngx_file.h src/core/ngx_palloc.c src/core/ngx_palloc.h src/http/modules/ngx_http_dav_module.c src/http/modules/perl/nginx.xs src/http/ngx_http_core_module.c src/http/ngx_http_request.h src/http/ngx_http_request_body.c
diffstat 9 files changed, 47 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -19,7 +19,7 @@ ngx_write_chain_to_temp_file(ngx_temp_fi
 
     if (tf->file.fd == NGX_INVALID_FILE) {
         rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
-                                  tf->persistent, tf->access);
+                                  tf->persistent, tf->clean, tf->access);
 
         if (rc == NGX_ERROR || rc == NGX_AGAIN) {
             return rc;
@@ -37,7 +37,7 @@ ngx_write_chain_to_temp_file(ngx_temp_fi
 
 ngx_int_t
 ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,
-    ngx_uint_t persistent, ngx_uint_t access)
+    ngx_uint_t persistent, ngx_uint_t clean, ngx_uint_t access)
 {
     ngx_err_t                 err;
     ngx_atomic_uint_t         n;
@@ -79,7 +79,7 @@ ngx_create_temp_file(ngx_file_t *file, n
 
         if (file->fd != NGX_INVALID_FILE) {
 
-            cln->handler = ngx_pool_cleanup_file;
+            cln->handler = clean ? ngx_pool_delete_file : ngx_pool_cleanup_file;
             clnf = cln->data;
 
             clnf->fd = file->fd;
--- a/src/core/ngx_file.h
+++ b/src/core/ngx_file.h
@@ -53,6 +53,7 @@ typedef struct {
 
     unsigned            log_level:8;
     unsigned            persistent:1;
+    unsigned            clean:1;
 } ngx_temp_file_t;
 
 
@@ -81,7 +82,8 @@ struct ngx_tree_ctx_s {
 
 ssize_t ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain);
 ngx_int_t ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
-    ngx_pool_t *pool, ngx_uint_t persistent,ngx_uint_t access);
+    ngx_pool_t *pool, ngx_uint_t persistent, ngx_uint_t clean,
+    ngx_uint_t access);
 void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path);
 ngx_int_t ngx_create_path(ngx_file_t *file, ngx_path_t *path);
 ngx_err_t ngx_create_full_path(u_char *dir, ngx_uint_t access);
--- a/src/core/ngx_palloc.c
+++ b/src/core/ngx_palloc.c
@@ -243,6 +243,32 @@ ngx_pool_cleanup_file(void *data)
 }
 
 
+void
+ngx_pool_delete_file(void *data)
+{
+    ngx_pool_cleanup_file_t  *c = data;
+
+    ngx_err_t  err;
+
+    ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, c->log, 0, "run cleanup: %p, fd:%d %s",
+                   c, c->fd, c->name);
+
+    if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
+        err = ngx_errno;
+
+        if (err != NGX_ENOENT) {
+            ngx_log_error(NGX_LOG_CRIT, c->log, err,
+                          ngx_delete_file_n " \"%s\" failed", c->name);
+        }
+    }
+
+    if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
+        ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
+                      ngx_close_file_n " \"%s\" failed", c->name);
+    }
+}
+
+
 #if 0
 
 static void *
--- a/src/core/ngx_palloc.h
+++ b/src/core/ngx_palloc.h
@@ -60,7 +60,6 @@ typedef struct {
 } ngx_pool_cleanup_file_t;
 
 
-
 void *ngx_alloc(size_t size, ngx_log_t *log);
 void *ngx_calloc(size_t size, ngx_log_t *log);
 
@@ -74,6 +73,7 @@ ngx_int_t ngx_pfree(ngx_pool_t *pool, vo
 
 ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);
 void ngx_pool_cleanup_file(void *data);
+void ngx_pool_delete_file(void *data);
 
 
 #endif /* _NGX_PALLOC_H_INCLUDED_ */
--- a/src/http/modules/ngx_http_dav_module.c
+++ b/src/http/modules/ngx_http_dav_module.c
@@ -135,7 +135,7 @@ ngx_http_dav_handler(ngx_http_request_t 
 
         r->request_body_in_file_only = 1;
         r->request_body_in_persistent_file = 1;
-        r->request_body_delete_incomplete_file = 1;
+        r->request_body_in_clean_file = 1;
         r->request_body_file_group_access = 1;
         r->request_body_file_log_level = 0;
 
--- a/src/http/modules/perl/nginx.xs
+++ b/src/http/modules/perl/nginx.xs
@@ -363,7 +363,7 @@ has_request_body(r, next)
 
     r->request_body_in_single_buf = 1;
     r->request_body_in_persistent_file = 1;
-    r->request_body_delete_incomplete_file = 1;
+    r->request_body_in_clean_file = 1;
 
     if (r->request_body_in_file_only) {
         r->request_body_file_log_level = 0;
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -25,7 +25,7 @@ typedef struct {
 
 #define NGX_HTTP_REQUEST_BODY_FILE_OFF    0
 #define NGX_HTTP_REQUEST_BODY_FILE_ON     1
-#define NGX_HTTP_REQUEST_BODY_FILE_ANY    2
+#define NGX_HTTP_REQUEST_BODY_FILE_CLEAN  2
 
 
 static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r,
@@ -82,7 +82,7 @@ static ngx_conf_deprecated_t  ngx_conf_d
 static ngx_conf_enum_t  ngx_http_core_request_body_in_file[] = {
     { ngx_string("off"), NGX_HTTP_REQUEST_BODY_FILE_OFF },
     { ngx_string("on"), NGX_HTTP_REQUEST_BODY_FILE_ON },
-    { ngx_string("any"), NGX_HTTP_REQUEST_BODY_FILE_ANY },
+    { ngx_string("clean"), NGX_HTTP_REQUEST_BODY_FILE_CLEAN },
     { ngx_null_string, 0 }
 };
 
@@ -890,12 +890,10 @@ ngx_http_update_location_config(ngx_http
     if (clcf->client_body_in_file_only) {
         r->request_body_in_file_only = 1;
         r->request_body_in_persistent_file = 1;
+        r->request_body_in_clean_file =
+            clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_CLEAN;
         r->request_body_file_log_level = NGX_LOG_NOTICE;
 
-        if (clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_ON) {
-            r->request_body_delete_incomplete_file = 1;
-        }
-
     } else {
         r->request_body_file_log_level = NGX_LOG_WARN;
     }
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -409,7 +409,7 @@ struct ngx_http_request_s {
     unsigned                          request_body_in_single_buf:1;
     unsigned                          request_body_in_file_only:1;
     unsigned                          request_body_in_persistent_file:1;
-    unsigned                          request_body_delete_incomplete_file:1;
+    unsigned                          request_body_in_clean_file:1;
     unsigned                          request_body_file_group_access:1;
     unsigned                          request_body_file_log_level:3;
 
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -14,7 +14,6 @@ static void ngx_http_read_client_request
 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r);
 static ngx_int_t ngx_http_write_request_body(ngx_http_request_t *r,
     ngx_chain_t *body);
-static void ngx_http_finalize_request_body(ngx_http_request_t *r, ngx_int_t rc);
 static void ngx_http_read_discarded_body_handler(ngx_http_request_t *r);
 static ngx_int_t ngx_http_read_discarded_body(ngx_http_request_t *r);
 
@@ -32,7 +31,6 @@ ngx_http_read_client_request_body(ngx_ht
 {
     size_t                     preread;
     ssize_t                    size;
-    ngx_int_t                  rc;
     ngx_buf_t                 *b;
     ngx_chain_t               *cl, **next;
     ngx_temp_file_t           *tf;
@@ -73,6 +71,7 @@ ngx_http_read_client_request_body(ngx_ht
             tf->warn = "a client request body is buffered to a temporary file";
             tf->log_level = r->request_body_file_log_level;
             tf->persistent = r->request_body_in_persistent_file;
+            tf->clean = r->request_body_in_clean_file;
 
             if (r->request_body_file_group_access) {
                 tf->access = 0660;
@@ -81,7 +80,7 @@ ngx_http_read_client_request_body(ngx_ht
             rb->temp_file = tf;
 
             if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
-                                     tf->persistent, tf->access)
+                                     tf->persistent, tf->clean, tf->access)
                 != NGX_OK)
             {
                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -168,14 +167,7 @@ ngx_http_read_client_request_body(ngx_ht
 
             r->read_event_handler = ngx_http_read_client_request_body_handler;
 
-            rc = ngx_http_do_read_client_request_body(r);
-
-            if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
-                ngx_http_finalize_request_body(r, rc);
-                return NGX_DONE;
-            }
-
-            return rc;
+            return ngx_http_do_read_client_request_body(r);
         }
 
         next = &rb->bufs->next;
@@ -235,14 +227,7 @@ ngx_http_read_client_request_body(ngx_ht
 
     r->read_event_handler = ngx_http_read_client_request_body_handler;
 
-    rc = ngx_http_do_read_client_request_body(r);
-
-    if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
-        ngx_http_finalize_request_body(r, rc);
-        return NGX_DONE;
-    }
-
-    return rc;
+    return ngx_http_do_read_client_request_body(r);
 }
 
 
@@ -253,14 +238,14 @@ ngx_http_read_client_request_body_handle
 
     if (r->connection->read->timedout) {
         r->connection->timedout = 1;
-        ngx_http_finalize_request_body(r, NGX_HTTP_REQUEST_TIME_OUT);
+        ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
         return;
     }
 
     rc = ngx_http_do_read_client_request_body(r);
 
     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
-        ngx_http_finalize_request_body(r, rc);
+        ngx_http_finalize_request(r, rc);
     }
 }
 
@@ -415,6 +400,7 @@ ngx_http_write_request_body(ngx_http_req
         tf->warn = "a client request body is buffered to a temporary file";
         tf->log_level = r->request_body_file_log_level;
         tf->persistent = r->request_body_in_persistent_file;
+        tf->clean = r->request_body_in_clean_file;
 
         if (r->request_body_file_group_access) {
             tf->access = 0660;
@@ -437,26 +423,6 @@ ngx_http_write_request_body(ngx_http_req
 }
 
 
-static void
-ngx_http_finalize_request_body(ngx_http_request_t *r, ngx_int_t rc)
-{
-    if (r->request_body->temp_file
-        && r->request_body_in_persistent_file
-        && r->request_body_delete_incomplete_file)
-    {
-        if (ngx_delete_file(r->request_body->temp_file->file.name.data)
-            == NGX_FILE_ERROR)
-        {
-            ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
-                          ngx_delete_file_n " \"%s\" failed",
-                          r->request_body->temp_file->file.name.data);
-        }
-    }
-
-    ngx_http_finalize_request(r, rc);
-}
-
-
 ngx_int_t
 ngx_http_discard_body(ngx_http_request_t *r)
 {