Mercurial > hg > nginx-mail
comparison src/http/modules/ngx_http_userid_filter_module.c @ 50:72eb30262aac NGINX_0_1_25
nginx 0.1.25
*) Bugfix: nginx did run on Linux parisc.
*) Feature: nginx now does not start under FreeBSD if the sysctl
kern.ipc.somaxconn value is too big.
*) Bugfix: if a request was internally redirected by the
ngx_http_index_module module to the ngx_http_proxy_module or
ngx_http_fastcgi_module modules, then the index file was not closed
after request completion.
*) Feature: the "proxy_pass" can be used in location with regular
expression.
*) Feature: the ngx_http_rewrite_filter_module module supports the
condition like "if ($HTTP_USER_AGENT ~ MSIE)".
*) Bugfix: nginx started too slow if the large number of addresses and
text values were used in the "geo" directive.
*) Change: a variable name must be declared as "$name" in the "geo"
directive. The previous variant without "$" is still supported, but
will be removed soon.
*) Feature: the "%{VARIABLE}v" logging parameter.
*) Feature: the "set $name value" directive.
*) Bugfix: gcc 4.0 compatibility.
*) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Sat, 19 Mar 2005 00:00:00 +0300 |
parents | src/http/modules/ngx_http_userid_filter.c@6cfc63e68377 |
children | 0d75d65c642f |
comparison
equal
deleted
inserted
replaced
49:93dabbc9efb9 | 50:72eb30262aac |
---|---|
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 #define NGX_HTTP_USERID_OFF 0 | |
13 #define NGX_HTTP_USERID_LOG 1 | |
14 #define NGX_HTTP_USERID_V1 2 | |
15 #define NGX_HTTP_USERID_ON 3 | |
16 | |
17 /* 31 Dec 2037 23:55:55 GMT */ | |
18 #define NGX_HTTP_USERID_MAX_EXPIRES 2145916555 | |
19 | |
20 | |
21 typedef struct { | |
22 ngx_flag_t enable; | |
23 | |
24 ngx_int_t service; | |
25 | |
26 ngx_str_t name; | |
27 ngx_str_t domain; | |
28 ngx_str_t path; | |
29 time_t expires; | |
30 ngx_str_t p3p; | |
31 } ngx_http_userid_conf_t; | |
32 | |
33 | |
34 typedef struct { | |
35 uint32_t uid_got[4]; | |
36 uint32_t uid_set[4]; | |
37 } ngx_http_userid_ctx_t; | |
38 | |
39 | |
40 static ngx_int_t ngx_http_userid_get_uid(ngx_http_request_t *r, | |
41 ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf); | |
42 static ngx_int_t ngx_http_userid_set_uid(ngx_http_request_t *r, | |
43 ngx_http_userid_ctx_t *ctx, ngx_http_userid_conf_t *conf); | |
44 | |
45 static size_t ngx_http_userid_log_uid_got_getlen(ngx_http_request_t *r, | |
46 uintptr_t data); | |
47 static u_char *ngx_http_userid_log_uid_got(ngx_http_request_t *r, u_char *buf, | |
48 ngx_http_log_op_t *op); | |
49 static size_t ngx_http_userid_log_uid_set_getlen(ngx_http_request_t *r, | |
50 uintptr_t data); | |
51 static u_char *ngx_http_userid_log_uid_set(ngx_http_request_t *r, u_char *buf, | |
52 ngx_http_log_op_t *op); | |
53 | |
54 static ngx_int_t ngx_http_userid_add_log_formats(ngx_conf_t *cf); | |
55 static ngx_int_t ngx_http_userid_init(ngx_cycle_t *cycle); | |
56 static void *ngx_http_userid_create_conf(ngx_conf_t *cf); | |
57 static char *ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent, | |
58 void *child); | |
59 static char *ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data); | |
60 static char *ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data); | |
61 static char *ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd, | |
62 void *conf); | |
63 static char *ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data); | |
64 | |
65 | |
66 static uint32_t sequencer_v1 = 1; | |
67 static uint32_t sequencer_v2 = 0x03030302; | |
68 | |
69 | |
70 static u_char expires[] = "; expires=Thu, 31-Dec-37 23:55:55 GMT"; | |
71 | |
72 | |
73 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
74 | |
75 | |
76 static ngx_conf_enum_t ngx_http_userid_state[] = { | |
77 { ngx_string("off"), NGX_HTTP_USERID_OFF }, | |
78 { ngx_string("log"), NGX_HTTP_USERID_LOG }, | |
79 { ngx_string("v1"), NGX_HTTP_USERID_V1 }, | |
80 { ngx_string("on"), NGX_HTTP_USERID_ON }, | |
81 { ngx_null_string, 0 } | |
82 }; | |
83 | |
84 | |
85 static ngx_conf_post_handler_pt ngx_http_userid_domain_p = | |
86 ngx_http_userid_domain; | |
87 | |
88 static ngx_conf_post_handler_pt ngx_http_userid_path_p = ngx_http_userid_path; | |
89 static ngx_conf_post_handler_pt ngx_http_userid_p3p_p = ngx_http_userid_p3p; | |
90 | |
91 | |
92 static ngx_command_t ngx_http_userid_commands[] = { | |
93 | |
94 { ngx_string("userid"), | |
95 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
96 ngx_conf_set_enum_slot, | |
97 NGX_HTTP_LOC_CONF_OFFSET, | |
98 offsetof(ngx_http_userid_conf_t, enable), | |
99 ngx_http_userid_state }, | |
100 | |
101 { ngx_string("userid_service"), | |
102 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
103 ngx_conf_set_num_slot, | |
104 NGX_HTTP_LOC_CONF_OFFSET, | |
105 offsetof(ngx_http_userid_conf_t, service), | |
106 NULL }, | |
107 | |
108 { ngx_string("userid_name"), | |
109 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
110 ngx_conf_set_str_slot, | |
111 NGX_HTTP_LOC_CONF_OFFSET, | |
112 offsetof(ngx_http_userid_conf_t, name), | |
113 NULL }, | |
114 | |
115 { ngx_string("userid_domain"), | |
116 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
117 ngx_conf_set_str_slot, | |
118 NGX_HTTP_LOC_CONF_OFFSET, | |
119 offsetof(ngx_http_userid_conf_t, domain), | |
120 &ngx_http_userid_domain_p }, | |
121 | |
122 { ngx_string("userid_path"), | |
123 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
124 ngx_conf_set_str_slot, | |
125 NGX_HTTP_LOC_CONF_OFFSET, | |
126 offsetof(ngx_http_userid_conf_t, path), | |
127 &ngx_http_userid_path_p }, | |
128 | |
129 { ngx_string("userid_expires"), | |
130 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
131 ngx_http_userid_expires, | |
132 NGX_HTTP_LOC_CONF_OFFSET, | |
133 0, | |
134 NULL }, | |
135 | |
136 { ngx_string("userid_p3p"), | |
137 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
138 ngx_conf_set_str_slot, | |
139 NGX_HTTP_LOC_CONF_OFFSET, | |
140 offsetof(ngx_http_userid_conf_t, p3p), | |
141 &ngx_http_userid_p3p_p }, | |
142 | |
143 ngx_null_command | |
144 }; | |
145 | |
146 | |
147 ngx_http_module_t ngx_http_userid_filter_module_ctx = { | |
148 ngx_http_userid_add_log_formats, /* pre conf */ | |
149 | |
150 NULL, /* create main configuration */ | |
151 NULL, /* init main configuration */ | |
152 | |
153 NULL, /* create server configuration */ | |
154 NULL, /* merge server configuration */ | |
155 | |
156 ngx_http_userid_create_conf, /* create location configration */ | |
157 ngx_http_userid_merge_conf /* merge location configration */ | |
158 }; | |
159 | |
160 | |
161 ngx_module_t ngx_http_userid_filter_module = { | |
162 NGX_MODULE, | |
163 &ngx_http_userid_filter_module_ctx, /* module context */ | |
164 ngx_http_userid_commands, /* module directives */ | |
165 NGX_HTTP_MODULE, /* module type */ | |
166 ngx_http_userid_init, /* init module */ | |
167 NULL /* init process */ | |
168 }; | |
169 | |
170 | |
171 static ngx_http_log_op_name_t ngx_http_userid_log_fmt_ops[] = { | |
172 { ngx_string("uid_got"), 0, NULL, | |
173 ngx_http_userid_log_uid_got_getlen, | |
174 ngx_http_userid_log_uid_got }, | |
175 { ngx_string("uid_set"), 0, NULL, | |
176 ngx_http_userid_log_uid_set_getlen, | |
177 ngx_http_userid_log_uid_set }, | |
178 { ngx_null_string, 0, NULL, NULL, NULL } | |
179 }; | |
180 | |
181 | |
182 static ngx_int_t | |
183 ngx_http_userid_filter(ngx_http_request_t *r) | |
184 { | |
185 ngx_int_t rc; | |
186 ngx_http_userid_ctx_t *ctx; | |
187 ngx_http_userid_conf_t *conf; | |
188 | |
189 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module); | |
190 | |
191 if (conf->enable == NGX_HTTP_USERID_OFF) { | |
192 return ngx_http_next_header_filter(r); | |
193 } | |
194 | |
195 | |
196 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_userid_ctx_t)); | |
197 if (ctx == NULL) { | |
198 return NGX_ERROR; | |
199 } | |
200 | |
201 ngx_http_set_ctx(r, ctx, ngx_http_userid_filter_module); | |
202 | |
203 | |
204 rc = ngx_http_userid_get_uid(r, ctx, conf); | |
205 | |
206 if (rc != NGX_OK) { | |
207 return rc; | |
208 } | |
209 | |
210 if (conf->enable == NGX_HTTP_USERID_LOG || ctx->uid_got[3] != 0) { | |
211 return ngx_http_next_header_filter(r); | |
212 } | |
213 | |
214 rc = ngx_http_userid_set_uid(r, ctx, conf); | |
215 | |
216 if (rc != NGX_OK) { | |
217 return rc; | |
218 } | |
219 | |
220 return ngx_http_next_header_filter(r); | |
221 } | |
222 | |
223 | |
224 static ngx_int_t | |
225 ngx_http_userid_get_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx, | |
226 ngx_http_userid_conf_t *conf) | |
227 { | |
228 u_char *start, *last, *end; | |
229 ngx_uint_t i; | |
230 ngx_str_t src, dst; | |
231 ngx_table_elt_t **cookies; | |
232 | |
233 cookies = r->headers_in.cookies.elts; | |
234 | |
235 for (i = 0; i < r->headers_in.cookies.nelts; i++) { | |
236 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
237 "cookie: \"%V\"", &cookies[i]->value); | |
238 | |
239 if (conf->name.len >= cookies[i]->value.len) { | |
240 continue; | |
241 } | |
242 | |
243 start = cookies[i]->value.data; | |
244 end = cookies[i]->value.data + cookies[i]->value.len; | |
245 | |
246 while (start < end) { | |
247 | |
248 if (ngx_strncmp(start, conf->name.data, conf->name.len) != 0) { | |
249 | |
250 while (start < end && *start++ != ';') { /* void */ } | |
251 while (start < end && *start == ' ') { start++; } | |
252 | |
253 continue; | |
254 } | |
255 | |
256 start += conf->name.len; | |
257 | |
258 while (start < end && *start == ' ') { start++; } | |
259 | |
260 if (start == end || *start++ != '=') { | |
261 /* the invalid "Cookie" header */ | |
262 break; | |
263 } | |
264 | |
265 while (start < end && *start == ' ') { start++; } | |
266 | |
267 last = start; | |
268 | |
269 while (last < end && *last++ != ';') { /* void */ } | |
270 | |
271 if (last - start < 22) { | |
272 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
273 "client sent too short userid cookie \"%V\"", | |
274 &cookies[i]->value); | |
275 break; | |
276 } | |
277 | |
278 /* | |
279 * we have to limit encoded string to 22 characters | |
280 * because there are already the millions cookies with a garbage | |
281 * instead of the correct base64 trail "==" | |
282 */ | |
283 | |
284 src.len = 22; | |
285 src.data = start; | |
286 dst.data = (u_char *) ctx->uid_got; | |
287 | |
288 if (ngx_decode_base64(&dst, &src) == NGX_ERROR) { | |
289 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
290 "client sent invalid userid cookie \"%V\"", | |
291 &cookies[i]->value); | |
292 break; | |
293 } | |
294 | |
295 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
296 "uid: %08XD%08XD%08XD%08XD", | |
297 ctx->uid_got[0], ctx->uid_got[1], | |
298 ctx->uid_got[2], ctx->uid_got[3]); | |
299 | |
300 return NGX_OK; | |
301 } | |
302 } | |
303 | |
304 return NGX_OK; | |
305 } | |
306 | |
307 | |
308 static ngx_int_t | |
309 ngx_http_userid_set_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx, | |
310 ngx_http_userid_conf_t *conf) | |
311 { | |
312 u_char *cookie, *p; | |
313 size_t len; | |
314 socklen_t slen; | |
315 struct sockaddr_in sin; | |
316 ngx_str_t src, dst; | |
317 ngx_table_elt_t *set_cookie, *p3p; | |
318 | |
319 /* TODO: mutex for sequencers */ | |
320 | |
321 if (conf->enable == NGX_HTTP_USERID_V1) { | |
322 if (conf->service == NGX_CONF_UNSET) { | |
323 ctx->uid_set[0] = 0; | |
324 } else { | |
325 ctx->uid_set[0] = htonl(conf->service); | |
326 } | |
327 | |
328 ctx->uid_set[1] = ngx_time(); | |
329 ctx->uid_set[2] = ngx_pid; | |
330 ctx->uid_set[3] = sequencer_v1; | |
331 sequencer_v1 += 0x100; | |
332 | |
333 } else { | |
334 if (conf->service == NGX_CONF_UNSET) { | |
335 if (r->in_addr == 0) { | |
336 slen = sizeof(struct sockaddr_in); | |
337 if (getsockname(r->connection->fd, | |
338 (struct sockaddr *) &sin, &slen) == -1) | |
339 { | |
340 ngx_log_error(NGX_LOG_CRIT, r->connection->log, | |
341 ngx_socket_errno, "getsockname() failed"); | |
342 } | |
343 | |
344 r->in_addr = sin.sin_addr.s_addr; | |
345 } | |
346 | |
347 ctx->uid_set[0] = htonl(r->in_addr); | |
348 | |
349 } else { | |
350 ctx->uid_set[0] = htonl(conf->service); | |
351 } | |
352 | |
353 ctx->uid_set[1] = htonl(ngx_time()); | |
354 ctx->uid_set[2] = htonl(ngx_pid); | |
355 ctx->uid_set[3] = htonl(sequencer_v2); | |
356 sequencer_v2 += 0x100; | |
357 if (sequencer_v2 < 0x03030302) { | |
358 sequencer_v2 = 0x03030302; | |
359 } | |
360 } | |
361 | |
362 len = conf->name.len + 1 + ngx_base64_encoded_length(16) + conf->path.len; | |
363 | |
364 if (conf->expires) { | |
365 len += sizeof(expires) - 1 + 2; | |
366 } | |
367 | |
368 if (conf->domain.len) { | |
369 len += conf->domain.len; | |
370 } | |
371 | |
372 cookie = ngx_palloc(r->pool, len); | |
373 if (cookie == NULL) { | |
374 return NGX_ERROR; | |
375 } | |
376 | |
377 p = ngx_cpymem(cookie, conf->name.data, conf->name.len); | |
378 *p++ = '='; | |
379 | |
380 src.len = 16; | |
381 src.data = (u_char *) ctx->uid_set; | |
382 dst.data = p; | |
383 | |
384 ngx_encode_base64(&dst, &src); | |
385 | |
386 p += dst.len; | |
387 | |
388 if (conf->expires == NGX_HTTP_USERID_MAX_EXPIRES) { | |
389 p = ngx_cpymem(p, expires, sizeof(expires) - 1); | |
390 | |
391 } else if (conf->expires) { | |
392 p = ngx_cpymem(p, expires, sizeof("; expires=") - 1); | |
393 p = ngx_http_cookie_time(p, ngx_time() + conf->expires); | |
394 } | |
395 | |
396 if (conf->domain.len) { | |
397 p = ngx_cpymem(p, conf->domain.data, conf->domain.len); | |
398 } | |
399 | |
400 p = ngx_cpymem(p, conf->path.data, conf->path.len); | |
401 | |
402 set_cookie = ngx_list_push(&r->headers_out.headers); | |
403 if (set_cookie == NULL) { | |
404 return NGX_ERROR; | |
405 } | |
406 | |
407 set_cookie->key.len = sizeof("Set-Cookie") - 1; | |
408 set_cookie->key.data = (u_char *) "Set-Cookie"; | |
409 set_cookie->value.len = p - cookie; | |
410 set_cookie->value.data = cookie; | |
411 | |
412 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
413 "uid cookie: \"%V\"", &set_cookie->value); | |
414 | |
415 if (conf->p3p.len == 0) { | |
416 return NGX_OK; | |
417 } | |
418 | |
419 p3p = ngx_list_push(&r->headers_out.headers); | |
420 if (p3p == NULL) { | |
421 return NGX_ERROR; | |
422 } | |
423 | |
424 p3p->key.len = sizeof("P3P") - 1; | |
425 p3p->key.data = (u_char *) "P3P"; | |
426 p3p->value = conf->p3p; | |
427 | |
428 return NGX_OK; | |
429 } | |
430 | |
431 | |
432 static size_t | |
433 ngx_http_userid_log_uid_got_getlen(ngx_http_request_t *r, uintptr_t data) | |
434 { | |
435 ngx_http_userid_ctx_t *ctx; | |
436 ngx_http_userid_conf_t *conf; | |
437 | |
438 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module); | |
439 | |
440 if (ctx == NULL || ctx->uid_got[3] == 0) { | |
441 return 1; | |
442 } | |
443 | |
444 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module); | |
445 | |
446 return conf->name.len + 1 + 32; | |
447 } | |
448 | |
449 | |
450 static u_char * | |
451 ngx_http_userid_log_uid_got(ngx_http_request_t *r, u_char *buf, | |
452 ngx_http_log_op_t *op) | |
453 { | |
454 ngx_http_userid_ctx_t *ctx; | |
455 ngx_http_userid_conf_t *conf; | |
456 | |
457 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module); | |
458 | |
459 if (ctx == NULL || ctx->uid_got[3] == 0) { | |
460 *buf = '-'; | |
461 return buf + 1; | |
462 } | |
463 | |
464 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module); | |
465 | |
466 buf = ngx_cpymem(buf, conf->name.data, conf->name.len); | |
467 | |
468 *buf++ = '='; | |
469 | |
470 return ngx_sprintf(buf, "%08XD%08XD%08XD%08XD", | |
471 ctx->uid_got[0], ctx->uid_got[1], | |
472 ctx->uid_got[2], ctx->uid_got[3]); | |
473 } | |
474 | |
475 | |
476 static size_t | |
477 ngx_http_userid_log_uid_set_getlen(ngx_http_request_t *r, uintptr_t data) | |
478 { | |
479 ngx_http_userid_ctx_t *ctx; | |
480 ngx_http_userid_conf_t *conf; | |
481 | |
482 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module); | |
483 | |
484 if (ctx == NULL || ctx->uid_set[3] == 0) { | |
485 return 1; | |
486 } | |
487 | |
488 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module); | |
489 | |
490 return conf->name.len + 1 + 32; | |
491 } | |
492 | |
493 | |
494 static u_char * | |
495 ngx_http_userid_log_uid_set(ngx_http_request_t *r, u_char *buf, | |
496 ngx_http_log_op_t *op) | |
497 { | |
498 ngx_http_userid_ctx_t *ctx; | |
499 ngx_http_userid_conf_t *conf; | |
500 | |
501 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module); | |
502 | |
503 if (ctx == NULL || ctx->uid_set[3] == 0) { | |
504 *buf = '-'; | |
505 return buf + 1; | |
506 } | |
507 | |
508 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module); | |
509 | |
510 buf = ngx_cpymem(buf, conf->name.data, conf->name.len); | |
511 | |
512 *buf++ = '='; | |
513 | |
514 return ngx_sprintf(buf, "%08XD%08XD%08XD%08XD", | |
515 ctx->uid_set[0], ctx->uid_set[1], | |
516 ctx->uid_set[2], ctx->uid_set[3]); | |
517 } | |
518 | |
519 | |
520 static ngx_int_t | |
521 ngx_http_userid_add_log_formats(ngx_conf_t *cf) | |
522 { | |
523 ngx_http_log_op_name_t *op; | |
524 | |
525 for (op = ngx_http_userid_log_fmt_ops; op->name.len; op++) { /* void */ } | |
526 op->run = NULL; | |
527 | |
528 for (op = ngx_http_log_fmt_ops; op->run; op++) { | |
529 if (op->name.len == 0) { | |
530 op = (ngx_http_log_op_name_t *) op->run; | |
531 } | |
532 } | |
533 | |
534 op->run = (ngx_http_log_op_run_pt) ngx_http_userid_log_fmt_ops; | |
535 | |
536 return NGX_OK; | |
537 } | |
538 | |
539 | |
540 static ngx_int_t | |
541 ngx_http_userid_init(ngx_cycle_t *cycle) | |
542 { | |
543 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
544 ngx_http_top_header_filter = ngx_http_userid_filter; | |
545 | |
546 return NGX_OK; | |
547 } | |
548 | |
549 | |
550 static void * | |
551 ngx_http_userid_create_conf(ngx_conf_t *cf) | |
552 { | |
553 ngx_http_userid_conf_t *conf; | |
554 | |
555 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_userid_conf_t)); | |
556 if (conf == NULL) { | |
557 return NGX_CONF_ERROR; | |
558 } | |
559 | |
560 /* | |
561 * set by ngx_pcalloc(): | |
562 * | |
563 * conf->name.len = 0; | |
564 * conf->name.date = NULL; | |
565 * conf->domain.len = 0; | |
566 * conf->domain.date = NULL; | |
567 * conf->path.len = 0; | |
568 * conf->path.date = NULL; | |
569 * conf->p3p.len = 0; | |
570 * conf->p3p.date = NULL; | |
571 */ | |
572 | |
573 conf->enable = NGX_CONF_UNSET; | |
574 conf->service = NGX_CONF_UNSET; | |
575 conf->expires = NGX_CONF_UNSET; | |
576 | |
577 return conf; | |
578 } | |
579 | |
580 | |
581 static char * | |
582 ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
583 { | |
584 ngx_http_userid_conf_t *prev = parent; | |
585 ngx_http_userid_conf_t *conf = child; | |
586 | |
587 ngx_conf_merge_value(conf->enable, prev->enable, NGX_HTTP_USERID_OFF); | |
588 | |
589 ngx_conf_merge_str_value(conf->name, prev->name, "uid"); | |
590 ngx_conf_merge_str_value(conf->domain, prev->domain, ""); | |
591 ngx_conf_merge_str_value(conf->path, prev->path, "; path=/"); | |
592 ngx_conf_merge_str_value(conf->p3p, prev->p3p, ""); | |
593 | |
594 ngx_conf_merge_value(conf->service, prev->service, NGX_CONF_UNSET); | |
595 ngx_conf_merge_sec_value(conf->expires, prev->expires, 0); | |
596 | |
597 return NGX_CONF_OK; | |
598 } | |
599 | |
600 | |
601 static char * | |
602 ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data) | |
603 { | |
604 ngx_str_t *domain = data; | |
605 | |
606 u_char *p, *new; | |
607 | |
608 if (domain->len == 4 && ngx_strcmp(domain->data, "none") == 0) { | |
609 domain->len = 0; | |
610 domain->data = (u_char *) ""; | |
611 | |
612 return NGX_CONF_OK; | |
613 } | |
614 | |
615 new = ngx_palloc(cf->pool, sizeof("; domain=") - 1 + domain->len); | |
616 if (new == NULL) { | |
617 return NGX_CONF_ERROR; | |
618 } | |
619 | |
620 p = ngx_cpymem(new, "; domain=", sizeof("; domain=") - 1); | |
621 ngx_memcpy(p, domain->data, domain->len); | |
622 | |
623 domain->len += sizeof("; domain=") - 1; | |
624 domain->data = new; | |
625 | |
626 return NGX_CONF_OK; | |
627 } | |
628 | |
629 | |
630 static char * | |
631 ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data) | |
632 { | |
633 ngx_str_t *path = data; | |
634 | |
635 u_char *p, *new; | |
636 | |
637 new = ngx_palloc(cf->pool, sizeof("; path=") - 1 + path->len); | |
638 if (new == NULL) { | |
639 return NGX_CONF_ERROR; | |
640 } | |
641 | |
642 p = ngx_cpymem(new, "; path=", sizeof("; path=") - 1); | |
643 ngx_memcpy(p, path->data, path->len); | |
644 | |
645 path->len += sizeof("; path=") - 1; | |
646 path->data = new; | |
647 | |
648 return NGX_CONF_OK; | |
649 } | |
650 | |
651 | |
652 static char * | |
653 ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
654 { | |
655 ngx_http_userid_conf_t *ucf = conf; | |
656 | |
657 ngx_str_t *value; | |
658 | |
659 if (ucf->expires != NGX_CONF_UNSET) { | |
660 return "is duplicate"; | |
661 } | |
662 | |
663 value = cf->args->elts; | |
664 | |
665 if (ngx_strcmp(value[1].data, "max") == 0) { | |
666 ucf->expires = NGX_HTTP_USERID_MAX_EXPIRES; | |
667 return NGX_CONF_OK; | |
668 } | |
669 | |
670 if (ngx_strcmp(value[1].data, "off") == 0) { | |
671 ucf->expires = 0; | |
672 return NGX_CONF_OK; | |
673 } | |
674 | |
675 ucf->expires = ngx_parse_time(&value[1], 1); | |
676 if (ucf->expires == NGX_ERROR) { | |
677 return "invalid value"; | |
678 } | |
679 | |
680 if (ucf->expires == NGX_PARSE_LARGE_TIME) { | |
681 return "value must be less than 68 years"; | |
682 } | |
683 | |
684 return NGX_CONF_OK; | |
685 } | |
686 | |
687 | |
688 static char * | |
689 ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data) | |
690 { | |
691 ngx_str_t *p3p = data; | |
692 | |
693 if (p3p->len == 4 && ngx_strcmp(p3p->data, "none") == 0) { | |
694 p3p->len = 0; | |
695 p3p->data = (u_char *) ""; | |
696 } | |
697 | |
698 return NGX_CONF_OK; | |
699 } |