# HG changeset patch # User Igor Sysoev # Date 1193669571 0 # Node ID d5db0e96bcc620a3186836a09bb99190c6e04cb9 # Parent f5e9d597a751d44ad8fa4cde335f3135e044da8d r1542, r1543, r1544, r1549, r1550, r1551, r1555 merge: *) ngx_strstrn() and ngx_strcasestrn() *) fix memcached END test diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c --- a/src/core/ngx_string.c +++ b/src/core/ngx_string.c @@ -442,7 +442,7 @@ ngx_vsnprintf(u_char *buf, size_t max, c /* - * We use ngx_strcasecmp()/ngx_strncasecmp() for 7-bit ASCII string only, + * We use ngx_strcasecmp()/ngx_strncasecmp() for 7-bit ASCII strings only, * and implement our own ngx_strcasecmp()/ngx_strncasecmp() * to avoid libc locale overhead. Besides, we use the ngx_uint_t's * instead of the u_char's, because they are slightly faster. @@ -503,6 +503,61 @@ ngx_strncasecmp(u_char *s1, u_char *s2, } +/* + * ngx_strstrn() and ngx_strcasestrn() are intended to search for static + * substring with known length in null-terminated string. The argument n + * must be length of the second substring - 1. + */ + +u_char * +ngx_strstrn(u_char *s1, char *s2, size_t n) +{ + u_char c1, c2; + + c2 = *(u_char *) s2++; + + do { + do { + c1 = *s1++; + + if (c1 == 0) { + return NULL; + } + + } while (c1 != c2); + + } while (ngx_strncmp(s1, (u_char *) s2, n) != 0); + + return --s1; +} + + +u_char * +ngx_strcasestrn(u_char *s1, char *s2, size_t n) +{ + ngx_uint_t c1, c2; + + c2 = (ngx_uint_t) *s2++; + c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2; + + do { + do { + c1 = (ngx_uint_t) *s1++; + + if (c1 == 0) { + return NULL; + } + + c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1; + + } while (c1 != c2); + + } while (ngx_strncasecmp(s1, (u_char *) s2, n) != 0); + + return --s1; +} + + ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n) { diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h --- a/src/core/ngx_string.h +++ b/src/core/ngx_string.h @@ -126,6 +126,9 @@ u_char *ngx_vsnprintf(u_char *buf, size_ ngx_int_t ngx_strcasecmp(u_char *s1, u_char *s2); ngx_int_t ngx_strncasecmp(u_char *s1, u_char *s2, size_t n); +u_char *ngx_strstrn(u_char *s1, char *s2, size_t n); +u_char *ngx_strcasestrn(u_char *s1, char *s2, size_t n); + ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n); ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n); ngx_int_t ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2); diff --git a/src/http/modules/ngx_http_gzip_filter_module.c b/src/http/modules/ngx_http_gzip_filter_module.c --- a/src/http/modules/ngx_http_gzip_filter_module.c +++ b/src/http/modules/ngx_http_gzip_filter_module.c @@ -279,7 +279,9 @@ ngx_http_gzip_header_filter(ngx_http_req || r->headers_in.accept_encoding == NULL || (r->headers_out.content_length_n != -1 && r->headers_out.content_length_n < conf->min_length) - || ngx_strstr(r->headers_in.accept_encoding->value.data, "gzip") == NULL + || ngx_strcasestrn(r->headers_in.accept_encoding->value.data, + "gzip", 4 - 1) + == NULL ) { return ngx_http_next_header_filter(r); diff --git a/src/http/modules/ngx_http_memcached_module.c b/src/http/modules/ngx_http_memcached_module.c --- a/src/http/modules/ngx_http_memcached_module.c +++ b/src/http/modules/ngx_http_memcached_module.c @@ -425,16 +425,16 @@ ngx_http_memcached_filter(void *data, ss if (u->length == ctx->rest) { if (ngx_strncmp(b->last, - ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - - ctx->rest, - bytes) != 0) + ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - ctx->rest, + ctx->rest) + != 0) { ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, "memcached sent invalid trailer"); } - u->length -= bytes; - ctx->rest -= bytes; + u->length = 0; + ctx->rest = 0; return NGX_OK; } @@ -453,7 +453,8 @@ ngx_http_memcached_filter(void *data, ss *ll = cl; - cl->buf->pos = b->last; + last = b->last; + cl->buf->pos = last; b->last += bytes; cl->buf->last = b->last; @@ -461,20 +462,19 @@ ngx_http_memcached_filter(void *data, ss "memcached filter bytes:%z size:%z length:%z rest:%z", bytes, b->last - b->pos, u->length, ctx->rest); - if (b->last - b->pos <= (ssize_t) (u->length - NGX_HTTP_MEMCACHED_END)) { + if (bytes <= (ssize_t) (u->length - NGX_HTTP_MEMCACHED_END)) { u->length -= bytes; return NGX_OK; } - - last = b->pos + u->length - NGX_HTTP_MEMCACHED_END; + last += u->length - NGX_HTTP_MEMCACHED_END; if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) { ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, "memcached sent invalid trailer"); } - ctx->rest = u->length - (b->last - b->pos); + ctx->rest -= b->last - last; b->last = last; cl->buf->last = last; u->length = ctx->rest; diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -1205,10 +1205,10 @@ static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h, ngx_uint_t offset) { - if (ngx_strstr(h->value.data, "close")) { + if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) { r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE; - } else if (ngx_strstr(h->value.data, "keep-alive")) { + } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) { r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE; } @@ -1320,7 +1320,8 @@ ngx_http_process_request_header(ngx_http } if (r->headers_in.transfer_encoding - && ngx_strstr(r->headers_in.transfer_encoding->value.data, "chunked")) + && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data, + "chunked", 7 - 1)) { ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "client sent \"Transfer-Encoding: chunked\" header"); @@ -1352,7 +1353,7 @@ ngx_http_process_request_header(ngx_http user_agent = r->headers_in.user_agent->value.data; - ua = (u_char *) ngx_strstr(user_agent, "MSIE"); + ua = ngx_strstrn(user_agent, "MSIE", 4 - 1); if (ua && ua + 8 < user_agent + r->headers_in.user_agent->value.len) { @@ -1370,7 +1371,7 @@ ngx_http_process_request_header(ngx_http #endif } - if (ngx_strstr(user_agent, "Opera")) { + if (ngx_strstrn(user_agent, "Opera", 5 - 1)) { r->headers_in.opera = 1; r->headers_in.msie = 0; r->headers_in.msie4 = 0; @@ -1378,10 +1379,10 @@ ngx_http_process_request_header(ngx_http if (!r->headers_in.msie && !r->headers_in.opera) { - if (ngx_strstr(user_agent, "Gecko/")) { + if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) { r->headers_in.gecko = 1; - } else if (ngx_strstr(user_agent, "Konqueror")) { + } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) { r->headers_in.konqueror = 1; } } diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -2632,7 +2632,7 @@ ngx_http_upstream_rewrite_refresh(ngx_ht if (r->upstream->rewrite_redirect) { - p = (u_char *) ngx_strstr(ho->value.data, "url="); + p = ngx_strcasestrn(ho->value.data, "url=", 4 - 1); if (p) { rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data);