Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_memcached_module.c @ 809:da9c1521319d
AUTH PLAIN LOGIN CRAM-MD5
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 24 Oct 2006 18:38:31 +0000 |
parents | 3095bf59059b |
children | b630109560b7 |
rev | line source |
---|---|
581 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_event.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 typedef struct { | |
14 ngx_http_upstream_conf_t upstream; | |
15 ngx_peers_t *peers; | |
16 } ngx_http_memcached_loc_conf_t; | |
17 | |
18 | |
19 typedef struct { | |
20 size_t rest; | |
21 ngx_http_request_t *request; | |
597 | 22 ngx_str_t key; |
581 | 23 } ngx_http_memcached_ctx_t; |
24 | |
25 | |
26 static ngx_int_t ngx_http_memcached_create_request(ngx_http_request_t *r); | |
27 static ngx_int_t ngx_http_memcached_reinit_request(ngx_http_request_t *r); | |
28 static ngx_int_t ngx_http_memcached_process_header(ngx_http_request_t *r); | |
29 static ngx_int_t ngx_http_memcached_filter_init(void *data); | |
30 static ngx_int_t ngx_http_memcached_filter(void *data, ssize_t bytes); | |
31 static void ngx_http_memcached_abort_request(ngx_http_request_t *r); | |
32 static void ngx_http_memcached_finalize_request(ngx_http_request_t *r, | |
33 ngx_int_t rc); | |
34 | |
35 static void *ngx_http_memcached_create_loc_conf(ngx_conf_t *cf); | |
36 static char *ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, | |
37 void *parent, void *child); | |
38 | |
39 static char *ngx_http_memcached_pass(ngx_conf_t *cf, ngx_command_t *cmd, | |
40 void *conf); | |
41 | |
42 | |
43 static ngx_conf_bitmask_t ngx_http_memcached_next_upstream_masks[] = { | |
44 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, | |
45 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, | |
46 { ngx_string("invalid_response"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, | |
47 { ngx_string("not_found"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, | |
665 | 48 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, |
581 | 49 { ngx_null_string, 0 } |
50 }; | |
51 | |
52 | |
53 static ngx_command_t ngx_http_memcached_commands[] = { | |
54 | |
55 { ngx_string("memcached_pass"), | |
56 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
57 ngx_http_memcached_pass, | |
58 NGX_HTTP_LOC_CONF_OFFSET, | |
59 0, | |
60 NULL }, | |
61 | |
62 { ngx_string("memcached_connect_timeout"), | |
63 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
64 ngx_conf_set_msec_slot, | |
65 NGX_HTTP_LOC_CONF_OFFSET, | |
66 offsetof(ngx_http_memcached_loc_conf_t, upstream.connect_timeout), | |
67 NULL }, | |
68 | |
69 { ngx_string("memcached_send_timeout"), | |
70 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
71 ngx_conf_set_msec_slot, | |
72 NGX_HTTP_LOC_CONF_OFFSET, | |
73 offsetof(ngx_http_memcached_loc_conf_t, upstream.send_timeout), | |
74 NULL }, | |
75 | |
76 { ngx_string("memcached_buffer_size"), | |
77 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
78 ngx_conf_set_size_slot, | |
79 NGX_HTTP_LOC_CONF_OFFSET, | |
80 offsetof(ngx_http_memcached_loc_conf_t, upstream.buffer_size), | |
81 NULL }, | |
82 | |
83 { ngx_string("memcached_read_timeout"), | |
84 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
85 ngx_conf_set_msec_slot, | |
86 NGX_HTTP_LOC_CONF_OFFSET, | |
87 offsetof(ngx_http_memcached_loc_conf_t, upstream.read_timeout), | |
88 NULL }, | |
89 | |
90 { ngx_string("memcached_next_upstream"), | |
91 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
92 ngx_conf_set_bitmask_slot, | |
93 NGX_HTTP_LOC_CONF_OFFSET, | |
94 offsetof(ngx_http_memcached_loc_conf_t, upstream.next_upstream), | |
95 &ngx_http_memcached_next_upstream_masks }, | |
96 | |
97 { ngx_string("memcached_upstream_max_fails"), | |
98 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
99 ngx_conf_set_num_slot, | |
100 NGX_HTTP_LOC_CONF_OFFSET, | |
101 offsetof(ngx_http_memcached_loc_conf_t, upstream.max_fails), | |
102 NULL }, | |
103 | |
104 { ngx_string("memcached_upstream_fail_timeout"), | |
105 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
106 ngx_conf_set_sec_slot, | |
107 NGX_HTTP_LOC_CONF_OFFSET, | |
108 offsetof(ngx_http_memcached_loc_conf_t, upstream.fail_timeout), | |
109 NULL }, | |
110 | |
111 ngx_null_command | |
112 }; | |
113 | |
114 | |
667 | 115 static ngx_http_module_t ngx_http_memcached_module_ctx = { |
581 | 116 NULL, /* preconfiguration */ |
117 NULL, /* postconfiguration */ | |
118 | |
119 NULL, /* create main configuration */ | |
120 NULL, /* init main configuration */ | |
121 | |
122 NULL, /* create server configuration */ | |
123 NULL, /* merge server configuration */ | |
124 | |
125 ngx_http_memcached_create_loc_conf, /* create location configration */ | |
126 ngx_http_memcached_merge_loc_conf /* merge location configration */ | |
127 }; | |
128 | |
129 | |
130 ngx_module_t ngx_http_memcached_module = { | |
131 NGX_MODULE_V1, | |
132 &ngx_http_memcached_module_ctx, /* module context */ | |
133 ngx_http_memcached_commands, /* module directives */ | |
134 NGX_HTTP_MODULE, /* module type */ | |
135 NULL, /* init master */ | |
136 NULL, /* init module */ | |
137 NULL, /* init process */ | |
138 NULL, /* init thread */ | |
139 NULL, /* exit thread */ | |
140 NULL, /* exit process */ | |
141 NULL, /* exit master */ | |
142 NGX_MODULE_V1_PADDING | |
143 }; | |
144 | |
145 | |
146 #define NGX_HTTP_MEMCACHED_END (sizeof(ngx_http_memcached_end) - 1) | |
147 static u_char ngx_http_memcached_end[] = CRLF "END" CRLF; | |
148 | |
149 | |
150 static ngx_int_t | |
151 ngx_http_memcached_handler(ngx_http_request_t *r) | |
152 { | |
153 ngx_int_t rc; | |
154 ngx_http_upstream_t *u; | |
155 ngx_http_memcached_ctx_t *ctx; | |
156 ngx_http_memcached_loc_conf_t *mlcf; | |
157 | |
645 | 158 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { |
581 | 159 return NGX_HTTP_NOT_ALLOWED; |
160 } | |
161 | |
162 rc = ngx_http_discard_body(r); | |
163 | |
164 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
165 return rc; | |
166 } | |
167 | |
168 if (ngx_http_set_content_type(r) != NGX_OK) { | |
169 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
170 } | |
171 | |
172 mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memcached_module); | |
173 | |
174 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); | |
175 if (u == NULL) { | |
176 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
177 } | |
178 | |
179 u->peer.log = r->connection->log; | |
180 u->peer.log_error = NGX_ERROR_ERR; | |
181 u->peer.peers = mlcf->peers; | |
182 u->peer.tries = mlcf->peers->number; | |
183 #if (NGX_THREADS) | |
184 u->peer.lock = &r->connection->lock; | |
185 #endif | |
186 | |
187 u->output.tag = (ngx_buf_tag_t) &ngx_http_memcached_module; | |
188 | |
189 u->conf = &mlcf->upstream; | |
190 | |
191 u->create_request = ngx_http_memcached_create_request; | |
192 u->reinit_request = ngx_http_memcached_reinit_request; | |
193 u->process_header = ngx_http_memcached_process_header; | |
194 u->abort_request = ngx_http_memcached_abort_request; | |
195 u->finalize_request = ngx_http_memcached_finalize_request; | |
196 | |
197 r->upstream = u; | |
198 | |
199 ctx = ngx_palloc(r->pool, sizeof(ngx_http_memcached_ctx_t)); | |
200 if (ctx == NULL) { | |
201 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
202 } | |
203 | |
204 ctx->rest = NGX_HTTP_MEMCACHED_END; | |
205 ctx->request = r; | |
206 | |
597 | 207 ngx_http_set_ctx(r, ctx, ngx_http_memcached_module); |
208 | |
581 | 209 u->input_filter_init = ngx_http_memcached_filter_init; |
210 u->input_filter = ngx_http_memcached_filter; | |
211 u->input_filter_ctx = ctx; | |
212 | |
213 ngx_http_upstream_init(r); | |
214 | |
215 return NGX_DONE; | |
216 } | |
217 | |
218 | |
219 static ngx_int_t | |
220 ngx_http_memcached_create_request(ngx_http_request_t *r) | |
221 { | |
597 | 222 size_t len; |
223 ngx_buf_t *b; | |
224 ngx_chain_t *cl; | |
225 ngx_http_memcached_ctx_t *ctx; | |
581 | 226 |
227 len = sizeof("get ") - 1 + r->uri.len + sizeof(" " CRLF) - 1; | |
228 if (r->args.len) { | |
597 | 229 len += 1 + r->args.len; |
581 | 230 } |
231 | |
232 b = ngx_create_temp_buf(r->pool, len); | |
233 if (b == NULL) { | |
234 return NGX_ERROR; | |
235 } | |
236 | |
237 cl = ngx_alloc_chain_link(r->pool); | |
238 if (cl == NULL) { | |
239 return NGX_ERROR; | |
240 } | |
241 | |
242 cl->buf = b; | |
243 cl->next = NULL; | |
244 | |
245 r->upstream->request_bufs = cl; | |
246 | |
247 *b->last++ = 'g'; *b->last++ = 'e'; *b->last++ = 't'; *b->last++ = ' '; | |
248 | |
597 | 249 ctx = ngx_http_get_module_ctx(r, ngx_http_memcached_module); |
250 | |
251 ctx->key.data = b->last; | |
252 | |
581 | 253 b->last = ngx_copy(b->last, r->uri.data, r->uri.len); |
254 | |
255 if (r->args.len) { | |
256 *b->last++ = '?'; | |
257 b->last = ngx_copy(b->last, r->args.data, r->args.len); | |
258 } | |
259 | |
597 | 260 ctx->key.len = b->last - ctx->key.data; |
581 | 261 |
262 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
597 | 263 "http memcached request: \"%V\"", &ctx->key); |
581 | 264 |
265 *b->last++ = ' '; *b->last++ = CR; *b->last++ = LF; | |
266 | |
267 return NGX_OK; | |
268 } | |
269 | |
270 | |
271 static ngx_int_t | |
272 ngx_http_memcached_reinit_request(ngx_http_request_t *r) | |
273 { | |
274 return NGX_OK; | |
275 } | |
276 | |
277 | |
278 static ngx_int_t | |
279 ngx_http_memcached_process_header(ngx_http_request_t *r) | |
280 { | |
597 | 281 u_char *p, *len; |
282 ngx_str_t line; | |
283 ngx_http_upstream_t *u; | |
284 ngx_http_memcached_ctx_t *ctx; | |
581 | 285 |
286 u = r->upstream; | |
287 | |
288 for (p = u->buffer.pos; p < u->buffer.last; p++) { | |
289 if (*p == LF) { | |
290 goto found; | |
291 } | |
292 } | |
293 | |
294 return NGX_AGAIN; | |
295 | |
296 found: | |
297 | |
298 *p = '\0'; | |
299 | |
300 line.len = p - u->buffer.pos - 1; | |
301 line.data = u->buffer.pos; | |
302 | |
303 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
304 "memcached: \"%V\"", &line); | |
305 | |
306 p = u->buffer.pos; | |
307 | |
597 | 308 ctx = ngx_http_get_module_ctx(r, ngx_http_memcached_module); |
309 | |
581 | 310 if (ngx_strncmp(p, "VALUE ", sizeof("VALUE ") - 1) == 0) { |
311 | |
312 p += sizeof("VALUE ") - 1; | |
313 | |
597 | 314 if (ngx_strncmp(p, ctx->key.data, ctx->key.len) != 0) { |
581 | 315 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
316 "memcached sent invalid key in response \"%V\" " | |
317 "for key \"%V\"", | |
597 | 318 &line, &ctx->key); |
581 | 319 |
320 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
321 } | |
322 | |
597 | 323 p += ctx->key.len; |
581 | 324 |
325 if (*p++ != ' ') { | |
326 goto no_valid; | |
327 } | |
328 | |
329 /* skip flags */ | |
330 | |
331 while (*p) { | |
332 if (*p++ == ' ') { | |
333 goto length; | |
334 } | |
335 } | |
336 | |
337 goto no_valid; | |
338 | |
339 length: | |
340 | |
341 len = p; | |
342 | |
343 while (*p && *p++ != CR) { /* void */ } | |
344 | |
345 r->headers_out.content_length_n = ngx_atoof(len, p - len - 1); | |
346 if (r->headers_out.content_length_n == -1) { | |
347 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
348 "memcached sent invalid length in response \"%V\" " | |
349 "for key \"%V\"", | |
597 | 350 &line, &ctx->key); |
581 | 351 return NGX_HTTP_UPSTREAM_INVALID_HEADER; |
352 } | |
353 | |
354 u->headers_in.status_n = 200; | |
355 u->buffer.pos = p + 1; | |
356 | |
357 return NGX_OK; | |
358 } | |
359 | |
360 if (ngx_strcmp(p, "END\x0d") == 0) { | |
361 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, | |
597 | 362 "key: \"%V\" was not found by memcached", &ctx->key); |
581 | 363 |
364 u->headers_in.status_n = 404; | |
365 | |
366 return NGX_OK; | |
367 } | |
368 | |
369 no_valid: | |
370 | |
371 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
372 "memcached sent invalid response: \"%V\"", &line); | |
373 | |
374 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
375 } | |
376 | |
377 | |
378 static ngx_int_t | |
379 ngx_http_memcached_filter_init(void *data) | |
380 { | |
381 ngx_http_memcached_ctx_t *ctx = data; | |
382 | |
383 ngx_http_upstream_t *u; | |
384 | |
385 u = ctx->request->upstream; | |
386 | |
387 u->length += NGX_HTTP_MEMCACHED_END; | |
388 | |
389 return NGX_OK; | |
390 } | |
391 | |
392 | |
393 static ngx_int_t | |
394 ngx_http_memcached_filter(void *data, ssize_t bytes) | |
395 { | |
396 ngx_http_memcached_ctx_t *ctx = data; | |
397 | |
398 u_char *last; | |
399 ngx_buf_t *b; | |
400 ngx_chain_t *cl, **ll; | |
401 ngx_http_upstream_t *u; | |
402 | |
403 u = ctx->request->upstream; | |
404 b = &u->buffer; | |
405 | |
406 if (u->length == ctx->rest) { | |
407 | |
408 if (ngx_strncmp(b->last, | |
409 ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END | |
410 - ctx->rest, | |
411 bytes) != 0) | |
412 { | |
413 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, | |
414 "memcached sent invalid trailer"); | |
415 } | |
416 | |
417 u->length -= bytes; | |
418 ctx->rest -= bytes; | |
419 | |
420 return NGX_OK; | |
421 } | |
422 | |
423 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { | |
424 ll = &cl->next; | |
425 } | |
426 | |
427 cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs); | |
428 if (cl == NULL) { | |
429 return NGX_ERROR; | |
430 } | |
431 | |
432 cl->buf->flush = 1; | |
433 cl->buf->memory = 1; | |
434 | |
435 *ll = cl; | |
436 | |
437 cl->buf->pos = b->last; | |
438 b->last += bytes; | |
439 cl->buf->last = b->last; | |
440 | |
441 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0, | |
442 "memcached filter bytes:%z size:%z length:%z rest:%z", | |
443 bytes, b->last - b->pos, u->length, ctx->rest); | |
444 | |
445 if (b->last - b->pos <= (ssize_t) (u->length - NGX_HTTP_MEMCACHED_END)) { | |
446 u->length -= bytes; | |
447 return NGX_OK; | |
448 } | |
449 | |
450 | |
451 last = b->pos + u->length - NGX_HTTP_MEMCACHED_END; | |
452 | |
453 if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) { | |
454 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, | |
455 "memcached sent invalid trailer"); | |
456 } | |
457 | |
458 ctx->rest = u->length - (b->last - b->pos); | |
459 b->last = last; | |
460 cl->buf->last = last; | |
461 u->length = ctx->rest; | |
462 | |
463 return NGX_OK; | |
464 } | |
465 | |
466 | |
467 static void | |
468 ngx_http_memcached_abort_request(ngx_http_request_t *r) | |
469 { | |
470 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
471 "abort http memcached request"); | |
472 return; | |
473 } | |
474 | |
475 | |
476 static void | |
477 ngx_http_memcached_finalize_request(ngx_http_request_t *r, ngx_int_t rc) | |
478 { | |
479 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
480 "finalize http memcached request"); | |
481 return; | |
482 } | |
483 | |
484 | |
485 static void * | |
486 ngx_http_memcached_create_loc_conf(ngx_conf_t *cf) | |
487 { | |
488 ngx_http_memcached_loc_conf_t *conf; | |
489 | |
490 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_memcached_loc_conf_t)); | |
491 if (conf == NULL) { | |
492 return NGX_CONF_ERROR; | |
493 } | |
494 | |
495 /* | |
496 * set by ngx_pcalloc(): | |
497 * | |
498 * conf->upstream.bufs.num = 0; | |
499 * conf->upstream.next_upstream = 0; | |
500 * conf->upstream.temp_path = NULL; | |
501 * conf->upstream.schema = { 0, NULL }; | |
502 * conf->upstream.uri = { 0, NULL }; | |
503 * conf->upstream.location = NULL; | |
504 * | |
505 * conf->peers = NULL; | |
506 */ | |
507 | |
508 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; | |
509 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; | |
510 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC; | |
511 | |
512 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; | |
513 | |
514 conf->upstream.max_fails = NGX_CONF_UNSET_UINT; | |
515 conf->upstream.fail_timeout = NGX_CONF_UNSET; | |
516 | |
517 /* "fastcgi_cyclic_temp_file" is disabled */ | |
518 conf->upstream.cyclic_temp_file = 0; | |
519 | |
520 /* the hardcoded values */ | |
629 | 521 conf->upstream.buffering = 0; |
522 conf->upstream.ignore_client_abort = 0; | |
581 | 523 conf->upstream.send_lowat = 0; |
524 conf->upstream.bufs.num = 0; | |
525 conf->upstream.busy_buffers_size = 0; | |
526 conf->upstream.max_temp_file_size = 0; | |
527 conf->upstream.temp_file_write_size = 0; | |
657 | 528 conf->upstream.intercept_errors = 1; |
675 | 529 conf->upstream.intercept_404 = 1; |
581 | 530 conf->upstream.pass_request_headers = 0; |
531 conf->upstream.pass_request_body = 0; | |
532 | |
533 return conf; | |
534 } | |
535 | |
536 | |
537 static char * | |
538 ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
539 { | |
540 ngx_http_memcached_loc_conf_t *prev = parent; | |
541 ngx_http_memcached_loc_conf_t *conf = child; | |
542 | |
543 ngx_uint_t i; | |
544 | |
545 ngx_conf_merge_msec_value(conf->upstream.connect_timeout, | |
546 prev->upstream.connect_timeout, 60000); | |
547 | |
548 ngx_conf_merge_msec_value(conf->upstream.send_timeout, | |
549 prev->upstream.send_timeout, 60000); | |
550 | |
551 ngx_conf_merge_msec_value(conf->upstream.read_timeout, | |
552 prev->upstream.read_timeout, 60000); | |
553 | |
554 ngx_conf_merge_size_value(conf->upstream.buffer_size, | |
555 prev->upstream.buffer_size, | |
556 (size_t) ngx_pagesize); | |
557 | |
558 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, | |
559 prev->upstream.next_upstream, | |
560 (NGX_CONF_BITMASK_SET | |
561 |NGX_HTTP_UPSTREAM_FT_ERROR | |
562 |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); | |
563 | |
665 | 564 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { |
565 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET | |
566 |NGX_HTTP_UPSTREAM_FT_OFF; | |
567 } | |
568 | |
663 | 569 ngx_conf_merge_uint_value(conf->upstream.max_fails, |
581 | 570 prev->upstream.max_fails, 1); |
571 | |
572 ngx_conf_merge_sec_value(conf->upstream.fail_timeout, | |
573 prev->upstream.fail_timeout, 10); | |
574 | |
575 if (conf->peers && conf->peers->number > 1) { | |
576 for (i = 0; i < conf->peers->number; i++) { | |
577 conf->peers->peer[i].weight = 1; | |
578 conf->peers->peer[i].max_fails = conf->upstream.max_fails; | |
579 conf->peers->peer[i].fail_timeout = conf->upstream.fail_timeout; | |
580 } | |
581 } | |
582 | |
583 return NGX_CONF_OK; | |
584 } | |
585 | |
586 | |
587 static char * | |
588 ngx_http_memcached_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
589 { | |
590 ngx_http_memcached_loc_conf_t *lcf = conf; | |
591 | |
592 ngx_str_t *value; | |
807
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
593 ngx_url_t u; |
581 | 594 ngx_http_core_loc_conf_t *clcf; |
595 | |
596 if (lcf->upstream.schema.len) { | |
597 return "is duplicate"; | |
598 } | |
599 | |
600 value = cf->args->elts; | |
601 | |
807
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
602 ngx_memzero(&u, sizeof(ngx_url_t)); |
581 | 603 |
807
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
604 u.url = value[1]; |
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
605 u.uri_part = 1; |
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
606 |
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
607 if (ngx_parse_url(cf, &u) != NGX_OK) { |
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
608 if (u.err) { |
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
609 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
610 "%s in \"%V\"", u.err, &u.url); |
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
611 } |
581 | 612 } |
613 | |
807
3095bf59059b
now the "memcached_pass" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
675
diff
changeset
|
614 lcf->peers = u.peers; |
581 | 615 lcf->upstream.schema.len = sizeof("memcached://") - 1; |
616 lcf->upstream.schema.data = (u_char *) "memcached://"; | |
617 | |
618 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
619 | |
620 clcf->handler = ngx_http_memcached_handler; | |
621 | |
622 lcf->upstream.location = clcf->name; | |
623 | |
624 if (clcf->name.data[clcf->name.len - 1] == '/') { | |
625 clcf->auto_redirect = 1; | |
626 } | |
627 | |
628 return NGX_CONF_OK; | |
629 } |