nginx/src/http/ngx_http_request_body.c

173 lines
4.7 KiB
C
Raw Normal View History

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);
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
int ngx_http_read_client_request_body(ngx_http_request_t *r,
int 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) {
ngx_test_null(h, ngx_calloc_hunk(r->pool),
NGX_HTTP_INTERNAL_SERVER_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-10-27 16:53:49 +08:00
ngx_alloc_link_and_set_hunk(r->request_hunks, h, r->pool,
NGX_HTTP_INTERNAL_SERVER_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-27 16:53:49 +08:00
ngx_test_null(r->request_body_hunk,
ngx_create_temp_hunk(r->pool, size, 0, 0),
NGX_HTTP_INTERNAL_SERVER_ERROR);
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
r->connection->read->event_handler =
ngx_http_read_client_request_body_handler;
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
ngx_http_read_client_request_body_handler(r->connection->read);
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,
NGX_HTTP_INTERNAL_SERVER_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
2003-10-27 16:53:49 +08:00
if (r->request_body_len) {
return NGX_AGAIN;
}
2003-04-25 22:43:13 +08:00
return NGX_OK;
}
2003-10-27 16:53:49 +08:00
static void ngx_http_read_client_request_body_handler(ngx_event_t *rev)
2003-04-25 22:43:13 +08:00
{
2003-10-28 05:01:00 +08:00
ssize_t n, size;
ngx_hunk_t *h;
ngx_connection_t *c;
ngx_http_request_t *r;
ngx_http_core_loc_conf_t *clcf;
2003-10-27 16:53:49 +08:00
c = rev->data;
r = c->data;
if (r->request_body_hunk->end - r->request_body_hunk->last == 0) {
n = ngx_write_chain_to_temp_file(r->temp_file,
r->request_hunks->next ? r->request_hunks->next:
r->request_hunks);
/* TODO: n == 0 or not complete and level event */
2003-10-28 05:01:00 +08:00
if (n == NGX_ERROR) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
2003-10-27 16:53:49 +08:00
r->request_body_hunk->pos = r->request_body_hunk->start;
r->request_body_hunk->last = r->request_body_hunk->start;
}
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
size = r->request_body_hunk->end - r->request_body_hunk->last;
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
if (size > r->request_body_len) {
size = r->request_body_len;
2003-04-25 22:43:13 +08:00
}
2003-10-27 16:53:49 +08:00
n = ngx_recv(c, r->request_body_hunk->last, size);
2003-04-28 23:06:39 +08:00
2003-10-27 16:53:49 +08:00
if (n == NGX_AGAIN) {
2003-10-28 05:01:00 +08:00
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_add_timer(rev, clcf->client_body_timeout);
2003-10-27 16:53:49 +08:00
if (ngx_handle_read_event(rev) == NGX_ERROR) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
}
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
return;
}
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
if (n == 0) {
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"client closed prematurely connection");
}
2003-04-25 22:43:13 +08:00
2003-10-27 16:53:49 +08:00
if (n == 0 || n == NGX_ERROR) {
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
return;
2003-04-25 22:43:13 +08:00
}
2003-10-27 16:53:49 +08:00
r->request_body_hunk->last += n;
r->request_body_len -= n;
if (r->request_body_len) {
return;
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);
/* 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) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
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) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
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);
2003-04-25 22:43:13 +08:00
}