Skip to content

Commit 4160a6b

Browse files
michaellee8commit-bot@chromium.org
authored andcommitted
[Http] Fix handling of ipv6 address in host header
The current implementation of _HttpHeaders._addHost does not handle ipv6 address correctly. This patch fixes the handling and add test case for that. References: - flutter/flutter#83609 (comment) TEST=http_headers_test.dart has been updated with new cases. Bug: flutter/flutter#83609 (comment) Change-Id: Idc4e9bbe66d66f0d846c7a32a40854201c3b0153 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/205280 Reviewed-by: Siva Annamalai <[email protected]> Commit-Queue: Siva Annamalai <[email protected]>
1 parent 0acfd2a commit 4160a6b

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

sdk/lib/_http/http_headers.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,13 @@ class _HttpHeaders implements HttpHeaders {
421421

422422
void _addHost(String name, value) {
423423
if (value is String) {
424-
int pos = value.indexOf(":");
425-
if (pos == -1) {
424+
// value.indexOf will only work for ipv4, ipv6 which has multiple : in its
425+
// host part needs lastIndexOf
426+
int pos = value.lastIndexOf(":");
427+
// According to RFC 3986, section 3.2.2, host part of ipv6 address must be
428+
// enclosed by square brackets.
429+
// https://serverfault.com/questions/205793/how-can-one-distinguish-the-host-and-the-port-in-an-ipv6-url
430+
if (pos == -1 || value.startsWith("[") && value.endsWith("]")) {
426431
_host = value;
427432
_port = HttpClient.defaultHttpPort;
428433
} else {

tests/standalone/io/http_headers_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,40 @@ void testHost() {
173173
Expect.equals(":1234", headers.value(HttpHeaders.hostHeader));
174174
Expect.isNull(headers.host);
175175
Expect.equals(1234, headers.port);
176+
177+
// ipv4
178+
host = "123.45.67.89";
179+
int port = 1234;
180+
headers = new _HttpHeaders("1.1");
181+
headers.add(HttpHeaders.hostHeader, "$host:$port");
182+
Expect.equals("$host:$port", headers.value(HttpHeaders.hostHeader));
183+
Expect.equals(host, headers.host);
184+
Expect.equals(port, headers.port);
185+
186+
// ipv6: host+port
187+
host = "[2001:db8::1]";
188+
port = 1234;
189+
headers = new _HttpHeaders("1.1");
190+
headers.add(HttpHeaders.hostHeader, "$host:$port");
191+
Expect.equals("$host:$port", headers.value(HttpHeaders.hostHeader));
192+
Expect.equals(host, headers.host);
193+
Expect.equals(port, headers.port);
194+
195+
// ipv6: host only
196+
host = "[2001:db8::1]";
197+
headers = new _HttpHeaders("1.1");
198+
headers.add(HttpHeaders.hostHeader, "$host");
199+
Expect.equals("$host", headers.value(HttpHeaders.hostHeader));
200+
Expect.equals(host, headers.host);
201+
Expect.equals(headers.port, HttpClient.defaultHttpPort);
202+
203+
// ipv6: host + invalid port
204+
host = "[2001:db8::1]";
205+
headers = new _HttpHeaders("1.1");
206+
headers.add(HttpHeaders.hostHeader, "$host:xxx");
207+
Expect.equals("$host:xxx", headers.value(HttpHeaders.hostHeader));
208+
Expect.equals(host, headers.host);
209+
Expect.isNull(headers.port);
176210
}
177211

178212
void testTransferEncoding() {

0 commit comments

Comments
 (0)