Skip to content

Commit

Permalink
bugfix: file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Sep 18, 2021
1 parent 85c8995 commit d447a34
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
63 changes: 18 additions & 45 deletions module/ngx_http_hi_module/cpp_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -116,5 +91,3 @@ namespace hi
}

} // namespace hi


1 change: 1 addition & 0 deletions module/ngx_http_hi_module/module_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions module/ngx_http_hi_module/ngx_http_hi_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions module/ngx_http_hi_module/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace hi
return std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
}

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
Expand All @@ -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)
Expand Down

0 comments on commit d447a34

Please sign in to comment.