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