diff src/http/ngx_http_output_filter.c @ 160:e7e094d34162

nginx-0.0.1-2003-10-27-11:53:49 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 27 Oct 2003 08:53:49 +0000
parents 46eb23d9471d
children 4db54fdbcbe7
line wrap: on
line diff
--- a/src/http/ngx_http_output_filter.c
+++ b/src/http/ngx_http_output_filter.c
@@ -9,33 +9,6 @@ typedef struct {
 } 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);
@@ -79,244 +52,34 @@ ngx_module_t  ngx_http_output_filter_mod
 
 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_output_chain_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;
-    }
+        conf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
+                                            ngx_http_output_filter_module);
 
-    /*
-     * 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) {
+        ngx_http_create_ctx(r, ctx, ngx_http_output_filter_module,
+                            sizeof(ngx_output_chain_ctx_t), NGX_ERROR);
 
-        if (in == NULL) {
-            return ngx_http_top_body_filter(r, in);
-        }
+        ctx->sendfile = r->sendfile;
+        ctx->need_in_memory = r->filter & NGX_HTTP_FILTER_NEED_IN_MEMORY;
+        ctx->need_in_temp = r->filter & NGX_HTTP_FILTER_NEED_TEMP;
 
-        if (in->next == NULL
-            && (!ngx_http_output_filter_need_to_copy(r, in->hunk)))
-        {
-            return ngx_http_top_body_filter(r, in);
-        }
-    }
+        ctx->pool = r->pool;
+        ctx->bufs = conf->bufs;
+        ctx->tag = (ngx_hunk_tag_t) &ngx_http_output_filter_module;
 
-    /* add the incoming hunk to the chain ctx->in */
+        ctx->output_filter = (ngx_output_chain_filter_pt)
+                                                      ngx_http_top_body_filter;
+        ctx->output_ctx = r;
 
-    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;
+    return ngx_output_chain(ctx, in);
 }
 
 
@@ -340,7 +103,7 @@ static char *ngx_http_output_filter_merg
     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);
+    ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 1, 32768);
 
     return NULL;
 }