Skip to content

Commit

Permalink
Upstream: added check if a response is complete.
Browse files Browse the repository at this point in the history
Checks were added to both buffered and unbuffered code paths to detect
and complain if a response is incomplete.  Appropriate error codes are
now passed to ngx_http_upstream_finalize_request().

With this change in unbuffered mode we now use u->length set to -1 as an
indicator that EOF is allowed per protocol and used to indicate response
end (much like its with p->length in buffered mode).  Proxy module was
changed to set u->length to 1 (instead of previously used -1) in case of
chunked transfer encoding used to comply with the above.
  • Loading branch information
mdounin committed Jul 25, 2013
1 parent 416b922 commit 960d0bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/http/modules/ngx_http_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ ngx_http_proxy_input_filter_init(void *data)
u->pipe->length = 3; /* "0" LF LF */

u->input_filter = ngx_http_proxy_non_buffered_chunked_filter;
u->length = -1;
u->length = 1;

} else if (u->headers_in.content_length_n == 0) {
/* empty body: special case as filter won't be called */
Expand Down
33 changes: 30 additions & 3 deletions src/http/ngx_http_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -2748,13 +2748,27 @@ ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
if (u->busy_bufs == NULL) {

if (u->length == 0
|| upstream->read->eof
|| upstream->read->error)
|| (upstream->read->eof && u->length == -1))
{
ngx_http_upstream_finalize_request(r, u, 0);
return;
}

if (upstream->read->eof) {
ngx_log_error(NGX_LOG_ERR, upstream->log, 0,
"upstream prematurely closed connection");

ngx_http_upstream_finalize_request(r, u,
NGX_HTTP_BAD_GATEWAY);
return;
}

if (upstream->read->error) {
ngx_http_upstream_finalize_request(r, u,
NGX_HTTP_BAD_GATEWAY);
return;
}

b->pos = b->start;
b->last = b->start;
}
Expand Down Expand Up @@ -3027,7 +3041,20 @@ ngx_http_upstream_process_request(ngx_http_request_t *r)
if (p->upstream_done || p->upstream_eof || p->upstream_error) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http upstream exit: %p", p->out);
ngx_http_upstream_finalize_request(r, u, 0);

if (p->upstream_done
|| (p->upstream_eof && p->length == -1))
{
ngx_http_upstream_finalize_request(r, u, 0);
return;
}

if (p->upstream_eof) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"upstream prematurely closed connection");
}

ngx_http_upstream_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);
return;
}
}
Expand Down

0 comments on commit 960d0bf

Please sign in to comment.