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