diff options
Diffstat (limited to 'src/http/ngx_http_request.c')
-rw-r--r-- | src/http/ngx_http_request.c | 77 |
1 files changed, 53 insertions, 24 deletions
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c index aea2228fd..fbb7915b3 100644 --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -1674,56 +1674,85 @@ static ssize_t ngx_http_validate_host(ngx_http_request_t *r, u_char **host, size_t len, ngx_uint_t alloc) { - u_char *h, ch; - size_t i, last; - ngx_uint_t dot; + u_char *h, ch; + size_t i, dot_pos, host_len; + + enum { + sw_usual = 0, + sw_literal, + sw_rest + } state; + + dot_pos = len; + host_len = len; - last = len; h = *host; - dot = 0; + + state = sw_usual; for (i = 0; i < len; i++) { ch = h[i]; - if (ch == '.') { - if (dot) { + switch (ch) { + + case '.': + if (dot_pos == i - 1) { return 0; } + dot_pos = i; + break; - dot = 1; - continue; - } + case ':': + if (state == sw_usual) { + host_len = i; + state = sw_rest; + } + break; - dot = 0; + case '[': + if (i == 0) { + state = sw_literal; + } + break; - if (ch == ':') { - last = i; - continue; - } + case ']': + if (state == sw_literal) { + host_len = i + 1; + state = sw_rest; + } + break; - if (ngx_path_separator(ch) || ch == '\0') { + case '\0': return 0; - } - if (ch >= 'A' || ch < 'Z') { - alloc = 1; + default: + + if (ngx_path_separator(ch)) { + return 0; + } + + if (ch >= 'A' && ch <= 'Z') { + alloc = 1; + } + + break; } } - if (dot) { - last--; + if (dot_pos == host_len - 1) { + host_len--; } if (alloc) { - *host = ngx_pnalloc(r->pool, last) ; + *host = ngx_pnalloc(r->pool, host_len); if (*host == NULL) { return -1; } - ngx_strlow(*host, h, last); + ngx_strlow(*host, h, host_len); } - return last; + return host_len; } |