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