Repository navigation
Expand file tree
/
Copy pathrequestParserUtil.ts
More file actions
63 lines (55 loc) · 2.2 KB
/
Copy pathrequestParserUtil.ts
File metadata and controls
63 lines (55 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as fs from 'fs-extra';
import * as path from 'path';
import { Uri } from 'vscode';
import { RequestHeaders } from "../models/base";
import { removeHeader } from './misc';
import { getCurrentTextDocument, getWorkspaceRootPath } from './workspaceUtility';
export function parseRequestHeaders(headerLines: string[], defaultHeaders: RequestHeaders, url: string): RequestHeaders {
// message-header = field-name ":" [ field-value ]
const headers: RequestHeaders = {};
const headerNames: { [key: string]: string } = {};
headerLines.forEach(headerLine => {
let fieldName: string;
let fieldValue: string;
const separatorIndex = headerLine.indexOf(':');
if (separatorIndex === -1) {
fieldName = headerLine.trim();
fieldValue = '';
} else {
fieldName = headerLine.substring(0, separatorIndex).trim();
fieldValue = headerLine.substring(separatorIndex + 1).trim();
}
const normalizedFieldName = fieldName.toLowerCase();
if (!headerNames[normalizedFieldName]) {
headerNames[normalizedFieldName] = fieldName;
headers[fieldName] = fieldValue;
} else {
const splitter = normalizedFieldName === 'cookie' ? ';' : ',';
headers[headerNames[normalizedFieldName]] += `${splitter}${fieldValue}`;
}
});
if (url[0] !== '/') {
removeHeader(defaultHeaders, 'host');
}
return { ...defaultHeaders, ...headers };
}
export async function resolveRequestBodyPath(refPath: string): Promise<string | undefined> {
if (path.isAbsolute(refPath)) {
return (await fs.pathExists(refPath)) ? refPath : undefined;
}
const workspaceRoot = getWorkspaceRootPath();
if (workspaceRoot) {
const absolutePath = path.join(Uri.parse(workspaceRoot).fsPath, refPath);
if (await fs.pathExists(absolutePath)) {
return absolutePath;
}
}
const currentFile = getCurrentTextDocument()?.fileName;
if (currentFile) {
const absolutePath = path.join(path.dirname(currentFile), refPath);
if (await fs.pathExists(absolutePath)) {
return absolutePath;
}
}
return undefined;
}