@ohos-ports/curlconverter 4.12.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +153 -0
- package/dist/src/Headers.d.ts +23 -0
- package/dist/src/Headers.js +287 -0
- package/dist/src/Headers.js.map +1 -0
- package/dist/src/Query.d.ts +8 -0
- package/dist/src/Query.js +136 -0
- package/dist/src/Query.js.map +1 -0
- package/dist/src/Request.d.ts +200 -0
- package/dist/src/Request.js +1191 -0
- package/dist/src/Request.js.map +1 -0
- package/dist/src/Warnings.d.ts +15 -0
- package/dist/src/Warnings.js +111 -0
- package/dist/src/Warnings.js.map +1 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +333 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/curl/auth.d.ts +12 -0
- package/dist/src/curl/auth.js +40 -0
- package/dist/src/curl/auth.js.map +1 -0
- package/dist/src/curl/form.d.ts +28 -0
- package/dist/src/curl/form.js +218 -0
- package/dist/src/curl/form.js.map +1 -0
- package/dist/src/curl/opts.d.ts +2052 -0
- package/dist/src/curl/opts.js +1000 -0
- package/dist/src/curl/opts.js.map +1 -0
- package/dist/src/curl/url.d.ts +14 -0
- package/dist/src/curl/url.js +218 -0
- package/dist/src/curl/url.js.map +1 -0
- package/dist/src/generators/ansible.d.ts +5 -0
- package/dist/src/generators/ansible.js +282 -0
- package/dist/src/generators/ansible.js.map +1 -0
- package/dist/src/generators/c.d.ts +12 -0
- package/dist/src/generators/c.js +1230 -0
- package/dist/src/generators/c.js.map +1 -0
- package/dist/src/generators/cfml.d.ts +5 -0
- package/dist/src/generators/cfml.js +134 -0
- package/dist/src/generators/cfml.js.map +1 -0
- package/dist/src/generators/clojure.d.ts +7 -0
- package/dist/src/generators/clojure.js +436 -0
- package/dist/src/generators/clojure.js.map +1 -0
- package/dist/src/generators/csharp.d.ts +8 -0
- package/dist/src/generators/csharp.js +356 -0
- package/dist/src/generators/csharp.js.map +1 -0
- package/dist/src/generators/dart.d.ts +5 -0
- package/dist/src/generators/dart.js +273 -0
- package/dist/src/generators/dart.js.map +1 -0
- package/dist/src/generators/elixir.d.ts +8 -0
- package/dist/src/generators/elixir.js +296 -0
- package/dist/src/generators/elixir.js.map +1 -0
- package/dist/src/generators/go.d.ts +5 -0
- package/dist/src/generators/go.js +228 -0
- package/dist/src/generators/go.js.map +1 -0
- package/dist/src/generators/har.d.ts +7 -0
- package/dist/src/generators/har.js +115 -0
- package/dist/src/generators/har.js.map +1 -0
- package/dist/src/generators/http.d.ts +7 -0
- package/dist/src/generators/http.js +165 -0
- package/dist/src/generators/http.js.map +1 -0
- package/dist/src/generators/httpie.d.ts +5 -0
- package/dist/src/generators/httpie.js +446 -0
- package/dist/src/generators/httpie.js.map +1 -0
- package/dist/src/generators/java/httpurlconnection.d.ts +5 -0
- package/dist/src/generators/java/httpurlconnection.js +123 -0
- package/dist/src/generators/java/httpurlconnection.js.map +1 -0
- package/dist/src/generators/java/java.d.ts +8 -0
- package/dist/src/generators/java/java.js +247 -0
- package/dist/src/generators/java/java.js.map +1 -0
- package/dist/src/generators/java/jsoup.d.ts +5 -0
- package/dist/src/generators/java/jsoup.js +152 -0
- package/dist/src/generators/java/jsoup.js.map +1 -0
- package/dist/src/generators/java/okhttp.d.ts +5 -0
- package/dist/src/generators/java/okhttp.js +232 -0
- package/dist/src/generators/java/okhttp.js.map +1 -0
- package/dist/src/generators/javascript/axios.d.ts +5 -0
- package/dist/src/generators/javascript/axios.js +332 -0
- package/dist/src/generators/javascript/axios.js.map +1 -0
- package/dist/src/generators/javascript/got.d.ts +5 -0
- package/dist/src/generators/javascript/got.js +277 -0
- package/dist/src/generators/javascript/got.js.map +1 -0
- package/dist/src/generators/javascript/http.d.ts +8 -0
- package/dist/src/generators/javascript/http.js +183 -0
- package/dist/src/generators/javascript/http.js.map +1 -0
- package/dist/src/generators/javascript/javascript.d.ts +37 -0
- package/dist/src/generators/javascript/javascript.js +676 -0
- package/dist/src/generators/javascript/javascript.js.map +1 -0
- package/dist/src/generators/javascript/jquery.d.ts +15 -0
- package/dist/src/generators/javascript/jquery.js +297 -0
- package/dist/src/generators/javascript/jquery.js.map +1 -0
- package/dist/src/generators/javascript/ky.d.ts +7 -0
- package/dist/src/generators/javascript/ky.js +245 -0
- package/dist/src/generators/javascript/ky.js.map +1 -0
- package/dist/src/generators/javascript/request.d.ts +5 -0
- package/dist/src/generators/javascript/request.js +113 -0
- package/dist/src/generators/javascript/request.js.map +1 -0
- package/dist/src/generators/javascript/superagent.d.ts +10 -0
- package/dist/src/generators/javascript/superagent.js +301 -0
- package/dist/src/generators/javascript/superagent.js.map +1 -0
- package/dist/src/generators/javascript/xhr.d.ts +8 -0
- package/dist/src/generators/javascript/xhr.js +147 -0
- package/dist/src/generators/javascript/xhr.js.map +1 -0
- package/dist/src/generators/json.d.ts +45 -0
- package/dist/src/generators/json.js +197 -0
- package/dist/src/generators/json.js.map +1 -0
- package/dist/src/generators/julia.d.ts +8 -0
- package/dist/src/generators/julia.js +365 -0
- package/dist/src/generators/julia.js.map +1 -0
- package/dist/src/generators/kotlin.d.ts +8 -0
- package/dist/src/generators/kotlin.js +325 -0
- package/dist/src/generators/kotlin.js.map +1 -0
- package/dist/src/generators/lua.d.ts +5 -0
- package/dist/src/generators/lua.js +192 -0
- package/dist/src/generators/lua.js.map +1 -0
- package/dist/src/generators/matlab/common.d.ts +16 -0
- package/dist/src/generators/matlab/common.js +238 -0
- package/dist/src/generators/matlab/common.js.map +1 -0
- package/dist/src/generators/matlab/httpinterface.d.ts +2 -0
- package/dist/src/generators/matlab/httpinterface.js +211 -0
- package/dist/src/generators/matlab/httpinterface.js.map +1 -0
- package/dist/src/generators/matlab/matlab.d.ts +5 -0
- package/dist/src/generators/matlab/matlab.js +29 -0
- package/dist/src/generators/matlab/matlab.js.map +1 -0
- package/dist/src/generators/matlab/webservices.d.ts +2 -0
- package/dist/src/generators/matlab/webservices.js +191 -0
- package/dist/src/generators/matlab/webservices.js.map +1 -0
- package/dist/src/generators/objectivec.d.ts +8 -0
- package/dist/src/generators/objectivec.js +269 -0
- package/dist/src/generators/objectivec.js.map +1 -0
- package/dist/src/generators/ocaml.d.ts +8 -0
- package/dist/src/generators/ocaml.js +150 -0
- package/dist/src/generators/ocaml.js.map +1 -0
- package/dist/src/generators/perl.d.ts +9 -0
- package/dist/src/generators/perl.js +209 -0
- package/dist/src/generators/perl.js.map +1 -0
- package/dist/src/generators/php/guzzle.d.ts +5 -0
- package/dist/src/generators/php/guzzle.js +328 -0
- package/dist/src/generators/php/guzzle.js.map +1 -0
- package/dist/src/generators/php/php.d.ts +8 -0
- package/dist/src/generators/php/php.js +217 -0
- package/dist/src/generators/php/php.js.map +1 -0
- package/dist/src/generators/php/requests.d.ts +5 -0
- package/dist/src/generators/php/requests.js +89 -0
- package/dist/src/generators/php/requests.js.map +1 -0
- package/dist/src/generators/powershell.d.ts +11 -0
- package/dist/src/generators/powershell.js +377 -0
- package/dist/src/generators/powershell.js.map +1 -0
- package/dist/src/generators/python/http.d.ts +5 -0
- package/dist/src/generators/python/http.js +128 -0
- package/dist/src/generators/python/http.js.map +1 -0
- package/dist/src/generators/python/python.d.ts +22 -0
- package/dist/src/generators/python/python.js +1693 -0
- package/dist/src/generators/python/python.js.map +1 -0
- package/dist/src/generators/r/httr.d.ts +10 -0
- package/dist/src/generators/r/httr.js +298 -0
- package/dist/src/generators/r/httr.js.map +1 -0
- package/dist/src/generators/r/httr2.d.ts +5 -0
- package/dist/src/generators/r/httr2.js +214 -0
- package/dist/src/generators/r/httr2.js.map +1 -0
- package/dist/src/generators/ruby/httparty.d.ts +6 -0
- package/dist/src/generators/ruby/httparty.js +308 -0
- package/dist/src/generators/ruby/httparty.js.map +1 -0
- package/dist/src/generators/ruby/ruby.d.ts +14 -0
- package/dist/src/generators/ruby/ruby.js +515 -0
- package/dist/src/generators/ruby/ruby.js.map +1 -0
- package/dist/src/generators/rust.d.ts +8 -0
- package/dist/src/generators/rust.js +177 -0
- package/dist/src/generators/rust.js.map +1 -0
- package/dist/src/generators/swift.d.ts +8 -0
- package/dist/src/generators/swift.js +219 -0
- package/dist/src/generators/swift.js.map +1 -0
- package/dist/src/generators/wget.d.ts +8 -0
- package/dist/src/generators/wget.js +472 -0
- package/dist/src/generators/wget.js.map +1 -0
- package/dist/src/index.d.ts +53 -0
- package/dist/src/index.js +54 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/parse.d.ts +13 -0
- package/dist/src/parse.js +43 -0
- package/dist/src/parse.js.map +1 -0
- package/dist/src/shell/Parser.d.ts +4 -0
- package/dist/src/shell/Parser.js +6 -0
- package/dist/src/shell/Parser.js.map +1 -0
- package/dist/src/shell/Word.d.ts +48 -0
- package/dist/src/shell/Word.js +486 -0
- package/dist/src/shell/Word.js.map +1 -0
- package/dist/src/shell/tokenizer.d.ts +3 -0
- package/dist/src/shell/tokenizer.js +610 -0
- package/dist/src/shell/tokenizer.js.map +1 -0
- package/dist/src/shell/webParser.d.ts +4 -0
- package/dist/src/shell/webParser.js +15 -0
- package/dist/src/shell/webParser.js.map +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.js +21 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tools/compare-requests.d.ts +2 -0
- package/dist/tools/compare-requests.js +448 -0
- package/dist/tools/compare-requests.js.map +1 -0
- package/dist/tools/gen-test.d.ts +2 -0
- package/dist/tools/gen-test.js +114 -0
- package/dist/tools/gen-test.js.map +1 -0
- package/dist/tree-sitter-bash.wasm +0 -0
- package/index.d.ts +3 -0
- package/package.json +97 -0
- package/src/Headers.ts +334 -0
- package/src/Query.ts +147 -0
- package/src/Request.ts +1649 -0
- package/src/Warnings.ts +154 -0
- package/src/cli.ts +561 -0
- package/src/curl/auth.ts +45 -0
- package/src/curl/form.ts +297 -0
- package/src/curl/opts.ts +1417 -0
- package/src/curl/url.ts +241 -0
- package/src/generators/ansible.ts +364 -0
- package/src/generators/c.ts +1371 -0
- package/src/generators/cfml.ts +155 -0
- package/src/generators/clojure.ts +521 -0
- package/src/generators/csharp.ts +383 -0
- package/src/generators/dart.ts +293 -0
- package/src/generators/elixir.ts +349 -0
- package/src/generators/go.ts +269 -0
- package/src/generators/har.ts +147 -0
- package/src/generators/http.ts +194 -0
- package/src/generators/httpie.ts +506 -0
- package/src/generators/java/httpurlconnection.ts +151 -0
- package/src/generators/java/java.ts +280 -0
- package/src/generators/java/jsoup.ts +172 -0
- package/src/generators/java/okhttp.ts +257 -0
- package/src/generators/javascript/axios.ts +395 -0
- package/src/generators/javascript/got.ts +362 -0
- package/src/generators/javascript/http.ts +232 -0
- package/src/generators/javascript/javascript.ts +825 -0
- package/src/generators/javascript/jquery.ts +360 -0
- package/src/generators/javascript/ky.ts +315 -0
- package/src/generators/javascript/request.ts +152 -0
- package/src/generators/javascript/superagent.ts +373 -0
- package/src/generators/javascript/xhr.ts +192 -0
- package/src/generators/json.ts +293 -0
- package/src/generators/julia.ts +398 -0
- package/src/generators/kotlin.ts +355 -0
- package/src/generators/lua.ts +209 -0
- package/src/generators/matlab/common.ts +281 -0
- package/src/generators/matlab/httpinterface.ts +284 -0
- package/src/generators/matlab/matlab.ts +39 -0
- package/src/generators/matlab/webservices.ts +263 -0
- package/src/generators/objectivec.ts +297 -0
- package/src/generators/ocaml.ts +168 -0
- package/src/generators/perl.ts +237 -0
- package/src/generators/php/guzzle.ts +368 -0
- package/src/generators/php/php.ts +239 -0
- package/src/generators/php/requests.ts +103 -0
- package/src/generators/powershell.ts +430 -0
- package/src/generators/python/http.ts +167 -0
- package/src/generators/python/python.ts +1934 -0
- package/src/generators/r/httr.ts +328 -0
- package/src/generators/r/httr2.ts +268 -0
- package/src/generators/ruby/httparty.ts +351 -0
- package/src/generators/ruby/ruby.ts +564 -0
- package/src/generators/rust.ts +243 -0
- package/src/generators/swift.ts +235 -0
- package/src/generators/wget.ts +533 -0
- package/src/index.ts +89 -0
- package/src/parse.ts +72 -0
- package/src/shell/Parser.ts +8 -0
- package/src/shell/Word.ts +531 -0
- package/src/shell/tokenizer.ts +736 -0
- package/src/shell/webParser.ts +18 -0
- package/src/utils.ts +25 -0
- package/tools/compare-requests.ts +571 -0
- package/tools/extract_curl_args.py +511 -0
- package/tools/gen-test.ts +129 -0
package/src/curl/auth.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export const CURLAUTH_BASIC = 1 << 0;
|
|
2
|
+
export const CURLAUTH_DIGEST = 1 << 1;
|
|
3
|
+
export const CURLAUTH_NEGOTIATE = 1 << 2;
|
|
4
|
+
export const CURLAUTH_NTLM = 1 << 3;
|
|
5
|
+
export const CURLAUTH_DIGEST_IE = 1 << 4;
|
|
6
|
+
export const CURLAUTH_NTLM_WB = 1 << 5;
|
|
7
|
+
export const CURLAUTH_BEARER = 1 << 6;
|
|
8
|
+
export const CURLAUTH_AWS_SIGV4 = 1 << 7;
|
|
9
|
+
export const CURLAUTH_ANY = ~CURLAUTH_DIGEST_IE;
|
|
10
|
+
|
|
11
|
+
export type ProxyAuthType = "basic" | "digest" | "ntlm" | "negotiate" | "none";
|
|
12
|
+
|
|
13
|
+
export type AuthType = ProxyAuthType | "ntlm-wb" | "bearer" | "aws-sigv4";
|
|
14
|
+
|
|
15
|
+
// This is this function
|
|
16
|
+
// https://github.com/curl/curl/blob/curl-7_86_0/lib/http.c#L455
|
|
17
|
+
// which is not the correct function, since it works on the response.
|
|
18
|
+
//
|
|
19
|
+
// Curl also filters out auth schemes it doesn't support,
|
|
20
|
+
// https://github.com/curl/curl/blob/curl-7_86_0/lib/setopt.c#L970
|
|
21
|
+
// but we "support" all of them, so we don't need to do that.
|
|
22
|
+
export function pickAuth(mask: number): AuthType {
|
|
23
|
+
if (mask === CURLAUTH_ANY) {
|
|
24
|
+
return "basic";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const auths: [number, AuthType][] = [
|
|
28
|
+
[CURLAUTH_NEGOTIATE, "negotiate"],
|
|
29
|
+
[CURLAUTH_BEARER, "bearer"],
|
|
30
|
+
[CURLAUTH_DIGEST, "digest"],
|
|
31
|
+
[CURLAUTH_NTLM, "ntlm"],
|
|
32
|
+
[CURLAUTH_NTLM_WB, "ntlm-wb"],
|
|
33
|
+
[CURLAUTH_BASIC, "basic"],
|
|
34
|
+
// This check happens outside this function because we obviously
|
|
35
|
+
// don't need to to specify --no-basic to use aws-sigv4
|
|
36
|
+
// https://github.com/curl/curl/blob/curl-7_86_0/lib/setopt.c#L678-L679
|
|
37
|
+
[CURLAUTH_AWS_SIGV4, "aws-sigv4"],
|
|
38
|
+
];
|
|
39
|
+
for (const [auth, authName] of auths) {
|
|
40
|
+
if (mask & auth) {
|
|
41
|
+
return authName;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return "none";
|
|
45
|
+
}
|
package/src/curl/form.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { CCError } from "../utils.js";
|
|
2
|
+
import type { Warnings } from "../Warnings.js";
|
|
3
|
+
import { Token, Word, eq } from "../shell/Word.js";
|
|
4
|
+
import { SrcFormParam } from "../curl/opts.js";
|
|
5
|
+
|
|
6
|
+
// contentFile is the actual file to read from
|
|
7
|
+
export type Details = {
|
|
8
|
+
contentType?: Word;
|
|
9
|
+
// name of the filename= to send to the server, if one needs to be sent
|
|
10
|
+
filename?: Word;
|
|
11
|
+
// binary, 8bit, 7bit, base64, or quoted-printable
|
|
12
|
+
encoder?: Word;
|
|
13
|
+
// extra headers or files to read extra headers from
|
|
14
|
+
headers?: Word[];
|
|
15
|
+
headerFiles?: Word[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type FormParamPrototype = {
|
|
19
|
+
name: Word;
|
|
20
|
+
content?: Word; // actually mandatory
|
|
21
|
+
contentFile?: Word; // .content can be renamed to .contentFile later
|
|
22
|
+
} & Details;
|
|
23
|
+
|
|
24
|
+
export type FormParam = {
|
|
25
|
+
name: Word;
|
|
26
|
+
} & ({ content: Word } | { contentFile: Word }) &
|
|
27
|
+
Details;
|
|
28
|
+
|
|
29
|
+
export type Supported = {
|
|
30
|
+
filename?: boolean;
|
|
31
|
+
encoder?: boolean;
|
|
32
|
+
headers?: boolean;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function parseDetails(
|
|
36
|
+
formParam: FormParamPrototype,
|
|
37
|
+
p: Word,
|
|
38
|
+
ptr: number,
|
|
39
|
+
supported: Supported,
|
|
40
|
+
warnings: Warnings,
|
|
41
|
+
): FormParamPrototype {
|
|
42
|
+
while (ptr < p.length && p.charAt(ptr) === ";") {
|
|
43
|
+
ptr += 1;
|
|
44
|
+
while (ptr < p.length && isSpace(p.charAt(ptr))) {
|
|
45
|
+
ptr += 1;
|
|
46
|
+
}
|
|
47
|
+
if (ptr >= p.length) {
|
|
48
|
+
return formParam;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const value = p.slice(ptr);
|
|
52
|
+
if (value.startsWith("type=")) {
|
|
53
|
+
// TODO: the syntax for type= is more complicated
|
|
54
|
+
[formParam.contentType, ptr] = getParamWord(p, ptr + 5, warnings);
|
|
55
|
+
} else if (value.startsWith("filename=")) {
|
|
56
|
+
const [filename, filenameEnd] = getParamWord(p, ptr + 9, warnings);
|
|
57
|
+
ptr = filenameEnd;
|
|
58
|
+
if (supported.filename) {
|
|
59
|
+
formParam.filename = filename;
|
|
60
|
+
} else {
|
|
61
|
+
warnings.push([
|
|
62
|
+
"unsupported-form-detail",
|
|
63
|
+
"Field file name not allowed here: " + filename.toString(),
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
} else if (value.startsWith("encoder=")) {
|
|
67
|
+
const [encoder, encoderEnd] = getParamWord(p, ptr + 8, warnings);
|
|
68
|
+
ptr = encoderEnd;
|
|
69
|
+
if (supported.encoder) {
|
|
70
|
+
formParam.encoder = encoder;
|
|
71
|
+
} else {
|
|
72
|
+
warnings.push([
|
|
73
|
+
"unsupported-form-detail",
|
|
74
|
+
"Field encoder not allowed here: " + encoder.toString(),
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
} else if (value.startsWith("headers=")) {
|
|
78
|
+
const [headers, headersEnd] = getParamWord(p, ptr + 8, warnings);
|
|
79
|
+
ptr = headersEnd;
|
|
80
|
+
if (supported.headers) {
|
|
81
|
+
if (headers.startsWith("@")) {
|
|
82
|
+
if (formParam.headerFiles === undefined) {
|
|
83
|
+
formParam.headerFiles = [];
|
|
84
|
+
}
|
|
85
|
+
formParam.headerFiles.push(headers.slice(1));
|
|
86
|
+
} else {
|
|
87
|
+
if (formParam.headers === undefined) {
|
|
88
|
+
formParam.headers = [];
|
|
89
|
+
}
|
|
90
|
+
formParam.headers.push(headers);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
warnings.push([
|
|
94
|
+
"unsupported-form-detail",
|
|
95
|
+
"Field headers not allowed here: " + headers.toString(),
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
// TODO: it would be more consistent for curl to skip until the first "=", then
|
|
100
|
+
// getParamWord, because quoting a ; in an unknown value breaks values that
|
|
101
|
+
// come after it, e.g.:
|
|
102
|
+
// curl -F 'myname=myvalue;bfilename="f;oo";filename=oeu' localhost:8888
|
|
103
|
+
const unknown = getParamWord(p, ptr, warnings);
|
|
104
|
+
const unknownEnd = unknown[1];
|
|
105
|
+
ptr = unknownEnd;
|
|
106
|
+
warnings.push([
|
|
107
|
+
"unknown-form-detail",
|
|
108
|
+
"skip unknown form field: " + value.toString(),
|
|
109
|
+
]);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return formParam;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isSpace(c: Token): boolean {
|
|
117
|
+
// Implements the following macro from curl:
|
|
118
|
+
// #define ISBLANK(x) (((x) == ' ') || ((x) == '\t'))
|
|
119
|
+
// #define ISSPACE(x) (ISBLANK(x) || (((x) >= 0xa) && ((x) <= 0x0d)))
|
|
120
|
+
return (
|
|
121
|
+
typeof c === "string" &&
|
|
122
|
+
(c === " " || c === "\t" || (c >= "\n" && c <= "\r"))
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function getParamWord(
|
|
127
|
+
p: Word,
|
|
128
|
+
start: number,
|
|
129
|
+
warnings: Warnings,
|
|
130
|
+
): [Word, number] {
|
|
131
|
+
let ptr = start;
|
|
132
|
+
if (p.charAt(ptr) === '"') {
|
|
133
|
+
ptr += 1;
|
|
134
|
+
const parts: Token[] = [];
|
|
135
|
+
while (ptr < p.length) {
|
|
136
|
+
let curChar = p.charAt(ptr);
|
|
137
|
+
if (curChar === "\\") {
|
|
138
|
+
if (ptr + 1 < p.length) {
|
|
139
|
+
const nextChar = p.charAt(ptr + 1);
|
|
140
|
+
if (nextChar === '"' || nextChar === "\\") {
|
|
141
|
+
ptr += 1;
|
|
142
|
+
curChar = p.charAt(ptr);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
} else if (curChar === '"') {
|
|
146
|
+
ptr += 1;
|
|
147
|
+
let trailingData = false;
|
|
148
|
+
while (ptr < p.length && p.charAt(ptr) !== ";") {
|
|
149
|
+
if (!isSpace(p.charAt(ptr))) {
|
|
150
|
+
trailingData = true;
|
|
151
|
+
}
|
|
152
|
+
ptr += 1;
|
|
153
|
+
}
|
|
154
|
+
if (trailingData) {
|
|
155
|
+
warnings.push([
|
|
156
|
+
"trailing-form-data",
|
|
157
|
+
"Trailing data after quoted form parameter",
|
|
158
|
+
]);
|
|
159
|
+
}
|
|
160
|
+
return [new Word(parts), ptr];
|
|
161
|
+
}
|
|
162
|
+
parts.push(curChar);
|
|
163
|
+
ptr += 1;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let sepIdx = p.indexOf(";", start);
|
|
167
|
+
if (sepIdx === -1) {
|
|
168
|
+
sepIdx = p.length;
|
|
169
|
+
}
|
|
170
|
+
return [p.slice(start, sepIdx), sepIdx];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function getParamPart(
|
|
174
|
+
formParam: FormParamPrototype,
|
|
175
|
+
p: Word,
|
|
176
|
+
ptr: number,
|
|
177
|
+
supported: Supported,
|
|
178
|
+
warnings: Warnings,
|
|
179
|
+
): FormParamPrototype {
|
|
180
|
+
while (ptr < p.length && isSpace(p.charAt(ptr))) {
|
|
181
|
+
ptr += 1;
|
|
182
|
+
}
|
|
183
|
+
const [content, contentEnd] = getParamWord(p, ptr, warnings);
|
|
184
|
+
formParam.content = content;
|
|
185
|
+
parseDetails(formParam, p, contentEnd, supported, warnings);
|
|
186
|
+
return formParam;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// TODO: https://curl.se/docs/manpage.html#-F
|
|
190
|
+
// https://github.com/curl/curl/blob/curl-7_88_1/src/tool_formparse.c
|
|
191
|
+
// -F is a complicated option to parse.
|
|
192
|
+
export function parseForm(
|
|
193
|
+
form: SrcFormParam[],
|
|
194
|
+
warnings: Warnings,
|
|
195
|
+
): FormParam[] {
|
|
196
|
+
const multipartUploads = [];
|
|
197
|
+
let depth = 0;
|
|
198
|
+
for (const multipartArgument of form) {
|
|
199
|
+
const isString = multipartArgument.type === "string";
|
|
200
|
+
|
|
201
|
+
if (!multipartArgument.value.includes("=")) {
|
|
202
|
+
throw new CCError(
|
|
203
|
+
'invalid value for --form/-F, missing "=": ' +
|
|
204
|
+
JSON.stringify(multipartArgument.value.toString()),
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
const [name, value] = multipartArgument.value.split("=", 2);
|
|
208
|
+
const formParam: FormParamPrototype = { name };
|
|
209
|
+
if (!isString && value.charAt(0) === "(") {
|
|
210
|
+
depth += 1;
|
|
211
|
+
warnings.push([
|
|
212
|
+
"nested-form",
|
|
213
|
+
'Nested form data with "=(" is not supported, it will be flattened',
|
|
214
|
+
]);
|
|
215
|
+
getParamPart(
|
|
216
|
+
formParam,
|
|
217
|
+
value,
|
|
218
|
+
1,
|
|
219
|
+
{
|
|
220
|
+
headers: true,
|
|
221
|
+
},
|
|
222
|
+
warnings,
|
|
223
|
+
);
|
|
224
|
+
} else if (!isString && name.length === 0 && eq(value, ")")) {
|
|
225
|
+
depth -= 1;
|
|
226
|
+
if (depth < 0) {
|
|
227
|
+
throw new CCError(
|
|
228
|
+
"no multipart to terminate: " +
|
|
229
|
+
JSON.stringify(multipartArgument.value.toString()),
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
} else if (!isString && value.charAt(0) === "@") {
|
|
233
|
+
// TODO: there can be multiple files separated by a comma
|
|
234
|
+
getParamPart(
|
|
235
|
+
formParam,
|
|
236
|
+
value,
|
|
237
|
+
1,
|
|
238
|
+
{
|
|
239
|
+
filename: true,
|
|
240
|
+
encoder: true,
|
|
241
|
+
headers: true,
|
|
242
|
+
},
|
|
243
|
+
warnings,
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
formParam.contentFile = formParam.content;
|
|
247
|
+
delete formParam.content;
|
|
248
|
+
if (formParam.filename === null || formParam.filename === undefined) {
|
|
249
|
+
formParam.filename = formParam.contentFile;
|
|
250
|
+
}
|
|
251
|
+
if (
|
|
252
|
+
formParam.contentType === null ||
|
|
253
|
+
formParam.contentType === undefined
|
|
254
|
+
) {
|
|
255
|
+
// TODO: set from contentFile extension
|
|
256
|
+
}
|
|
257
|
+
} else if (!isString && value.charAt(0) === "<") {
|
|
258
|
+
getParamPart(
|
|
259
|
+
formParam,
|
|
260
|
+
value,
|
|
261
|
+
1,
|
|
262
|
+
{
|
|
263
|
+
encoder: true,
|
|
264
|
+
headers: true,
|
|
265
|
+
},
|
|
266
|
+
warnings,
|
|
267
|
+
);
|
|
268
|
+
formParam.contentFile = formParam.content;
|
|
269
|
+
delete formParam.content;
|
|
270
|
+
if (
|
|
271
|
+
formParam.contentType === null ||
|
|
272
|
+
formParam.contentType === undefined
|
|
273
|
+
) {
|
|
274
|
+
// TODO: set from contentFile extension
|
|
275
|
+
}
|
|
276
|
+
} else {
|
|
277
|
+
if (isString) {
|
|
278
|
+
formParam.content = value;
|
|
279
|
+
} else {
|
|
280
|
+
getParamPart(
|
|
281
|
+
formParam,
|
|
282
|
+
value,
|
|
283
|
+
0,
|
|
284
|
+
{
|
|
285
|
+
filename: true,
|
|
286
|
+
encoder: true,
|
|
287
|
+
headers: true,
|
|
288
|
+
},
|
|
289
|
+
warnings,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
multipartUploads.push(formParam);
|
|
295
|
+
}
|
|
296
|
+
return multipartUploads as FormParam[];
|
|
297
|
+
}
|