Skip to content

Commit

Permalink
Improve http/io.ts parseHTTPVersion (denoland/deno#4930)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 authored and denobot committed Jan 31, 2021
1 parent d38362f commit 5872443
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
21 changes: 5 additions & 16 deletions http/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export async function writeResponse(

/**
* ParseHTTPVersion parses a HTTP version string.
* "HTTP/1.0" returns (1, 0, true).
* "HTTP/1.0" returns (1, 0).
* Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request.go#L766-L792
*/
export function parseHTTPVersion(vers: string): [number, number] {
Expand All @@ -300,7 +300,6 @@ export function parseHTTPVersion(vers: string): [number, number] {

default: {
const Big = 1000000; // arbitrary upper bound
const digitReg = /^\d+$/; // test if string is only digit

if (!vers.startsWith("HTTP/")) {
break;
Expand All @@ -312,24 +311,14 @@ export function parseHTTPVersion(vers: string): [number, number] {
}

const majorStr = vers.substring(vers.indexOf("/") + 1, dot);
const major = parseInt(majorStr);
if (
!digitReg.test(majorStr) ||
isNaN(major) ||
major < 0 ||
major > Big
) {
const major = Number(majorStr);
if (!Number.isInteger(major) || major < 0 || major > Big) {
break;
}

const minorStr = vers.substring(dot + 1);
const minor = parseInt(minorStr);
if (
!digitReg.test(minorStr) ||
isNaN(minor) ||
minor < 0 ||
minor > Big
) {
const minor = Number(minorStr);
if (!Number.isInteger(minor) || minor < 0 || minor > Big) {
break;
}

Expand Down
1 change: 1 addition & 0 deletions http/io_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ test("parseHttpVersion", (): void => {
{ in: "HTTP/0.-1", err: true },
{ in: "HTTP/", err: true },
{ in: "HTTP/1,0", err: true },
{ in: "HTTP/1.1000001", err: true },
];
for (const t of testCases) {
let r, err;
Expand Down

0 comments on commit 5872443

Please sign in to comment.