@@ -226,14 +226,36 @@ class _InternetAddress implements InternetAddress {
226226 return _InternetAddress (
227227 InternetAddressType .unix, address, null , rawAddress);
228228 } else {
229- var in_addr = _parse (address);
230- if (in_addr == null ) {
231- throw ArgumentError ("Invalid internet address $address " );
229+ int index = address.indexOf ('%' );
230+ String originalAddress = address;
231+ String ? scopeID;
232+ if (index > 0 ) {
233+ scopeID = address.substring (index, address.length);
234+ address = address.substring (0 , index);
232235 }
233- InternetAddressType type = in_addr.length == _IPv4AddrLength
236+ var inAddr = _parse (address);
237+ if (inAddr == null ) {
238+ throw ArgumentError .value ("Invalid internet address $address " );
239+ }
240+ InternetAddressType type = inAddr.length == _IPv4AddrLength
234241 ? InternetAddressType .IPv4
235242 : InternetAddressType .IPv6 ;
236- return _InternetAddress (type, address, null , in_addr);
243+ if (scopeID != null && scopeID.length > 0 ) {
244+ if (type != InternetAddressType .IPv6 ) {
245+ throw ArgumentError .value ("IPv4 addresses cannot have a scope id" );
246+ }
247+ // This is an IPv6 address with scope id.
248+ var list = _parseScopedLinkLocalAddress (originalAddress);
249+
250+ if (list is ! OSError && (list as List ).isNotEmpty) {
251+ return _InternetAddress (InternetAddressType .IPv6 , originalAddress,
252+ null , inAddr, list.first);
253+ } else {
254+ throw ArgumentError .value (
255+ "Invalid IPv6 address $address with scope ID" );
256+ }
257+ }
258+ return _InternetAddress (type, originalAddress, null , inAddr, 0 );
237259 }
238260 }
239261
@@ -262,12 +284,11 @@ class _InternetAddress implements InternetAddress {
262284
263285 static _InternetAddress ? tryParse (String address) {
264286 checkNotNullable (address, "address" );
265- var addressBytes = _parse (address);
266- if (addressBytes == null ) return null ;
267- var type = addressBytes.length == _IPv4AddrLength
268- ? InternetAddressType .IPv4
269- : InternetAddressType .IPv6 ;
270- return _InternetAddress (type, address, null , addressBytes);
287+ try {
288+ return _InternetAddress .fromString (address);
289+ } catch (e) {
290+ return null ;
291+ }
271292 }
272293
273294 factory _InternetAddress .fixed (int id) {
@@ -297,7 +318,8 @@ class _InternetAddress implements InternetAddress {
297318
298319 // Create a clone of this _InternetAddress replacing the host.
299320 _InternetAddress _cloneWithNewHost (String host) {
300- return _InternetAddress (type, address, host, Uint8List .fromList (_in_addr));
321+ return _InternetAddress (
322+ type, address, host, Uint8List .fromList (_in_addr), _scope_id);
301323 }
302324
303325 bool operator == (other) {
@@ -330,7 +352,8 @@ class _InternetAddress implements InternetAddress {
330352
331353 static String _rawAddrToString (Uint8List address)
332354 native "InternetAddress_RawAddrToString" ;
333-
355+ static List _parseScopedLinkLocalAddress (String address)
356+ native "InternetAddress_ParseScopedLinkLocalAddress" ;
334357 static Uint8List ? _parse (String address) native "InternetAddress_Parse" ;
335358}
336359
0 commit comments