3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
14 } ngx_http_copy_filter_conf_t;
17 static void *ngx_http_copy_filter_create_conf(ngx_conf_t *cf);
18 static char *ngx_http_copy_filter_merge_conf(ngx_conf_t *cf,
19 void *parent, void *child);
20 static ngx_int_t ngx_http_copy_filter_init(ngx_conf_t *cf);
23 static ngx_command_t ngx_http_copy_filter_commands[] = {
25 { ngx_string("output_buffers"),
26 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
27 ngx_conf_set_bufs_slot,
28 NGX_HTTP_LOC_CONF_OFFSET,
29 offsetof(ngx_http_copy_filter_conf_t, bufs),
36 static ngx_http_module_t ngx_http_copy_filter_module_ctx = {
37 NULL, /* preconfiguration */
38 ngx_http_copy_filter_init, /* postconfiguration */
40 NULL, /* create main configuration */
41 NULL, /* init main configuration */
43 NULL, /* create server configuration */
44 NULL, /* merge server configuration */
46 ngx_http_copy_filter_create_conf, /* create location configuration */
47 ngx_http_copy_filter_merge_conf /* merge location configuration */
51 ngx_module_t ngx_http_copy_filter_module = {
53 &ngx_http_copy_filter_module_ctx, /* module context */
54 ngx_http_copy_filter_commands, /* module directives */
55 NGX_HTTP_MODULE, /* module type */
56 NULL, /* init master */
57 NULL, /* init module */
58 NULL, /* init process */
59 NULL, /* init thread */
60 NULL, /* exit thread */
61 NULL, /* exit process */
62 NULL, /* exit master */
67 static ngx_http_output_body_filter_pt ngx_http_next_filter;
71 ngx_http_copy_filter(ngx_http_request_t *r, ngx_chain_t *in)
75 ngx_output_chain_ctx_t *ctx;
76 ngx_http_copy_filter_conf_t *conf;
80 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
81 "copy filter: \"%V?%V\"", &r->uri, &r->args);
83 ctx = ngx_http_get_module_ctx(r, ngx_http_copy_filter_module);
86 conf = ngx_http_get_module_loc_conf(r, ngx_http_copy_filter_module);
88 ctx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t));
93 ngx_http_set_ctx(r, ctx, ngx_http_copy_filter_module);
95 ctx->sendfile = c->sendfile;
96 ctx->need_in_memory = r->main_filter_need_in_memory
97 || r->filter_need_in_memory;
98 ctx->need_in_temp = r->filter_need_temporary;
101 ctx->bufs = conf->bufs;
102 ctx->tag = (ngx_buf_tag_t) &ngx_http_copy_filter_module;
104 ctx->output_filter = (ngx_output_chain_filter_pt) ngx_http_next_filter;
107 r->request_output = 1;
110 rc = ngx_output_chain(ctx, in);
114 if (ctx->in == NULL) {
115 r->buffered &= ~NGX_HTTP_COPY_BUFFERED;
117 r->buffered |= NGX_HTTP_COPY_BUFFERED;
120 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
121 "copy filter: %i \"%V?%V\"", rc, &r->uri, &r->args);
129 ngx_http_copy_filter_create_conf(ngx_conf_t *cf)
131 ngx_http_copy_filter_conf_t *conf;
133 conf = ngx_palloc(cf->pool, sizeof(ngx_http_copy_filter_conf_t));
145 ngx_http_copy_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
147 ngx_http_copy_filter_conf_t *prev = parent;
148 ngx_http_copy_filter_conf_t *conf = child;
150 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 1, 32768);
157 ngx_http_copy_filter_init(ngx_conf_t *cf)
159 ngx_http_next_filter = ngx_http_top_body_filter;
160 ngx_http_top_body_filter = ngx_http_copy_filter;