From d447a348c2fdffb1d4f597e0f8b63f7c9447e987 Mon Sep 17 00:00:00 2001 From: "pangpang@hi-nginx.com" Date: Sat, 18 Sep 2021 13:28:33 +0800 Subject: [PATCH] bugfix: file upload --- module/ngx_http_hi_module/cpp_handler.hpp | 63 ++++++------------- module/ngx_http_hi_module/module_config.hpp | 1 + .../ngx_http_hi_module/ngx_http_hi_module.cpp | 9 +-- module/ngx_http_hi_module/utils.hpp | 4 +- 4 files changed, 26 insertions(+), 51 deletions(-) diff --git a/module/ngx_http_hi_module/cpp_handler.hpp b/module/ngx_http_hi_module/cpp_handler.hpp index ad2ea37..14e75aa 100644 --- a/module/ngx_http_hi_module/cpp_handler.hpp +++ b/module/ngx_http_hi_module/cpp_handler.hpp @@ -39,69 +39,44 @@ namespace hi { h->hash = 1; - h->key.data = (u_char*) item.first.c_str(); + h->key.data = (u_char *)item.first.c_str(); h->key.len = item.first.size(); - h->value.data = (u_char*) item.second.c_str(); + h->value.data = (u_char *)item.second.c_str(); h->value.len = item.second.size(); } } } - static ngx_str_t get_input_body(ngx_http_request_t *r) + static std::string get_input_body(ngx_http_request_t *r) { - u_char *p; - u_char *data; size_t len; ngx_buf_t *buf, *next; ngx_chain_t *cl; - ngx_str_t body = ngx_null_string; + std::string body; if (r->request_body == NULL || r->request_body->bufs == NULL) { return body; } - if (r->request_body->temp_file) - { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "temp_file: %s", r->request_body->temp_file->file.name.data); - body = r->request_body->temp_file->file.name; - return body; - } - else + cl = r->request_body->bufs; + + do { - cl = r->request_body->bufs; buf = cl->buf; + len = buf->last - buf->pos; + body.append((const char *)buf->pos, len); + cl = cl->next; + } while (cl); - if (cl->next == NULL) - { - len = buf->last - buf->pos; - p = (u_char *)ngx_pnalloc(r->pool, len + 1); - if (p == NULL) - { - return body; - } - data = p; - ngx_memcpy(p, buf->pos, len); - data[len] = 0; - } - else - { - next = cl->next->buf; - len = (buf->last - buf->pos) + (next->last - next->pos); - p = (u_char *)ngx_pnalloc(r->pool, len + 1); - data = p; - if (p == NULL) - { - return body; - } - p = ngx_cpymem(p, buf->pos, buf->last - buf->pos); - ngx_memcpy(p, next->pos, next->last - next->pos); - data[len] = 0; - } - } + if (r->request_body->temp_file) + { + hi::file_mmap fm; + auto ret = fm.get((char *)r->request_body->temp_file->file.name.data); + body.append(ret.first, ret.second.st_size); - body.len = len; - body.data = data; + return body; + } return body; } @@ -116,5 +91,3 @@ namespace hi } } // namespace hi - - diff --git a/module/ngx_http_hi_module/module_config.hpp b/module/ngx_http_hi_module/module_config.hpp index efcf819..52c5888 100644 --- a/module/ngx_http_hi_module/module_config.hpp +++ b/module/ngx_http_hi_module/module_config.hpp @@ -57,6 +57,7 @@ extern "C" #include "lib/MPFDParser/Parser.h" #include "lib/leveldb/db.h" #include "lib/shared_memory.hpp" +#include "lib/file_mmap.hpp" #include "cache_t.hpp" #include "application_t.hpp" diff --git a/module/ngx_http_hi_module/ngx_http_hi_module.cpp b/module/ngx_http_hi_module/ngx_http_hi_module.cpp index c04c8f7..424afd2 100644 --- a/module/ngx_http_hi_module/ngx_http_hi_module.cpp +++ b/module/ngx_http_hi_module/ngx_http_hi_module.cpp @@ -16,7 +16,6 @@ #include "java_handler.hpp" #endif - #include "cpp_handler.hpp" static ngx_int_t ngx_http_hi_init(ngx_conf_t *cf); @@ -286,7 +285,6 @@ static void *ngx_http_hi_create_loc_conf(ngx_conf_t *cf) conf->cache_expires = NGX_CONF_UNSET; conf->cache_method = NGX_CONF_UNSET_UINT; - conf->kvdb_size = NGX_CONF_UNSET_UINT; conf->kvdb_expires = NGX_CONF_UNSET; @@ -586,13 +584,16 @@ static ngx_int_t ngx_http_hi_normal_handler(ngx_http_request_t *r) } if (r->headers_in.content_length_n > 0) { - ngx_str_t body = hi::get_input_body(r); + std::string input_body = std::move(hi::get_input_body(r)); + ngx_str_t body = ngx_null_string; + body.data = (u_char *)input_body.c_str(); + body.len = input_body.size(); if (ngx_strncasecmp(r->headers_in.content_type->value.data, (u_char *)FORM_MULTIPART_TYPE, FORM_MULTIPART_TYPE_LEN) == 0) { ngx_http_core_loc_conf_t *clcf = (ngx_http_core_loc_conf_t *)ngx_http_get_module_loc_conf(r, ngx_http_core_module); std::string upload_err_msg; - if (!hi::upload(ngx_request, &body, clcf, r, TEMP_DIRECTORY, upload_err_msg)) + if (!hi::upload(ngx_request, input_body, clcf, r, TEMP_DIRECTORY, upload_err_msg)) { ngx_response.content = std::move(upload_err_msg); ngx_response.status = 500; diff --git a/module/ngx_http_hi_module/utils.hpp b/module/ngx_http_hi_module/utils.hpp index 2407f47..e9ec319 100644 --- a/module/ngx_http_hi_module/utils.hpp +++ b/module/ngx_http_hi_module/utils.hpp @@ -220,7 +220,7 @@ namespace hi return std::string((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); } - static bool upload(hi::request &req, ngx_str_t *body, ngx_http_core_loc_conf_t *clcf, ngx_http_request_t *r, const std::string &temp_dir, std::string &err_msg) + static bool upload(hi::request &req, const std::string& body, ngx_http_core_loc_conf_t *clcf, ngx_http_request_t *r, const std::string &temp_dir, std::string &err_msg) { bool result = false; try @@ -232,7 +232,7 @@ namespace hi POSTParser->SetUploadedFilesStorage(MPFD::Parser::StoreUploadedFilesInFilesystem); POSTParser->SetMaxCollectedDataLength(clcf->client_max_body_size); POSTParser->SetContentType((char *)r->headers_in.content_type->value.data); - POSTParser->AcceptSomeData((char *)body->data, body->len); + POSTParser->AcceptSomeData(body.c_str(), body.size()); auto fields = POSTParser->GetFieldsMap(); for (auto &item : fields)