fix range in $r->sendfile

This commit is contained in:
Igor Sysoev 2006-12-28 20:55:25 +00:00
parent c49d3ec7d4
commit 0e8fc7a6b4

View File

@ -336,6 +336,11 @@ ngx_http_range_header_filter(ngx_http_request_t *r)
r->headers_out.content_length_n = range->end - range->start; r->headers_out.content_length_n = range->end - range->start;
if (r->headers_out.content_length) {
r->headers_out.content_length->hash = 0;
r->headers_out.content_length = NULL;
}
return ngx_http_next_header_filter(r); return ngx_http_next_header_filter(r);
} }
@ -441,36 +446,61 @@ ngx_http_range_header_filter(ngx_http_request_t *r)
} }
r->headers_out.content_length_n = len; r->headers_out.content_length_n = len;
if (r->headers_out.content_length) {
r->headers_out.content_length->hash = 0;
r->headers_out.content_length = NULL; r->headers_out.content_length = NULL;
}
return ngx_http_next_header_filter(r); return ngx_http_next_header_filter(r);
} }
/*
* TODO:
* buffer or buffers is in memory
* range or ranges are overlapped in buffers
*/
static ngx_int_t static ngx_int_t
ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in) ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{ {
ngx_uint_t i; ngx_uint_t i;
ngx_buf_t *b; ngx_buf_t *b, *buf;
ngx_chain_t *out, *hcl, *rcl, *dcl, **ll; ngx_chain_t *out, *hcl, *rcl, *dcl, **ll;
ngx_http_range_t *range; ngx_http_range_t *range;
ngx_http_range_filter_ctx_t *ctx; ngx_http_range_filter_ctx_t *ctx;
if (r->headers_out.ranges.nelts == 0) { if (in == NULL || r->headers_out.ranges.nelts == 0) {
return ngx_http_next_body_filter(r, in); return ngx_http_next_body_filter(r, in);
} }
range = r->headers_out.ranges.elts;
buf = in->buf;
if (!buf->in_file) {
goto in_memory;
}
if (!buf->last_buf) {
for (i = 0; i < r->headers_out.ranges.nelts; i++) {
if (buf->file_pos > range[i].start
|| buf->file_last < range[i].end)
{
goto overlapped;
}
}
}
/* /*
* the optimized version for the static files only * the optimized version for the static files only
* that are passed in the single file buf * that are passed in the single file buffer
*/ */
if (in && in->buf->in_file && in->buf->last_buf) {
range = r->headers_out.ranges.elts;
if (r->headers_out.ranges.nelts == 1) { if (r->headers_out.ranges.nelts == 1) {
in->buf->file_pos = range->start;
in->buf->file_last = range->end; buf->file_pos = range->start;
buf->file_last = range->end;
return ngx_http_next_body_filter(r, in); return ngx_http_next_body_filter(r, in);
} }
@ -534,7 +564,7 @@ ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
b->in_file = 1; b->in_file = 1;
b->file_pos = range[i].start; b->file_pos = range[i].start;
b->file_last = range[i].end; b->file_last = range[i].end;
b->file = in->buf->file; b->file = buf->file;
dcl = ngx_alloc_chain_link(r->pool); dcl = ngx_alloc_chain_link(r->pool);
if (dcl == NULL) { if (dcl == NULL) {
@ -580,11 +610,15 @@ ngx_http_range_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
*ll = hcl; *ll = hcl;
return ngx_http_next_body_filter(r, out); return ngx_http_next_body_filter(r, out);
}
/* TODO: alert */ in_memory:
return ngx_http_next_body_filter(r, in); overlapped:
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"range in memory buffer or in overlapped buffers");
return NGX_ERROR;
} }