2003-04-25 22:43:13 +08:00
|
|
|
|
2003-04-28 23:06:39 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2003-10-27 16:53:49 +08:00
|
|
|
#include <ngx_event.h>
|
2003-04-28 23:06:39 +08:00
|
|
|
#include <ngx_http.h>
|
2003-04-25 22:43:13 +08:00
|
|
|
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
static void ngx_http_read_client_request_body_handler(ngx_event_t *rev);
|
2004-03-19 13:25:53 +08:00
|
|
|
static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_int_t ngx_http_read_client_request_body(ngx_http_request_t *r,
|
|
|
|
size_t request_buffer_size)
|
2003-04-25 22:43:13 +08:00
|
|
|
{
|
2003-10-27 16:53:49 +08:00
|
|
|
ssize_t size;
|
|
|
|
ngx_hunk_t *h;
|
|
|
|
ngx_chain_t *cl;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
size = r->header_in->last - r->header_in->pos;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (size) {
|
2003-11-04 01:33:31 +08:00
|
|
|
ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
|
|
|
|
h->start = h->pos = r->header_in->pos;
|
|
|
|
h->end = h->last = r->header_in->last;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-11-04 01:33:31 +08:00
|
|
|
ngx_alloc_link_and_set_hunk(r->request_hunks, h, r->pool, NGX_ERROR);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (size >= r->headers_in.content_length_n) {
|
|
|
|
r->header_in->pos += r->headers_in.content_length_n;
|
2003-10-28 05:01:00 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
return NGX_OK;
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|
2003-10-28 05:01:00 +08:00
|
|
|
|
|
|
|
r->header_in->pos = r->header_in->last;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
r->request_body_len = r->headers_in.content_length_n - size;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (r->request_body_len < request_buffer_size + (request_buffer_size >> 2))
|
|
|
|
{
|
|
|
|
size = r->request_body_len;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
} else {
|
|
|
|
size = request_buffer_size;
|
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
ngx_test_null(r->request_body_hunk, ngx_create_temp_hunk(r->pool, size),
|
2003-11-04 01:33:31 +08:00
|
|
|
NGX_ERROR);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
ngx_alloc_link_and_set_hunk(cl, r->request_body_hunk, r->pool,
|
2003-11-04 01:33:31 +08:00
|
|
|
NGX_ERROR);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (r->request_hunks) {
|
|
|
|
r->request_hunks->next = cl;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
} else {
|
|
|
|
r->request_hunks = cl;
|
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
r->connection->read->event_handler =
|
|
|
|
ngx_http_read_client_request_body_handler;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
return ngx_http_do_read_client_request_body(r);
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
static void ngx_http_read_client_request_body_handler(ngx_event_t *rev)
|
2004-03-19 13:25:53 +08:00
|
|
|
{
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_http_request_t *r;
|
|
|
|
|
|
|
|
c = rev->data;
|
|
|
|
r = c->data;
|
|
|
|
|
|
|
|
rc = ngx_http_do_read_client_request_body(r);
|
|
|
|
|
|
|
|
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
|
|
|
ngx_http_finalize_request(r, rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r)
|
2003-04-25 22:43:13 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
size_t size;
|
|
|
|
ssize_t n;
|
2003-10-28 05:01:00 +08:00
|
|
|
ngx_hunk_t *h;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_http_core_loc_conf_t *clcf;
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
c = r->connection;
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
|
|
"http read client request body");
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
if (r->request_body_hunk->last == r->request_body_hunk->end) {
|
|
|
|
n = ngx_write_chain_to_temp_file(r->temp_file,
|
2003-10-27 16:53:49 +08:00
|
|
|
r->request_hunks->next ? r->request_hunks->next:
|
|
|
|
r->request_hunks);
|
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
/* TODO: n == 0 or not complete and level event */
|
|
|
|
|
|
|
|
if (n == NGX_ERROR) {
|
2004-03-19 13:25:53 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2004-03-17 05:26:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
r->temp_file->offset += n;
|
|
|
|
|
|
|
|
r->request_body_hunk->pos = r->request_body_hunk->start;
|
|
|
|
r->request_body_hunk->last = r->request_body_hunk->start;
|
2003-10-28 05:01:00 +08:00
|
|
|
}
|
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
size = r->request_body_hunk->end - r->request_body_hunk->last;
|
2003-11-03 06:56:18 +08:00
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
if (size > r->request_body_len) {
|
|
|
|
size = r->request_body_len;
|
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
n = ngx_recv(c, r->request_body_hunk->last, size);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
|
|
"http client request body recv " SIZE_T_FMT, n);
|
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
if (n == NGX_AGAIN) {
|
|
|
|
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
2004-03-19 13:25:53 +08:00
|
|
|
ngx_add_timer(c->read, clcf->client_body_timeout);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2004-03-17 05:26:01 +08:00
|
|
|
}
|
2003-04-28 23:06:39 +08:00
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
return NGX_AGAIN;
|
2004-03-17 05:26:01 +08:00
|
|
|
}
|
2003-10-28 05:01:00 +08:00
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
if (n == 0) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
|
|
"client closed prematurely connection");
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
if (n == 0 || n == NGX_ERROR) {
|
2004-03-23 14:01:52 +08:00
|
|
|
r->closed = 1;
|
2004-03-19 13:25:53 +08:00
|
|
|
return NGX_HTTP_BAD_REQUEST;
|
2004-03-17 05:26:01 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
r->request_body_hunk->last += n;
|
|
|
|
r->request_body_len -= n;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-23 14:01:52 +08:00
|
|
|
if (r->request_body_len == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-03-17 05:26:01 +08:00
|
|
|
if (r->request_body_hunk->last < r->request_body_hunk->end) {
|
|
|
|
break;
|
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
|
|
"http client request body left " SIZE_T_FMT,
|
|
|
|
r->request_body_len);
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (r->request_body_len) {
|
2004-03-19 13:25:53 +08:00
|
|
|
return NGX_AGAIN;
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (r->temp_file->file.fd != NGX_INVALID_FILE) {
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
/* save the last part */
|
|
|
|
n = ngx_write_chain_to_temp_file(r->temp_file,
|
|
|
|
r->request_hunks->next ? r->request_hunks->next:
|
|
|
|
r->request_hunks);
|
2004-03-17 05:26:01 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
/* TODO: n == 0 or not complete and level event */
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-28 05:01:00 +08:00
|
|
|
if (n == NGX_ERROR) {
|
2004-03-19 13:25:53 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2003-10-28 05:01:00 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
h = ngx_calloc_hunk(r->pool);
|
|
|
|
if (h == NULL) {
|
2004-03-19 13:25:53 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
h->type = NGX_HUNK_FILE;
|
|
|
|
h->file_pos = 0;
|
|
|
|
h->file_last = r->temp_file->file.offset;
|
|
|
|
h->file = &r->temp_file->file;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (r->request_hunks->next) {
|
|
|
|
r->request_hunks->next->hunk = h;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
} else {
|
|
|
|
r->request_hunks->hunk = h;
|
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
r->request_body_handler(r->data);
|
2004-03-19 13:25:53 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|