From d140e7b1ff44b06bc54c2b790d06e9c7325503fe Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 2 Feb 2011 14:45:57 -0800 Subject: http: parser handles IPv6 bracketed IP hostnames Just in case we have people that don't use DNS, we can support folks who enter ugly IPv6 addresses... IPv6 uses brackets around the address to avoid confusing the colons used in the address with the colon used to denote the TCP port number in URIs. --- ext/unicorn_http/unicorn_http.rl | 13 ++++++++++++- ext/unicorn_http/unicorn_http_common.rl | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'ext') diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl index 292601c..971cdc1 100644 --- a/ext/unicorn_http/unicorn_http.rl +++ b/ext/unicorn_http/unicorn_http.rl @@ -505,7 +505,18 @@ static void set_server_vars(VALUE env, VALUE *server_port) if (!NIL_P(host)) { char *host_ptr = RSTRING_PTR(host); long host_len = RSTRING_LEN(host); - char *colon = memchr(host_ptr, ':', host_len); + char *colon; + + if (*host_ptr == '[') { /* ipv6 address format */ + char *rbracket = memchr(host_ptr + 1, ']', host_len - 1); + + if (rbracket) + colon = (rbracket[1] == ':') ? rbracket + 1 : NULL; + else + colon = memchr(host_ptr + 1, ':', host_len - 1); + } else { + colon = memchr(host_ptr, ':', host_len); + } if (colon) { long port_start = colon - host_ptr + 1; diff --git a/ext/unicorn_http/unicorn_http_common.rl b/ext/unicorn_http/unicorn_http_common.rl index f165e3f..cf93fec 100644 --- a/ext/unicorn_http/unicorn_http_common.rl +++ b/ext/unicorn_http/unicorn_http_common.rl @@ -26,7 +26,7 @@ # URI schemes and absolute paths scheme = ( "http"i ("s"i)? ) $downcase_char >mark %scheme; - hostname = (alnum | "-" | "." | "_")+; + hostname = ((alnum | "-" | "." | "_")+ | ("[" (":" | xdigit)+ "]")); host_with_port = (hostname (":" digit*)?) >mark %host; userinfo = ((unreserved | escape | ";" | ":" | "&" | "=" | "+")+ "@")*; -- cgit v1.2.3-24-ge0c7