2003-10-27 16:53:49 +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-10-27 16:53:49 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2003-10-28 05:01:00 +08:00
|
|
|
#include <ngx_event.h>
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if 0
|
|
|
|
#define NGX_SENDFILE_LIMIT 4096
|
|
|
|
#endif
|
|
|
|
|
2008-11-21 00:21:39 +08:00
|
|
|
/*
|
|
|
|
* When DIRECTIO is enabled FreeBSD, Solaris, and MacOSX read directly
|
|
|
|
* to an application memory from a device if parameters are aligned
|
2009-08-28 16:15:55 +08:00
|
|
|
* to device sector boundary (512 bytes). They fallback to usual read
|
2008-11-21 00:21:39 +08:00
|
|
|
* operation if the parameters are not aligned.
|
|
|
|
* Linux allows DIRECTIO only if the parameters are aligned to a filesystem
|
|
|
|
* sector boundary, otherwise it returns EINVAL. The sector size is
|
|
|
|
* usually 512 bytes, however, on XFS it may be 4096 bytes.
|
|
|
|
*/
|
2004-11-26 00:17:31 +08:00
|
|
|
|
|
|
|
#define NGX_NONE 1
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
|
2004-11-21 03:52:20 +08:00
|
|
|
static ngx_inline ngx_int_t
|
2008-09-05 22:48:47 +08:00
|
|
|
ngx_output_chain_as_is(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf);
|
2004-11-26 00:17:31 +08:00
|
|
|
static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool,
|
2005-03-04 22:06:57 +08:00
|
|
|
ngx_chain_t **chain, ngx_chain_t *in);
|
2008-09-05 22:48:47 +08:00
|
|
|
static ngx_int_t ngx_output_chain_align_file_buf(ngx_output_chain_ctx_t *ctx,
|
|
|
|
off_t bsize);
|
2008-09-03 18:01:29 +08:00
|
|
|
static ngx_int_t ngx_output_chain_get_buf(ngx_output_chain_ctx_t *ctx,
|
|
|
|
off_t bsize);
|
2008-09-05 22:48:47 +08:00
|
|
|
static ngx_int_t ngx_output_chain_copy_buf(ngx_output_chain_ctx_t *ctx);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in)
|
2003-10-27 16:53:49 +08:00
|
|
|
{
|
2004-12-03 02:40:46 +08:00
|
|
|
off_t bsize;
|
2006-01-11 23:26:57 +08:00
|
|
|
ngx_int_t rc, last;
|
2003-10-27 16:53:49 +08:00
|
|
|
ngx_chain_t *cl, *out, **last_out;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
if (ctx->in == NULL && ctx->busy == NULL) {
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
/*
|
|
|
|
* the short path for the case when the ctx->in and ctx->busy chains
|
|
|
|
* are empty, the incoming chain is empty too or has the single buf
|
|
|
|
* that does not require the copy
|
|
|
|
*/
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
if (in == NULL) {
|
2004-03-23 14:01:52 +08:00
|
|
|
return ctx->output_filter(ctx->filter_ctx, in);
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2003-10-28 05:01:00 +08:00
|
|
|
if (in->next == NULL
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_SENDFILE_LIMIT)
|
|
|
|
&& !(in->buf->in_file && in->buf->file_last > NGX_SENDFILE_LIMIT)
|
|
|
|
#endif
|
2008-09-05 22:48:47 +08:00
|
|
|
&& ngx_output_chain_as_is(ctx, in->buf))
|
2003-10-27 16:53:49 +08:00
|
|
|
{
|
2004-03-23 14:01:52 +08:00
|
|
|
return ctx->output_filter(ctx->filter_ctx, in);
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
/* add the incoming buf to the chain ctx->in */
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
if (in) {
|
2004-11-26 00:17:31 +08:00
|
|
|
if (ngx_output_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) {
|
2003-10-27 16:53:49 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-13 14:28:17 +08:00
|
|
|
#if (NGX_HAVE_FILE_AIO)
|
|
|
|
if (ctx->aio) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-10-29 16:30:44 +08:00
|
|
|
out = NULL;
|
2003-10-27 16:53:49 +08:00
|
|
|
last_out = &out;
|
2005-03-04 22:06:57 +08:00
|
|
|
last = NGX_NONE;
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
while (ctx->in) {
|
|
|
|
|
2004-04-19 03:06:02 +08:00
|
|
|
/*
|
2004-05-28 23:49:23 +08:00
|
|
|
* cycle while there are the ctx->in bufs
|
2008-09-05 22:48:47 +08:00
|
|
|
* and there are the free output bufs to copy in
|
2004-04-19 03:06:02 +08:00
|
|
|
*/
|
|
|
|
|
2004-07-29 03:21:26 +08:00
|
|
|
bsize = ngx_buf_size(ctx->in->buf);
|
|
|
|
|
|
|
|
if (bsize == 0 && !ngx_buf_special(ctx->in->buf)) {
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0,
|
2005-01-25 20:27:35 +08:00
|
|
|
"zero size buf in output "
|
|
|
|
"t:%d r:%d f:%d %p %p-%p %p %O-%O",
|
|
|
|
ctx->in->buf->temporary,
|
|
|
|
ctx->in->buf->recycled,
|
|
|
|
ctx->in->buf->in_file,
|
|
|
|
ctx->in->buf->start,
|
|
|
|
ctx->in->buf->pos,
|
|
|
|
ctx->in->buf->last,
|
|
|
|
ctx->in->buf->file,
|
|
|
|
ctx->in->buf->file_pos,
|
|
|
|
ctx->in->buf->file_last);
|
2004-07-29 03:21:26 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
ngx_debug_point();
|
|
|
|
|
2004-07-29 03:21:26 +08:00
|
|
|
ctx->in = ctx->in->next;
|
2004-09-20 02:27:00 +08:00
|
|
|
|
2004-07-29 03:21:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
if (ngx_output_chain_as_is(ctx, ctx->in->buf)) {
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-04-19 03:06:02 +08:00
|
|
|
/* move the chain link to the output chain */
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
cl = ctx->in;
|
|
|
|
ctx->in = cl->next;
|
|
|
|
|
|
|
|
*last_out = cl;
|
|
|
|
last_out = &cl->next;
|
|
|
|
cl->next = NULL;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (ctx->buf == NULL) {
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
rc = ngx_output_chain_align_file_buf(ctx, bsize);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
if (rc != NGX_OK) {
|
2004-05-12 13:37:55 +08:00
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
if (ctx->free) {
|
2004-05-12 13:37:55 +08:00
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
/* get the free buf */
|
|
|
|
|
|
|
|
cl = ctx->free;
|
|
|
|
ctx->buf = cl->buf;
|
|
|
|
ctx->free = cl->next;
|
|
|
|
|
|
|
|
ngx_free_chain(ctx->pool, cl);
|
|
|
|
|
|
|
|
} else if (out || ctx->allocated == ctx->bufs.num) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (ngx_output_chain_get_buf(ctx, bsize) != NGX_OK) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
rc = ngx_output_chain_copy_buf(ctx);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == NGX_AGAIN) {
|
|
|
|
if (out) {
|
|
|
|
break;
|
|
|
|
}
|
2004-09-20 02:27:00 +08:00
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
/* delete the completed buf from the ctx->in chain */
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (ngx_buf_size(ctx->in->buf) == 0) {
|
2003-10-27 16:53:49 +08:00
|
|
|
ctx->in = ctx->in->next;
|
|
|
|
}
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
cl = ngx_alloc_chain_link(ctx->pool);
|
|
|
|
if (cl == NULL) {
|
2004-11-11 22:07:14 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2005-03-19 20:38:37 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
cl->buf = ctx->buf;
|
|
|
|
cl->next = NULL;
|
2003-10-27 16:53:49 +08:00
|
|
|
*last_out = cl;
|
|
|
|
last_out = &cl->next;
|
2004-05-28 23:49:23 +08:00
|
|
|
ctx->buf = NULL;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (out == NULL && last != NGX_NONE) {
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
|
|
|
|
if (ctx->in) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2004-03-23 14:01:52 +08:00
|
|
|
last = ctx->output_filter(ctx->filter_ctx, out);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2005-10-24 23:09:41 +08:00
|
|
|
if (last == NGX_ERROR || last == NGX_DONE) {
|
2004-04-19 03:06:02 +08:00
|
|
|
return last;
|
|
|
|
}
|
2005-10-24 23:09:41 +08:00
|
|
|
|
|
|
|
ngx_chain_update_chains(&ctx->free, &ctx->busy, &out, ctx->tag);
|
|
|
|
last_out = &out;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-21 03:52:20 +08:00
|
|
|
static ngx_inline ngx_int_t
|
2008-09-05 22:48:47 +08:00
|
|
|
ngx_output_chain_as_is(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf)
|
2003-10-27 16:53:49 +08:00
|
|
|
{
|
2004-11-26 00:17:31 +08:00
|
|
|
ngx_uint_t sendfile;
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (ngx_buf_special(buf)) {
|
2008-09-05 22:48:47 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->in_file && buf->file->directio) {
|
2003-10-27 16:53:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
sendfile = ctx->sendfile;
|
|
|
|
|
|
|
|
#if (NGX_SENDFILE_LIMIT)
|
|
|
|
|
|
|
|
if (buf->in_file && buf->file_pos >= NGX_SENDFILE_LIMIT) {
|
|
|
|
sendfile = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!sendfile) {
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (!ngx_buf_in_memory(buf)) {
|
2008-09-05 22:48:47 +08:00
|
|
|
return 0;
|
2003-11-03 06:56:18 +08:00
|
|
|
}
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
buf->in_file = 0;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (ctx->need_in_memory && !ngx_buf_in_memory(buf)) {
|
2008-09-05 22:48:47 +08:00
|
|
|
return 0;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (ctx->need_in_temp && (buf->memory || buf->mmap)) {
|
2008-09-05 22:48:47 +08:00
|
|
|
return 0;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
return 1;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_output_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain,
|
|
|
|
ngx_chain_t *in)
|
2004-11-26 00:17:31 +08:00
|
|
|
{
|
|
|
|
ngx_chain_t *cl, **ll;
|
|
|
|
#if (NGX_SENDFILE_LIMIT)
|
|
|
|
ngx_buf_t *b, *buf;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ll = chain;
|
|
|
|
|
|
|
|
for (cl = *chain; cl; cl = cl->next) {
|
|
|
|
ll = &cl->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (in) {
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
cl = ngx_alloc_chain_link(pool);
|
|
|
|
if (cl == NULL) {
|
2004-11-26 00:17:31 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (NGX_SENDFILE_LIMIT)
|
|
|
|
|
|
|
|
buf = in->buf;
|
|
|
|
|
|
|
|
if (buf->in_file
|
|
|
|
&& buf->file_pos < NGX_SENDFILE_LIMIT
|
|
|
|
&& buf->file_last > NGX_SENDFILE_LIMIT)
|
|
|
|
{
|
2008-09-05 22:48:47 +08:00
|
|
|
/* split a file buf on two bufs by the sendfile limit */
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
b = ngx_calloc_buf(pool);
|
|
|
|
if (b == NULL) {
|
2004-11-26 00:17:31 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(b, buf, sizeof(ngx_buf_t));
|
|
|
|
|
|
|
|
if (ngx_buf_in_memory(buf)) {
|
|
|
|
buf->pos += (ssize_t) (NGX_SENDFILE_LIMIT - buf->file_pos);
|
|
|
|
b->last = buf->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->file_pos = NGX_SENDFILE_LIMIT;
|
|
|
|
b->file_last = NGX_SENDFILE_LIMIT;
|
|
|
|
|
|
|
|
cl->buf = b;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cl->buf = buf;
|
|
|
|
in = in->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
cl->buf = in->buf;
|
|
|
|
in = in->next;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-06-03 00:07:13 +08:00
|
|
|
cl->next = NULL;
|
2004-11-26 00:17:31 +08:00
|
|
|
*ll = cl;
|
|
|
|
ll = &cl->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_output_chain_align_file_buf(ngx_output_chain_ctx_t *ctx, off_t bsize)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
ngx_buf_t *in;
|
|
|
|
|
|
|
|
in = ctx->in->buf;
|
|
|
|
|
|
|
|
if (in->file == NULL || !in->file->directio) {
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->directio = 1;
|
|
|
|
|
2009-08-28 16:15:55 +08:00
|
|
|
size = (size_t) (in->file_pos - (in->file_pos & ~(ctx->alignment - 1)));
|
2008-09-05 22:48:47 +08:00
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
|
2008-09-08 17:33:37 +08:00
|
|
|
if (bsize >= (off_t) ctx->bufs.size) {
|
2008-09-05 22:48:47 +08:00
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = (size_t) bsize;
|
|
|
|
|
|
|
|
} else {
|
2009-08-28 19:46:12 +08:00
|
|
|
size = (size_t) ctx->alignment - size;
|
2008-09-05 22:48:47 +08:00
|
|
|
|
2008-09-08 17:33:37 +08:00
|
|
|
if ((off_t) size > bsize) {
|
2008-09-05 22:48:47 +08:00
|
|
|
size = (size_t) bsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->buf = ngx_create_temp_buf(ctx->pool, size);
|
|
|
|
if (ctx->buf == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we do not set ctx->buf->tag, because we do not want
|
|
|
|
* to reuse the buf via ctx->free list
|
|
|
|
*/
|
|
|
|
|
2008-09-12 21:50:12 +08:00
|
|
|
#if (NGX_HAVE_ALIGNED_DIRECTIO)
|
|
|
|
ctx->unaligned = 1;
|
|
|
|
#endif
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-03 18:01:29 +08:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_output_chain_get_buf(ngx_output_chain_ctx_t *ctx, off_t bsize)
|
|
|
|
{
|
|
|
|
size_t size;
|
2008-09-05 22:48:47 +08:00
|
|
|
ngx_buf_t *b, *in;
|
2008-09-03 18:01:29 +08:00
|
|
|
ngx_uint_t recycled;
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
in = ctx->in->buf;
|
2008-09-03 18:01:29 +08:00
|
|
|
size = ctx->bufs.size;
|
|
|
|
recycled = 1;
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
if (in->last_in_chain) {
|
2008-09-03 18:01:29 +08:00
|
|
|
|
|
|
|
if (bsize < (off_t) size) {
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
/*
|
|
|
|
* allocate a small temp buf for a small last buf
|
|
|
|
* or its small last part
|
|
|
|
*/
|
2008-09-03 18:01:29 +08:00
|
|
|
|
|
|
|
size = (size_t) bsize;
|
|
|
|
recycled = 0;
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
} else if (!ctx->directio
|
|
|
|
&& ctx->bufs.num == 1
|
|
|
|
&& (bsize < (off_t) (size + size / 4)))
|
|
|
|
{
|
2008-09-03 18:01:29 +08:00
|
|
|
/*
|
2008-09-05 22:48:47 +08:00
|
|
|
* allocate a temp buf that equals to a last buf,
|
|
|
|
* if there is no directio, the last buf size is lesser
|
|
|
|
* than 1.25 of bufs.size and the temp buf is single
|
2008-09-03 18:01:29 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
size = (size_t) bsize;
|
|
|
|
recycled = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b = ngx_calloc_buf(ctx->pool);
|
|
|
|
if (b == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
if (ctx->directio) {
|
2008-09-03 18:01:29 +08:00
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
/*
|
|
|
|
* allocate block aligned to a disk sector size to enable
|
|
|
|
* userland buffer direct usage conjunctly with directio
|
|
|
|
*/
|
|
|
|
|
2009-08-28 19:46:12 +08:00
|
|
|
b->start = ngx_pmemalign(ctx->pool, size, (size_t) ctx->alignment);
|
2008-09-05 22:48:47 +08:00
|
|
|
if (b->start == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
b->start = ngx_palloc(ctx->pool, size);
|
|
|
|
if (b->start == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2008-09-03 18:01:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
b->pos = b->start;
|
|
|
|
b->last = b->start;
|
|
|
|
b->end = b->last + size;
|
|
|
|
b->temporary = 1;
|
|
|
|
b->tag = ctx->tag;
|
|
|
|
b->recycled = recycled;
|
|
|
|
|
|
|
|
ctx->buf = b;
|
|
|
|
ctx->allocated++;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
static ngx_int_t
|
2008-09-05 22:48:47 +08:00
|
|
|
ngx_output_chain_copy_buf(ngx_output_chain_ctx_t *ctx)
|
2003-10-27 16:53:49 +08:00
|
|
|
{
|
2008-09-05 22:48:47 +08:00
|
|
|
off_t size;
|
|
|
|
ssize_t n;
|
|
|
|
ngx_buf_t *src, *dst;
|
|
|
|
ngx_uint_t sendfile;
|
|
|
|
|
|
|
|
src = ctx->in->buf;
|
|
|
|
dst = ctx->buf;
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
size = ngx_buf_size(src);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (size > dst->end - dst->pos) {
|
2003-10-27 16:53:49 +08:00
|
|
|
size = dst->end - dst->pos;
|
|
|
|
}
|
|
|
|
|
2008-09-05 22:48:47 +08:00
|
|
|
sendfile = ctx->sendfile & !ctx->directio;
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_SENDFILE_LIMIT)
|
|
|
|
|
|
|
|
if (src->in_file && src->file_pos >= NGX_SENDFILE_LIMIT) {
|
|
|
|
sendfile = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
if (ngx_buf_in_memory(src)) {
|
2004-12-03 02:40:46 +08:00
|
|
|
ngx_memcpy(dst->pos, src->pos, (size_t) size);
|
|
|
|
src->pos += (size_t) size;
|
|
|
|
dst->last += (size_t) size;
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
if (src->in_file) {
|
|
|
|
|
|
|
|
if (sendfile) {
|
|
|
|
dst->in_file = 1;
|
|
|
|
dst->file = src->file;
|
|
|
|
dst->file_pos = src->file_pos;
|
|
|
|
dst->file_last = src->file_pos + size;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
dst->in_file = 0;
|
|
|
|
}
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
src->file_pos += size;
|
2004-10-11 23:07:03 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
dst->in_file = 0;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2007-06-04 03:58:30 +08:00
|
|
|
if (src->pos == src->last) {
|
|
|
|
dst->flush = src->flush;
|
|
|
|
dst->last_buf = src->last_buf;
|
2009-05-28 23:42:27 +08:00
|
|
|
dst->last_in_chain = src->last_in_chain;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2008-09-12 21:50:12 +08:00
|
|
|
|
|
|
|
#if (NGX_HAVE_ALIGNED_DIRECTIO)
|
|
|
|
|
|
|
|
if (ctx->unaligned) {
|
|
|
|
if (ngx_directio_off(src->file->fd) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, ngx_errno,
|
|
|
|
ngx_directio_off_n " \"%s\" failed",
|
|
|
|
src->file->name.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-08-28 16:12:35 +08:00
|
|
|
#if (NGX_HAVE_FILE_AIO)
|
|
|
|
|
2009-09-13 14:28:17 +08:00
|
|
|
if (ctx->aio_handler) {
|
2009-08-28 16:12:35 +08:00
|
|
|
n = ngx_file_aio_read(src->file, dst->pos, (size_t) size,
|
|
|
|
src->file_pos, ctx->pool);
|
|
|
|
if (n == NGX_AGAIN) {
|
2009-09-13 14:28:17 +08:00
|
|
|
ctx->aio_handler(ctx, src->file);
|
2009-08-28 16:12:35 +08:00
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
n = ngx_read_file(src->file, dst->pos, (size_t) size,
|
|
|
|
src->file_pos);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
n = ngx_read_file(src->file, dst->pos, (size_t) size, src->file_pos);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
2009-08-28 16:12:35 +08:00
|
|
|
#endif
|
|
|
|
|
2008-09-12 21:50:12 +08:00
|
|
|
#if (NGX_HAVE_ALIGNED_DIRECTIO)
|
|
|
|
|
|
|
|
if (ctx->unaligned) {
|
|
|
|
ngx_err_t err;
|
|
|
|
|
|
|
|
err = ngx_errno;
|
|
|
|
|
|
|
|
if (ngx_directio_on(src->file->fd) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, ngx_errno,
|
|
|
|
ngx_directio_on_n " \"%s\" failed",
|
|
|
|
src->file->name.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_set_errno(err);
|
|
|
|
|
|
|
|
ctx->unaligned = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-10-27 16:53:49 +08:00
|
|
|
if (n == NGX_ERROR) {
|
2005-05-23 20:07:45 +08:00
|
|
|
return (ngx_int_t) n;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (n != size) {
|
2008-09-05 22:48:47 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0,
|
2008-09-11 23:52:11 +08:00
|
|
|
ngx_read_file_n " read only %z of %O from \"%s\"",
|
|
|
|
n, size, src->file->name.data);
|
2009-09-22 17:44:57 +08:00
|
|
|
return NGX_ERROR;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dst->last += n;
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
if (sendfile) {
|
|
|
|
dst->in_file = 1;
|
|
|
|
dst->file = src->file;
|
|
|
|
dst->file_pos = src->file_pos;
|
2004-10-21 23:34:38 +08:00
|
|
|
dst->file_last = src->file_pos + n;
|
2004-10-11 23:07:03 +08:00
|
|
|
|
|
|
|
} else {
|
2004-05-28 23:49:23 +08:00
|
|
|
dst->in_file = 0;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
src->file_pos += n;
|
|
|
|
|
2007-06-06 13:56:51 +08:00
|
|
|
if (src->file_pos == src->file_last) {
|
2007-06-04 03:58:30 +08:00
|
|
|
dst->flush = src->flush;
|
|
|
|
dst->last_buf = src->last_buf;
|
2009-05-28 23:42:27 +08:00
|
|
|
dst->last_in_chain = src->last_in_chain;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2003-10-28 05:01:00 +08:00
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_chain_writer(void *data, ngx_chain_t *in)
|
2003-10-28 05:01:00 +08:00
|
|
|
{
|
2003-10-30 16:51:06 +08:00
|
|
|
ngx_chain_writer_ctx_t *ctx = data;
|
2003-10-28 05:01:00 +08:00
|
|
|
|
2008-04-10 02:56:36 +08:00
|
|
|
off_t size;
|
|
|
|
ngx_chain_t *cl;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
|
|
|
|
c = ctx->connection;
|
2003-10-28 05:01:00 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
for (size = 0; in; in = in->next) {
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
if (ngx_buf_size(in->buf) == 0 && !ngx_buf_special(in->buf)) {
|
|
|
|
ngx_debug_point();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size += ngx_buf_size(in->buf);
|
2004-08-27 23:40:59 +08:00
|
|
|
|
2008-04-10 02:56:36 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, c->log, 0,
|
2007-06-04 03:56:27 +08:00
|
|
|
"chain writer buf fl:%d s:%uO",
|
|
|
|
in->buf->flush, ngx_buf_size(in->buf));
|
2004-08-27 23:40:59 +08:00
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
cl = ngx_alloc_chain_link(ctx->pool);
|
|
|
|
if (cl == NULL) {
|
2004-11-11 22:07:14 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2005-03-19 20:38:37 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
cl->buf = in->buf;
|
|
|
|
cl->next = NULL;
|
2003-10-28 05:01:00 +08:00
|
|
|
*ctx->last = cl;
|
|
|
|
ctx->last = &cl->next;
|
|
|
|
}
|
|
|
|
|
2008-04-10 02:56:36 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"chain writer in: %p", ctx->out);
|
2004-03-16 21:35:20 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
for (cl = ctx->out; cl; cl = cl->next) {
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
|
|
|
|
ngx_debug_point();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size += ngx_buf_size(cl->buf);
|
|
|
|
}
|
|
|
|
|
2008-04-10 02:56:36 +08:00
|
|
|
if (size == 0 && !c->buffered) {
|
2004-12-03 02:40:46 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2008-04-10 02:56:36 +08:00
|
|
|
ctx->out = c->send_chain(c, ctx->out, ctx->limit);
|
2003-10-28 05:01:00 +08:00
|
|
|
|
2008-04-10 02:56:36 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"chain writer out: %p", ctx->out);
|
2004-03-16 21:35:20 +08:00
|
|
|
|
2003-10-28 05:01:00 +08:00
|
|
|
if (ctx->out == NGX_CHAIN_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->out == NULL) {
|
|
|
|
ctx->last = &ctx->out;
|
2007-05-28 19:09:18 +08:00
|
|
|
|
2008-04-10 02:56:36 +08:00
|
|
|
if (!c->buffered) {
|
2007-05-29 20:06:42 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
2003-10-28 05:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|