Changeset c79fa6
- Timestamp:
- 02/19/08 16:28:06 (4 years ago)
- Branches:
- master-v0.7, master-v0.6, upstream_count_limit
- Children:
- 034113
- Parents:
- 2a12f7 (diff), 2340f0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - git-author:
- Tomash Brechko <tomash.brechko@…> (02/19/08 16:28:06)
- git-committer:
- Tomash Brechko <tomash.brechko@…> (02/19/08 16:28:06)
- Files:
-
- 16 edited
-
memcached_hash/Changes (modified) (1 diff)
-
memcached_hash/README (modified) (3 diffs)
-
memcached_hash/TODO (modified) (1 diff)
-
memcached_hash/ngx_http_upstream_memcached_hash_module.c (modified) (7 diffs)
-
server/src/core/ngx_regex.c (modified) (1 diff)
-
server/src/core/ngx_regex.h (modified) (2 diffs)
-
server/src/http/modules/ngx_http_fastcgi_module.c (modified) (1 diff)
-
server/src/http/modules/ngx_http_gzip_filter_module.c (modified) (27 diffs)
-
server/src/http/modules/ngx_http_memcached_module.c (modified) (7 diffs)
-
server/src/http/modules/ngx_http_proxy_module.c (modified) (1 diff)
-
server/src/http/ngx_http_core_module.c (modified) (9 diffs)
-
server/src/http/ngx_http_core_module.h (modified) (3 diffs)
-
server/src/http/ngx_http_request.h (modified) (1 diff)
-
server/src/http/ngx_http_upstream.c (modified) (3 diffs)
-
server/src/http/ngx_http_upstream.h (modified) (1 diff)
-
server/src/http/ngx_http_variables.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
memcached_hash/Changes
r432a6a r2340f0 1 1 Revision history of ngx_http_upstream_memcached_hash_module. 2 3 4 0.02 2008-02-19 5 - add support for $memcached_namespace variable. 6 7 If Cache::Memcached::Fast uses 8 9 namespace => 'prefix', 10 11 then nginx configuration file should have 12 13 set $memcached_namespace "prefix"; 14 15 This is not the same as prepending "prefix" to $memcached_key: 16 namespace prefix should not be hashed. 2 17 3 18 -
memcached_hash/README
r1e6e0c r2340f0 1 ngx_http_upstream_memcached_hash_module 0.0 11 ngx_http_upstream_memcached_hash_module 0.02 2 2 ============================================ 3 3 … … 85 85 location / { 86 86 set $memcached_key "$uri$is_args$args"; 87 set $memcached_namespace "prefix"; 87 88 memcached_pass memcached_cluster; 88 89 error_page 404 502 504 = @fallback; … … 169 170 supports only integer weights and does not support consistent hashing. 170 171 172 If the client uses a namespace, i.e. constructor has 173 174 namespace => 'prefix', 175 176 then you have to set $memcached_namespace variable in nginx 177 configuration file: 178 179 set $memcached_namespace "prefix"; 180 181 Note that this is not the same as prepending prefix to $memcached_key: 182 namespace prefix is not hashed when the key is hashed to decide which 183 memcached server to talk to. 184 171 185 Also note that nginx escapes an URI key before sending the request to 172 186 memcached. As of this writing the transformation is equivalent to the -
memcached_hash/TODO
r7fe430 r2340f0 1 - Comment the code, especially integer computations.2 3 - Add gzip support.4 5 1 - Add populate.pl to the distribution. 6 2 -
memcached_hash/ngx_http_upstream_memcached_hash_module.c
r7fe430 r030d22 7 7 source code. 8 8 9 Version 0.0 1.9 Version 0.02. 10 10 */ 11 11 … … 16 16 17 17 #define CONTINUUM_MAX_POINT 0xffffffffU 18 19 20 static ngx_str_t memcached_ns = ngx_string("memcached_namespace"); 18 21 19 22 … … 43 46 unsigned int ketama_points; 44 47 unsigned int scale; 48 ngx_int_t ns_index; 45 49 }; 46 50 … … 50 54 struct memcached_hash *memd; 51 55 ngx_http_upstream_server_t *server; 52 ngx_ chain_t **request_bufs;56 ngx_http_request_t *request; 53 57 }; 54 58 … … 133 137 else 134 138 { 139 ngx_chain_t *request_bufs = find_ctx->request->upstream->request_bufs; 140 ngx_http_variable_value_t *ns_vv = 141 ngx_http_get_indexed_variable(find_ctx->request, memd->ns_index); 142 135 143 /* 136 144 We take the key directly from request_buf, because there it is 137 145 in the escaped form that will be seen by memcached server. 138 146 */ 139 key = (*find_ctx->request_bufs)->buf->start + (sizeof("get ") - 1); 140 len = (((*find_ctx->request_bufs)->buf->last - key) 141 - (sizeof("\r\n") - 1)); 147 key = request_bufs->buf->start + (sizeof("get ") - 1); 148 if (ns_vv && ! ns_vv->not_found && ns_vv->len != 0) 149 { 150 key += ns_vv->len + 2 * ngx_escape_uri(NULL, ns_vv->data, ns_vv->len, 151 NGX_ESCAPE_MEMCACHED); 152 } 153 154 len = request_bufs->buf->last - key - (sizeof("\r\n") - 1); 142 155 143 156 point = ngx_crc32_long(key, len); … … 218 231 return NGX_ERROR; 219 232 find_ctx->memd = memd; 220 find_ctx->request _bufs = &r->upstream->request_bufs;233 find_ctx->request = r; 221 234 find_ctx->server = us->servers->elts; 222 235 … … 454 467 memd->ketama_points = ketama_points; 455 468 memd->scale = scale; 469 memd->ns_index = ngx_http_get_variable_index(cf, &memcached_ns); 470 471 if (memd->ns_index == NGX_ERROR) { 472 return NGX_CONF_ERROR; 473 } 456 474 457 475 uscf->peer.data = memd; -
server/src/core/ngx_regex.c
r0019fc r5af58e 115 115 116 116 117 ngx_int_t 118 ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log) 119 { 120 ngx_int_t n; 121 ngx_uint_t i; 122 ngx_regex_elt_t *re; 123 124 re = a->elts; 125 126 for (i = 0; i < a->nelts; i++) { 127 128 n = ngx_regex_exec(re[i].regex, s, NULL, 0); 129 130 if (n == NGX_REGEX_NO_MATCHED) { 131 continue; 132 } 133 134 if (n < 0) { 135 ngx_log_error(NGX_LOG_ALERT, log, 0, 136 ngx_regex_exec_n " failed: %d on \"%V\" using \"%s\"", 137 n, s, re[i].name); 138 return NGX_ERROR; 139 } 140 141 /* match */ 142 143 return NGX_OK; 144 } 145 146 return NGX_DECLINED; 147 } 148 149 117 150 static void * ngx_libc_cdecl 118 151 ngx_regex_malloc(size_t size) -
server/src/core/ngx_regex.h
r0019fc r5af58e 21 21 typedef pcre ngx_regex_t; 22 22 23 typedef struct { 24 ngx_regex_t *regex; 25 u_char *name; 26 } ngx_regex_elt_t; 27 28 23 29 void ngx_regex_init(void); 24 30 ngx_regex_t *ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options, … … 27 33 ngx_int_t ngx_regex_exec(ngx_regex_t *re, ngx_str_t *s, int *captures, 28 34 ngx_int_t size); 35 ngx_int_t ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log); 36 29 37 30 38 #define ngx_regex_exec_n "pcre_exec()" -
server/src/http/modules/ngx_http_fastcgi_module.c
r0019fc r66a597 180 180 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, 181 181 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, 182 { ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 }, 182 183 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, 184 { ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 }, 185 { ngx_string("http_507"), NGX_HTTP_UPSTREAM_FT_HTTP_507 }, 183 186 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, 184 187 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, -
server/src/http/modules/ngx_http_gzip_filter_module.c
r0019fc r95d817 15 15 ngx_flag_t enable; 16 16 ngx_flag_t no_buffer; 17 ngx_flag_t vary;18 17 19 18 ngx_array_t *types; /* array of ngx_str_t */ 20 19 21 20 ngx_bufs_t bufs; 22 23 ngx_uint_t http_version;24 ngx_uint_t proxied;25 21 26 22 ngx_int_t level; … … 29 25 ssize_t min_length; 30 26 } ngx_http_gzip_conf_t; 31 32 33 #define NGX_HTTP_GZIP_PROXIED_OFF 0x000234 #define NGX_HTTP_GZIP_PROXIED_EXPIRED 0x000435 #define NGX_HTTP_GZIP_PROXIED_NO_CACHE 0x000836 #define NGX_HTTP_GZIP_PROXIED_NO_STORE 0x001037 #define NGX_HTTP_GZIP_PROXIED_PRIVATE 0x002038 #define NGX_HTTP_GZIP_PROXIED_NO_LM 0x004039 #define NGX_HTTP_GZIP_PROXIED_NO_ETAG 0x008040 #define NGX_HTTP_GZIP_PROXIED_AUTH 0x010041 #define NGX_HTTP_GZIP_PROXIED_ANY 0x020042 27 43 28 … … 71 56 72 57 73 static ngx_int_t ngx_http_gzip_proxied(ngx_http_request_t *r,74 ngx_http_gzip_conf_t *conf);75 58 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items, 76 59 u_int size); … … 100 83 101 84 102 static ngx_conf_enum_t ngx_http_gzip_http_version[] = {103 { ngx_string("1.0"), NGX_HTTP_VERSION_10 },104 { ngx_string("1.1"), NGX_HTTP_VERSION_11 },105 { ngx_null_string, 0 }106 };107 108 109 static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = {110 { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },111 { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },112 { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },113 { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },114 { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },115 { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },116 { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },117 { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },118 { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },119 { ngx_null_string, 0 }120 };121 122 123 85 static ngx_command_t ngx_http_gzip_filter_commands[] = { 124 86 … … 173 135 NULL }, 174 136 175 { ngx_string("gzip_http_version"),176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,177 ngx_conf_set_enum_slot,178 NGX_HTTP_LOC_CONF_OFFSET,179 offsetof(ngx_http_gzip_conf_t, http_version),180 &ngx_http_gzip_http_version },181 182 { ngx_string("gzip_proxied"),183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,184 ngx_conf_set_bitmask_slot,185 NGX_HTTP_LOC_CONF_OFFSET,186 offsetof(ngx_http_gzip_conf_t, proxied),187 &ngx_http_gzip_proxied_mask },188 189 137 { ngx_string("gzip_min_length"), 190 138 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, … … 192 140 NGX_HTTP_LOC_CONF_OFFSET, 193 141 offsetof(ngx_http_gzip_conf_t, min_length), 194 NULL },195 196 { ngx_string("gzip_vary"),197 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,198 ngx_conf_set_flag_slot,199 NGX_HTTP_LOC_CONF_OFFSET,200 offsetof(ngx_http_gzip_conf_t, vary),201 142 NULL }, 202 143 … … 256 197 257 198 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio"); 258 static ngx_str_t ngx_http_gzip_no_cache = ngx_string("no-cache");259 static ngx_str_t ngx_http_gzip_no_store = ngx_string("no-store");260 static ngx_str_t ngx_http_gzip_private = ngx_string("private");261 262 199 263 200 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; … … 268 205 ngx_http_gzip_header_filter(ngx_http_request_t *r) 269 206 { 270 ngx_str_t *type; 271 ngx_uint_t i; 272 ngx_table_elt_t *header; 273 ngx_http_gzip_ctx_t *ctx; 274 ngx_http_gzip_conf_t *conf; 207 ngx_str_t *type; 208 ngx_uint_t i; 209 ngx_table_elt_t *h; 210 ngx_http_gzip_ctx_t *ctx; 211 ngx_http_gzip_conf_t *conf; 212 ngx_http_core_loc_conf_t *clcf; 275 213 276 214 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module); 277 215 278 if (!conf->enable 279 || (r->headers_out.status != NGX_HTTP_OK 280 && r->headers_out.status != NGX_HTTP_FORBIDDEN 281 && r->headers_out.status != NGX_HTTP_NOT_FOUND) 216 if ((r->headers_out.status != NGX_HTTP_OK 217 && r->headers_out.status != NGX_HTTP_FORBIDDEN 218 && r->headers_out.status != NGX_HTTP_NOT_FOUND) 282 219 || r->header_only 283 || r != r->main 284 || r->http_version < conf->http_version 285 || r->headers_out.content_type.len == 0 286 || (r->headers_out.content_encoding 287 && r->headers_out.content_encoding->value.len) 288 || r->headers_in.accept_encoding == NULL 289 || (r->headers_out.content_length_n != -1 290 && r->headers_out.content_length_n < conf->min_length) 291 || ngx_strcasestrn(r->headers_in.accept_encoding->value.data, 292 "gzip", 4 - 1) 293 == NULL 294 ) 220 || r->headers_out.content_type.len == 0) 295 221 { 296 222 return ngx_http_next_header_filter(r); 297 223 } 298 224 299 300 type = conf->types->elts; 301 for (i = 0; i < conf->types->nelts; i++) { 302 if (r->headers_out.content_type.len >= type[i].len 303 && ngx_strncasecmp(r->headers_out.content_type.data, 304 type[i].data, type[i].len) == 0) 305 { 306 goto found; 307 } 308 } 309 310 return ngx_http_next_header_filter(r); 311 312 313 found: 314 315 if (r->headers_in.via) { 316 if (conf->proxied & NGX_HTTP_GZIP_PROXIED_OFF) { 317 return ngx_http_next_header_filter(r); 318 } 319 320 if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_ANY) 321 && ngx_http_gzip_proxied(r, conf) == NGX_DECLINED) 225 if (!r->gunzip) { 226 if (!conf->enable 227 || (r->headers_out.content_encoding 228 && r->headers_out.content_encoding->value.len) 229 || (r->headers_out.content_length_n != -1 230 && r->headers_out.content_length_n < conf->min_length) 231 || ngx_http_gzip_ok(r) != NGX_OK) 322 232 { 323 233 return ngx_http_next_header_filter(r); 324 234 } 325 } 326 327 328 /* 329 * if the URL (without the "http://" prefix) is longer than 253 bytes 330 * then MSIE 4.x can not handle the compressed stream - it waits too long, 331 * hangs up or crashes 332 */ 333 334 if (r->headers_in.msie4 && r->unparsed_uri.len > 200) { 235 236 type = conf->types->elts; 237 for (i = 0; i < conf->types->nelts; i++) { 238 if (r->headers_out.content_type.len >= type[i].len 239 && ngx_strncasecmp(r->headers_out.content_type.data, 240 type[i].data, type[i].len) == 0) 241 { 242 goto found; 243 } 244 } 245 335 246 return ngx_http_next_header_filter(r); 336 247 } 248 249 found: 337 250 338 251 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gzip_ctx_t)); … … 343 256 ngx_http_set_ctx(r, ctx, ngx_http_gzip_filter_module); 344 257 345 346 258 ctx->request = r; 347 259 348 header = ngx_list_push(&r->headers_out.headers); 349 if (header == NULL) { 350 return NGX_ERROR; 351 } 352 353 header->hash = 1; 354 header->key.len = sizeof("Content-Encoding") - 1; 355 header->key.data = (u_char *) "Content-Encoding"; 356 header->value.len = sizeof("gzip") - 1; 357 header->value.data = (u_char *) "gzip"; 358 359 r->headers_out.content_encoding = header; 360 361 if (conf->vary) { 362 header = ngx_list_push(&r->headers_out.headers); 363 if (header == NULL) { 260 if (!r->gunzip) { 261 h = ngx_list_push(&r->headers_out.headers); 262 if (h == NULL) { 364 263 return NGX_ERROR; 365 264 } 366 265 367 header->hash = 1; 368 header->key.len = sizeof("Vary") - 1; 369 header->key.data = (u_char *) "Vary"; 370 header->value.len = sizeof("Accept-Encoding") - 1; 371 header->value.data = (u_char *) "Accept-Encoding"; 266 h->hash = 1; 267 h->key.len = sizeof("Content-Encoding") - 1; 268 h->key.data = (u_char *) "Content-Encoding"; 269 h->value.len = sizeof("gzip") - 1; 270 h->value.data = (u_char *) "gzip"; 271 272 r->headers_out.content_encoding = h; 273 274 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 275 276 if (clcf->gzip_vary) { 277 h = ngx_list_push(&r->headers_out.headers); 278 if (h == NULL) { 279 return NGX_ERROR; 280 } 281 282 h->hash = 1; 283 h->key.len = sizeof("Vary") - 1; 284 h->key.data = (u_char *) "Vary"; 285 h->value.len = sizeof("Accept-Encoding") - 1; 286 h->value.data = (u_char *) "Accept-Encoding"; 287 } 372 288 } 373 289 … … 380 296 381 297 return ngx_http_next_header_filter(r); 382 }383 384 385 static ngx_int_t386 ngx_http_gzip_proxied(ngx_http_request_t *r, ngx_http_gzip_conf_t *conf)387 {388 time_t date, expires;389 390 if (r->headers_in.authorization391 && (conf->proxied & NGX_HTTP_GZIP_PROXIED_AUTH))392 {393 return NGX_OK;394 }395 396 if (r->headers_out.expires) {397 398 if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {399 return NGX_DECLINED;400 }401 402 expires = ngx_http_parse_time(r->headers_out.expires->value.data,403 r->headers_out.expires->value.len);404 if (expires == NGX_ERROR) {405 return NGX_DECLINED;406 }407 408 if (r->headers_out.date) {409 date = ngx_http_parse_time(r->headers_out.date->value.data,410 r->headers_out.date->value.len);411 if (date == NGX_ERROR) {412 return NGX_DECLINED;413 }414 415 } else {416 date = ngx_time();417 }418 419 if (expires < date) {420 return NGX_OK;421 }422 423 return NGX_DECLINED;424 }425 426 if (r->headers_out.cache_control.elts) {427 428 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_CACHE)429 && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,430 &ngx_http_gzip_no_cache, NULL) >= 0)431 {432 return NGX_OK;433 }434 435 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_STORE)436 && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,437 &ngx_http_gzip_no_store, NULL) >= 0)438 {439 return NGX_OK;440 }441 442 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_PRIVATE)443 && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,444 &ngx_http_gzip_private, NULL) >= 0)445 {446 return NGX_OK;447 }448 449 return NGX_DECLINED;450 }451 452 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_LM)453 && r->headers_out.last_modified)454 {455 return NGX_DECLINED;456 }457 458 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_ETAG)459 && r->headers_out.etag)460 {461 return NGX_DECLINED;462 }463 464 return NGX_OK;465 298 } 466 299 … … 476 309 ngx_http_gzip_ctx_t *ctx; 477 310 ngx_http_gzip_conf_t *conf; 311 const char *method = (r->gunzip ? "inflate" : "deflate"); 478 312 479 313 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module); … … 486 320 487 321 if (ctx->preallocated == NULL) { 488 wbits = conf->wbits;322 wbits = (!r->gunzip ? conf->wbits : 15); 489 323 memlevel = conf->memlevel; 490 324 491 if ( ctx->length > 0) {325 if (!r->gunzip && ctx->length > 0) { 492 326 493 327 /* the actual zlib window size is smaller by 262 bytes */ … … 524 358 ctx->zstream.opaque = ctx; 525 359 526 rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED, 527 -wbits, memlevel, Z_DEFAULT_STRATEGY); 360 if (!r->gunzip) { 361 rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED, 362 -wbits, memlevel, Z_DEFAULT_STRATEGY); 363 } else { 364 rc = inflateInit2(&ctx->zstream, 15 + 16); 365 } 528 366 529 367 if (rc != Z_OK) { 530 368 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 531 " deflateInit2() failed: %d", rc);369 "%sInit2() failed: %d", method, rc); 532 370 ngx_http_gzip_error(ctx); 533 371 return NGX_ERROR; 534 372 } 535 373 536 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); 537 if (b == NULL) { 538 ngx_http_gzip_error(ctx); 539 return NGX_ERROR; 540 } 541 542 b->memory = 1; 543 b->pos = gzheader; 544 b->last = b->pos + 10; 545 546 out.buf = b; 547 out.next = NULL; 548 549 /* 550 * We pass the gzheader to the next filter now to avoid its linking 551 * to the ctx->busy chain. zlib does not usually output the compressed 552 * data in the initial iterations, so the gzheader that was linked 553 * to the ctx->busy chain would be flushed by ngx_http_write_filter(). 554 */ 555 556 if (ngx_http_next_body_filter(r, &out) == NGX_ERROR) { 557 ngx_http_gzip_error(ctx); 558 return NGX_ERROR; 374 if (!r->gunzip) { 375 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); 376 if (b == NULL) { 377 ngx_http_gzip_error(ctx); 378 return NGX_ERROR; 379 } 380 381 b->memory = 1; 382 b->pos = gzheader; 383 b->last = b->pos + 10; 384 385 out.buf = b; 386 out.next = NULL; 387 388 /* 389 * We pass the gzheader to the next filter now to avoid 390 * its linking to the ctx->busy chain. zlib does not 391 * usually output the compressed data in the initial 392 * iterations, so the gzheader that was linked to the 393 * ctx->busy chain would be flushed by 394 * ngx_http_write_filter(). 395 */ 396 397 if (ngx_http_next_body_filter(r, &out) == NGX_ERROR) { 398 ngx_http_gzip_error(ctx); 399 return NGX_ERROR; 400 } 401 402 ctx->crc32 = crc32(0L, Z_NULL, 0); 559 403 } 560 404 … … 563 407 ctx->last_out = &ctx->out; 564 408 565 ctx->crc32 = crc32(0L, Z_NULL, 0);566 409 ctx->flush = Z_NO_FLUSH; 567 410 } … … 614 457 615 458 if (ctx->in_buf->last_buf) { 616 ctx->flush = Z_FINISH; 459 /* 460 * We use Z_BLOCK below for the following reason: 461 * if we would use Z_FINISH, then decompression 462 * may return Z_BUF_ERROR, meaning there wasn't 463 * enough room for decompressed data. This error 464 * is not fatal according to zlib.h, however 465 * ignoring it is dangerous and may mask real 466 * bugs. For instance, sometimes completely empty 467 * last buffer is passed to this filter, and 468 * decompression would enter the infinite loop: no 469 * progress is possible because input is void and 470 * Z_BUF_ERROR is ignored. We can't use 471 * Z_NO_FLUSH, because it will never return 472 * Z_STREAM_END. We can't use Z_SYNC_FLUSH, 473 * because it has a special meaning. So we use 474 * Z_BLOCK, which eventually would return 475 * Z_STREAM_END. 476 * 477 * Perhaps the above is also true for compression, 478 * but right now we won't try to change the old 479 * behaviour. 480 */ 481 ctx->flush = (!r->gunzip ? Z_FINISH : Z_BLOCK); 617 482 618 483 } else if (ctx->in_buf->flush) { … … 625 490 } 626 491 627 } else {492 } else if (!r->gunzip) { 628 493 ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in, 629 494 ctx->zstream.avail_in); … … 632 497 633 498 634 /* is there a space for the gzippeddata ? */499 /* is there a space for the output data ? */ 635 500 636 501 if (ctx->zstream.avail_out == 0) { … … 661 526 } 662 527 663 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 664 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d", 528 ngx_log_debug7(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 529 "%s in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d", 530 method, 665 531 ctx->zstream.next_in, ctx->zstream.next_out, 666 532 ctx->zstream.avail_in, ctx->zstream.avail_out, 667 533 ctx->flush, ctx->redo); 668 534 669 rc = deflate(&ctx->zstream, ctx->flush); 535 if (!r->gunzip) { 536 rc = deflate(&ctx->zstream, ctx->flush); 537 } else { 538 rc = inflate(&ctx->zstream, ctx->flush); 539 } 670 540 671 541 if (rc != Z_OK && rc != Z_STREAM_END) { 672 542 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 673 " deflate() failed: %d, %d", ctx->flush, rc);543 "%s() failed: %d, %d", method, ctx->flush, rc); 674 544 ngx_http_gzip_error(ctx); 675 545 return NGX_ERROR; 676 546 } 677 547 678 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 679 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d", 548 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 549 "%s out: ni:%p no:%p ai:%ud ao:%ud rc:%d", 550 method, 680 551 ctx->zstream.next_in, ctx->zstream.next_out, 681 552 ctx->zstream.avail_in, ctx->zstream.avail_out, 682 553 rc); 683 554 684 ngx_log_debug 2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,685 " gzipin_buf:%p pos:%p",686 ctx->in_buf, ctx->in_buf->pos);555 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 556 "%s in_buf:%p pos:%p", 557 method, ctx->in_buf, ctx->in_buf->pos); 687 558 688 559 … … 697 568 ctx->out_buf->last = ctx->zstream.next_out; 698 569 699 if (ctx->zstream.avail_out == 0) {700 701 /* zlib wants to output some more gzipped data */702 703 cl = ngx_alloc_chain_link(r->pool);704 if (cl == NULL) {705 ngx_http_gzip_error(ctx);706 return NGX_ERROR;707 }708 709 cl->buf = ctx->out_buf;710 cl->next = NULL;711 *ctx->last_out = cl;712 ctx->last_out = &cl->next;713 714 ctx->redo = 1;715 716 continue;717 }718 719 570 ctx->redo = 0; 720 571 721 572 if (ctx->flush == Z_SYNC_FLUSH) { 722 573 723 ctx->zstream.avail_out = 0;724 574 ctx->out_buf->flush = 1; 725 575 ctx->flush = Z_NO_FLUSH; 726 576 727 cl = ngx_alloc_chain_link(r->pool); 728 if (cl == NULL) { 729 ngx_http_gzip_error(ctx); 730 return NGX_ERROR; 731 } 732 733 cl->buf = ctx->out_buf; 734 cl->next = NULL; 735 *ctx->last_out = cl; 736 ctx->last_out = &cl->next; 737 738 break; 739 } 740 741 if (rc == Z_STREAM_END) { 742 743 ctx->zin = ctx->zstream.total_in; 744 ctx->zout = 10 + ctx->zstream.total_out + 8; 745 746 rc = deflateEnd(&ctx->zstream); 747 748 if (rc != Z_OK) { 749 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 750 "deflateEnd() failed: %d", rc); 751 ngx_http_gzip_error(ctx); 752 return NGX_ERROR; 753 } 754 755 ngx_pfree(r->pool, ctx->preallocated); 756 757 cl = ngx_alloc_chain_link(r->pool); 758 if (cl == NULL) { 759 ngx_http_gzip_error(ctx); 760 return NGX_ERROR; 761 } 762 763 cl->buf = ctx->out_buf; 764 cl->next = NULL; 765 *ctx->last_out = cl; 766 ctx->last_out = &cl->next; 767 768 if (ctx->zstream.avail_out >= 8) { 769 trailer = (struct gztrailer *) ctx->out_buf->last; 770 ctx->out_buf->last += 8; 771 ctx->out_buf->last_buf = 1; 772 773 } else { 774 b = ngx_create_temp_buf(r->pool, 8); 775 if (b == NULL) { 776 ngx_http_gzip_error(ctx); 777 return NGX_ERROR; 778 } 779 780 b->last_buf = 1; 577 /* 578 * On decompression there might be not enough input 579 * data to produce any output data. 580 */ 581 if (ctx->out_buf->last > ctx->out_buf->pos) { 582 583 ctx->zstream.avail_out = 0; 781 584 782 585 cl = ngx_alloc_chain_link(r->pool); … … 786 589 } 787 590 788 cl->buf = b;591 cl->buf = ctx->out_buf; 789 592 cl->next = NULL; 790 593 *ctx->last_out = cl; 791 594 ctx->last_out = &cl->next; 792 trailer = (struct gztrailer *) b->pos; 793 b->last += 8; 794 } 795 796 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED) 797 798 trailer->crc32 = ctx->crc32; 799 trailer->zlen = ctx->zin; 800 801 #else 802 trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff); 803 trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff); 804 trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff); 805 trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff); 806 807 trailer->zlen[0] = (u_char) (ctx->zin & 0xff); 808 trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff); 809 trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff); 810 trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff); 811 #endif 812 813 ctx->zstream.avail_in = 0; 814 ctx->zstream.avail_out = 0; 815 816 ctx->done = 1; 817 818 r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED; 819 820 break; 821 } 822 823 if (conf->no_buffer && ctx->in == NULL) { 595 596 break; 597 } 598 } 599 600 if (rc == Z_STREAM_END) { 601 602 ctx->zin = ctx->zstream.total_in; 603 if (!r->gunzip) { 604 ctx->zout = 10 + ctx->zstream.total_out + 8; 605 606 rc = deflateEnd(&ctx->zstream); 607 } else { 608 ctx->zout = ctx->zstream.total_out; 609 610 rc = inflateEnd(&ctx->zstream); 611 } 612 613 if (rc != Z_OK) { 614 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 615 "%sEnd() failed: %d", method, rc); 616 ngx_http_gzip_error(ctx); 617 return NGX_ERROR; 618 } 619 620 ngx_pfree(r->pool, ctx->preallocated); 824 621 825 622 cl = ngx_alloc_chain_link(r->pool); … … 829 626 } 830 627 628 /* 629 * On decompression we could already output everything 630 * under (ctx->flush == Z_SYNC_FLUSH), so we test here 631 * that the buffer is not empty. 632 */ 633 if (!r->gunzip || ctx->out_buf->last > ctx->out_buf->pos) { 634 cl->buf = ctx->out_buf; 635 cl->next = NULL; 636 *ctx->last_out = cl; 637 ctx->last_out = &cl->next; 638 } 639 640 if (!r->gunzip) { 641 if (ctx->zstream.avail_out >= 8) { 642 trailer = (struct gztrailer *) ctx->out_buf->last; 643 ctx->out_buf->last += 8; 644 ctx->out_buf->last_buf = 1; 645 646 } else { 647 b = ngx_create_temp_buf(r->pool, 8); 648 if (b == NULL) { 649 ngx_http_gzip_error(ctx); 650 return NGX_ERROR; 651 } 652 653 b->last_buf = 1; 654 655 cl = ngx_alloc_chain_link(r->pool); 656 if (cl == NULL) { 657 ngx_http_gzip_error(ctx); 658 return NGX_ERROR; 659 } 660 661 cl->buf = b; 662 cl->next = NULL; 663 *ctx->last_out = cl; 664 ctx->last_out = &cl->next; 665 trailer = (struct gztrailer *) b->pos; 666 b->last += 8; 667 } 668 669 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED) 670 671 trailer->crc32 = ctx->crc32; 672 trailer->zlen = ctx->zin; 673 674 #else 675 trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff); 676 trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff); 677 trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff); 678 trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff); 679 680 trailer->zlen[0] = (u_char) (ctx->zin & 0xff); 681 trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff); 682 trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff); 683 trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff); 684 #endif 685 } else { 686 ctx->out_buf->last_buf = 1; 687 r->gunzip = 0; 688 } 689 690 ctx->zstream.avail_in = 0; 691 ctx->zstream.avail_out = 0; 692 693 ctx->done = 1; 694 695 r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED; 696 697 break; 698 } 699 700 if (ctx->zstream.avail_out == 0) { 701 702 /* zlib wants to output some more gzipped data */ 703 704 cl = ngx_alloc_chain_link(r->pool); 705 if (cl == NULL) { 706 ngx_http_gzip_error(ctx); 707 return NGX_ERROR; 708 } 709 831 710 cl->buf = ctx->out_buf; 832 711 cl->next = NULL; … … 834 713 ctx->last_out = &cl->next; 835 714 715 ctx->redo = 1; 716 717 continue; 718 } 719 720 if (conf->no_buffer && ctx->in == NULL) { 721 722 cl = ngx_alloc_chain_link(r->pool); 723 if (cl == NULL) { 724 ngx_http_gzip_error(ctx); 725 return NGX_ERROR; 726 } 727 728 cl->buf = ctx->out_buf; 729 cl->next = NULL; 730 *ctx->last_out = cl; 731 ctx->last_out = &cl->next; 732 836 733 break; 837 734 } … … 929 826 ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx) 930 827 { 931 deflateEnd(&ctx->zstream); 828 if (! ctx->request->gunzip) { 829 deflateEnd(&ctx->zstream); 830 } else { 831 ctx->request->gunzip = 0; 832 inflateEnd(&ctx->zstream); 833 } 932 834 933 835 if (ctx->preallocated) { … … 1018 920 * 1019 921 * conf->bufs.num = 0; 1020 * conf->proxied = 0;1021 922 * conf->types = NULL; 1022 923 */ … … 1024 925 conf->enable = NGX_CONF_UNSET; 1025 926 conf->no_buffer = NGX_CONF_UNSET; 1026 conf->vary = NGX_CONF_UNSET;1027 1028 conf->http_version = NGX_CONF_UNSET_UINT;1029 927 1030 928 conf->level = NGX_CONF_UNSET; … … 1048 946 1049 947 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, ngx_pagesize); 1050 1051 ngx_conf_merge_uint_value(conf->http_version, prev->http_version,1052 NGX_HTTP_VERSION_11);1053 ngx_conf_merge_bitmask_value(conf->proxied, prev->proxied,1054 (NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));1055 948 1056 949 ngx_conf_merge_value(conf->level, prev->level, 1); … … 1060 953 ngx_conf_merge_value(conf->min_length, prev->min_length, 20); 1061 954 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0); 1062 ngx_conf_merge_value(conf->vary, prev->vary, 0);1063 955 1064 956 if (conf->types == NULL) { -
server/src/http/modules/ngx_http_memcached_module.c
re1c857 rc79fa6 15 15 ngx_int_t index; 16 16 ngx_uint_t gzip_flag; 17 ngx_int_t ns_index; 17 18 } ngx_http_memcached_loc_conf_t; 18 19 … … 158 159 159 160 static ngx_str_t ngx_http_memcached_key = ngx_string("memcached_key"); 161 static ngx_str_t ngx_http_memcached_ns = ngx_string("memcached_namespace"); 160 162 161 163 … … 235 237 { 236 238 size_t len; 237 uintptr_t escape ;239 uintptr_t escape, ns_escape = 0; 238 240 ngx_buf_t *b; 239 241 ngx_chain_t *cl; 240 242 ngx_http_memcached_ctx_t *ctx; 241 ngx_http_variable_value_t *vv ;243 ngx_http_variable_value_t *vv, *ns_vv; 242 244 ngx_http_memcached_loc_conf_t *mlcf; 243 245 … … 254 256 escape = 2 * ngx_escape_uri(NULL, vv->data, vv->len, NGX_ESCAPE_MEMCACHED); 255 257 256 len = sizeof("get ") - 1 + vv->len + escape + sizeof(CRLF) - 1; 258 ns_vv = ngx_http_get_indexed_variable(r, mlcf->ns_index); 259 260 if (ns_vv != NULL && !ns_vv->not_found && ns_vv->len != 0) { 261 ns_escape = 2 * ngx_escape_uri(NULL, ns_vv->data, 262 ns_vv->len, NGX_ESCAPE_MEMCACHED); 263 } 264 265 len = sizeof("get ") - 1 + ns_vv->len + ns_escape 266 + vv->len + escape + sizeof(CRLF) - 1; 257 267 258 268 b = ngx_create_temp_buf(r->pool, len); … … 276 286 277 287 ctx->key.data = b->last; 288 289 if (ns_vv != NULL && !ns_vv->not_found && ns_vv->len != 0) { 290 if (ns_escape == 0) { 291 b->last = ngx_copy(b->last, ns_vv->data, ns_vv->len); 292 } else { 293 b->last = (u_char *) ngx_escape_uri(b->last, ns_vv->data, 294 ns_vv->len, 295 NGX_ESCAPE_MEMCACHED); 296 } 297 } 278 298 279 299 if (escape == 0) { … … 588 608 * 589 609 * conf->index = 0; 610 * conf->ns_index = 0; 590 611 */ 591 612 … … 697 718 } 698 719 720 lcf->ns_index = ngx_http_get_variable_index(cf, &ngx_http_memcached_ns); 721 722 if (lcf->ns_index == NGX_ERROR) { 723 return NGX_CONF_ERROR; 724 } 725 699 726 return NGX_CONF_OK; 700 727 } -
server/src/http/modules/ngx_http_proxy_module.c
ra64318 r66a597 134 134 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, 135 135 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, 136 { ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 }, 136 137 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, 138 { ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 }, 139 { ngx_string("http_507"), NGX_HTTP_UPSTREAM_FT_HTTP_507 }, 137 140 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, 138 141 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, -
server/src/http/ngx_http_core_module.c
r0019fc r233310 68 68 static char *ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd, 69 69 void *conf); 70 #if (NGX_HTTP_GZIP) 71 static char *ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd, 72 void *conf); 73 #endif 70 74 71 75 static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data); … … 91 95 92 96 97 #if (NGX_HTTP_GZIP) 98 99 static ngx_conf_enum_t ngx_http_gzip_http_version[] = { 100 { ngx_string("1.0"), NGX_HTTP_VERSION_10 }, 101 { ngx_string("1.1"), NGX_HTTP_VERSION_11 }, 102 { ngx_null_string, 0 } 103 }; 104 105 106 static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = { 107 { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF }, 108 { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED }, 109 { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE }, 110 { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE }, 111 { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE }, 112 { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM }, 113 { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG }, 114 { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH }, 115 { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY }, 116 { ngx_null_string, 0 } 117 }; 118 119 120 static ngx_str_t ngx_http_gzip_no_cache = ngx_string("no-cache"); 121 static ngx_str_t ngx_http_gzip_no_store = ngx_string("no-store"); 122 static ngx_str_t ngx_http_gzip_private = ngx_string("private"); 123 124 #endif 125 126 93 127 static ngx_command_t ngx_http_core_commands[] = { 94 128 … … 470 504 NGX_HTTP_LOC_CONF_OFFSET, 471 505 offsetof(ngx_http_core_loc_conf_t, open_files), 506 NULL }, 507 508 #endif 509 510 #if (NGX_HTTP_GZIP) 511 512 { ngx_string("gzip_vary"), 513 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, 514 ngx_conf_set_flag_slot, 515 NGX_HTTP_LOC_CONF_OFFSET, 516 offsetof(ngx_http_core_loc_conf_t, gzip_vary), 517 NULL }, 518 519 { ngx_string("gzip_http_version"), 520 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 521 ngx_conf_set_enum_slot, 522 NGX_HTTP_LOC_CONF_OFFSET, 523 offsetof(ngx_http_core_loc_conf_t, gzip_http_version), 524 &ngx_http_gzip_http_version }, 525 526 { ngx_string("gzip_proxied"), 527 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, 528 ngx_conf_set_bitmask_slot, 529 NGX_HTTP_LOC_CONF_OFFSET, 530 offsetof(ngx_http_core_loc_conf_t, gzip_proxied), 531 &ngx_http_gzip_proxied_mask }, 532 533 { ngx_string("gzip_disable"), 534 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, 535 ngx_http_gzip_disable, 536 NGX_HTTP_LOC_CONF_OFFSET, 537 0, 538 NULL }, 539 540 { ngx_string("gunzip"), 541 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, 542 ngx_conf_set_flag_slot, 543 NGX_HTTP_LOC_CONF_OFFSET, 544 offsetof(ngx_http_core_loc_conf_t, gunzip), 472 545 NULL }, 473 546 … … 566 639 567 640 r->valid_location = 1; 641 r->gzip = 0; 642 r->gunzip = 0; 568 643 569 644 r->write_event_handler = ngx_http_core_run_phases; … … 1349 1424 return NGX_OK; 1350 1425 } 1426 1427 1428 #if (NGX_HTTP_GZIP) 1429 1430 ngx_int_t 1431 ngx_http_gzip_ok(ngx_http_request_t *r) 1432 { 1433 time_t date, expires; 1434 ngx_uint_t p; 1435 ngx_array_t *cc; 1436 ngx_table_elt_t *e, *d; 1437 ngx_http_core_loc_conf_t *clcf; 1438 1439 if (r->gzip == 1) { 1440 return NGX_OK; 1441 } 1442 1443 if (r->gzip == 2) { 1444 return NGX_DECLINED; 1445 } 1446 1447 r->gzip = 2; 1448 1449 if (r != r->main 1450 || r->headers_in.accept_encoding == NULL 1451 || ngx_strcasestrn(r->headers_in.accept_encoding->value.data, 1452 "gzip", 4 - 1) 1453 == NULL 1454 1455 /* 1456 * if the URL (without the "http://" prefix) is longer than 253 bytes, 1457 * then MSIE 4.x can not handle the compressed stream - it waits 1458 * too long, hangs up or crashes 1459 */ 1460 1461 || (r->headers_in.msie4 && r->unparsed_uri.len > 200)) 1462 { 1463 return NGX_DECLINED; 1464 } 1465 1466 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 1467 1468 if (r->http_version < clcf->gzip_http_version) { 1469 return NGX_DECLINED; 1470 } 1471 1472 if (r->headers_in.via == NULL) { 1473 goto ok; 1474 } 1475 1476 p = clcf->gzip_proxied; 1477 1478 if (p & NGX_HTTP_GZIP_PROXIED_OFF) { 1479 return NGX_DECLINED; 1480 } 1481 1482 if (p & NGX_HTTP_GZIP_PROXIED_ANY) { 1483 goto ok; 1484 } 1485 1486 if (r->headers_in.authorization && (p & NGX_HTTP_GZIP_PROXIED_AUTH)) { 1487 goto ok; 1488 } 1489 1490 e = r->headers_out.expires; 1491 1492 if (e) { 1493 1494 if (!(p & NGX_HTTP_GZIP_PROXIED_EXPIRED)) { 1495 return NGX_DECLINED; 1496 } 1497 1498 expires = ngx_http_parse_time(e->value.data, e->value.len); 1499 if (expires == NGX_ERROR) { 1500 return NGX_DECLINED; 1501 } 1502 1503 d = r->headers_out.date; 1504 1505 if (d) { 1506 date = ngx_http_parse_time(d->value.data, d->value.len); 1507 if (date == NGX_ERROR) { 1508 return NGX_DECLINED; 1509 } 1510 1511 } else { 1512 date = ngx_time(); 1513 } 1514 1515 if (expires < date) { 1516 goto ok; 1517 } 1518 1519 return NGX_DECLINED; 1520 } 1521 1522 cc = &r->headers_out.cache_control; 1523 1524 if (cc->elts) { 1525 1526 if ((p & NGX_HTTP_GZIP_PROXIED_NO_CACHE) 1527 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_cache, 1528 NULL) 1529 >= 0) 1530 { 1531 goto ok; 1532 } 1533 1534 if ((p & NGX_HTTP_GZIP_PROXIED_NO_STORE) 1535 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_store, 1536 NULL) 1537 >= 0) 1538 { 1539 goto ok; 1540 } 1541 1542 if ((p & NGX_HTTP_GZIP_PROXIED_PRIVATE) 1543 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_private, 1544 NULL) 1545 >= 0) 1546 { 1547 goto ok; 1548 } 1549 1550 return NGX_DECLINED; 1551 } 1552 1553 if ((p & NGX_HTTP_GZIP_PROXIED_NO_LM) && r->headers_out.last_modified) { 1554 return NGX_DECLINED; 1555 } 1556 1557 if ((p & NGX_HTTP_GZIP_PROXIED_NO_ETAG) && r->headers_out.etag) { 1558 return NGX_DECLINED; 1559 } 1560 1561 ok: 1562 1563 #if (NGX_PCRE) 1564 1565 if (clcf->gzip_disable && r->headers_in.user_agent) { 1566 1567 if (ngx_regex_exec_array(clcf->gzip_disable, 1568 &r->headers_in.user_agent->value, 1569 r->connection->log) 1570 != NGX_DECLINED) 1571 { 1572 return NGX_DECLINED; 1573 } 1574 } 1575 1576 #endif 1577 1578 r->gzip = 1; 1579 1580 return NGX_OK; 1581 } 1582 1583 #endif 1351 1584 1352 1585 … … 2352 2585 * lcf->auto_redirect = 0; 2353 2586 * lcf->alias = 0; 2587 * lcf->gzip_proxied = 0; 2354 2588 */ 2355 2589 … … 2382 2616 lcf->types_hash_bucket_size = NGX_CONF_UNSET_UINT; 2383 2617 2618 #if (NGX_HTTP_GZIP) 2619 lcf->gzip_vary = NGX_CONF_UNSET; 2620 lcf->gzip_http_version = NGX_CONF_UNSET_UINT; 2621 lcf->gzip_disable = NGX_CONF_UNSET_PTR; 2622 lcf->gunzip = NGX_CONF_UNSET; 2623 #endif 2624 2384 2625 return lcf; 2385 2626 } … … 2568 2809 conf->open_files = prev->open_files; 2569 2810 } 2811 2812 #if (NGX_HTTP_GZIP) 2813 2814 ngx_conf_merge_value(conf->gzip_vary, prev->gzip_vary, 0); 2815 ngx_conf_merge_value(conf->gunzip, prev->gunzip, 0); 2816 ngx_conf_merge_uint_value(conf->gzip_http_version, prev->gzip_http_version, 2817 NGX_HTTP_VERSION_11); 2818 ngx_conf_merge_bitmask_value(conf->gzip_proxied, prev->gzip_proxied, 2819 (NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF)); 2820 2821 ngx_conf_merge_ptr_value(conf->gzip_disable, prev->gzip_disable, NULL); 2822 2823 #endif 2570 2824 2571 2825 return NGX_CONF_OK; … … 3253 3507 3254 3508 3509 #if (NGX_HTTP_GZIP) 3510 3511 static char * 3512 ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 3513 { 3514 #if (NGX_PCRE) 3515 ngx_http_core_loc_conf_t *clcf = conf; 3516 3517 ngx_str_t err, *value; 3518 ngx_uint_t i; 3519 ngx_regex_elt_t *re; 3520 u_char errstr[NGX_MAX_CONF_ERRSTR]; 3521 3522 if (clcf->gzip_disable == NGX_CONF_UNSET_PTR) { 3523 clcf->gzip_disable = ngx_array_create(cf->pool, 2, 3524 sizeof(ngx_regex_elt_t)); 3525 if (clcf->gzip_disable == NULL) { 3526 return NGX_CONF_ERROR; 3527 } 3528 } 3529 3530 value = cf->args->elts; 3531 3532 err.len = NGX_MAX_CONF_ERRSTR; 3533 err.data = errstr; 3534 3535 for (i = 1; i < cf->args->nelts; i++) { 3536 3537 re = ngx_array_push(clcf->gzip_disable); 3538 if (re == NULL) { 3539 return NGX_CONF_ERROR; 3540 } 3541 3542 re->regex = ngx_regex_compile(&value[i], NGX_REGEX_CASELESS, cf->pool, 3543 &err); 3544 3545 if (re->regex == NULL) { 3546 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data); 3547 return NGX_CONF_ERROR; 3548 } 3549 3550 re->name = value[i].data; 3551 } 3552 3553 return NGX_CONF_OK; 3554 3555 #else 3556 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 3557 "\"gzip_disable\" requires PCRE library"); 3558 3559 return NGX_CONF_ERROR; 3560 #endif 3561 } 3562 3563 #endif 3564 3565 3255 3566 static char * 3256 3567 ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data) -
server/src/http/ngx_http_core_module.h
r0019fc r233310 12 12 #include <ngx_array.h> 13 13 #include <ngx_http.h> 14 15 16 #define NGX_HTTP_GZIP_PROXIED_OFF 0x0002 17 #define NGX_HTTP_GZIP_PROXIED_EXPIRED 0x0004 18 #define NGX_HTTP_GZIP_PROXIED_NO_CACHE 0x0008 19 #define NGX_HTTP_GZIP_PROXIED_NO_STORE 0x0010 20 #define NGX_HTTP_GZIP_PROXIED_PRIVATE 0x0020 21 #define NGX_HTTP_GZIP_PROXIED_NO_LM 0x0040 22 #define NGX_HTTP_GZIP_PROXIED_NO_ETAG 0x0080 23 #define NGX_HTTP_GZIP_PROXIED_AUTH 0x0100 24 #define NGX_HTTP_GZIP_PROXIED_ANY 0x0200 14 25 15 26 … … 288 299 ngx_flag_t server_tokens; /* server_tokens */ 289 300 301 #if (NGX_HTTP_GZIP) 302 ngx_flag_t gzip_vary; /* gzip_vary */ 303 304 ngx_uint_t gzip_http_version; /* gzip_http_version */ 305 ngx_uint_t gzip_proxied; /* gzip_proxied */ 306 ngx_flag_t gunzip; 307 308 #if (NGX_PCRE) 309 ngx_array_t *gzip_disable; /* gzip_disable */ 310 #endif 311 #endif 312 290 313 ngx_array_t *error_pages; /* error_page */ 291 314 … … 324 347 size_t *root_length, size_t reserved); 325 348 ngx_int_t ngx_http_auth_basic_user(ngx_http_request_t *r); 349 #if (NGX_HTTP_GZIP) 350 ngx_int_t ngx_http_gzip_ok(ngx_http_request_t *r); 351 #endif 352 326 353 327 354 ngx_int_t ngx_http_subrequest(ngx_http_request_t *r, -
server/src/http/ngx_http_request.h
ra64318 r233310 429 429 430 430 unsigned header_timeout_set:1; 431 432 unsigned gzip:2; 433 unsigned gunzip:1; 431 434 432 435 unsigned proxy:1; -
server/src/http/ngx_http_upstream.c
rde9561 r445d49 1094 1094 } 1095 1095 1096 if (u->headers_in.status_n == NGX_HTTP_BAD_GATEWAY) { 1097 1098 if (u->peer.tries > 1 1099 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_502) 1100 { 1101 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_502); 1102 return; 1103 } 1104 } 1105 1106 if (u->headers_in.status_n == NGX_HTTP_SERVICE_UNAVAILABLE) { 1107 1108 if (u->peer.tries > 1 1109 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_503) 1110 { 1111 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_503); 1112 return; 1113 } 1114 } 1115 1116 if (u->headers_in.status_n == NGX_HTTP_GATEWAY_TIME_OUT) { 1117 1118 if (u->peer.tries > 1 1119 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_504) 1120 { 1121 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_504); 1122 return; 1123 } 1124 } 1125 1126 if (u->headers_in.status_n == NGX_HTTP_INSUFFICIENT_STORAGE) { 1127 1128 if (u->peer.tries > 1 1129 && u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_HTTP_507) 1130 { 1131 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_HTTP_507); 1132 return; 1133 } 1134 } 1096 1135 1097 1136 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST … … 2173 2212 switch(ft_type) { 2174 2213 2214 case NGX_HTTP_UPSTREAM_FT_HTTP_504: 2175 2215 case NGX_HTTP_UPSTREAM_FT_TIMEOUT: 2176 2216 status = NGX_HTTP_GATEWAY_TIME_OUT; … … 2183 2223 case NGX_HTTP_UPSTREAM_FT_HTTP_404: 2184 2224 status = NGX_HTTP_NOT_FOUND; 2225 break; 2226 2227 case NGX_HTTP_UPSTREAM_FT_HTTP_502: 2228 status = NGX_HTTP_BAD_GATEWAY; 2229 break; 2230 2231 case NGX_HTTP_UPSTREAM_FT_HTTP_503: 2232 status = NGX_HTTP_SERVICE_UNAVAILABLE; 2233 break; 2234 2235 case NGX_HTTP_UPSTREAM_FT_HTTP_507: 2236 status = NGX_HTTP_INSUFFICIENT_STORAGE; 2185 2237 break; 2186 2238 -
server/src/http/ngx_http_upstream.h
rde9561 r445d49 25 25 #define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK 0x00000080 26 26 #define NGX_HTTP_UPSTREAM_FT_MAX_WAITING 0x00000100 27 #define NGX_HTTP_UPSTREAM_FT_HTTP_502 0x00000200 28 #define NGX_HTTP_UPSTREAM_FT_HTTP_504 0x00000400 29 #define NGX_HTTP_UPSTREAM_FT_HTTP_507 0x00000800 27 30 #define NGX_HTTP_UPSTREAM_FT_OFF 0x80000000 28 31 -
server/src/http/ngx_http_variables.c
r0019fc r7bf8e1 1313 1313 1314 1314 1315 static ngx_int_t 1316 ngx_http_optional_variable(ngx_http_request_t *r, 1317 ngx_http_variable_value_t *v, uintptr_t data) 1318 { 1319 *v = ngx_http_variable_null_value; 1320 return NGX_OK; 1321 } 1322 1323 1315 1324 ngx_int_t 1316 1325 ngx_http_variables_init_vars(ngx_conf_t *cf) … … 1374 1383 } 1375 1384 1385 if (ngx_strncmp(v[i].name.data, "memcached_namespace", 19) == 0) { 1386 v[i].get_handler = ngx_http_optional_variable; 1387 1388 continue; 1389 } 1390 1376 1391 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, 1377 1392 "unknown \"%V\" variable", &v[i].name);
Note: See TracChangeset
for help on using the changeset viewer.
