-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
386 lines (309 loc) · 10.1 KB
/
Copy pathindex.ts
File metadata and controls
386 lines (309 loc) · 10.1 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* Curl Command Parser / Serializer
*
* The aim of this module is NOT to be fully compliant with curl, thus,
* only subset of the curl options are supported.
* The currently supported options are listed in `curlOptions`.
*
* See: https://man7.org/linux/man-pages/man1/curl.1.html
*/
import { escape, split } from './shellwords';
class CurlOption {
constructor(
/** short version of the option */
public short: string | null,
/** long version of the option */
public long: string | null,
/** whether this option expects an argument or not */
public expectsValue: boolean,
/** the corresponding flag of this option */
public flag: keyof CurlCommandFlags | null = null,
/** Allow boolean option to be negated with --no- prefix */
public allowsNegating = false,
) {}
}
const curlOptions: CurlOption[] = [
new CurlOption(null, 'anyauth', false, 'anyauth'),
new CurlOption('b', 'cookie', true),
new CurlOption(null, 'basic', false, 'basic'),
new CurlOption(null, 'compressed', false, 'compressed', true),
new CurlOption(null, 'crlf', false, 'crlf', true),
new CurlOption(null, 'compressed-ssh', false, 'compressedSsh', true),
new CurlOption('d', 'data', true),
new CurlOption(null, 'data-ascii', true),
new CurlOption(null, 'data-binary', true),
new CurlOption(null, 'data-raw', true),
new CurlOption(null, 'data-urlencode', true),
new CurlOption('f', 'fail', false, 'fail', true),
new CurlOption('g', 'globoff', false, 'globoff', true),
new CurlOption('H', 'header', true),
new CurlOption('L', 'location', false, 'location', true),
new CurlOption('S', 'show-error', false, 'showError', true),
new CurlOption('s', 'silent', false, 'silent', true),
new CurlOption('X', 'request', true),
new CurlOption(null, 'url', true),
new CurlOption('A', 'user-agent', true),
new CurlOption('k', 'insecure', false, 'insecure', true),
new CurlOption(null, 'digest', false, 'digest', true),
new CurlOption(null, 'ntlm', false, 'ntlm'),
new CurlOption('G', 'get', false, 'get', true),
new CurlOption('I', 'head', false, 'head', true),
new CurlOption('u', 'user', true),
new CurlOption('F', 'form', true),
];
export interface CurlCommandFlags {
anyauth?: boolean;
basic?: boolean;
compressed?: boolean;
crlf?: boolean;
compressedSsh?: boolean;
fail?: boolean;
/**
-g, --globoff
This option switches off the "URL globbing parser". When
you set this option, you can specify URLs that contain the
letters {}[] without having curl itself interpret them.
Note that these letters are not normal legal URL contents
but they should be encoded according to the URI standard.
Providing -g, --globoff multiple times has no extra effect.
Disable it again with --no-globoff.
Example:
curl -g "https://example.com/{[]}}}}"
See also -K, --config and -q, --disable.
*/
globoff?: boolean;
/**
-L, --location
(HTTP) If the server reports that the requested page has
moved to a different location (indicated with a Location:
header and a 3XX response code), this option makes curl
redo the request on the new place. If used together with
-i, --include or -I, --head, headers from all requested
pages are shown.
*/
location?: boolean;
showError?: boolean;
/**
-s, --silent
Silent or quiet mode. Do not show progress meter or error
messages. Makes Curl mute. It still outputs the data you
ask for, potentially even to the terminal/stdout unless you
redirect it.
Use -S, --show-error in addition to this option to disable
progress meter but still show error messages.
Providing -s, --silent multiple times has no extra effect.
Disable it again with --no-silent.
*/
silent?: boolean;
insecure?: boolean;
/**
(HTTP) Enables HTTP Digest authentication. This is an
authentication scheme that prevents the password from being
sent over the wire in clear text. Use this in combination
with the normal -u, --user option to set user name and
password.
Providing --digest multiple times has no extra effect.
Disable it again with --no-digest.
*/
digest?: boolean;
ntlm?: boolean;
head?: boolean;
get?: boolean;
}
export interface CurlCommand {
url: string | null;
headers: { key: string; value: string }[];
body: string | null;
/**
* Which argument was used to pass body..
* data: -d --data
* ascii: --data-ascii
* binary: --data-binary
* raw: --data-raw
* urlencode: --data-urlencode
*/
bodyArg: 'data' | 'ascii' | 'binary' | 'raw' | 'urlencode' | null;
/**
* lowercased request method
*/
method: string;
/**
* curl's boolean command-line options
*/
flags: CurlCommandFlags;
cookies: string | null;
userAgent?: string;
user?: string;
formData?: { key: string; value: string }[];
}
type State = 'command' | 'url-or-arg' | 'argument-value';
export function stringify(cmd: CurlCommand & { url: string }): string {
const args = ['curl'];
if (cmd.method.toLowerCase() !== 'get') {
args.push('-X', cmd.method.toLowerCase());
}
for (const header of cmd.headers) {
args.push('-H', `'${header.key}:${header.value}'`);
}
if (cmd.body) {
args.push('-d', cmd.body);
}
args.push(cmd.url);
return args.map(escape).join(' ');
}
export function parse(command: string): CurlCommand {
const args = split(command).filter((arg) => arg !== '\\');
const result: CurlCommand = {
url: null,
headers: [],
body: null,
bodyArg: null,
method: 'get',
flags: {},
cookies: null,
};
let state: State = 'command';
let currentOpt: CurlOption | null = null;
for (const arg of args) {
switch (state) {
case 'command':
if (arg !== 'curl') throw new Error(`Invalid command: ${arg}`);
state = 'url-or-arg';
break;
case 'url-or-arg': {
let curlOpt: CurlOption | undefined;
if (arg.startsWith('--no-')) {
curlOpt = curlOptions.find((opt) => opt.long === arg.slice(5));
if (!curlOpt || !curlOpt.flag)
throw new Error(`Unrecognized argument: ${arg}`);
if (!curlOpt.allowsNegating)
throw new Error(`Option does not support negating: ${arg}`);
result.flags[curlOpt.flag] = false;
continue;
} else if (arg.startsWith('--')) {
curlOpt = curlOptions.find((opt) => opt.long === arg.slice(2));
if (!curlOpt) throw new Error(`Unrecognized argument: ${arg}`);
// enable the flag
if (curlOpt.flag) {
result.flags[curlOpt.flag] = true;
}
} else if (arg.startsWith('-')) {
const flags = arg.slice(1).split('');
let handled = false;
flags.forEach((flag, i) => {
if (handled) return;
curlOpt = curlOptions.find((opt) => opt.short === flag);
if (!curlOpt)
throw new Error(`Unrecognized option: ${flag} in "${arg}"`);
const isFirst = i === 0;
if (isFirst && curlOpt.expectsValue && arg.length > 2) {
// handle concatenated values
currentOpt = curlOpt;
handleArgValue(arg.slice(2));
handled = true;
currentOpt = null;
return;
}
const isLast = i === flags.length - 1;
if (curlOpt.expectsValue && !isLast) {
throw new Error(
`Value expecting argument "${flag}" must be the last in ${arg}`,
);
}
// enable the flag
if (curlOpt.flag) {
result.flags[curlOpt.flag] = true;
}
});
if (handled) continue;
}
if (curlOpt) {
if (curlOpt.expectsValue) {
state = 'argument-value';
currentOpt = curlOpt;
}
continue;
}
if (result.url)
throw new Error(
`unrecognized positional argument ${arg}. Url was already set.`,
);
result.url = arg;
break;
}
case 'argument-value': {
handleArgValue(arg);
state = 'url-or-arg';
currentOpt = null;
}
}
}
function handleArgValue(arg: string) {
switch (currentOpt?.long) {
case 'cookie':
result.cookies = arg;
break;
case 'data-ascii':
result.bodyArg = 'ascii';
result.body = arg;
break;
case 'data-binary':
result.bodyArg = 'binary';
result.body = arg;
break;
case 'data':
result.bodyArg = 'data';
result.body = arg;
break;
case 'data-raw':
result.bodyArg = 'raw';
result.body = arg;
break;
case 'data-urlencode': {
let formatted = arg.replace(/^=/, '');
if (!formatted.includes('=')) formatted += '=';
result.bodyArg = 'urlencode';
result.body =
result.body === null ? formatted : `${result.body}&${formatted}`;
break;
}
// parse header argument value
case 'header': {
const matches = /^([^:]+)(:\s?(.+))?;?$/.exec(arg);
if (!matches) {
throw new Error(`Invalid header value: ${arg}`);
}
result.headers.push({
key: matches[1],
value: matches[3] ?? '',
});
break;
}
case 'request':
result.method = arg.toLowerCase();
break;
case 'user-agent':
result.userAgent = arg;
break;
case 'user':
result.user = arg;
break;
case 'url':
result.url = arg;
break;
case 'form': {
const eqIdx = arg.indexOf('=');
if (eqIdx === -1) throw new Error(`Invalid form value: ${arg}`);
result.formData = result.formData ?? [];
result.formData.push({
key: arg.slice(0, eqIdx),
value: arg.slice(eqIdx + 1),
});
break;
}
default:
throw new Error(`no argument set for option ${currentOpt?.long}`);
}
}
return result;
}