From e6464833e0af413fe5d93efe43febfc2edd9af3e Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 4 Jan 2023 10:58:49 +0100 Subject: [PATCH 1/3] fixes build with msvc (missing ftruncate/strncasecmp) --- ngx_http_upload_module.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ngx_http_upload_module.c b/ngx_http_upload_module.c index 1f05b25..9556842 100644 --- a/ngx_http_upload_module.c +++ b/ngx_http_upload_module.c @@ -18,7 +18,9 @@ typedef ngx_md5_t MD5_CTX; #define MD5Update ngx_md5_update #define MD5Final ngx_md5_final +#ifndef MD5_DIGEST_LENGTH #define MD5_DIGEST_LENGTH 16 +#endif #include @@ -45,6 +47,19 @@ typedef ngx_md5_t MD5_CTX; #endif +#ifdef _MSC_VER + #define strncasecmp _strnicmp + + static ngx_inline void + ftruncate(ngx_fd_t fd, off_t file_pos) { + LARGE_INTEGER li; + li.QuadPart = file_pos; + SetFilePointerEx(fd, li, &li, FILE_BEGIN); + SetEndOfFile(fd); + } +#endif + + #define MULTIPART_FORM_DATA_STRING "multipart/form-data" #define BOUNDARY_STRING "boundary=" #define CONTENT_DISPOSITION_STRING "Content-Disposition:" From 5cc8972c62dbfba797e5bf32c33961945bd47d0b Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 4 Jan 2023 11:02:07 +0100 Subject: [PATCH 2/3] named location starts with @ only (don't segfault on empty location) --- ngx_http_upload_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ngx_http_upload_module.c b/ngx_http_upload_module.c index 9556842..8fdbbe7 100644 --- a/ngx_http_upload_module.c +++ b/ngx_http_upload_module.c @@ -1355,7 +1355,7 @@ static ngx_int_t ngx_http_upload_body_handler(ngx_http_request_t *r) { /* {{{ */ r->main->count--; #endif - if(uri.len != 0 && uri.data[0] == '/') { + if(uri.len == 0 || uri.data[0] != '@') { rc = ngx_http_internal_redirect(r, &uri, &args); } else{ From 3b3d44fd859369c83a8f3808f9f85d2fdb3e7c47 Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 4 Jan 2023 11:09:28 +0100 Subject: [PATCH 3/3] more msvc related fixes, suppress warnings, several small nginx compat tweaks --- ngx_http_upload_module.c | 49 ++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/ngx_http_upload_module.c b/ngx_http_upload_module.c index 8fdbbe7..091f988 100644 --- a/ngx_http_upload_module.c +++ b/ngx_http_upload_module.c @@ -1632,7 +1632,7 @@ static void ngx_http_upload_finish_handler(ngx_http_upload_ctx_t *u) { /* {{{ */ if(u->is_file) { ucln = u->cln->data; - ucln->fd = -1; + ucln->fd = (ngx_fd_t)NGX_INVALID_FILE; ngx_close_file(u->output_file.fd); @@ -1744,7 +1744,7 @@ static void ngx_http_upload_abort_handler(ngx_http_upload_ctx_t *u) { /* {{{ */ * cleanup record as aborted. */ ucln = u->cln->data; - ucln->fd = -1; + ucln->fd = (ngx_fd_t)NGX_INVALID_FILE; ucln->aborted = 1; ngx_close_file(u->output_file.fd); @@ -1786,7 +1786,7 @@ static ngx_int_t ngx_http_upload_flush_output_buffer(ngx_http_upload_ctx_t *u, u return NGX_OK; if(u->output_file.offset + (off_t)len > u->content_range_n.end + 1) - len = u->content_range_n.end - u->output_file.offset + 1; + len = (size_t)(u->content_range_n.end - u->output_file.offset + 1); } if(u->md5_ctx) @@ -1871,11 +1871,10 @@ ngx_http_upload_append_str(ngx_http_upload_ctx_t *u, ngx_buf_t *b, ngx_chain_t * if(u->chain == NULL) { u->chain = cl; - u->last = cl; }else{ u->last->next = cl; - u->last = cl; } + u->last = cl; u->output_body_len += s->len; } /* }}} */ @@ -1884,13 +1883,15 @@ static ngx_int_t /* {{{ ngx_http_upload_append_field */ ngx_http_upload_append_field(ngx_http_upload_ctx_t *u, ngx_str_t *name, ngx_str_t *value) { ngx_http_upload_loc_conf_t *ulcf = ngx_http_get_module_loc_conf(u->request, ngx_http_upload_module); - ngx_str_t boundary = { u->first_part ? u->boundary.len - 2 : u->boundary.len, - u->first_part ? u->boundary.data + 2 : u->boundary.data }; + ngx_str_t boundary = u->boundary; ngx_buf_t *b; ngx_chain_t *cl; if(name->len > 0) { + + if (u->first_part) { boundary.len -= 2; boundary.data += 2; } + if(ulcf->max_output_body_len != 0) { if(u->output_body_len + boundary.len + ngx_upload_field_part1.len + name->len + ngx_upload_field_part2.len + value->len > ulcf->max_output_body_len) @@ -2108,9 +2109,8 @@ static ngx_int_t /* {{{ ngx_http_upload_merge_ranges */ ngx_http_upload_merge_ranges(ngx_http_upload_ctx_t *u, ngx_http_upload_range_t *range_n) { ngx_file_t *state_file = &u->state_file; ngx_http_upload_merger_state_t ms; - off_t remaining; + off_t remaining, st_size; ssize_t rc; - __attribute__((__unused__)) int result; ngx_buf_t in_buf; ngx_buf_t out_buf; ngx_http_upload_loc_conf_t *ulcf = ngx_http_get_module_loc_conf(u->request, ngx_http_upload_module); @@ -2128,6 +2128,7 @@ ngx_http_upload_merge_ranges(ngx_http_upload_ctx_t *u, ngx_http_upload_range_t * ngx_lock_fd(state_file->fd); ngx_fd_info(state_file->fd, &state_file->info); + st_size = ngx_file_size(&state_file->info); state_file->offset = 0; state_file->log = u->log; @@ -2158,11 +2159,11 @@ ngx_http_upload_merge_ranges(ngx_http_upload_ctx_t *u, ngx_http_upload_range_t * in_buf.file_pos = state_file->offset; in_buf.pos = in_buf.last = in_buf.start; - if(state_file->offset < state_file->info.st_size) { - remaining = state_file->info.st_size - state_file->offset > in_buf.end - in_buf.start - ? in_buf.end - in_buf.start : state_file->info.st_size - state_file->offset; + if(state_file->offset < st_size) { + remaining = st_size - (state_file->offset > in_buf.end - in_buf.start + ? in_buf.end - in_buf.start : st_size - state_file->offset); - rc = ngx_read_file(state_file, in_buf.pos, remaining, state_file->offset); + rc = ngx_read_file(state_file, in_buf.pos, (size_t)remaining, state_file->offset); if(rc < 0 || rc != remaining) { goto failed; @@ -2171,7 +2172,7 @@ ngx_http_upload_merge_ranges(ngx_http_upload_ctx_t *u, ngx_http_upload_range_t * in_buf.last = in_buf.pos + rc; } - in_buf.last_buf = state_file->offset == state_file->info.st_size ? 1 : 0; + in_buf.last_buf = state_file->offset == st_size ? 1 : 0; if(out_buf.pos != out_buf.last) { rc = ngx_write_file(state_file, out_buf.pos, out_buf.last - out_buf.pos, out_buf.file_pos); @@ -2191,7 +2192,7 @@ ngx_http_upload_merge_ranges(ngx_http_upload_ctx_t *u, ngx_http_upload_range_t * rc = NGX_ERROR; goto failed; } - } while(state_file->offset < state_file->info.st_size); + } while(state_file->offset < st_size); if(out_buf.pos != out_buf.last) { rc = ngx_write_file(state_file, out_buf.pos, out_buf.last - out_buf.pos, out_buf.file_pos); @@ -2203,8 +2204,8 @@ ngx_http_upload_merge_ranges(ngx_http_upload_ctx_t *u, ngx_http_upload_range_t * out_buf.file_pos += out_buf.last - out_buf.pos; } - if(out_buf.file_pos < state_file->info.st_size) { - result = ftruncate(state_file->fd, out_buf.file_pos); + if(out_buf.file_pos < st_size) { + (void)ftruncate(state_file->fd, out_buf.file_pos); } rc = ms.complete_ranges ? NGX_OK : NGX_AGAIN; @@ -2925,7 +2926,7 @@ ngx_http_upload_cleanup(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - *s = status; + *s = (uint16_t)status; } } @@ -3236,8 +3237,8 @@ ngx_http_read_upload_client_request_body(ngx_http_request_t *r) { size = clcf->client_body_buffer_size; size += size >> 2; - if (rb->rest < (ssize_t) size) { - size = rb->rest; + if (rb->rest < size) { + size = (ssize_t)rb->rest; if (r->request_body_in_single_buf) { size += preread; @@ -3266,7 +3267,7 @@ ngx_http_read_upload_client_request_body(ngx_http_request_t *r) { cl->next = NULL; if (b && r->request_body_in_single_buf) { - size = b->last - b->pos; + size = (ssize_t)(b->last - b->pos); ngx_memcpy(rb->buf->pos, b->pos, size); rb->buf->last += size; @@ -3384,7 +3385,7 @@ ngx_http_do_read_upload_client_request_body(ngx_http_request_t *r) } if (u->limit_rate) { - limit = u->limit_rate * (ngx_time() - r->start_sec + 1) - u->received; + limit = u->limit_rate * (ssize_t)(ngx_time() - r->start_sec + 1) - u->received; if (limit < 0) { c->read->delayed = 1; @@ -4240,7 +4241,7 @@ ngx_upload_cleanup_handler(void *data) u_char do_cleanup = 0; if(!cln->aborted) { - if(cln->fd >= 0) { + if(cln->fd != NGX_INVALID_FILE) { if (ngx_close_file(cln->fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ALERT, cln->log, ngx_errno, ngx_close_file_n " \"%s\" failed", cln->filename); @@ -4258,7 +4259,7 @@ ngx_upload_cleanup_handler(void *data) } if(do_cleanup) { - if(ngx_delete_file(cln->filename) == NGX_FILE_ERROR) { + if(ngx_delete_file(cln->filename) == NGX_FILE_ERROR && ngx_errno != NGX_ENOENT) { ngx_log_error(NGX_LOG_ERR, cln->log, ngx_errno , "failed to remove destination file \"%s\" after http status %l" , cln->filename