Skip to content

Commit 3d5218c

Browse files
committed
Parse the HTTP version from HAR data
1 parent cb29157 commit 3d5218c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/model/http/har.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ function parseHarRequest(
699699
timingEvents,
700700
tags: [],
701701
matchedRuleId: false,
702+
httpVersion: parseHttpVersion(request.httpVersion, request.headers),
702703
protocol: request.url.split(':')[0],
703704
method: request.method,
704705
url: request.url,
@@ -717,6 +718,27 @@ function parseHarRequest(
717718
}
718719
}
719720

721+
function parseHttpVersion(
722+
versionString: string | undefined,
723+
headers: HarFormat.Header[]
724+
) {
725+
if (!versionString) {
726+
// Make a best guess:
727+
if (headers.some(({ name }) => name.startsWith(':'))) {
728+
return '2.0';
729+
} else {
730+
return '1.1';
731+
}
732+
}
733+
734+
const regexMatch = /^(HTTP\/)?([\d\.]+)$/i.exec(versionString);
735+
if (regexMatch) {
736+
return regexMatch[2];
737+
}
738+
739+
throw TypeError(`Invalid HTTP version: ${versionString}`);
740+
}
741+
720742
function parseHarRequestContents(data: RequestContentData): Buffer {
721743
if (data.encoding && Buffer.isEncoding(data.encoding)) {
722744
return Buffer.from(data.text, data.encoding);

0 commit comments

Comments
 (0)