3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static void ngx_http_read_client_request_body_handler(ngx_http_request_t *r);
13 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r);
14 static ngx_int_t ngx_http_write_request_body(ngx_http_request_t *r,
16 static void ngx_http_read_discarded_request_body_handler(ngx_http_request_t *r);
17 static ngx_int_t ngx_http_read_discarded_request_body(ngx_http_request_t *r);
18 static ngx_int_t ngx_http_test_expect(ngx_http_request_t *r);
22 * on completion ngx_http_read_client_request_body() adds to
23 * r->request_body->bufs one or two bufs:
24 * *) one memory buf that was preread in r->header_in;
25 * *) one memory or file buf that contains the rest of the body
29 ngx_http_read_client_request_body(ngx_http_request_t *r,
30 ngx_http_client_body_handler_pt post_handler)
35 ngx_chain_t *cl, **next;
37 ngx_http_request_body_t *rb;
38 ngx_http_core_loc_conf_t *clcf;
40 if (r->request_body || r->discard_body) {
45 if (ngx_http_test_expect(r) != NGX_OK) {
46 return NGX_HTTP_INTERNAL_SERVER_ERROR;
49 rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
51 return NGX_HTTP_INTERNAL_SERVER_ERROR;
56 if (r->headers_in.content_length_n < 0) {
61 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
63 if (r->headers_in.content_length_n == 0) {
65 if (r->request_body_in_file_only) {
66 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
68 return NGX_HTTP_INTERNAL_SERVER_ERROR;
71 tf->file.fd = NGX_INVALID_FILE;
72 tf->file.log = r->connection->log;
73 tf->path = clcf->client_body_temp_path;
75 tf->warn = "a client request body is buffered to a temporary file";
76 tf->log_level = r->request_body_file_log_level;
77 tf->persistent = r->request_body_in_persistent_file;
78 tf->clean = r->request_body_in_clean_file;
80 if (r->request_body_file_group_access) {
86 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
87 tf->persistent, tf->clean, tf->access)
90 return NGX_HTTP_INTERNAL_SERVER_ERROR;
99 rb->post_handler = post_handler;
102 * set by ngx_pcalloc():
109 preread = r->header_in->last - r->header_in->pos;
113 /* there is the pre-read part of the request body */
115 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
116 "http client request body preread %uz", preread);
118 b = ngx_calloc_buf(r->pool);
120 return NGX_HTTP_INTERNAL_SERVER_ERROR;
124 b->start = r->header_in->pos;
125 b->pos = r->header_in->pos;
126 b->last = r->header_in->last;
127 b->end = r->header_in->end;
129 rb->bufs = ngx_alloc_chain_link(r->pool);
130 if (rb->bufs == NULL) {
131 return NGX_HTTP_INTERNAL_SERVER_ERROR;
135 rb->bufs->next = NULL;
139 if ((off_t) preread >= r->headers_in.content_length_n) {
141 /* the whole request body was pre-read */
143 r->header_in->pos += (size_t) r->headers_in.content_length_n;
144 r->request_length += r->headers_in.content_length_n;
146 if (r->request_body_in_file_only) {
147 if (ngx_http_write_request_body(r, rb->bufs) != NGX_OK) {
148 return NGX_HTTP_INTERNAL_SERVER_ERROR;
158 * to not consider the body as pipelined request in
159 * ngx_http_set_keepalive()
161 r->header_in->pos = r->header_in->last;
163 r->request_length += preread;
165 rb->rest = r->headers_in.content_length_n - preread;
167 if (rb->rest <= (off_t) (b->end - b->last)) {
169 /* the whole request body may be placed in r->header_in */
171 rb->to_write = rb->bufs;
173 r->read_event_handler = ngx_http_read_client_request_body_handler;
175 return ngx_http_do_read_client_request_body(r);
178 next = &rb->bufs->next;
182 rb->rest = r->headers_in.content_length_n;
186 size = clcf->client_body_buffer_size;
189 if (rb->rest < size) {
190 size = (ssize_t) rb->rest;
192 if (r->request_body_in_single_buf) {
197 size = clcf->client_body_buffer_size;
199 /* disable copying buffer for r->request_body_in_single_buf */
203 rb->buf = ngx_create_temp_buf(r->pool, size);
204 if (rb->buf == NULL) {
205 return NGX_HTTP_INTERNAL_SERVER_ERROR;
208 cl = ngx_alloc_chain_link(r->pool);
210 return NGX_HTTP_INTERNAL_SERVER_ERROR;
216 if (b && r->request_body_in_single_buf) {
217 size = b->last - b->pos;
218 ngx_memcpy(rb->buf->pos, b->pos, size);
219 rb->buf->last += size;
226 if (r->request_body_in_file_only || r->request_body_in_single_buf) {
227 rb->to_write = rb->bufs;
230 rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs;
233 r->read_event_handler = ngx_http_read_client_request_body_handler;
235 return ngx_http_do_read_client_request_body(r);
240 ngx_http_read_client_request_body_handler(ngx_http_request_t *r)
244 if (r->connection->read->timedout) {
245 r->connection->timedout = 1;
246 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
250 rc = ngx_http_do_read_client_request_body(r);
252 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
253 ngx_http_finalize_request(r, rc);
259 ngx_http_do_read_client_request_body(ngx_http_request_t *r)
265 ngx_http_request_body_t *rb;
266 ngx_http_core_loc_conf_t *clcf;
269 rb = r->request_body;
271 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
272 "http read client request body");
276 if (rb->buf->last == rb->buf->end) {
278 if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) {
279 return NGX_HTTP_INTERNAL_SERVER_ERROR;
282 rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs;
283 rb->buf->last = rb->buf->start;
286 size = rb->buf->end - rb->buf->last;
288 if ((off_t) size > rb->rest) {
289 size = (size_t) rb->rest;
292 n = c->recv(c, rb->buf->last, size);
294 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
295 "http client request body recv %z", n);
297 if (n == NGX_AGAIN) {
302 ngx_log_error(NGX_LOG_INFO, c->log, 0,
303 "client closed prematurely connection");
306 if (n == 0 || n == NGX_ERROR) {
308 return NGX_HTTP_BAD_REQUEST;
313 r->request_length += n;
319 if (rb->buf->last < rb->buf->end) {
324 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
325 "http client request body rest %O", rb->rest);
331 if (!c->read->ready) {
332 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
333 ngx_add_timer(c->read, clcf->client_body_timeout);
335 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
336 return NGX_HTTP_INTERNAL_SERVER_ERROR;
343 if (c->read->timer_set) {
344 ngx_del_timer(c->read);
347 if (rb->temp_file || r->request_body_in_file_only) {
349 /* save the last part */
351 if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) {
352 return NGX_HTTP_INTERNAL_SERVER_ERROR;
355 b = ngx_calloc_buf(r->pool);
357 return NGX_HTTP_INTERNAL_SERVER_ERROR;
362 b->file_last = rb->temp_file->file.offset;
363 b->file = &rb->temp_file->file;
365 if (rb->bufs->next) {
366 rb->bufs->next->buf = b;
373 if (r->request_body_in_file_only && rb->bufs->next) {
374 rb->bufs = rb->bufs->next;
384 ngx_http_write_request_body(ngx_http_request_t *r, ngx_chain_t *body)
388 ngx_http_request_body_t *rb;
389 ngx_http_core_loc_conf_t *clcf;
391 rb = r->request_body;
393 if (rb->temp_file == NULL) {
394 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
399 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
401 tf->file.fd = NGX_INVALID_FILE;
402 tf->file.log = r->connection->log;
403 tf->path = clcf->client_body_temp_path;
405 tf->warn = "a client request body is buffered to a temporary file";
406 tf->log_level = r->request_body_file_log_level;
407 tf->persistent = r->request_body_in_persistent_file;
408 tf->clean = r->request_body_in_clean_file;
410 if (r->request_body_file_group_access) {
417 n = ngx_write_chain_to_temp_file(rb->temp_file, body);
419 /* TODO: n == 0 or not complete and level event */
421 if (n == NGX_ERROR) {
425 rb->temp_file->offset += n;
432 ngx_http_discard_request_body(ngx_http_request_t *r)
437 if (r != r->main || r->discard_body) {
441 if (ngx_http_test_expect(r) != NGX_OK) {
442 return NGX_HTTP_INTERNAL_SERVER_ERROR;
445 rev = r->connection->read;
447 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http set discard body");
449 if (rev->timer_set) {
453 if (r->headers_in.content_length_n <= 0 || r->request_body) {
457 size = r->header_in->last - r->header_in->pos;
460 if (r->headers_in.content_length_n > size) {
461 r->headers_in.content_length_n -= size;
464 r->header_in->pos += (size_t) r->headers_in.content_length_n;
465 r->headers_in.content_length_n = 0;
472 r->read_event_handler = ngx_http_read_discarded_request_body_handler;
474 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
475 return NGX_HTTP_INTERNAL_SERVER_ERROR;
478 (void) ngx_http_read_discarded_request_body(r);
485 ngx_http_read_discarded_request_body_handler(ngx_http_request_t *r)
491 ngx_http_core_loc_conf_t *clcf;
499 ngx_http_finalize_request(r, 0);
503 if (r->lingering_time) {
504 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
508 ngx_http_finalize_request(r, 0);
516 rc = ngx_http_read_discarded_request_body(r);
523 ngx_http_finalize_request(r, 0);
529 /* rc == NGX_AGAIN */
531 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
533 ngx_http_finalize_request(r, rc);
539 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
543 if (timer > clcf->lingering_timeout) {
544 timer = clcf->lingering_timeout;
547 ngx_add_timer(rev, timer);
553 ngx_http_read_discarded_request_body(ngx_http_request_t *r)
557 u_char buffer[NGX_HTTP_DISCARD_BUFFER_SIZE];
559 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
560 "http read discarded body");
563 if (r->headers_in.content_length_n == 0) {
564 r->read_event_handler = ngx_http_block_reading;
568 size = (r->headers_in.content_length_n > NGX_HTTP_DISCARD_BUFFER_SIZE) ?
569 NGX_HTTP_DISCARD_BUFFER_SIZE:
570 (size_t) r->headers_in.content_length_n;
572 n = r->connection->recv(r->connection, buffer, size);
574 if (n == NGX_ERROR) {
575 r->connection->error = 1;
579 if (n == NGX_AGAIN) {
587 r->headers_in.content_length_n -= n;
589 } while (r->connection->read->ready);
596 ngx_http_test_expect(ngx_http_request_t *r)
602 || r->headers_in.expect == NULL
603 || r->http_version < NGX_HTTP_VERSION_11)
608 r->expect_tested = 1;
610 expect = &r->headers_in.expect->value;
612 if (expect->len != sizeof("100-continue") - 1
613 || ngx_strncasecmp(expect->data, (u_char *) "100-continue",
614 sizeof("100-continue") - 1)
620 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
621 "send 100 Continue");
623 n = r->connection->send(r->connection,
624 (u_char *) "HTTP/1.1 100 Continue" CRLF CRLF,
625 sizeof("HTTP/1.1 100 Continue" CRLF CRLF) - 1);
627 if (n == sizeof("HTTP/1.1 100 Continue" CRLF CRLF) - 1) {
631 /* we assume that such small packet should be send successfully */