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