Repository navigation
Expand file tree
/
Copy pathcurlRequestParser.ts
More file actions
90 lines (74 loc) · 3.41 KB
/
Copy pathcurlRequestParser.ts
File metadata and controls
90 lines (74 loc) · 3.41 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import * as fs from 'fs-extra';
import { RequestHeaders } from '../models/base';
import { SystemSettings } from '../models/configurationSettings';
import { HttpRequest } from '../models/httpRequest';
import { RequestParser } from '../models/requestParser';
import { base64, hasHeader } from './misc';
import { parseRequestHeaders, resolveRequestBodyPath } from './requestParserUtil';
const yargsParser = require('yargs-parser');
const DefaultContentType: string = 'application/x-www-form-urlencoded';
export class CurlRequestParser implements RequestParser {
public constructor(private readonly requestRawText: string, private readonly settings: SystemSettings) {
}
public async parseHttpRequest(name?: string): Promise<HttpRequest> {
let requestText = CurlRequestParser.mergeMultipleSpacesIntoSingle(
CurlRequestParser.mergeIntoSingleLine(this.requestRawText.trim()));
requestText = requestText
.replace(/(-X)(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE)/, '$1 $2')
.replace(/(-I|--head)(?=\s+)/, '-X HEAD');
const parsedArguments = yargsParser(requestText);
// parse url
let url = parsedArguments._[1];
if (!url) {
url = parsedArguments.L || parsedArguments.location || parsedArguments.compressed || parsedArguments.url;
}
// parse header
let headers: RequestHeaders = {};
let parsedHeaders = parsedArguments.H || parsedArguments.header;
if (parsedHeaders) {
if (!Array.isArray(parsedHeaders)) {
parsedHeaders = [parsedHeaders];
}
headers = parseRequestHeaders(parsedHeaders, this.settings.defaultHeaders, url);
}
// parse cookie
const cookieString: string = parsedArguments.b || parsedArguments.cookie;
if (cookieString?.includes('=')) {
// Doesn't support cookie jar
headers['Cookie'] = cookieString;
}
const user = parsedArguments.u || parsedArguments.user;
if (user) {
headers['Authorization'] = `Basic ${base64(user)}`;
}
// parse body
let body = parsedArguments.d || parsedArguments.data || parsedArguments['data-ascii'] || parsedArguments['data-binary'] || parsedArguments['data-raw'];
if (Array.isArray(body)) {
body = body.join('&');
}
if (typeof body === 'string' && body[0] === '@') {
const fileAbsolutePath = await resolveRequestBodyPath(body.substring(1));
if (fileAbsolutePath) {
body = fs.createReadStream(fileAbsolutePath);
} else {
body = body.substring(1);
}
}
// Set Content-Type header to application/x-www-form-urlencoded if has body and missing this header
if (body && !hasHeader(headers, 'content-type')) {
headers['Content-Type'] = DefaultContentType;
}
// parse method
let method: string = (parsedArguments.X || parsedArguments.request) as string;
if (!method) {
method = body ? "POST" : "GET";
}
return new HttpRequest(method, url, headers, body, body, name);
}
private static mergeIntoSingleLine(text: string): string {
return text.replace(/\\\r|\\\n/g, '');
}
private static mergeMultipleSpacesIntoSingle(text: string): string {
return text.replace(/\s{2,}/g, ' ');
}
}