From 9b6364cd64ac9a441595bd1cad9e637a49f8c501 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Thu, 5 Nov 2020 23:36:38 -0500 Subject: [PATCH] fix ipv6 addresses in parsed uris --- cgi.d | 30 +++++++++++++++++++++++++++++- http2.d | 15 ++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/cgi.d b/cgi.d index 5d50210..ce4fc3d 100644 --- a/cgi.d +++ b/cgi.d @@ -2797,7 +2797,20 @@ struct Uri { authority = authority[idx2 + 1 .. $]; } - idx2 = authority.indexOf(":"); + if(authority.length && authority[0] == '[') { + // ipv6 address special casing + idx2 = authority.indexOf(']'); + if(idx2 != -1) { + auto end = authority[idx2 + 1 .. $]; + if(end.length && end[0] == ':') + idx2 = idx2 + 1; + else + idx2 = -1; + } + } else { + idx2 = authority.indexOf(":"); + } + if(idx2 == -1) { port = 0; // 0 means not specified; we should use the default for the scheme host = authority; @@ -2946,6 +2959,21 @@ struct Uri { uri = Uri("?lol#foo"); assert(uri.fragment == "foo"); assert(uri.query == "lol"); + + uri = Uri("http://127.0.0.1/"); + assert(uri.host == "127.0.0.1"); + assert(uri.port == 0); + + uri = Uri("http://127.0.0.1:123/"); + assert(uri.host == "127.0.0.1"); + assert(uri.port == 123); + + uri = Uri("http://[ff:ff::0]/"); + assert(uri.host == "[ff:ff::0]"); + + uri = Uri("http://[ff:ff::0]:123/"); + assert(uri.host == "[ff:ff::0]"); + assert(uri.port == 123); } // This can sometimes be a big pain in the butt for me, so lots of copy/paste here to cover diff --git a/http2.d b/http2.d index 1fb2d2e..04fb6ca 100644 --- a/http2.d +++ b/http2.d @@ -484,7 +484,20 @@ struct Uri { authority = authority[idx2 + 1 .. $]; } - idx2 = authority.indexOf(":"); + if(authority.length && authority[0] == '[') { + // ipv6 address special casing + idx2 = authority.indexOf(']'); + if(idx2 != -1) { + auto end = authority[idx2 + 1 .. $]; + if(end.length && end[0] == ':') + idx2 = idx2 + 1; + else + idx2 = -1; + } + } else { + idx2 = authority.indexOf(":"); + } + if(idx2 == -1) { port = 0; // 0 means not specified; we should use the default for the scheme host = authority;