Klaus Demo nginx / cd5b99a
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() Igor Sysoev 15 years ago
9 changed file(s) with 47 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
1818
1919 if (tf->file.fd == NGX_INVALID_FILE) {
2020 rc = ngx_create_temp_file(&tf->file, tf->path, tf->pool,
21 tf->persistent, tf->access);
21 tf->persistent, tf->clean, tf->access);
2222
2323 if (rc == NGX_ERROR || rc == NGX_AGAIN) {
2424 return rc;
3636
3737 ngx_int_t
3838 ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, ngx_pool_t *pool,
39 ngx_uint_t persistent, ngx_uint_t access)
39 ngx_uint_t persistent, ngx_uint_t clean, ngx_uint_t access)
4040 {
4141 ngx_err_t err;
4242 ngx_atomic_uint_t n;
7878
7979 if (file->fd != NGX_INVALID_FILE) {
8080
81 cln->handler = ngx_pool_cleanup_file;
81 cln->handler = clean ? ngx_pool_delete_file : ngx_pool_cleanup_file;
8282 clnf = cln->data;
8383
8484 clnf->fd = file->fd;
5252
5353 unsigned log_level:8;
5454 unsigned persistent:1;
55 unsigned clean:1;
5556 } ngx_temp_file_t;
5657
5758
8081
8182 ssize_t ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain);
8283 ngx_int_t ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
83 ngx_pool_t *pool, ngx_uint_t persistent,ngx_uint_t access);
84 ngx_pool_t *pool, ngx_uint_t persistent, ngx_uint_t clean,
85 ngx_uint_t access);
8486 void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path);
8587 ngx_int_t ngx_create_path(ngx_file_t *file, ngx_path_t *path);
8688 ngx_err_t ngx_create_full_path(u_char *dir, ngx_uint_t access);
242242 }
243243
244244
245 void
246 ngx_pool_delete_file(void *data)
247 {
248 ngx_pool_cleanup_file_t *c = data;
249
250 ngx_err_t err;
251
252 ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, c->log, 0, "run cleanup: %p, fd:%d %s",
253 c, c->fd, c->name);
254
255 if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
256 err = ngx_errno;
257
258 if (err != NGX_ENOENT) {
259 ngx_log_error(NGX_LOG_CRIT, c->log, err,
260 ngx_delete_file_n " \"%s\" failed", c->name);
261 }
262 }
263
264 if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
265 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
266 ngx_close_file_n " \"%s\" failed", c->name);
267 }
268 }
269
270
245271 #if 0
246272
247273 static void *
5959 } ngx_pool_cleanup_file_t;
6060
6161
62
6362 void *ngx_alloc(size_t size, ngx_log_t *log);
6463 void *ngx_calloc(size_t size, ngx_log_t *log);
6564
7372
7473 ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);
7574 void ngx_pool_cleanup_file(void *data);
75 void ngx_pool_delete_file(void *data);
7676
7777
7878 #endif /* _NGX_PALLOC_H_INCLUDED_ */
134134
135135 r->request_body_in_file_only = 1;
136136 r->request_body_in_persistent_file = 1;
137 r->request_body_delete_incomplete_file = 1;
137 r->request_body_in_clean_file = 1;
138138 r->request_body_file_group_access = 1;
139139 r->request_body_file_log_level = 0;
140140
362362
363363 r->request_body_in_single_buf = 1;
364364 r->request_body_in_persistent_file = 1;
365 r->request_body_delete_incomplete_file = 1;
365 r->request_body_in_clean_file = 1;
366366
367367 if (r->request_body_in_file_only) {
368368 r->request_body_file_log_level = 0;
2424
2525 #define NGX_HTTP_REQUEST_BODY_FILE_OFF 0
2626 #define NGX_HTTP_REQUEST_BODY_FILE_ON 1
27 #define NGX_HTTP_REQUEST_BODY_FILE_ANY 2
27 #define NGX_HTTP_REQUEST_BODY_FILE_CLEAN 2
2828
2929
3030 static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r,
8181 static ngx_conf_enum_t ngx_http_core_request_body_in_file[] = {
8282 { ngx_string("off"), NGX_HTTP_REQUEST_BODY_FILE_OFF },
8383 { ngx_string("on"), NGX_HTTP_REQUEST_BODY_FILE_ON },
84 { ngx_string("any"), NGX_HTTP_REQUEST_BODY_FILE_ANY },
84 { ngx_string("clean"), NGX_HTTP_REQUEST_BODY_FILE_CLEAN },
8585 { ngx_null_string, 0 }
8686 };
8787
889889 if (clcf->client_body_in_file_only) {
890890 r->request_body_in_file_only = 1;
891891 r->request_body_in_persistent_file = 1;
892 r->request_body_in_clean_file =
893 clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_CLEAN;
892894 r->request_body_file_log_level = NGX_LOG_NOTICE;
893
894 if (clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_ON) {
895 r->request_body_delete_incomplete_file = 1;
896 }
897895
898896 } else {
899897 r->request_body_file_log_level = NGX_LOG_WARN;
408408 unsigned request_body_in_single_buf:1;
409409 unsigned request_body_in_file_only:1;
410410 unsigned request_body_in_persistent_file:1;
411 unsigned request_body_delete_incomplete_file:1;
411 unsigned request_body_in_clean_file:1;
412412 unsigned request_body_file_group_access:1;
413413 unsigned request_body_file_log_level:3;
414414
1313 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r);
1414 static ngx_int_t ngx_http_write_request_body(ngx_http_request_t *r,
1515 ngx_chain_t *body);
16 static void ngx_http_finalize_request_body(ngx_http_request_t *r, ngx_int_t rc);
1716 static void ngx_http_read_discarded_body_handler(ngx_http_request_t *r);
1817 static ngx_int_t ngx_http_read_discarded_body(ngx_http_request_t *r);
1918
3130 {
3231 size_t preread;
3332 ssize_t size;
34 ngx_int_t rc;
3533 ngx_buf_t *b;
3634 ngx_chain_t *cl, **next;
3735 ngx_temp_file_t *tf;
7270 tf->warn = "a client request body is buffered to a temporary file";
7371 tf->log_level = r->request_body_file_log_level;
7472 tf->persistent = r->request_body_in_persistent_file;
73 tf->clean = r->request_body_in_clean_file;
7574
7675 if (r->request_body_file_group_access) {
7776 tf->access = 0660;
8079 rb->temp_file = tf;
8180
8281 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
83 tf->persistent, tf->access)
82 tf->persistent, tf->clean, tf->access)
8483 != NGX_OK)
8584 {
8685 return NGX_HTTP_INTERNAL_SERVER_ERROR;
167166
168167 r->read_event_handler = ngx_http_read_client_request_body_handler;
169168
170 rc = ngx_http_do_read_client_request_body(r);
171
172 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
173 ngx_http_finalize_request_body(r, rc);
174 return NGX_DONE;
175 }
176
177 return rc;
169 return ngx_http_do_read_client_request_body(r);
178170 }
179171
180172 next = &rb->bufs->next;
234226
235227 r->read_event_handler = ngx_http_read_client_request_body_handler;
236228
237 rc = ngx_http_do_read_client_request_body(r);
238
239 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
240 ngx_http_finalize_request_body(r, rc);
241 return NGX_DONE;
242 }
243
244 return rc;
229 return ngx_http_do_read_client_request_body(r);
245230 }
246231
247232
252237
253238 if (r->connection->read->timedout) {
254239 r->connection->timedout = 1;
255 ngx_http_finalize_request_body(r, NGX_HTTP_REQUEST_TIME_OUT);
240 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
256241 return;
257242 }
258243
259244 rc = ngx_http_do_read_client_request_body(r);
260245
261246 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
262 ngx_http_finalize_request_body(r, rc);
247 ngx_http_finalize_request(r, rc);
263248 }
264249 }
265250
414399 tf->warn = "a client request body is buffered to a temporary file";
415400 tf->log_level = r->request_body_file_log_level;
416401 tf->persistent = r->request_body_in_persistent_file;
402 tf->clean = r->request_body_in_clean_file;
417403
418404 if (r->request_body_file_group_access) {
419405 tf->access = 0660;
433419 rb->temp_file->offset += n;
434420
435421 return NGX_OK;
436 }
437
438
439 static void
440 ngx_http_finalize_request_body(ngx_http_request_t *r, ngx_int_t rc)
441 {
442 if (r->request_body->temp_file
443 && r->request_body_in_persistent_file
444 && r->request_body_delete_incomplete_file)
445 {
446 if (ngx_delete_file(r->request_body->temp_file->file.name.data)
447 == NGX_FILE_ERROR)
448 {
449 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
450 ngx_delete_file_n " \"%s\" failed",
451 r->request_body->temp_file->file.name.data);
452 }
453 }
454
455 ngx_http_finalize_request(r, rc);
456422 }
457423
458424