-
Notifications
You must be signed in to change notification settings - Fork 57
/
curl.js
71 lines (60 loc) · 2.42 KB
/
curl.js
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
"use strict";
function escapeGlobbing(url) {
return url.replace(/[[\]{}]/g, (m) => `\\${m.slice(0, 1)}`);
}
window.curl = function (url, method, headers, payload, filename, options) {
const esc = window.escapeShellArg;
let contentType;
let parts = ["curl"];
for (let header of headers) {
let headerName = header.name.toLowerCase();
if (headerName === "content-type") {
contentType = header.value.toLowerCase();
let v = header.value;
if (v.startsWith("multipart/form-data;")) v = v.slice(0, 19);
let h = esc(`${header.name}: ${v}`, options.doubleQuotes);
parts.push(`--header ${h}`);
} else if (headerName === "content-length") {
// Implicitly added by curl
} else if (headerName === "referer") {
parts.push(`--referer ${esc(header.value, options.doubleQuotes)}`);
} else if (headerName === "cookie") {
parts.push(`--cookie ${esc(header.value, options.doubleQuotes)}`);
} else if (headerName === "user-agent") {
parts.push(`--user-agent ${esc(header.value, options.doubleQuotes)}`);
} else {
let h = esc(`${header.name}: ${header.value}`, options.doubleQuotes);
parts.push(`--header ${h}`);
}
}
if (method !== "GET" || payload) parts.push(`--request ${method}`);
if (payload)
if (payload.formData) {
if (contentType === "application/x-www-form-urlencoded")
for (let [key, values] of Object.entries(payload.formData))
for (let value of values) {
let v = esc(
`${encodeURIComponent(key)}=${value}`,
options.doubleQuotes
);
parts.push(`--data-urlencode ${v}`);
}
else if (contentType.startsWith("multipart/form-data;"))
// TODO comment about escaping of name value (e.g. = " ')
for (let [key, values] of Object.entries(payload.formData))
for (let value of values) {
let v = esc(
`${encodeURIComponent(key)}=${value}`,
options.doubleQuotes
);
parts.push(`--form-string ${v}`);
}
} else if (payload.raw) {
throw new Error("Unsupported upload data");
}
parts.push(esc(escapeGlobbing(url), options.doubleQuotes));
if (filename) parts.push(`--output ${esc(filename, options.doubleQuotes)}`);
else parts.push("--remote-name --remote-header-name");
if (options.curlOptions) parts.push(options.curlOptions);
return parts.join(" ");
};