2003-04-25 22:43:13 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +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-30 01:43:58 +08:00
|
|
|
ngx_int_t ngx_http_read_client_request_body(ngx_http_request_t *r)
|
2003-04-25 22:43:13 +08:00
|
|
|
{
|
2004-04-02 00:20:53 +08:00
|
|
|
ssize_t size;
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_buf_t *b;
|
2004-04-02 00:20:53 +08:00
|
|
|
ngx_chain_t *cl;
|
|
|
|
ngx_http_core_loc_conf_t *clcf;
|
2004-03-30 01:43:58 +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) {
|
2004-03-27 00:13:01 +08:00
|
|
|
|
|
|
|
/* there is the pre-read part of the request body */
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_test_null(b, ngx_calloc_buf(r->pool),
|
2004-03-30 01:43:58 +08:00
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
b->temporary = 1;
|
|
|
|
b->start = b->pos = r->header_in->pos;
|
|
|
|
b->end = b->last = r->header_in->last;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_alloc_link_and_set_buf(r->request_body->bufs, b, r->pool,
|
2004-03-30 01:43:58 +08:00
|
|
|
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) {
|
2003-10-28 05:01:00 +08:00
|
|
|
|
2004-03-27 00:13:01 +08:00
|
|
|
/* the whole request body was pre-read */
|
|
|
|
|
|
|
|
r->header_in->pos += r->headers_in.content_length_n;
|
2004-03-30 23:59:50 +08:00
|
|
|
|
|
|
|
r->request_body->handler(r->request_body->data);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2004-04-02 00:20:53 +08:00
|
|
|
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
r->request_body->rest = r->headers_in.content_length_n - size;
|
2004-03-27 00:13:01 +08:00
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->rest
|
2004-04-02 00:20:53 +08:00
|
|
|
< clcf->client_body_buffer_size
|
|
|
|
+ (clcf->client_body_buffer_size >> 2))
|
2003-10-27 16:53:49 +08:00
|
|
|
{
|
2004-03-30 01:43:58 +08:00
|
|
|
size = r->request_body->rest;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
} else {
|
2004-04-02 00:20:53 +08:00
|
|
|
size = clcf->client_body_buffer_size;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_test_null(r->request_body->buf, ngx_create_temp_buf(r->pool, size),
|
2004-03-30 01:43:58 +08:00
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_alloc_link_and_set_buf(cl, r->request_body->buf, r->pool,
|
|
|
|
NGX_HTTP_INTERNAL_SERVER_ERROR);
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->bufs) {
|
|
|
|
r->request_body->bufs->next = cl;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
} else {
|
2004-03-30 01:43:58 +08:00
|
|
|
r->request_body->bufs = cl;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
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;
|
|
|
|
|
2004-03-30 23:59:50 +08:00
|
|
|
if (rev->timedout) {
|
|
|
|
ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-19 13:25:53 +08:00
|
|
|
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;
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_buf_t *b;
|
2003-10-28 05:01:00 +08:00
|
|
|
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 ( ;; ) {
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->buf->last == r->request_body->buf->end) {
|
|
|
|
n = ngx_write_chain_to_temp_file(r->request_body->temp_file,
|
|
|
|
r->request_body->bufs->next ? r->request_body->bufs->next:
|
|
|
|
r->request_body->bufs);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
r->request_body->temp_file->offset += n;
|
2004-03-17 05:26:01 +08:00
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
r->request_body->buf->pos = r->request_body->buf->start;
|
|
|
|
r->request_body->buf->last = r->request_body->buf->start;
|
2003-10-28 05:01:00 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
size = r->request_body->buf->end - r->request_body->buf->last;
|
2003-11-03 06:56:18 +08:00
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (size > r->request_body->rest) {
|
|
|
|
size = r->request_body->rest;
|
2004-03-17 05:26:01 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-07-19 03:11:20 +08:00
|
|
|
n = c->recv(c, r->request_body->buf->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-30 01:43:58 +08:00
|
|
|
r->request_body->buf->last += n;
|
|
|
|
r->request_body->rest -= n;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->rest == 0) {
|
2004-03-23 14:01:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->buf->last < r->request_body->buf->end) {
|
2004-03-17 05:26:01 +08:00
|
|
|
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,
|
2004-03-30 01:43:58 +08:00
|
|
|
"http client request body rest " SIZE_T_FMT,
|
|
|
|
r->request_body->rest);
|
2004-03-19 13:25:53 +08:00
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->rest) {
|
2004-03-19 13:25:53 +08:00
|
|
|
return NGX_AGAIN;
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->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 */
|
2004-03-30 01:43:58 +08:00
|
|
|
n = ngx_write_chain_to_temp_file(r->request_body->temp_file,
|
|
|
|
r->request_body->bufs->next ? r->request_body->bufs->next:
|
|
|
|
r->request_body->bufs);
|
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
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (!(b = ngx_calloc_buf(r->pool))) {
|
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
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
b->in_file = 1;
|
|
|
|
b->file_pos = 0;
|
|
|
|
b->file_last = r->request_body->temp_file->file.offset;
|
|
|
|
b->file = &r->request_body->temp_file->file;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
if (r->request_body->bufs->next) {
|
2004-05-28 23:49:23 +08:00
|
|
|
r->request_body->bufs->next->buf = b;
|
2003-04-25 22:43:13 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
} else {
|
2004-05-28 23:49:23 +08:00
|
|
|
r->request_body->bufs->buf = b;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
r->request_body->handler(r->request_body->data);
|
2004-03-19 13:25:53 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
2003-04-25 22:43:13 +08:00
|
|
|
}
|