comparison src/http/modules/ngx_http_mirror_module.c @ 7072:0bb747b2d7cb

Mirror module.
author Roman Arutyunyan <arut@nginx.com>
date Thu, 20 Jul 2017 08:50:49 +0300
parents
children 230d16d35ebc
comparison
equal deleted inserted replaced
7071:cce6936ed2f4 7072:0bb747b2d7cb
1
2 /*
3 * Copyright (C) Roman Arutyunyan
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
11
12
13 typedef struct {
14 ngx_array_t *mirror;
15 ngx_flag_t request_body;
16 } ngx_http_mirror_loc_conf_t;
17
18
19 typedef struct {
20 ngx_int_t status;
21 } ngx_http_mirror_ctx_t;
22
23
24 static ngx_int_t ngx_http_mirror_handler(ngx_http_request_t *r);
25 static void ngx_http_mirror_body_handler(ngx_http_request_t *r);
26 static ngx_int_t ngx_http_mirror_handler_internal(ngx_http_request_t *r);
27 static void *ngx_http_mirror_create_loc_conf(ngx_conf_t *cf);
28 static char *ngx_http_mirror_merge_loc_conf(ngx_conf_t *cf, void *parent,
29 void *child);
30 static ngx_int_t ngx_http_mirror_init(ngx_conf_t *cf);
31
32
33 static ngx_command_t ngx_http_mirror_commands[] = {
34
35 { ngx_string("mirror"),
36 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
37 ngx_conf_set_str_array_slot,
38 NGX_HTTP_LOC_CONF_OFFSET,
39 offsetof(ngx_http_mirror_loc_conf_t, mirror),
40 NULL },
41
42 { ngx_string("mirror_request_body"),
43 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
44 ngx_conf_set_flag_slot,
45 NGX_HTTP_LOC_CONF_OFFSET,
46 offsetof(ngx_http_mirror_loc_conf_t, request_body),
47 NULL },
48
49 ngx_null_command
50 };
51
52
53 static ngx_http_module_t ngx_http_mirror_module_ctx = {
54 NULL, /* preconfiguration */
55 ngx_http_mirror_init, /* postconfiguration */
56
57 NULL, /* create main configuration */
58 NULL, /* init main configuration */
59
60 NULL, /* create server configuration */
61 NULL, /* merge server configuration */
62
63 ngx_http_mirror_create_loc_conf, /* create location configuration */
64 ngx_http_mirror_merge_loc_conf /* merge location configuration */
65 };
66
67
68 ngx_module_t ngx_http_mirror_module = {
69 NGX_MODULE_V1,
70 &ngx_http_mirror_module_ctx, /* module context */
71 ngx_http_mirror_commands, /* module directives */
72 NGX_HTTP_MODULE, /* module type */
73 NULL, /* init master */
74 NULL, /* init module */
75 NULL, /* init process */
76 NULL, /* init thread */
77 NULL, /* exit thread */
78 NULL, /* exit process */
79 NULL, /* exit master */
80 NGX_MODULE_V1_PADDING
81 };
82
83
84 static ngx_int_t
85 ngx_http_mirror_handler(ngx_http_request_t *r)
86 {
87 ngx_int_t rc;
88 ngx_http_mirror_ctx_t *ctx;
89 ngx_http_mirror_loc_conf_t *mlcf;
90
91 if (r != r->main) {
92 return NGX_DECLINED;
93 }
94
95 mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mirror_module);
96
97 if (mlcf->mirror == NULL) {
98 return NGX_DECLINED;
99 }
100
101 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mirror handler");
102
103 if (mlcf->request_body) {
104 ctx = ngx_http_get_module_ctx(r, ngx_http_mirror_module);
105
106 if (ctx) {
107 return ctx->status;
108 }
109
110 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_mirror_ctx_t));
111 if (ctx == NULL) {
112 return NGX_ERROR;
113 }
114
115 ctx->status = NGX_DONE;
116
117 ngx_http_set_ctx(r, ctx, ngx_http_mirror_module);
118
119 rc = ngx_http_read_client_request_body(r, ngx_http_mirror_body_handler);
120 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
121 return rc;
122 }
123
124 ngx_http_finalize_request(r, NGX_DONE);
125 return NGX_DONE;
126 }
127
128 return ngx_http_mirror_handler_internal(r);
129 }
130
131
132 static void
133 ngx_http_mirror_body_handler(ngx_http_request_t *r)
134 {
135 ngx_http_mirror_ctx_t *ctx;
136
137 ctx = ngx_http_get_module_ctx(r, ngx_http_mirror_module);
138
139 ctx->status = ngx_http_mirror_handler_internal(r);
140
141 r->preserve_body = 1;
142
143 r->write_event_handler = ngx_http_core_run_phases;
144 ngx_http_core_run_phases(r);
145 }
146
147
148 static ngx_int_t
149 ngx_http_mirror_handler_internal(ngx_http_request_t *r)
150 {
151 ngx_str_t *name;
152 ngx_uint_t i;
153 ngx_http_request_t *sr;
154 ngx_http_mirror_loc_conf_t *mlcf;
155
156 mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mirror_module);
157
158 name = mlcf->mirror->elts;
159
160 for (i = 0; i < mlcf->mirror->nelts; i++) {
161 if (ngx_http_subrequest(r, &name[i], &r->args, &sr, NULL,
162 NGX_HTTP_SUBREQUEST_BACKGROUND)
163 != NGX_OK)
164 {
165 return NGX_HTTP_INTERNAL_SERVER_ERROR;
166 }
167
168 sr->header_only = 1;
169 sr->method = r->method;
170 sr->method_name = r->method_name;
171 }
172
173 return NGX_DECLINED;
174 }
175
176
177 static void *
178 ngx_http_mirror_create_loc_conf(ngx_conf_t *cf)
179 {
180 ngx_http_mirror_loc_conf_t *mlcf;
181
182 mlcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mirror_loc_conf_t));
183 if (mlcf == NULL) {
184 return NULL;
185 }
186
187 mlcf->mirror = NGX_CONF_UNSET_PTR;
188 mlcf->request_body = NGX_CONF_UNSET;
189
190 return mlcf;
191 }
192
193
194 static char *
195 ngx_http_mirror_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
196 {
197 ngx_http_mirror_loc_conf_t *prev = parent;
198 ngx_http_mirror_loc_conf_t *conf = child;
199
200 ngx_conf_merge_ptr_value(conf->mirror, prev->mirror, NULL);
201 ngx_conf_merge_value(conf->request_body, prev->request_body, 1);
202
203 return NGX_CONF_OK;
204 }
205
206
207 static ngx_int_t
208 ngx_http_mirror_init(ngx_conf_t *cf)
209 {
210 ngx_http_handler_pt *h;
211 ngx_http_core_main_conf_t *cmcf;
212
213 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
214
215 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers);
216 if (h == NULL) {
217 return NGX_ERROR;
218 }
219
220 *h = ngx_http_mirror_handler;
221
222 return NGX_OK;
223 }