Mercurial > hg > nginx
view src/http/ngx_http_output_filter.c @ 155:46eb23d9471d
nginx-0.0.1-2003-10-22-20:38:26 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 22 Oct 2003 16:38:26 +0000 |
parents | eac26585476e |
children | e7e094d34162 |
line wrap: on
line source
#include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> typedef struct { ngx_bufs_t bufs; } ngx_http_output_filter_conf_t; typedef struct { /* * NOTE: we do not need now to store hunk in ctx, * it's needed for the future NGX_FILE_AIO_READ support only */ ngx_hunk_t *hunk; ngx_chain_t *in; /* TODO: out and last_out should be local variables */ ngx_chain_t *out; ngx_chain_t **last_out; /* */ ngx_chain_t *free; ngx_chain_t *busy; int hunks; } ngx_http_output_filter_ctx_t; ngx_inline static int ngx_http_output_filter_need_to_copy(ngx_http_request_t *r, ngx_hunk_t *hunk); static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src, int sendfile); static void *ngx_http_output_filter_create_conf(ngx_conf_t *cf); static char *ngx_http_output_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child); static ngx_command_t ngx_http_output_filter_commands[] = { {ngx_string("output_buffers"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, ngx_conf_set_bufs_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_output_filter_conf_t, bufs), NULL}, ngx_null_command }; static ngx_http_module_t ngx_http_output_filter_module_ctx = { NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_output_filter_create_conf, /* create location configuration */ ngx_http_output_filter_merge_conf /* merge location configuration */ }; ngx_module_t ngx_http_output_filter_module = { NGX_MODULE, &ngx_http_output_filter_module_ctx, /* module context */ ngx_http_output_filter_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init module */ NULL /* init child */ }; int ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in) { int rc, last; ssize_t size; ngx_chain_t *cl; ngx_http_output_filter_ctx_t *ctx; ngx_http_output_filter_conf_t *conf; ctx = ngx_http_get_module_ctx(r->main ? r->main : r, ngx_http_output_filter_module); if (ctx == NULL) { ngx_http_create_ctx(r, ctx, ngx_http_output_filter_module, sizeof(ngx_http_output_filter_ctx_t), NGX_ERROR); ctx->last_out = &ctx->out; } /* * the short path for the case when the chain ctx->in is empty * and the incoming chain is empty too or it has the single hunk * that does not require the copy */ if (ctx->in == NULL) { if (in == NULL) { return ngx_http_top_body_filter(r, in); } if (in->next == NULL && (!ngx_http_output_filter_need_to_copy(r, in->hunk))) { return ngx_http_top_body_filter(r, in); } } /* add the incoming hunk to the chain ctx->in */ if (in) { if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) { return NGX_ERROR; } } conf = ngx_http_get_module_loc_conf(r->main ? r->main : r, ngx_http_output_filter_module); last = NGX_NONE; for ( ;; ) { while (ctx->in) { if (!ngx_http_output_filter_need_to_copy(r, ctx->in->hunk)) { /* move the chain link to the chain ctx->out */ cl = ctx->in; ctx->in = cl->next; *ctx->last_out = cl; ctx->last_out = &cl->next; cl->next = NULL; continue; } if (ctx->hunk == NULL) { /* get the free hunk */ if (ctx->free) { ctx->hunk = ctx->free->hunk; ctx->free = ctx->free->next; } else if (ctx->hunks < conf->bufs.num) { ngx_test_null(ctx->hunk, ngx_create_temp_hunk(r->pool, conf->bufs.size, 0, 0), NGX_ERROR); ctx->hunk->tag = (ngx_hunk_tag_t) &ngx_http_output_filter_module; ctx->hunk->type |= NGX_HUNK_RECYCLED; ctx->hunks++; } else { break; } } rc = ngx_http_output_filter_copy_hunk(ctx->hunk, ctx->in->hunk, r->sendfile); if (rc == NGX_ERROR) { return rc; } #if (NGX_FILE_AIO_READ) if (rc == NGX_AGAIN) { if (ctx->out) { break; } return rc; } #endif if (ctx->in->hunk->type & NGX_HUNK_IN_MEMORY) { size = ctx->in->hunk->last - ctx->in->hunk->pos; } else { size = (size_t) (ctx->in->hunk->file_last - ctx->in->hunk->file_pos); } /* delete the completed hunk from the chain ctx->in */ if (size == 0) { ctx->in = ctx->in->next; } ngx_alloc_link_and_set_hunk(cl, ctx->hunk, r->pool, NGX_ERROR); *ctx->last_out = cl; ctx->last_out = &cl->next; ctx->hunk = NULL; if (ctx->free == NULL) { break; } } if (ctx->out == NULL && last != NGX_NONE) { return last; } last = ngx_http_top_body_filter(r, ctx->out); ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out, (ngx_hunk_tag_t) &ngx_http_output_filter_module); ctx->last_out = &ctx->out; } } ngx_inline static int ngx_http_output_filter_need_to_copy(ngx_http_request_t *r, ngx_hunk_t *hunk) { if (ngx_hunk_special(hunk)) { return 0; } if (!r->sendfile) { return 1; } if ((r->filter & NGX_HTTP_FILTER_NEED_IN_MEMORY) && (!(hunk->type & NGX_HUNK_IN_MEMORY))) { return 1; } if ((r->filter & NGX_HTTP_FILTER_NEED_TEMP) && (hunk->type & (NGX_HUNK_MEMORY|NGX_HUNK_MMAP))) { return 1; } return 0; } static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src, int sendfile) { ssize_t n, size; if (src->type & NGX_HUNK_IN_MEMORY) { size = src->last - src->pos; } else { size = (size_t) (src->file_last - src->file_pos); } if (size > (dst->end - dst->pos)) { size = dst->end - dst->pos; } if (src->type & NGX_HUNK_IN_MEMORY) { ngx_memcpy(dst->pos, src->pos, size); src->pos += size; dst->last += size; if (src->type & NGX_HUNK_FILE) { src->file_pos += size; } if ((src->type & NGX_HUNK_LAST) && src->pos == src->last) { dst->type |= NGX_HUNK_LAST; } } else { n = ngx_read_file(src->file, dst->pos, size, src->file_pos); if (n == 0) { ngx_log_debug(src->file->log, "READ: %qd:%qd %X:%X %X:%X" _ src->file_pos _ src->file_last _ dst->pos _ dst->last _ dst->start _ dst->end); } if (n == NGX_ERROR) { return n; } #if (NGX_FILE_AIO_READ) if (n == NGX_AGAIN) { return n; } #endif if (n != size) { ngx_log_error(NGX_LOG_ALERT, src->file->log, 0, ngx_read_file_n " reads only %d of %d from file", n, size); if (n == 0) { return NGX_ERROR; } } src->file_pos += n; dst->last += n; if (!sendfile) { dst->type &= ~NGX_HUNK_FILE; } if ((src->type & NGX_HUNK_LAST) && src->file_pos == src->file_last) { dst->type |= NGX_HUNK_LAST; } } return NGX_OK; } static void *ngx_http_output_filter_create_conf(ngx_conf_t *cf) { ngx_http_output_filter_conf_t *conf; ngx_test_null(conf, ngx_palloc(cf->pool, sizeof(ngx_http_output_filter_conf_t)), NULL); conf->bufs.num = 0; return conf; } static char *ngx_http_output_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_http_output_filter_conf_t *prev = parent; ngx_http_output_filter_conf_t *conf = child; ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 2, 32768); return NULL; }