50
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
|
13 ngx_bufs_t bufs;
|
|
14 } ngx_http_copy_filter_conf_t;
|
|
15
|
|
16
|
|
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_cycle_t *cycle);
|
|
21
|
|
22
|
|
23 static ngx_command_t ngx_http_copy_filter_commands[] = {
|
|
24
|
|
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),
|
|
30 NULL },
|
|
31
|
|
32 ngx_null_command
|
|
33 };
|
|
34
|
|
35
|
|
36 static ngx_http_module_t ngx_http_copy_filter_module_ctx = {
|
58
|
37 NULL, /* preconfiguration */
|
|
38 NULL, /* postconfiguration */
|
50
|
39
|
|
40 NULL, /* create main configuration */
|
|
41 NULL, /* init main configuration */
|
|
42
|
|
43 NULL, /* create server configuration */
|
|
44 NULL, /* merge server configuration */
|
|
45
|
|
46 ngx_http_copy_filter_create_conf, /* create location configuration */
|
|
47 ngx_http_copy_filter_merge_conf /* merge location configuration */
|
|
48 };
|
|
49
|
|
50
|
|
51 ngx_module_t ngx_http_copy_filter_module = {
|
58
|
52 NGX_MODULE_V1,
|
50
|
53 &ngx_http_copy_filter_module_ctx, /* module context */
|
|
54 ngx_http_copy_filter_commands, /* module directives */
|
|
55 NGX_HTTP_MODULE, /* module type */
|
90
|
56 NULL, /* init master */
|
50
|
57 ngx_http_copy_filter_init, /* init module */
|
90
|
58 NULL, /* init process */
|
|
59 NULL, /* init thread */
|
|
60 NULL, /* exit thread */
|
|
61 NULL, /* exit process */
|
|
62 NULL, /* exit master */
|
|
63 NGX_MODULE_V1_PADDING
|
50
|
64 };
|
|
65
|
|
66
|
|
67 static ngx_http_output_body_filter_pt ngx_http_next_filter;
|
|
68
|
|
69
|
|
70 static ngx_int_t
|
|
71 ngx_http_copy_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
72 {
|
58
|
73 ngx_int_t rc;
|
50
|
74 ngx_output_chain_ctx_t *ctx;
|
|
75 ngx_http_copy_filter_conf_t *conf;
|
|
76
|
58
|
77 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
78 "copy filter: \"%V\"", &r->uri);
|
|
79
|
76
|
80 if (r->connection->closed) {
|
|
81 rc = ngx_http_next_filter(r, in);
|
|
82
|
|
83 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
84 "copy closed filter: %i \"%V\"", rc, &r->uri);
|
|
85 return rc;
|
|
86 }
|
|
87
|
58
|
88 ctx = ngx_http_get_module_ctx(r, ngx_http_copy_filter_module);
|
50
|
89
|
|
90 if (ctx == NULL) {
|
58
|
91 conf = ngx_http_get_module_loc_conf(r, ngx_http_copy_filter_module);
|
50
|
92
|
|
93 ctx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t));
|
|
94 if (ctx == NULL) {
|
|
95 return NGX_ERROR;
|
|
96 }
|
|
97
|
|
98 ngx_http_set_ctx(r, ctx, ngx_http_copy_filter_module);
|
|
99
|
|
100 ctx->sendfile = r->connection->sendfile;
|
62
|
101 ctx->need_in_memory = r->main_filter_need_in_memory
|
|
102 || r->filter_need_in_memory;
|
50
|
103 ctx->need_in_temp = r->filter_need_temporary;
|
|
104
|
|
105 ctx->pool = r->pool;
|
|
106 ctx->bufs = conf->bufs;
|
|
107 ctx->tag = (ngx_buf_tag_t) &ngx_http_copy_filter_module;
|
|
108
|
|
109 ctx->output_filter = (ngx_output_chain_filter_pt) ngx_http_next_filter;
|
|
110 ctx->filter_ctx = r;
|
|
111
|
|
112 }
|
|
113
|
58
|
114 rc = ngx_output_chain(ctx, in);
|
|
115
|
|
116 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
117 "copy filter: %i \"%V\"", rc, &r->uri);
|
|
118
|
|
119 return rc;
|
50
|
120 }
|
|
121
|
|
122
|
|
123 static void *
|
|
124 ngx_http_copy_filter_create_conf(ngx_conf_t *cf)
|
|
125 {
|
|
126 ngx_http_copy_filter_conf_t *conf;
|
|
127
|
|
128 conf = ngx_palloc(cf->pool, sizeof(ngx_http_copy_filter_conf_t));
|
|
129 if (conf == NULL) {
|
|
130 return NULL;
|
|
131 }
|
|
132
|
|
133 conf->bufs.num = 0;
|
|
134
|
|
135 return conf;
|
|
136 }
|
|
137
|
|
138
|
|
139 static char *
|
|
140 ngx_http_copy_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
141 {
|
|
142 ngx_http_copy_filter_conf_t *prev = parent;
|
|
143 ngx_http_copy_filter_conf_t *conf = child;
|
|
144
|
|
145 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 1, 32768);
|
|
146
|
|
147 return NULL;
|
|
148 }
|
|
149
|
|
150
|
|
151 static ngx_int_t
|
|
152 ngx_http_copy_filter_init(ngx_cycle_t *cycle)
|
|
153 {
|
|
154 ngx_http_next_filter = ngx_http_top_body_filter;
|
|
155 ngx_http_top_body_filter = ngx_http_copy_filter;
|
|
156
|
|
157 return NGX_OK;
|
|
158 }
|
|
159
|