comparison src/http/modules/ngx_http_split_clients_module.c @ 570:8246d8a2c2be NGINX_0_8_37

nginx 0.8.37 *) Feature: the ngx_http_split_clients_module. *) Feature: the "map" directive supports keys more than 255 characters. *) Bugfix: nginx ignored the "private" and "no-store" values in the "Cache-Control" backend response header line. *) Bugfix: a "stub" parameter of an "include" SSI directive was not used, if empty response has 200 status code. *) Bugfix: if a proxied or FastCGI request was internally redirected to another proxied or FastCGI location, then a segmentation fault might occur in a worker process; the bug had appeared in 0.8.33. Thanks to Yichun Zhang. *) Bugfix: IMAP connections may hang until they timed out while talking to Zimbra server. Thanks to Alan Batie.
author Igor Sysoev <http://sysoev.ru>
date Mon, 17 May 2010 00:00:00 +0400
parents
children bc110f60c0de
comparison
equal deleted inserted replaced
569:19b134bf21c0 570:8246d8a2c2be
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 uint32_t percent;
14 ngx_http_variable_value_t value;
15 } ngx_http_split_clients_part_t;
16
17
18 typedef struct {
19 ngx_http_complex_value_t value;
20 ngx_array_t parts;
21 } ngx_http_split_clients_ctx_t;
22
23
24 static char *ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd,
25 void *conf);
26 static char *ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy,
27 void *conf);
28
29 static ngx_command_t ngx_http_split_clients_commands[] = {
30
31 { ngx_string("split_clients"),
32 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
33 ngx_conf_split_clients_block,
34 NGX_HTTP_MAIN_CONF_OFFSET,
35 0,
36 NULL },
37
38 ngx_null_command
39 };
40
41
42 static ngx_http_module_t ngx_http_split_clients_module_ctx = {
43 NULL, /* preconfiguration */
44 NULL, /* postconfiguration */
45
46 NULL, /* create main configuration */
47 NULL, /* init main configuration */
48
49 NULL, /* create server configuration */
50 NULL, /* merge server configuration */
51
52 NULL, /* create location configuration */
53 NULL /* merge location configuration */
54 };
55
56
57 ngx_module_t ngx_http_split_clients_module = {
58 NGX_MODULE_V1,
59 &ngx_http_split_clients_module_ctx, /* module context */
60 ngx_http_split_clients_commands, /* module directives */
61 NGX_HTTP_MODULE, /* module type */
62 NULL, /* init master */
63 NULL, /* init module */
64 NULL, /* init process */
65 NULL, /* init thread */
66 NULL, /* exit thread */
67 NULL, /* exit process */
68 NULL, /* exit master */
69 NGX_MODULE_V1_PADDING
70 };
71
72
73 static ngx_int_t
74 ngx_http_split_clients_variable(ngx_http_request_t *r,
75 ngx_http_variable_value_t *v, uintptr_t data)
76 {
77 ngx_http_split_clients_ctx_t *ctx = (ngx_http_split_clients_ctx_t *) data;
78
79 uint32_t hash;
80 ngx_str_t val;
81 ngx_uint_t i;
82 ngx_http_split_clients_part_t *part;
83
84 *v = ngx_http_variable_null_value;
85
86 if (ngx_http_complex_value(r, &ctx->value, &val) != NGX_OK) {
87 return NGX_OK;
88 }
89
90 hash = ngx_crc32_short(val.data, val.len);
91
92 part = ctx->parts.elts;
93
94 for (i = 0; i < ctx->parts.nelts; i++) {
95
96 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
97 "%D %D", hash, part[i].percent);
98
99 if (hash < part[i].percent) {
100 *v = part[i].value;
101 return NGX_OK;
102 }
103 }
104
105 return NGX_OK;
106 }
107
108
109 static char *
110 ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
111 {
112 char *rv;
113 ngx_str_t *value, name;
114 ngx_uint_t i, sum, last;
115 ngx_conf_t save;
116 ngx_http_variable_t *var;
117 ngx_http_split_clients_ctx_t *ctx;
118 ngx_http_split_clients_part_t *part;
119 ngx_http_compile_complex_value_t ccv;
120
121 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_split_clients_ctx_t));
122 if (ctx == NULL) {
123 return NGX_CONF_ERROR;
124 }
125
126 value = cf->args->elts;
127
128 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
129
130 ccv.cf = cf;
131 ccv.value = &value[1];
132 ccv.complex_value = &ctx->value;
133
134 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
135 return NGX_CONF_ERROR;
136 }
137
138 name = value[2];
139 name.len--;
140 name.data++;
141
142 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
143 if (var == NULL) {
144 return NGX_CONF_ERROR;
145 }
146
147 var->get_handler = ngx_http_split_clients_variable;
148 var->data = (uintptr_t) ctx;
149
150 if (ngx_array_init(&ctx->parts, cf->pool, 2,
151 sizeof(ngx_http_split_clients_part_t))
152 != NGX_OK)
153 {
154 return NGX_CONF_ERROR;
155 }
156
157 save = *cf;
158 cf->ctx = ctx;
159 cf->handler = ngx_http_split_clients;
160 cf->handler_conf = conf;
161
162 rv = ngx_conf_parse(cf, NULL);
163
164 *cf = save;
165
166 sum = 0;
167 last = 0;
168 part = ctx->parts.elts;
169
170 for (i = 0; i < ctx->parts.nelts; i++) {
171 sum = part[i].percent ? sum + part[i].percent : 10000;
172 if (sum > 10000) {
173 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
174 "percent sum is more than 100%%");
175 return NGX_CONF_ERROR;
176 }
177
178 if (part[i].percent) {
179 part[i].percent = (uint32_t)
180 (last + 0xffffffff / 10000 * part[i].percent);
181 } else {
182 part[i].percent = 0xffffffff;
183 }
184
185 last = part[i].percent;
186 }
187
188 return rv;
189 }
190
191
192 static char *
193 ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
194 {
195 ngx_int_t n;
196 ngx_str_t *value;
197 ngx_http_split_clients_ctx_t *ctx;
198 ngx_http_split_clients_part_t *part;
199
200 ctx = cf->ctx;
201 value = cf->args->elts;
202
203 part = ngx_array_push(&ctx->parts);
204 if (part == NULL) {
205 return NGX_CONF_ERROR;
206 }
207
208 if (value[0].len == 1 && value[0].data[0] == '*') {
209 part->percent = 0;
210
211 } else {
212 if (value[0].data[value[0].len - 1] != '%') {
213 goto invalid;
214 }
215
216 n = ngx_atofp(value[0].data, value[0].len - 1, 2);
217 if (n == NGX_ERROR || n == 0) {
218 goto invalid;
219 }
220
221 part->percent = (uint32_t) n;
222 }
223
224 part->value.len = value[1].len;
225 part->value.valid = 1;
226 part->value.no_cacheable = 0;
227 part->value.not_found = 0;
228 part->value.data = value[1].data;
229
230 return NGX_CONF_OK;
231
232 invalid:
233
234 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
235 "invalid percent value \"%V\"", &value[0]);
236 return NGX_CONF_ERROR;
237 }