Skip to content

Commit

Permalink
Refactor the IPv4 pointer according to spec update
Browse files Browse the repository at this point in the history
This does not change the behavior but keeps us aligned with the spec. Follows whatwg/url#285.
  • Loading branch information
domenic committed Apr 13, 2017
1 parent 6e0f00c commit 9fc1925
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spe

## Current Status

whatwg-url is currently up to date with the URL spec up to commit [a562c5](https://github.com/whatwg/url/commit/a562c55d659e1657d1dc9f9fad600bd9f9d70d82).
whatwg-url is currently up to date with the URL spec up to commit [703fcd](https://github.com/whatwg/url/commit/703fcd0b92053345582a36b2e4a642ab65df076e).

## API

Expand Down
107 changes: 51 additions & 56 deletions src/url-state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ function parseIPv6(input) {
compressPtr = piecePtr;
}

let ipv4 = false;
Main:
while (pointer < input.length) {
if (piecePtr === 8) {
return failure;
Expand All @@ -270,77 +268,74 @@ function parseIPv6(input) {
++length;
}

switch (at(input, pointer)) {
case ".":
if (length === 0) {
return failure;
}
pointer -= length;
ipv4 = true;
break Main;
case ":":
++pointer;
if (input[pointer] === undefined) {
return failure;
}
break;
case undefined:
break;
default:
if (input[pointer] === p(".")) {
if (length === 0) {
return failure;
}
}

ip[piecePtr] = value;
++piecePtr;
}
pointer -= length;

if (ipv4 && piecePtr > 6) {
return failure;
} else if (input[pointer] !== undefined) {
let numbersSeen = 0;
if (piecePtr > 6) {
return failure;
}

while (input[pointer] !== undefined) {
let value = null;
let numbersSeen = 0;

if (numbersSeen > 0) {
if (input[pointer] === p(".") && numbersSeen < 4) {
++pointer;
} else {
return failure;
}
}
while (input[pointer] !== undefined) {
let ipv4Piece = null;

if (!isASCIIDigit(input[pointer])) {
return failure;
}
if (numbersSeen > 0) {
if (input[pointer] === p(".") && numbersSeen < 4) {
++pointer;
} else {
return failure;
}
}

while (isASCIIDigit(input[pointer])) {
const number = parseInt(at(input, pointer));
if (value === null) {
value = number;
} else if (value === 0) {
if (!isASCIIDigit(input[pointer])) {
return failure;
} else {
value = value * 10 + number;
}
++pointer;
if (value > 255) {
return failure;

while (isASCIIDigit(input[pointer])) {
const number = parseInt(at(input, pointer));
if (ipv4Piece === null) {
ipv4Piece = number;
} else if (ipv4Piece === 0) {
return failure;
} else {
ipv4Piece = ipv4Piece * 10 + number;
}
++pointer;
if (ipv4Piece > 255) {
return failure;
}
}
}

ip[piecePtr] = ip[piecePtr] * 0x100 + value;
ip[piecePtr] = ip[piecePtr] * 0x100 + ipv4Piece;

++numbersSeen;
++numbersSeen;

if (numbersSeen === 2 || numbersSeen === 4) {
++piecePtr;
}

if (numbersSeen === 2 || numbersSeen === 4) {
++piecePtr;
if (input[pointer] === undefined && numbersSeen !== 4) {
return failure;
}
}

if (input[pointer] === undefined && numbersSeen !== 4) {
break;
} else if (input[pointer] === p(":")) {
++pointer;
if (input[pointer] === undefined) {
return failure;
}
} else if (input[pointer] !== undefined) {
return failure;
}

ip[piecePtr] = value;
++piecePtr;
}

if (compressPtr !== null) {
Expand All @@ -353,7 +348,7 @@ function parseIPv6(input) {
--piecePtr;
--swaps;
}
} else if (piecePtr !== 8) {
} else if (compressPtr === null && piecePtr !== 8) {
return failure;
}

Expand Down

0 comments on commit 9fc1925

Please sign in to comment.