Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_split_clients_module.c @ 4464:7bf3b68239a3
Upstream: added callback hook for the "Set-Cookie" header.
No functional changes.
author | Valentin Bartenev <vbart@nginx.com> |
---|---|
date | Mon, 13 Feb 2012 11:01:58 +0000 |
parents | d620f497c50f |
children | ae63013cbffa |
rev | line source |
---|---|
3513 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
3513 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 typedef struct { | |
14 uint32_t percent; | |
15 ngx_http_variable_value_t value; | |
16 } ngx_http_split_clients_part_t; | |
17 | |
18 | |
19 typedef struct { | |
20 ngx_http_complex_value_t value; | |
21 ngx_array_t parts; | |
22 } ngx_http_split_clients_ctx_t; | |
23 | |
24 | |
25 static char *ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd, | |
26 void *conf); | |
27 static char *ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy, | |
28 void *conf); | |
29 | |
30 static ngx_command_t ngx_http_split_clients_commands[] = { | |
31 | |
32 { ngx_string("split_clients"), | |
33 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2, | |
34 ngx_conf_split_clients_block, | |
35 NGX_HTTP_MAIN_CONF_OFFSET, | |
36 0, | |
37 NULL }, | |
38 | |
39 ngx_null_command | |
40 }; | |
41 | |
42 | |
43 static ngx_http_module_t ngx_http_split_clients_module_ctx = { | |
44 NULL, /* preconfiguration */ | |
45 NULL, /* postconfiguration */ | |
46 | |
47 NULL, /* create main configuration */ | |
48 NULL, /* init main configuration */ | |
49 | |
50 NULL, /* create server configuration */ | |
51 NULL, /* merge server configuration */ | |
52 | |
53 NULL, /* create location configuration */ | |
54 NULL /* merge location configuration */ | |
55 }; | |
56 | |
57 | |
58 ngx_module_t ngx_http_split_clients_module = { | |
59 NGX_MODULE_V1, | |
60 &ngx_http_split_clients_module_ctx, /* module context */ | |
61 ngx_http_split_clients_commands, /* module directives */ | |
62 NGX_HTTP_MODULE, /* module type */ | |
63 NULL, /* init master */ | |
64 NULL, /* init module */ | |
65 NULL, /* init process */ | |
66 NULL, /* init thread */ | |
67 NULL, /* exit thread */ | |
68 NULL, /* exit process */ | |
69 NULL, /* exit master */ | |
70 NGX_MODULE_V1_PADDING | |
71 }; | |
72 | |
73 | |
74 static ngx_int_t | |
75 ngx_http_split_clients_variable(ngx_http_request_t *r, | |
76 ngx_http_variable_value_t *v, uintptr_t data) | |
77 { | |
78 ngx_http_split_clients_ctx_t *ctx = (ngx_http_split_clients_ctx_t *) data; | |
79 | |
80 uint32_t hash; | |
81 ngx_str_t val; | |
82 ngx_uint_t i; | |
83 ngx_http_split_clients_part_t *part; | |
84 | |
85 *v = ngx_http_variable_null_value; | |
86 | |
87 if (ngx_http_complex_value(r, &ctx->value, &val) != NGX_OK) { | |
88 return NGX_OK; | |
89 } | |
90 | |
3892
12d8d2f30205
use MurmurHash2 for split_clients, because
Igor Sysoev <igor@sysoev.ru>
parents:
3607
diff
changeset
|
91 hash = ngx_murmur_hash2(val.data, val.len); |
3513 | 92 |
93 part = ctx->parts.elts; | |
94 | |
95 for (i = 0; i < ctx->parts.nelts; i++) { | |
96 | |
97 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3918 | 98 "http split: %uD %uD", hash, part[i].percent); |
3513 | 99 |
100 if (hash < part[i].percent) { | |
101 *v = part[i].value; | |
102 return NGX_OK; | |
103 } | |
104 } | |
105 | |
106 return NGX_OK; | |
107 } | |
108 | |
109 | |
110 static char * | |
111 ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
112 { | |
113 char *rv; | |
114 ngx_str_t *value, name; | |
115 ngx_uint_t i, sum, last; | |
116 ngx_conf_t save; | |
117 ngx_http_variable_t *var; | |
118 ngx_http_split_clients_ctx_t *ctx; | |
119 ngx_http_split_clients_part_t *part; | |
120 ngx_http_compile_complex_value_t ccv; | |
121 | |
122 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_split_clients_ctx_t)); | |
123 if (ctx == NULL) { | |
124 return NGX_CONF_ERROR; | |
125 } | |
126 | |
127 value = cf->args->elts; | |
128 | |
129 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | |
130 | |
131 ccv.cf = cf; | |
132 ccv.value = &value[1]; | |
133 ccv.complex_value = &ctx->value; | |
134 | |
135 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | |
136 return NGX_CONF_ERROR; | |
137 } | |
138 | |
139 name = value[2]; | |
140 name.len--; | |
141 name.data++; | |
142 | |
143 var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); | |
144 if (var == NULL) { | |
145 return NGX_CONF_ERROR; | |
146 } | |
147 | |
148 var->get_handler = ngx_http_split_clients_variable; | |
149 var->data = (uintptr_t) ctx; | |
150 | |
151 if (ngx_array_init(&ctx->parts, cf->pool, 2, | |
152 sizeof(ngx_http_split_clients_part_t)) | |
153 != NGX_OK) | |
154 { | |
155 return NGX_CONF_ERROR; | |
156 } | |
157 | |
158 save = *cf; | |
159 cf->ctx = ctx; | |
160 cf->handler = ngx_http_split_clients; | |
161 cf->handler_conf = conf; | |
162 | |
163 rv = ngx_conf_parse(cf, NULL); | |
164 | |
165 *cf = save; | |
166 | |
3607
8bff43217171
do not try to calculate procent sum if there was an error
Igor Sysoev <igor@sysoev.ru>
parents:
3513
diff
changeset
|
167 if (rv != NGX_CONF_OK) { |
8bff43217171
do not try to calculate procent sum if there was an error
Igor Sysoev <igor@sysoev.ru>
parents:
3513
diff
changeset
|
168 return rv; |
8bff43217171
do not try to calculate procent sum if there was an error
Igor Sysoev <igor@sysoev.ru>
parents:
3513
diff
changeset
|
169 } |
8bff43217171
do not try to calculate procent sum if there was an error
Igor Sysoev <igor@sysoev.ru>
parents:
3513
diff
changeset
|
170 |
3513 | 171 sum = 0; |
172 last = 0; | |
173 part = ctx->parts.elts; | |
174 | |
175 for (i = 0; i < ctx->parts.nelts; i++) { | |
176 sum = part[i].percent ? sum + part[i].percent : 10000; | |
177 if (sum > 10000) { | |
178 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
179 "percent sum is more than 100%%"); | |
180 return NGX_CONF_ERROR; | |
181 } | |
182 | |
183 if (part[i].percent) { | |
184 part[i].percent = (uint32_t) | |
185 (last + 0xffffffff / 10000 * part[i].percent); | |
186 } else { | |
187 part[i].percent = 0xffffffff; | |
188 } | |
189 | |
190 last = part[i].percent; | |
191 } | |
192 | |
193 return rv; | |
194 } | |
195 | |
196 | |
197 static char * | |
198 ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) | |
199 { | |
200 ngx_int_t n; | |
201 ngx_str_t *value; | |
202 ngx_http_split_clients_ctx_t *ctx; | |
203 ngx_http_split_clients_part_t *part; | |
204 | |
205 ctx = cf->ctx; | |
206 value = cf->args->elts; | |
207 | |
208 part = ngx_array_push(&ctx->parts); | |
209 if (part == NULL) { | |
210 return NGX_CONF_ERROR; | |
211 } | |
212 | |
213 if (value[0].len == 1 && value[0].data[0] == '*') { | |
214 part->percent = 0; | |
215 | |
216 } else { | |
217 if (value[0].data[value[0].len - 1] != '%') { | |
218 goto invalid; | |
219 } | |
220 | |
221 n = ngx_atofp(value[0].data, value[0].len - 1, 2); | |
222 if (n == NGX_ERROR || n == 0) { | |
223 goto invalid; | |
224 } | |
225 | |
226 part->percent = (uint32_t) n; | |
227 } | |
228 | |
229 part->value.len = value[1].len; | |
230 part->value.valid = 1; | |
231 part->value.no_cacheable = 0; | |
232 part->value.not_found = 0; | |
233 part->value.data = value[1].data; | |
234 | |
235 return NGX_CONF_OK; | |
236 | |
237 invalid: | |
238 | |
239 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
240 "invalid percent value \"%V\"", &value[0]); | |
241 return NGX_CONF_ERROR; | |
242 } |