static void ngx_http_read_non_buffered_client_request_body_handler(ngx_http_request_t *r) { ngx_int_t rc; if (r->connection->read->timedout) { r->connection->timedout = 1; ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT); return; } rc = ngx_http_do_read_non_buffered_client_request_body(r); if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { ngx_http_finalize_request(r, rc); } if (rc == NGX_OK || rc == NGX_DECLINED) { r->request_body->post_handler(r); } }
static ngx_int_t ngx_http_read_non_buffered_client_request_body(ngx_http_request_t *r, ngx_http_client_body_handler_pt post_handler) { size_t preread; ngx_buf_t *b, buf; ngx_int_t rc; ngx_http_request_body_t *rb; ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); if (r->headers_in.content_length_n == 0) { post_handler(r); return NGX_OK; } rb = r->request_body; rb->post_handler = post_handler; preread = r->header_in->last - r->header_in->pos; if (preread) { /* there is the pre-read part of the request body */ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http client no buffered request body preread %uz", preread); b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } b->temporary = 1; b->start = r->header_in->pos; b->pos = r->header_in->pos; b->last = r->header_in->last; b->end = r->header_in->end; buf.start = r->header_in->pos; buf.pos = r->header_in->pos; buf.last = (off_t) preread >= r->headers_in.content_length_n ? r->header_in->pos + (size_t) r->headers_in.content_length_n : r->header_in->last; buf.end = r->header_in->end; rc = ngx_http_top_input_body_filter(r, &buf); if (rc != NGX_OK) { return rc; } rb->bufs = ngx_alloc_chain_link(r->pool); if (rb->bufs == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } rb->bufs->buf = b; rb->bufs->next = NULL; rb->buf = b; if ((off_t) preread >= r->headers_in.content_length_n) { /* the whole request body was pre-read */ r->header_in->pos += (size_t) r->headers_in.content_length_n; r->request_length += r->headers_in.content_length_n; b->last = r->header_in->pos; b->last_buf = 1; post_handler(r); return NGX_OK; } /* * to not consider the body as pipelined request in * ngx_http_set_keepalive() */ r->header_in->pos = r->header_in->last; r->request_length += preread; rb->rest = r->headers_in.content_length_n - preread; if (rb->rest <= (off_t) (b->end - b->last)) { /* the whole request body could be placed in r->header_in */ goto read_body; } rb->last_out = &rb->bufs->next; } else { rb->rest = r->headers_in.content_length_n; rb->last_out = &rb->bufs; } read_body: rb->buffered = 1; rb->postpone_size = preread; rc = ngx_http_do_read_non_buffered_client_request_body(r); if (rc == NGX_AGAIN) { if (rb->buffered) { r->read_event_handler = ngx_http_read_non_buffered_client_request_body_handler; } } else if (rc == NGX_OK || rc == NGX_DECLINED) { post_handler(r); } return rc; }
static ngx_int_t ngx_http_read_non_buffered_client_request_body(ngx_http_request_t *r, ngx_http_client_body_handler_pt post_handler) { size_t size; size_t preread; ngx_int_t rc; ngx_chain_t out; ngx_http_request_body_t *rb; ngx_http_core_loc_conf_t *clcf; ngx_http_request_body_non_buffered_t *nb; rb = r->request_body; clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); nb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_non_buffered_t)); if (nb == NULL) { rc = NGX_HTTP_INTERNAL_SERVER_ERROR; goto done; } rb->non_buffered = nb; nb->last_out = &nb->bufs; nb->read_handler = ngx_http_do_read_non_buffered_client_request_body; nb->update_handler = ngx_http_request_body_update_chains; nb->tag = (ngx_buf_tag_t *) nb->read_handler; preread = r->header_in->last - r->header_in->pos; if (preread) { /* there is the pre-read part of the request body */ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http client no buffered request body preread %uz", preread); out.buf = r->header_in; out.next = NULL; rc = ngx_http_request_body_filter(r, &out); if (rc != NGX_OK) { goto done; } r->request_length += preread - (r->header_in->last - r->header_in->pos); if (!r->headers_in.chunked && rb->rest > 0 && rb->rest <= (off_t) (r->header_in->end - r->header_in->last)) { /* the whole request body may be placed in r->header_in */ rb->buf = r->header_in; r->read_event_handler = ngx_http_read_non_buffered_client_request_body_handler; r->write_event_handler = ngx_http_request_empty_handler; rc = ngx_http_do_read_non_buffered_client_request_body(r); goto done; } } else { /* set rb->rest */ if (ngx_http_request_body_filter(r, NULL) != NGX_OK) { rc = NGX_HTTP_INTERNAL_SERVER_ERROR; goto done; } } if (rb->rest == 0) { ngx_http_copy_non_buffered_request_body(r); /* the whole request body was pre-read */ rb->post_handler(r); return NGX_OK; } if (rb->rest < 0) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "negative request body rest"); rc = NGX_HTTP_INTERNAL_SERVER_ERROR; goto done; } clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); size = clcf->client_body_buffer_size; size += size >> 2; if (!r->headers_in.chunked && (rb->rest < (off_t) size)) { size = (ssize_t) rb->rest; if (r->request_body_in_single_buf) { size += preread; } } else { size = clcf->client_body_buffer_size; } rb->buf = ngx_create_temp_buf(r->pool, size); if (rb->buf == NULL) { rc = NGX_HTTP_INTERNAL_SERVER_ERROR; goto done; } r->read_event_handler = ngx_http_read_non_buffered_client_request_body_handler; r->write_event_handler = ngx_http_request_empty_handler; nb->buffered = 1; nb->postpone_size = preread; rc = ngx_http_do_read_non_buffered_client_request_body(r); done: if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { r->main->count--; } if (rc == NGX_AGAIN) { if (nb->buffered) { r->read_event_handler = ngx_http_read_non_buffered_client_request_body_handler; } } else if (rc == NGX_OK || rc == NGX_DECLINED) { rb->post_handler(r); } return rc; }