comparison src/http/modules/ngx_http_memcached_module.c @ 130:82d695e3d662 NGINX_0_3_12

nginx 0.3.12 *) Security: if nginx was built with the ngx_http_realip_module and the "satisfy_any on" directive was used, then access and authorization directives did not work. The ngx_http_realip_module was not built and is not built by default. *) Change: the "$time_gmt" variable name was changed to "$time_local". *) Change: the "proxy_header_buffer_size" and "fastcgi_header_buffer_size" directives was renamed to the "proxy_buffer_size" and "fastcgi_buffer_size" directives. *) Feature: the ngx_http_memcached_module. *) Feature: the "proxy_buffering" directive. *) Bugfix: the changes in accept mutex handling when the "rtsig" method was used; bug appeared in 0.3.0. *) Bugfix: if the client sent the "Transfer-Encoding: chunked" header line, then nginx returns the 411 error. *) Bugfix: if the "auth_basic" directive was inherited from the http level, then the realm in the "WWW-Authenticate" header line was without the "Basic realm" text. *) Bugfix: if the "combined" format was explicitly specified in the "access_log" directive, then the empty lines was written to the log; bug appeared in 0.3.8. *) Bugfix: nginx did not run on the sparc platform under any OS except Solaris. *) Bugfix: now it is not necessary to place space between the quoted string and closing bracket in the "if" directive.
author Igor Sysoev <http://sysoev.ru>
date Sat, 26 Nov 2005 00:00:00 +0300
parents
children 36af50a5582d
comparison
equal deleted inserted replaced
129:a27c77ef3ad8 130:82d695e3d662
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;
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
41
42 static ngx_conf_bitmask_t ngx_http_memcached_next_upstream_masks[] = {
43 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
44 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
45 { ngx_string("invalid_response"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
46 { ngx_string("not_found"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
47 { ngx_null_string, 0 }
48 };
49
50
51 static ngx_command_t ngx_http_memcached_commands[] = {
52
53 { ngx_string("memcached_pass"),
54 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
55 ngx_http_memcached_pass,
56 NGX_HTTP_LOC_CONF_OFFSET,
57 0,
58 NULL },
59
60 { ngx_string("memcached_connect_timeout"),
61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
62 ngx_conf_set_msec_slot,
63 NGX_HTTP_LOC_CONF_OFFSET,
64 offsetof(ngx_http_memcached_loc_conf_t, upstream.connect_timeout),
65 NULL },
66
67 { ngx_string("memcached_send_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.send_timeout),
72 NULL },
73
74 { ngx_string("memcached_buffer_size"),
75 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
76 ngx_conf_set_size_slot,
77 NGX_HTTP_LOC_CONF_OFFSET,
78 offsetof(ngx_http_memcached_loc_conf_t, upstream.buffer_size),
79 NULL },
80
81 { ngx_string("memcached_read_timeout"),
82 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
83 ngx_conf_set_msec_slot,
84 NGX_HTTP_LOC_CONF_OFFSET,
85 offsetof(ngx_http_memcached_loc_conf_t, upstream.read_timeout),
86 NULL },
87
88 { ngx_string("memcached_next_upstream"),
89 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
90 ngx_conf_set_bitmask_slot,
91 NGX_HTTP_LOC_CONF_OFFSET,
92 offsetof(ngx_http_memcached_loc_conf_t, upstream.next_upstream),
93 &ngx_http_memcached_next_upstream_masks },
94
95 { ngx_string("memcached_upstream_max_fails"),
96 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
97 ngx_conf_set_num_slot,
98 NGX_HTTP_LOC_CONF_OFFSET,
99 offsetof(ngx_http_memcached_loc_conf_t, upstream.max_fails),
100 NULL },
101
102 { ngx_string("memcached_upstream_fail_timeout"),
103 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
104 ngx_conf_set_sec_slot,
105 NGX_HTTP_LOC_CONF_OFFSET,
106 offsetof(ngx_http_memcached_loc_conf_t, upstream.fail_timeout),
107 NULL },
108
109 ngx_null_command
110 };
111
112
113 ngx_http_module_t ngx_http_memcached_module_ctx = {
114 NULL, /* preconfiguration */
115 NULL, /* postconfiguration */
116
117 NULL, /* create main configuration */
118 NULL, /* init main configuration */
119
120 NULL, /* create server configuration */
121 NULL, /* merge server configuration */
122
123 ngx_http_memcached_create_loc_conf, /* create location configration */
124 ngx_http_memcached_merge_loc_conf /* merge location configration */
125 };
126
127
128 ngx_module_t ngx_http_memcached_module = {
129 NGX_MODULE_V1,
130 &ngx_http_memcached_module_ctx, /* module context */
131 ngx_http_memcached_commands, /* module directives */
132 NGX_HTTP_MODULE, /* module type */
133 NULL, /* init master */
134 NULL, /* init module */
135 NULL, /* init process */
136 NULL, /* init thread */
137 NULL, /* exit thread */
138 NULL, /* exit process */
139 NULL, /* exit master */
140 NGX_MODULE_V1_PADDING
141 };
142
143
144 #define NGX_HTTP_MEMCACHED_END (sizeof(ngx_http_memcached_end) - 1)
145 static u_char ngx_http_memcached_end[] = CRLF "END" CRLF;
146
147
148 static ngx_int_t
149 ngx_http_memcached_handler(ngx_http_request_t *r)
150 {
151 ngx_int_t rc;
152 ngx_http_upstream_t *u;
153 ngx_http_memcached_ctx_t *ctx;
154 ngx_http_memcached_loc_conf_t *mlcf;
155
156 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
157 return NGX_HTTP_NOT_ALLOWED;
158 }
159
160 rc = ngx_http_discard_body(r);
161
162 if (rc != NGX_OK && rc != NGX_AGAIN) {
163 return rc;
164 }
165
166 if (ngx_http_set_content_type(r) != NGX_OK) {
167 return NGX_HTTP_INTERNAL_SERVER_ERROR;
168 }
169
170 mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memcached_module);
171
172 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
173 if (u == NULL) {
174 return NGX_HTTP_INTERNAL_SERVER_ERROR;
175 }
176
177 u->peer.log = r->connection->log;
178 u->peer.log_error = NGX_ERROR_ERR;
179 u->peer.peers = mlcf->peers;
180 u->peer.tries = mlcf->peers->number;
181 #if (NGX_THREADS)
182 u->peer.lock = &r->connection->lock;
183 #endif
184
185 u->output.tag = (ngx_buf_tag_t) &ngx_http_memcached_module;
186
187 u->conf = &mlcf->upstream;
188
189 u->create_request = ngx_http_memcached_create_request;
190 u->reinit_request = ngx_http_memcached_reinit_request;
191 u->process_header = ngx_http_memcached_process_header;
192 u->abort_request = ngx_http_memcached_abort_request;
193 u->finalize_request = ngx_http_memcached_finalize_request;
194
195 r->upstream = u;
196
197 ctx = ngx_palloc(r->pool, sizeof(ngx_http_memcached_ctx_t));
198 if (ctx == NULL) {
199 return NGX_HTTP_INTERNAL_SERVER_ERROR;
200 }
201
202 ctx->rest = NGX_HTTP_MEMCACHED_END;
203 ctx->request = r;
204
205 u->input_filter_init = ngx_http_memcached_filter_init;
206 u->input_filter = ngx_http_memcached_filter;
207 u->input_filter_ctx = ctx;
208
209 ngx_http_upstream_init(r);
210
211 return NGX_DONE;
212 }
213
214
215 static ngx_int_t
216 ngx_http_memcached_create_request(ngx_http_request_t *r)
217 {
218 size_t len;
219 ngx_buf_t *b;
220 ngx_chain_t *cl;
221
222 len = sizeof("get ") - 1 + r->uri.len + sizeof(" " CRLF) - 1;
223 if (r->args.len) {
224 len += 1+ r->args.len;
225 }
226
227 b = ngx_create_temp_buf(r->pool, len);
228 if (b == NULL) {
229 return NGX_ERROR;
230 }
231
232 cl = ngx_alloc_chain_link(r->pool);
233 if (cl == NULL) {
234 return NGX_ERROR;
235 }
236
237 cl->buf = b;
238 cl->next = NULL;
239
240 r->upstream->request_bufs = cl;
241
242 *b->last++ = 'g'; *b->last++ = 'e'; *b->last++ = 't'; *b->last++ = ' ';
243
244 b->last = ngx_copy(b->last, r->uri.data, r->uri.len);
245
246 if (r->args.len) {
247 *b->last++ = '?';
248 b->last = ngx_copy(b->last, r->args.data, r->args.len);
249 }
250
251 #if (NGX_DEBUG)
252 {
253 ngx_str_t s;
254
255 s.len = b->last - b->pos;
256 s.data = b->pos;
257 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
258 "http memcached request: \"%V\"", &s);
259 }
260 #endif
261
262 *b->last++ = ' '; *b->last++ = CR; *b->last++ = LF;
263
264 return NGX_OK;
265 }
266
267
268 static ngx_int_t
269 ngx_http_memcached_reinit_request(ngx_http_request_t *r)
270 {
271 return NGX_OK;
272 }
273
274
275 static ngx_int_t
276 ngx_http_memcached_process_header(ngx_http_request_t *r)
277 {
278 u_char *p, *len;
279 ngx_str_t line;
280 ngx_http_upstream_t *u;
281
282 u = r->upstream;
283
284 for (p = u->buffer.pos; p < u->buffer.last; p++) {
285 if (*p == LF) {
286 goto found;
287 }
288 }
289
290 return NGX_AGAIN;
291
292 found:
293
294 *p = '\0';
295
296 line.len = p - u->buffer.pos - 1;
297 line.data = u->buffer.pos;
298
299 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
300 "memcached: \"%V\"", &line);
301
302 p = u->buffer.pos;
303
304 if (ngx_strncmp(p, "VALUE ", sizeof("VALUE ") - 1) == 0) {
305
306 p += sizeof("VALUE ") - 1;
307
308 if (ngx_strncmp(p, r->uri.data, r->uri.len) != 0) {
309 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
310 "memcached sent invalid key in response \"%V\" "
311 "for key \"%V\"",
312 &line, &r->uri);
313
314 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
315 }
316
317 p += r->uri.len;
318
319 if (*p++ != ' ') {
320 goto no_valid;
321 }
322
323 /* skip flags */
324
325 while (*p) {
326 if (*p++ == ' ') {
327 goto length;
328 }
329 }
330
331 goto no_valid;
332
333 length:
334
335 len = p;
336
337 while (*p && *p++ != CR) { /* void */ }
338
339 r->headers_out.content_length_n = ngx_atoof(len, p - len - 1);
340 if (r->headers_out.content_length_n == -1) {
341 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
342 "memcached sent invalid length in response \"%V\" "
343 "for key \"%V\"",
344 &line, &r->uri);
345 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
346 }
347
348 u->headers_in.status_n = 200;
349 u->buffer.pos = p + 1;
350
351 return NGX_OK;
352 }
353
354 if (ngx_strcmp(p, "END\x0d") == 0) {
355 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
356 "key: \"%V\" was not found by memcached", &r->uri);
357
358 u->headers_in.status_n = 404;
359
360 return NGX_OK;
361 }
362
363 no_valid:
364
365 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
366 "memcached sent invalid response: \"%V\"", &line);
367
368 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
369 }
370
371
372 static ngx_int_t
373 ngx_http_memcached_filter_init(void *data)
374 {
375 ngx_http_memcached_ctx_t *ctx = data;
376
377 ngx_http_upstream_t *u;
378
379 u = ctx->request->upstream;
380
381 u->length += NGX_HTTP_MEMCACHED_END;
382
383 return NGX_OK;
384 }
385
386
387 static ngx_int_t
388 ngx_http_memcached_filter(void *data, ssize_t bytes)
389 {
390 ngx_http_memcached_ctx_t *ctx = data;
391
392 u_char *last;
393 ngx_buf_t *b;
394 ngx_chain_t *cl, **ll;
395 ngx_http_upstream_t *u;
396
397 u = ctx->request->upstream;
398 b = &u->buffer;
399
400 if (u->length == ctx->rest) {
401
402 if (ngx_strncmp(b->last,
403 ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END
404 - ctx->rest,
405 bytes) != 0)
406 {
407 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
408 "memcached sent invalid trailer");
409 }
410
411 u->length -= bytes;
412 ctx->rest -= bytes;
413
414 return NGX_OK;
415 }
416
417 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
418 ll = &cl->next;
419 }
420
421 cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs);
422 if (cl == NULL) {
423 return NGX_ERROR;
424 }
425
426 cl->buf->flush = 1;
427 cl->buf->memory = 1;
428
429 *ll = cl;
430
431 cl->buf->pos = b->last;
432 b->last += bytes;
433 cl->buf->last = b->last;
434
435 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
436 "memcached filter bytes:%z size:%z length:%z rest:%z",
437 bytes, b->last - b->pos, u->length, ctx->rest);
438
439 if (b->last - b->pos <= (ssize_t) (u->length - NGX_HTTP_MEMCACHED_END)) {
440 u->length -= bytes;
441 return NGX_OK;
442 }
443
444
445 last = b->pos + u->length - NGX_HTTP_MEMCACHED_END;
446
447 if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) {
448 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
449 "memcached sent invalid trailer");
450 }
451
452 ctx->rest = u->length - (b->last - b->pos);
453 b->last = last;
454 cl->buf->last = last;
455 u->length = ctx->rest;
456
457 return NGX_OK;
458 }
459
460
461 static void
462 ngx_http_memcached_abort_request(ngx_http_request_t *r)
463 {
464 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
465 "abort http memcached request");
466 return;
467 }
468
469
470 static void
471 ngx_http_memcached_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
472 {
473 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
474 "finalize http memcached request");
475 return;
476 }
477
478
479 static void *
480 ngx_http_memcached_create_loc_conf(ngx_conf_t *cf)
481 {
482 ngx_http_memcached_loc_conf_t *conf;
483
484 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_memcached_loc_conf_t));
485 if (conf == NULL) {
486 return NGX_CONF_ERROR;
487 }
488
489 /*
490 * set by ngx_pcalloc():
491 *
492 * conf->upstream.bufs.num = 0;
493 * conf->upstream.next_upstream = 0;
494 * conf->upstream.temp_path = NULL;
495 * conf->upstream.schema = { 0, NULL };
496 * conf->upstream.uri = { 0, NULL };
497 * conf->upstream.location = NULL;
498 *
499 * conf->peers = NULL;
500 */
501
502 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
503 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
504 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
505
506 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
507
508 conf->upstream.max_fails = NGX_CONF_UNSET_UINT;
509 conf->upstream.fail_timeout = NGX_CONF_UNSET;
510
511 /* "fastcgi_cyclic_temp_file" is disabled */
512 conf->upstream.cyclic_temp_file = 0;
513
514 /* the hardcoded values */
515 conf->upstream.send_lowat = 0;
516 conf->upstream.bufs.num = 0;
517 conf->upstream.busy_buffers_size = 0;
518 conf->upstream.max_temp_file_size = 0;
519 conf->upstream.temp_file_write_size = 0;
520 conf->upstream.pass_x_powered_by = 0;
521 conf->upstream.redirect_errors = 1;
522 conf->upstream.redirect_404 = 1;
523 conf->upstream.pass_server = 1;
524 conf->upstream.pass_date = 1;
525 conf->upstream.pass_request_headers = 0;
526 conf->upstream.pass_request_body = 0;
527
528 return conf;
529 }
530
531
532 static char *
533 ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
534 {
535 ngx_http_memcached_loc_conf_t *prev = parent;
536 ngx_http_memcached_loc_conf_t *conf = child;
537
538 ngx_uint_t i;
539
540 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
541 prev->upstream.connect_timeout, 60000);
542
543 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
544 prev->upstream.send_timeout, 60000);
545
546 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
547 prev->upstream.read_timeout, 60000);
548
549 ngx_conf_merge_size_value(conf->upstream.buffer_size,
550 prev->upstream.buffer_size,
551 (size_t) ngx_pagesize);
552
553 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
554 prev->upstream.next_upstream,
555 (NGX_CONF_BITMASK_SET
556 |NGX_HTTP_UPSTREAM_FT_ERROR
557 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
558
559 ngx_conf_merge_unsigned_value(conf->upstream.max_fails,
560 prev->upstream.max_fails, 1);
561
562 ngx_conf_merge_sec_value(conf->upstream.fail_timeout,
563 prev->upstream.fail_timeout, 10);
564
565 if (conf->peers && conf->peers->number > 1) {
566 for (i = 0; i < conf->peers->number; i++) {
567 conf->peers->peer[i].weight = 1;
568 conf->peers->peer[i].max_fails = conf->upstream.max_fails;
569 conf->peers->peer[i].fail_timeout = conf->upstream.fail_timeout;
570 }
571 }
572
573 return NGX_CONF_OK;
574 }
575
576
577 static char *
578 ngx_http_memcached_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
579 {
580 ngx_http_memcached_loc_conf_t *lcf = conf;
581
582 ngx_str_t *value;
583 ngx_inet_upstream_t inet_upstream;
584 ngx_http_core_loc_conf_t *clcf;
585
586 if (lcf->upstream.schema.len) {
587 return "is duplicate";
588 }
589
590 value = cf->args->elts;
591
592 ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
593
594 inet_upstream.name = value[1];
595 inet_upstream.url = value[1];
596
597 lcf->peers = ngx_inet_upstream_parse(cf, &inet_upstream);
598 if (lcf->peers == NULL) {
599 return NGX_CONF_ERROR;
600 }
601
602 lcf->upstream.schema.len = sizeof("memcached://") - 1;
603 lcf->upstream.schema.data = (u_char *) "memcached://";
604
605 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
606
607 clcf->handler = ngx_http_memcached_handler;
608
609 lcf->upstream.location = clcf->name;
610
611 if (clcf->name.data[clcf->name.len - 1] == '/') {
612 clcf->auto_redirect = 1;
613 }
614
615 return NGX_CONF_OK;
616 }