@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.
Files changed (270) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +153 -0
  3. package/dist/src/Headers.d.ts +23 -0
  4. package/dist/src/Headers.js +287 -0
  5. package/dist/src/Headers.js.map +1 -0
  6. package/dist/src/Query.d.ts +8 -0
  7. package/dist/src/Query.js +136 -0
  8. package/dist/src/Query.js.map +1 -0
  9. package/dist/src/Request.d.ts +200 -0
  10. package/dist/src/Request.js +1191 -0
  11. package/dist/src/Request.js.map +1 -0
  12. package/dist/src/Warnings.d.ts +15 -0
  13. package/dist/src/Warnings.js +111 -0
  14. package/dist/src/Warnings.js.map +1 -0
  15. package/dist/src/cli.d.ts +2 -0
  16. package/dist/src/cli.js +333 -0
  17. package/dist/src/cli.js.map +1 -0
  18. package/dist/src/curl/auth.d.ts +12 -0
  19. package/dist/src/curl/auth.js +40 -0
  20. package/dist/src/curl/auth.js.map +1 -0
  21. package/dist/src/curl/form.d.ts +28 -0
  22. package/dist/src/curl/form.js +218 -0
  23. package/dist/src/curl/form.js.map +1 -0
  24. package/dist/src/curl/opts.d.ts +2052 -0
  25. package/dist/src/curl/opts.js +1000 -0
  26. package/dist/src/curl/opts.js.map +1 -0
  27. package/dist/src/curl/url.d.ts +14 -0
  28. package/dist/src/curl/url.js +218 -0
  29. package/dist/src/curl/url.js.map +1 -0
  30. package/dist/src/generators/ansible.d.ts +5 -0
  31. package/dist/src/generators/ansible.js +282 -0
  32. package/dist/src/generators/ansible.js.map +1 -0
  33. package/dist/src/generators/c.d.ts +12 -0
  34. package/dist/src/generators/c.js +1230 -0
  35. package/dist/src/generators/c.js.map +1 -0
  36. package/dist/src/generators/cfml.d.ts +5 -0
  37. package/dist/src/generators/cfml.js +134 -0
  38. package/dist/src/generators/cfml.js.map +1 -0
  39. package/dist/src/generators/clojure.d.ts +7 -0
  40. package/dist/src/generators/clojure.js +436 -0
  41. package/dist/src/generators/clojure.js.map +1 -0
  42. package/dist/src/generators/csharp.d.ts +8 -0
  43. package/dist/src/generators/csharp.js +356 -0
  44. package/dist/src/generators/csharp.js.map +1 -0
  45. package/dist/src/generators/dart.d.ts +5 -0
  46. package/dist/src/generators/dart.js +273 -0
  47. package/dist/src/generators/dart.js.map +1 -0
  48. package/dist/src/generators/elixir.d.ts +8 -0
  49. package/dist/src/generators/elixir.js +296 -0
  50. package/dist/src/generators/elixir.js.map +1 -0
  51. package/dist/src/generators/go.d.ts +5 -0
  52. package/dist/src/generators/go.js +228 -0
  53. package/dist/src/generators/go.js.map +1 -0
  54. package/dist/src/generators/har.d.ts +7 -0
  55. package/dist/src/generators/har.js +115 -0
  56. package/dist/src/generators/har.js.map +1 -0
  57. package/dist/src/generators/http.d.ts +7 -0
  58. package/dist/src/generators/http.js +165 -0
  59. package/dist/src/generators/http.js.map +1 -0
  60. package/dist/src/generators/httpie.d.ts +5 -0
  61. package/dist/src/generators/httpie.js +446 -0
  62. package/dist/src/generators/httpie.js.map +1 -0
  63. package/dist/src/generators/java/httpurlconnection.d.ts +5 -0
  64. package/dist/src/generators/java/httpurlconnection.js +123 -0
  65. package/dist/src/generators/java/httpurlconnection.js.map +1 -0
  66. package/dist/src/generators/java/java.d.ts +8 -0
  67. package/dist/src/generators/java/java.js +247 -0
  68. package/dist/src/generators/java/java.js.map +1 -0
  69. package/dist/src/generators/java/jsoup.d.ts +5 -0
  70. package/dist/src/generators/java/jsoup.js +152 -0
  71. package/dist/src/generators/java/jsoup.js.map +1 -0
  72. package/dist/src/generators/java/okhttp.d.ts +5 -0
  73. package/dist/src/generators/java/okhttp.js +232 -0
  74. package/dist/src/generators/java/okhttp.js.map +1 -0
  75. package/dist/src/generators/javascript/axios.d.ts +5 -0
  76. package/dist/src/generators/javascript/axios.js +332 -0
  77. package/dist/src/generators/javascript/axios.js.map +1 -0
  78. package/dist/src/generators/javascript/got.d.ts +5 -0
  79. package/dist/src/generators/javascript/got.js +277 -0
  80. package/dist/src/generators/javascript/got.js.map +1 -0
  81. package/dist/src/generators/javascript/http.d.ts +8 -0
  82. package/dist/src/generators/javascript/http.js +183 -0
  83. package/dist/src/generators/javascript/http.js.map +1 -0
  84. package/dist/src/generators/javascript/javascript.d.ts +37 -0
  85. package/dist/src/generators/javascript/javascript.js +676 -0
  86. package/dist/src/generators/javascript/javascript.js.map +1 -0
  87. package/dist/src/generators/javascript/jquery.d.ts +15 -0
  88. package/dist/src/generators/javascript/jquery.js +297 -0
  89. package/dist/src/generators/javascript/jquery.js.map +1 -0
  90. package/dist/src/generators/javascript/ky.d.ts +7 -0
  91. package/dist/src/generators/javascript/ky.js +245 -0
  92. package/dist/src/generators/javascript/ky.js.map +1 -0
  93. package/dist/src/generators/javascript/request.d.ts +5 -0
  94. package/dist/src/generators/javascript/request.js +113 -0
  95. package/dist/src/generators/javascript/request.js.map +1 -0
  96. package/dist/src/generators/javascript/superagent.d.ts +10 -0
  97. package/dist/src/generators/javascript/superagent.js +301 -0
  98. package/dist/src/generators/javascript/superagent.js.map +1 -0
  99. package/dist/src/generators/javascript/xhr.d.ts +8 -0
  100. package/dist/src/generators/javascript/xhr.js +147 -0
  101. package/dist/src/generators/javascript/xhr.js.map +1 -0
  102. package/dist/src/generators/json.d.ts +45 -0
  103. package/dist/src/generators/json.js +197 -0
  104. package/dist/src/generators/json.js.map +1 -0
  105. package/dist/src/generators/julia.d.ts +8 -0
  106. package/dist/src/generators/julia.js +365 -0
  107. package/dist/src/generators/julia.js.map +1 -0
  108. package/dist/src/generators/kotlin.d.ts +8 -0
  109. package/dist/src/generators/kotlin.js +325 -0
  110. package/dist/src/generators/kotlin.js.map +1 -0
  111. package/dist/src/generators/lua.d.ts +5 -0
  112. package/dist/src/generators/lua.js +192 -0
  113. package/dist/src/generators/lua.js.map +1 -0
  114. package/dist/src/generators/matlab/common.d.ts +16 -0
  115. package/dist/src/generators/matlab/common.js +238 -0
  116. package/dist/src/generators/matlab/common.js.map +1 -0
  117. package/dist/src/generators/matlab/httpinterface.d.ts +2 -0
  118. package/dist/src/generators/matlab/httpinterface.js +211 -0
  119. package/dist/src/generators/matlab/httpinterface.js.map +1 -0
  120. package/dist/src/generators/matlab/matlab.d.ts +5 -0
  121. package/dist/src/generators/matlab/matlab.js +29 -0
  122. package/dist/src/generators/matlab/matlab.js.map +1 -0
  123. package/dist/src/generators/matlab/webservices.d.ts +2 -0
  124. package/dist/src/generators/matlab/webservices.js +191 -0
  125. package/dist/src/generators/matlab/webservices.js.map +1 -0
  126. package/dist/src/generators/objectivec.d.ts +8 -0
  127. package/dist/src/generators/objectivec.js +269 -0
  128. package/dist/src/generators/objectivec.js.map +1 -0
  129. package/dist/src/generators/ocaml.d.ts +8 -0
  130. package/dist/src/generators/ocaml.js +150 -0
  131. package/dist/src/generators/ocaml.js.map +1 -0
  132. package/dist/src/generators/perl.d.ts +9 -0
  133. package/dist/src/generators/perl.js +209 -0
  134. package/dist/src/generators/perl.js.map +1 -0
  135. package/dist/src/generators/php/guzzle.d.ts +5 -0
  136. package/dist/src/generators/php/guzzle.js +328 -0
  137. package/dist/src/generators/php/guzzle.js.map +1 -0
  138. package/dist/src/generators/php/php.d.ts +8 -0
  139. package/dist/src/generators/php/php.js +217 -0
  140. package/dist/src/generators/php/php.js.map +1 -0
  141. package/dist/src/generators/php/requests.d.ts +5 -0
  142. package/dist/src/generators/php/requests.js +89 -0
  143. package/dist/src/generators/php/requests.js.map +1 -0
  144. package/dist/src/generators/powershell.d.ts +11 -0
  145. package/dist/src/generators/powershell.js +377 -0
  146. package/dist/src/generators/powershell.js.map +1 -0
  147. package/dist/src/generators/python/http.d.ts +5 -0
  148. package/dist/src/generators/python/http.js +128 -0
  149. package/dist/src/generators/python/http.js.map +1 -0
  150. package/dist/src/generators/python/python.d.ts +22 -0
  151. package/dist/src/generators/python/python.js +1693 -0
  152. package/dist/src/generators/python/python.js.map +1 -0
  153. package/dist/src/generators/r/httr.d.ts +10 -0
  154. package/dist/src/generators/r/httr.js +298 -0
  155. package/dist/src/generators/r/httr.js.map +1 -0
  156. package/dist/src/generators/r/httr2.d.ts +5 -0
  157. package/dist/src/generators/r/httr2.js +214 -0
  158. package/dist/src/generators/r/httr2.js.map +1 -0
  159. package/dist/src/generators/ruby/httparty.d.ts +6 -0
  160. package/dist/src/generators/ruby/httparty.js +308 -0
  161. package/dist/src/generators/ruby/httparty.js.map +1 -0
  162. package/dist/src/generators/ruby/ruby.d.ts +14 -0
  163. package/dist/src/generators/ruby/ruby.js +515 -0
  164. package/dist/src/generators/ruby/ruby.js.map +1 -0
  165. package/dist/src/generators/rust.d.ts +8 -0
  166. package/dist/src/generators/rust.js +177 -0
  167. package/dist/src/generators/rust.js.map +1 -0
  168. package/dist/src/generators/swift.d.ts +8 -0
  169. package/dist/src/generators/swift.js +219 -0
  170. package/dist/src/generators/swift.js.map +1 -0
  171. package/dist/src/generators/wget.d.ts +8 -0
  172. package/dist/src/generators/wget.js +472 -0
  173. package/dist/src/generators/wget.js.map +1 -0
  174. package/dist/src/index.d.ts +53 -0
  175. package/dist/src/index.js +54 -0
  176. package/dist/src/index.js.map +1 -0
  177. package/dist/src/parse.d.ts +13 -0
  178. package/dist/src/parse.js +43 -0
  179. package/dist/src/parse.js.map +1 -0
  180. package/dist/src/shell/Parser.d.ts +4 -0
  181. package/dist/src/shell/Parser.js +6 -0
  182. package/dist/src/shell/Parser.js.map +1 -0
  183. package/dist/src/shell/Word.d.ts +48 -0
  184. package/dist/src/shell/Word.js +486 -0
  185. package/dist/src/shell/Word.js.map +1 -0
  186. package/dist/src/shell/tokenizer.d.ts +3 -0
  187. package/dist/src/shell/tokenizer.js +610 -0
  188. package/dist/src/shell/tokenizer.js.map +1 -0
  189. package/dist/src/shell/webParser.d.ts +4 -0
  190. package/dist/src/shell/webParser.js +15 -0
  191. package/dist/src/shell/webParser.js.map +1 -0
  192. package/dist/src/utils.d.ts +6 -0
  193. package/dist/src/utils.js +21 -0
  194. package/dist/src/utils.js.map +1 -0
  195. package/dist/tools/compare-requests.d.ts +2 -0
  196. package/dist/tools/compare-requests.js +448 -0
  197. package/dist/tools/compare-requests.js.map +1 -0
  198. package/dist/tools/gen-test.d.ts +2 -0
  199. package/dist/tools/gen-test.js +114 -0
  200. package/dist/tools/gen-test.js.map +1 -0
  201. package/dist/tree-sitter-bash.wasm +0 -0
  202. package/index.d.ts +3 -0
  203. package/package.json +97 -0
  204. package/src/Headers.ts +334 -0
  205. package/src/Query.ts +147 -0
  206. package/src/Request.ts +1649 -0
  207. package/src/Warnings.ts +154 -0
  208. package/src/cli.ts +561 -0
  209. package/src/curl/auth.ts +45 -0
  210. package/src/curl/form.ts +297 -0
  211. package/src/curl/opts.ts +1417 -0
  212. package/src/curl/url.ts +241 -0
  213. package/src/generators/ansible.ts +364 -0
  214. package/src/generators/c.ts +1371 -0
  215. package/src/generators/cfml.ts +155 -0
  216. package/src/generators/clojure.ts +521 -0
  217. package/src/generators/csharp.ts +383 -0
  218. package/src/generators/dart.ts +293 -0
  219. package/src/generators/elixir.ts +349 -0
  220. package/src/generators/go.ts +269 -0
  221. package/src/generators/har.ts +147 -0
  222. package/src/generators/http.ts +194 -0
  223. package/src/generators/httpie.ts +506 -0
  224. package/src/generators/java/httpurlconnection.ts +151 -0
  225. package/src/generators/java/java.ts +280 -0
  226. package/src/generators/java/jsoup.ts +172 -0
  227. package/src/generators/java/okhttp.ts +257 -0
  228. package/src/generators/javascript/axios.ts +395 -0
  229. package/src/generators/javascript/got.ts +362 -0
  230. package/src/generators/javascript/http.ts +232 -0
  231. package/src/generators/javascript/javascript.ts +825 -0
  232. package/src/generators/javascript/jquery.ts +360 -0
  233. package/src/generators/javascript/ky.ts +315 -0
  234. package/src/generators/javascript/request.ts +152 -0
  235. package/src/generators/javascript/superagent.ts +373 -0
  236. package/src/generators/javascript/xhr.ts +192 -0
  237. package/src/generators/json.ts +293 -0
  238. package/src/generators/julia.ts +398 -0
  239. package/src/generators/kotlin.ts +355 -0
  240. package/src/generators/lua.ts +209 -0
  241. package/src/generators/matlab/common.ts +281 -0
  242. package/src/generators/matlab/httpinterface.ts +284 -0
  243. package/src/generators/matlab/matlab.ts +39 -0
  244. package/src/generators/matlab/webservices.ts +263 -0
  245. package/src/generators/objectivec.ts +297 -0
  246. package/src/generators/ocaml.ts +168 -0
  247. package/src/generators/perl.ts +237 -0
  248. package/src/generators/php/guzzle.ts +368 -0
  249. package/src/generators/php/php.ts +239 -0
  250. package/src/generators/php/requests.ts +103 -0
  251. package/src/generators/powershell.ts +430 -0
  252. package/src/generators/python/http.ts +167 -0
  253. package/src/generators/python/python.ts +1934 -0
  254. package/src/generators/r/httr.ts +328 -0
  255. package/src/generators/r/httr2.ts +268 -0
  256. package/src/generators/ruby/httparty.ts +351 -0
  257. package/src/generators/ruby/ruby.ts +564 -0
  258. package/src/generators/rust.ts +243 -0
  259. package/src/generators/swift.ts +235 -0
  260. package/src/generators/wget.ts +533 -0
  261. package/src/index.ts +89 -0
  262. package/src/parse.ts +72 -0
  263. package/src/shell/Parser.ts +8 -0
  264. package/src/shell/Word.ts +531 -0
  265. package/src/shell/tokenizer.ts +736 -0
  266. package/src/shell/webParser.ts +18 -0
  267. package/src/utils.ts +25 -0
  268. package/tools/compare-requests.ts +571 -0
  269. package/tools/extract_curl_args.py +511 -0
  270. package/tools/gen-test.ts +129 -0
@@ -0,0 +1,1191 @@
1
+ import { Word, eq, mergeWords, joinWords } from "./shell/Word.js";
2
+ import { CCError, has, isInt } from "./utils.js";
3
+ import { warnf, warnIfPartsIgnored } from "./Warnings.js";
4
+ import { Headers, parseCookies, parseCookiesStrict } from "./Headers.js";
5
+ import { pickAuth } from "./curl/auth.js";
6
+ import { parseurl } from "./curl/url.js";
7
+ import { parseQueryString, percentEncodePlus } from "./Query.js";
8
+ import { parseForm } from "./curl/form.js";
9
+ function buildURL(global_, config, url, uploadFile, outputFile, stdin, stdinFile) {
10
+ const originalUrl = url;
11
+ const u = parseurl(global_, config, url);
12
+ // https://github.com/curl/curl/blob/curl-7_85_0/src/tool_operate.c#L1124
13
+ // https://github.com/curl/curl/blob/curl-7_85_0/src/tool_operhlp.c#L76
14
+ if (uploadFile) {
15
+ // TODO: it's more complicated
16
+ if (u.path.isEmpty()) {
17
+ u.path = uploadFile.prepend("/");
18
+ }
19
+ else if (u.path.endsWith("/")) {
20
+ u.path = u.path.add(uploadFile);
21
+ }
22
+ if (config.get) {
23
+ warnf(global_, [
24
+ "data-ignored",
25
+ "curl doesn't let you pass --get and --upload-file together",
26
+ ]);
27
+ }
28
+ }
29
+ const urlWithOriginalQuery = mergeWords(u.scheme, "://", u.host, u.path, u.query, u.fragment);
30
+ // curl example.com example.com?foo=bar --url-query isshared=t
31
+ // will make requests for
32
+ // example.com/?isshared=t
33
+ // example.com/?foo=bar&isshared=t
34
+ //
35
+ // so the query could come from
36
+ // 1. `--url` (i.e. the requested URL)
37
+ // 2. `--url-query` or `--get --data` (the latter takes precedence)
38
+ //
39
+ // If it comes from the latter, we might need to generate code to read
40
+ // from one or more files.
41
+ // When there's multiple urls, the latter applies to all of them
42
+ // but the query from --url only applies to that URL.
43
+ //
44
+ // There's 3 cases for the query:
45
+ // 1. it's well-formed and can be expressed as a list of tuples (or a dict)
46
+ // `?one=1&one=1&two=2`
47
+ // 2. it can't, for example because one of the pieces doesn't have a '='
48
+ // `?one`
49
+ // 3. we need to generate code that reads from a file
50
+ //
51
+ // If there's only one URL we merge the query from the URL with the shared part.
52
+ //
53
+ // If there's multiple URLs and a shared part that reads from a file (case 3),
54
+ // we only write the file reading code once, pass it as the params= argument
55
+ // and the part from the URL has to be passed as a string in the URL
56
+ // and requests will combine the query in the URL with the query in params=.
57
+ //
58
+ // Otherwise, we print each query for each URL individually, either as a
59
+ // list of tuples if we can or in the URL if we can't.
60
+ //
61
+ // When files are passed in through --data-urlencode or --url-query
62
+ // we can usually treat them as case 1 as well (in Python), but that would
63
+ // generate code slightly different from curl because curl reads the file once
64
+ // upfront, whereas we would read the file multiple times and it might contain
65
+ // different data each time (for example if it's /dev/urandom).
66
+ let urlQueryArray = null;
67
+ let queryArray = null;
68
+ let queryStrReadsFile = null;
69
+ if (u.query.toBool() || (config["url-query"] && config["url-query"].length)) {
70
+ let queryStr = null;
71
+ let queryParts = [];
72
+ if (u.query.toBool()) {
73
+ // remove the leading '?'
74
+ queryParts.push(["raw", u.query.slice(1)]);
75
+ [queryArray, queryStr, queryStrReadsFile] = buildData(queryParts, stdin, stdinFile);
76
+ urlQueryArray = queryArray;
77
+ }
78
+ if (config["url-query"]) {
79
+ queryParts = queryParts.concat(config["url-query"]);
80
+ [queryArray, queryStr, queryStrReadsFile] = buildData(queryParts, stdin, stdinFile);
81
+ }
82
+ // TODO: check the curl source code
83
+ // TODO: curl localhost:8888/?
84
+ // will request /?
85
+ // but
86
+ // curl localhost:8888/? --url-query ''
87
+ // (or --get --data '') will request /
88
+ u.query = new Word();
89
+ if (queryStr && queryStr.toBool()) {
90
+ u.query = queryStr.prepend("?");
91
+ }
92
+ }
93
+ const urlWithoutQueryArray = mergeWords(u.scheme, "://", u.host, u.path, u.fragment);
94
+ url = mergeWords(u.scheme, "://", u.host, u.path, u.query, u.fragment);
95
+ let urlWithoutQueryList = url;
96
+ // TODO: parseQueryString() doesn't accept leading '?'
97
+ let [queryList, queryDict] = parseQueryString(u.query.toBool() ? u.query.slice(1) : new Word());
98
+ if (queryList && queryList.length) {
99
+ // TODO: remove the fragment too?
100
+ urlWithoutQueryList = mergeWords(u.scheme, "://", u.host, u.path, u.fragment);
101
+ }
102
+ else {
103
+ queryList = null;
104
+ queryDict = null;
105
+ }
106
+ // TODO: --path-as-is
107
+ // TODO: --request-target
108
+ // curl expects you to uppercase methods always. If you do -X PoSt, that's what it
109
+ // will send, but most APIs will helpfully uppercase what you pass in as the method.
110
+ //
111
+ // There are many places where curl determines the method, this is the last one:
112
+ // https://github.com/curl/curl/blob/curl-7_85_0/lib/http.c#L2032
113
+ let method = new Word("GET");
114
+ if (config.request &&
115
+ // Safari adds `-X null` if it can't determine the request type
116
+ // https://github.com/WebKit/WebKit/blob/f58ef38d48f42f5d7723691cb090823908ff5f9f/Source/WebInspectorUI/UserInterface/Models/Resource.js#L1250
117
+ !eq(config.request, "null")) {
118
+ method = config.request;
119
+ }
120
+ else if (config.head) {
121
+ method = new Word("HEAD");
122
+ }
123
+ else if (uploadFile && uploadFile.toBool()) {
124
+ // --upload-file '' doesn't do anything.
125
+ method = new Word("PUT");
126
+ }
127
+ else if (!config.get && (has(config, "data") || has(config, "form"))) {
128
+ method = new Word("POST");
129
+ }
130
+ const requestUrl = {
131
+ originalUrl,
132
+ urlWithoutQueryList,
133
+ url,
134
+ urlObj: u,
135
+ urlWithOriginalQuery,
136
+ urlWithoutQueryArray,
137
+ method,
138
+ };
139
+ if (queryStrReadsFile) {
140
+ requestUrl.queryReadsFile = queryStrReadsFile;
141
+ }
142
+ if (queryList) {
143
+ requestUrl.queryList = queryList;
144
+ if (queryDict) {
145
+ requestUrl.queryDict = queryDict;
146
+ }
147
+ }
148
+ if (queryArray) {
149
+ requestUrl.queryArray = queryArray;
150
+ }
151
+ if (urlQueryArray) {
152
+ requestUrl.urlQueryArray = urlQueryArray;
153
+ }
154
+ if (uploadFile) {
155
+ if (eq(uploadFile, "-") || eq(uploadFile, ".")) {
156
+ if (stdinFile) {
157
+ requestUrl.uploadFile = stdinFile;
158
+ }
159
+ else if (stdin) {
160
+ warnf(global_, [
161
+ "upload-file-with-stdin-content",
162
+ "--upload-file with stdin content is not supported",
163
+ ]);
164
+ requestUrl.uploadFile = uploadFile;
165
+ // TODO: this is complicated,
166
+ // --upload-file only applies per-URL so .data needs to become per-URL...
167
+ // if you pass --data and --upload-file or --get and --upload-file, curl will error
168
+ // if (config.url && config.url.length === 1) {
169
+ // config.data = [["raw", stdin]];
170
+ // } else {
171
+ // warnf(global_, [
172
+ // "upload-file-with-stdin-content-and-multiple-urls",
173
+ // "--upload-file with stdin content and multiple URLs is not supported",
174
+ // ]);
175
+ // }
176
+ }
177
+ else {
178
+ requestUrl.uploadFile = uploadFile;
179
+ }
180
+ }
181
+ else {
182
+ requestUrl.uploadFile = uploadFile;
183
+ }
184
+ }
185
+ if (outputFile) {
186
+ // TODO: get stdout redirects of command
187
+ requestUrl.output = outputFile;
188
+ }
189
+ // --user takes precedence over the URL
190
+ const auth = config.user || u.auth;
191
+ if (auth) {
192
+ const [user, pass] = auth.split(":", 2);
193
+ requestUrl.auth = [user, pass || new Word()];
194
+ }
195
+ return requestUrl;
196
+ }
197
+ function buildData(configData, stdin, stdinFile) {
198
+ const data = [];
199
+ let dataStrState = new Word();
200
+ for (const [i, x] of configData.entries()) {
201
+ const type = x[0];
202
+ let value = x[1];
203
+ let name = null;
204
+ if (i > 0 && type !== "json") {
205
+ dataStrState = dataStrState.append("&");
206
+ }
207
+ if (type === "urlencode") {
208
+ // curl checks for = before @
209
+ const splitOn = value.includes("=") || !value.includes("@") ? "=" : "@";
210
+ // If there's no = or @ then the entire content is treated as a value and encoded
211
+ if (value.includes("@") || value.includes("=")) {
212
+ [name, value] = value.split(splitOn, 2);
213
+ }
214
+ if (splitOn === "=") {
215
+ if (name && name.toBool()) {
216
+ dataStrState = dataStrState.add(name).append("=");
217
+ }
218
+ // curl's --data-urlencode percent-encodes spaces as "+"
219
+ // https://github.com/curl/curl/blob/curl-7_86_0/src/tool_getparam.c#L630
220
+ dataStrState = dataStrState.add(percentEncodePlus(value));
221
+ continue;
222
+ }
223
+ name = name && name.toBool() ? name : null;
224
+ value = value.prepend("@");
225
+ }
226
+ let filename = null;
227
+ if (type !== "raw" && value.startsWith("@")) {
228
+ filename = value.slice(1);
229
+ if (eq(filename, "-")) {
230
+ if (stdin !== undefined) {
231
+ switch (type) {
232
+ case "binary":
233
+ case "json":
234
+ value = stdin;
235
+ break;
236
+ case "urlencode":
237
+ value = mergeWords(name && name.length ? name.append("=") : new Word(), percentEncodePlus(stdin));
238
+ break;
239
+ default:
240
+ value = stdin.replace(/[\n\r]/g, "");
241
+ }
242
+ filename = null;
243
+ }
244
+ else if (stdinFile !== undefined) {
245
+ filename = stdinFile;
246
+ }
247
+ else {
248
+ // TODO: if stdin is read twice, it will be empty the second time
249
+ // TODO: `STDIN_SENTINEL` so that we can tell the difference between
250
+ // a stdinFile called "-" and stdin for the error message
251
+ }
252
+ }
253
+ }
254
+ if (filename !== null) {
255
+ if (dataStrState.toBool()) {
256
+ data.push(dataStrState);
257
+ dataStrState = new Word();
258
+ }
259
+ const dataParam = {
260
+ // If `filename` isn't null, then `type` can't be "raw"
261
+ filetype: type,
262
+ filename,
263
+ };
264
+ if (name) {
265
+ dataParam.name = name;
266
+ }
267
+ data.push(dataParam);
268
+ }
269
+ else {
270
+ dataStrState = dataStrState.add(value);
271
+ }
272
+ }
273
+ if (dataStrState.toBool()) {
274
+ data.push(dataStrState);
275
+ }
276
+ let dataStrReadsFile = null;
277
+ const dataStr = mergeWords(...data.map((d) => {
278
+ if (!(d instanceof Word)) {
279
+ dataStrReadsFile || (dataStrReadsFile = d.filename.toString()); // report first file
280
+ if (d.name) {
281
+ return mergeWords(d.name, "=@", d.filename);
282
+ }
283
+ return d.filename.prepend("@");
284
+ }
285
+ return d;
286
+ }));
287
+ return [data, dataStr, dataStrReadsFile];
288
+ }
289
+ // Parses a Content-Type header into a type and a list of parameters
290
+ function parseContentType(string) {
291
+ if (!string.includes(";")) {
292
+ return [string, []];
293
+ }
294
+ const semi = string.indexOf(";");
295
+ const type = string.slice(0, semi);
296
+ const rest = string.slice(semi);
297
+ // See https://www.w3.org/Protocols/rfc1341/4_Content-Type.html
298
+ // TODO: could be better, like reading to the next semicolon
299
+ const params = rest.match(/;\s*([^;=]+)=(?:("[^"]*")|([^()<>@,;:\\"/[\]?.=]*))/g);
300
+ if (rest.trim() && !params) {
301
+ return null;
302
+ }
303
+ const parsedParams = [];
304
+ for (const param of params || []) {
305
+ const parsedParam = param.match(/;\s*([^;=]+)=(?:("[^"]*")|([^()<>@,;:\\"/[\]?.=]*))/);
306
+ if (!parsedParam) {
307
+ return null;
308
+ }
309
+ const name = parsedParam[1];
310
+ const value = parsedParam[3] || parsedParam[2].slice(1, -1);
311
+ parsedParams.push([name, value]);
312
+ }
313
+ return [type, parsedParams];
314
+ }
315
+ // Parses out the boundary= value from a Content-Type header
316
+ function parseBoundary(string) {
317
+ const header = parseContentType(string);
318
+ if (!header) {
319
+ return null;
320
+ }
321
+ for (const [name, value] of header[1]) {
322
+ if (name === "boundary") {
323
+ return value;
324
+ }
325
+ }
326
+ return null;
327
+ }
328
+ function parseRawForm(data, boundary) {
329
+ const endBoundary = "\r\n--" + boundary + "--\r\n";
330
+ if (!data.endsWith(endBoundary)) {
331
+ return null;
332
+ }
333
+ data = data.slice(0, -endBoundary.length);
334
+ // TODO: if empty form should it be completely empty?
335
+ boundary = "--" + boundary + "\r\n";
336
+ if (data && !data.startsWith(boundary)) {
337
+ return null;
338
+ }
339
+ data = data.slice(boundary.length);
340
+ const parts = data.split("\r\n" + boundary);
341
+ const form = [];
342
+ let roundtrips = true;
343
+ for (const part of parts) {
344
+ const lines = part.split("\r\n");
345
+ if (lines.length < 2) {
346
+ return null;
347
+ }
348
+ const formParam = {
349
+ name: new Word(),
350
+ content: new Word(),
351
+ };
352
+ let seenContentDisposition = false;
353
+ const headers = [];
354
+ let i = 0;
355
+ for (; i < lines.length; i++) {
356
+ if (lines[i].length === 0) {
357
+ break;
358
+ }
359
+ const [name, value] = lines[i].split(": ", 2);
360
+ if (name === undefined || value === undefined) {
361
+ return null;
362
+ }
363
+ if (name.toLowerCase() === "content-disposition") {
364
+ if (seenContentDisposition) {
365
+ // should only have one
366
+ return null;
367
+ }
368
+ const contentDisposition = parseContentType(value);
369
+ if (!contentDisposition) {
370
+ return null;
371
+ }
372
+ const [type, params] = contentDisposition;
373
+ if (type !== "form-data") {
374
+ return null;
375
+ }
376
+ let extra = 0;
377
+ for (const [paramName, paramValue] of params) {
378
+ switch (paramName) {
379
+ case "name":
380
+ formParam.name = new Word(paramValue);
381
+ break;
382
+ case "filename":
383
+ formParam.filename = new Word(paramValue);
384
+ break;
385
+ default:
386
+ extra++;
387
+ break;
388
+ }
389
+ }
390
+ if (extra) {
391
+ roundtrips = false;
392
+ // TODO: warn?
393
+ }
394
+ seenContentDisposition = true;
395
+ }
396
+ else if (name.toLowerCase() === "content-type") {
397
+ formParam.contentType = new Word(value);
398
+ }
399
+ else {
400
+ headers.push(new Word(lines[i]));
401
+ }
402
+ }
403
+ if (headers.length) {
404
+ formParam.headers = headers;
405
+ }
406
+ if (!seenContentDisposition) {
407
+ return null;
408
+ }
409
+ if (i === lines.length) {
410
+ return null;
411
+ }
412
+ if (formParam.name.isEmpty()) {
413
+ return null;
414
+ }
415
+ formParam.content = new Word(lines.slice(i + 1).join("\n"));
416
+ form.push(formParam);
417
+ }
418
+ return [form, roundtrips];
419
+ }
420
+ function buildRequest(global_, config, stdin, stdinFile) {
421
+ var _a, _b;
422
+ if (!config.url || !config.url.length) {
423
+ // TODO: better error message (could be parsing fail)
424
+ throw new CCError("no URL specified!");
425
+ }
426
+ const headers = new Headers(config.header, global_.warnings);
427
+ const proxyHeaders = new Headers(config["proxy-header"], global_.warnings, "--proxy-header");
428
+ let cookies;
429
+ const cookieFiles = [];
430
+ const cookieHeader = headers.get("cookie");
431
+ if (cookieHeader) {
432
+ const parsedCookies = parseCookiesStrict(cookieHeader);
433
+ if (parsedCookies) {
434
+ cookies = parsedCookies;
435
+ }
436
+ }
437
+ else if (cookieHeader === undefined && config.cookie) {
438
+ // If there is a Cookie header, --cookies is ignored
439
+ const cookieStrings = [];
440
+ for (const c of config.cookie) {
441
+ // a --cookie without a = character reads from it as a filename
442
+ if (c.includes("=")) {
443
+ cookieStrings.push(c);
444
+ }
445
+ else {
446
+ cookieFiles.push(c);
447
+ }
448
+ }
449
+ if (cookieStrings.length) {
450
+ const cookieString = joinWords(config.cookie, "; ");
451
+ headers.setIfMissing("Cookie", cookieString);
452
+ const parsedCookies = parseCookies(cookieString);
453
+ if (parsedCookies) {
454
+ cookies = parsedCookies;
455
+ }
456
+ }
457
+ }
458
+ let refererAuto = false;
459
+ if (config["user-agent"]) {
460
+ headers.setIfMissing("User-Agent", config["user-agent"]);
461
+ }
462
+ if (config.referer) {
463
+ if (config.referer.includes(";auto")) {
464
+ refererAuto = true;
465
+ }
466
+ // referer can be ";auto" or followed by ";auto", we ignore that.
467
+ const referer = config.referer.replace(/;auto$/, "");
468
+ if (referer.length) {
469
+ headers.setIfMissing("Referer", referer);
470
+ }
471
+ }
472
+ if (config.range) {
473
+ let range = config.range.prepend("bytes=");
474
+ if (!range.includes("-")) {
475
+ range = range.append("-");
476
+ }
477
+ headers.setIfMissing("Range", range);
478
+ }
479
+ if (config["time-cond"]) {
480
+ let timecond = config["time-cond"];
481
+ let header = "If-Modified-Since";
482
+ switch (timecond.charAt(0)) {
483
+ case "+":
484
+ timecond = timecond.slice(1);
485
+ break;
486
+ case "-":
487
+ timecond = timecond.slice(1);
488
+ header = "If-Unmodified-Since";
489
+ break;
490
+ case "=":
491
+ timecond = timecond.slice(1);
492
+ header = "Last-Modified";
493
+ break;
494
+ }
495
+ // TODO: parse date
496
+ headers.setIfMissing(header, timecond);
497
+ }
498
+ let data;
499
+ let dataStr;
500
+ let dataStrReadsFile;
501
+ let queryArray;
502
+ if (config.data && config.data.length) {
503
+ if (config.get) {
504
+ // https://github.com/curl/curl/blob/curl-7_85_0/src/tool_operate.c#L721
505
+ // --get --data will overwrite --url-query, but if there's no --data, for example,
506
+ // curl --url-query bar --get example.com
507
+ // it won't
508
+ // https://daniel.haxx.se/blog/2022/11/10/append-data-to-the-url-query/
509
+ config["url-query"] = config.data;
510
+ delete config.data;
511
+ }
512
+ else {
513
+ [data, dataStr, dataStrReadsFile] = buildData(config.data, stdin, stdinFile);
514
+ }
515
+ }
516
+ if (config["url-query"]) {
517
+ [queryArray] = buildData(config["url-query"], stdin, stdinFile);
518
+ }
519
+ const urls = [];
520
+ const uploadFiles = config["upload-file"] || [];
521
+ const outputFiles = config.output || [];
522
+ for (const [i, url] of config.url.entries()) {
523
+ urls.push(buildURL(global_, config, url, uploadFiles[i], outputFiles[i], stdin, stdinFile));
524
+ }
525
+ // --get moves --data into the URL's query string
526
+ if (config.get && config.data) {
527
+ delete config.data;
528
+ }
529
+ if ((config["upload-file"] || []).length > config.url.length) {
530
+ warnf(global_, [
531
+ "too-many-upload-files",
532
+ "Got more --upload-file/-T options than URLs: " +
533
+ ((_a = config["upload-file"]) === null || _a === void 0 ? void 0 : _a.map((f) => JSON.stringify(f.toString())).join(", ")),
534
+ ]);
535
+ }
536
+ if ((config.output || []).length > config.url.length) {
537
+ warnf(global_, [
538
+ "too-many-output-files",
539
+ "Got more --output/-o options than URLs: " +
540
+ ((_b = config.output) === null || _b === void 0 ? void 0 : _b.map((f) => JSON.stringify(f.toString())).join(", ")),
541
+ ]);
542
+ }
543
+ const request = {
544
+ urls,
545
+ authType: pickAuth(config.authtype),
546
+ proxyAuthType: pickAuth(config.proxyauthtype),
547
+ headers,
548
+ proxyHeaders,
549
+ };
550
+ // TODO: warn about unused stdin?
551
+ if (stdin) {
552
+ request.stdin = stdin;
553
+ }
554
+ if (stdinFile) {
555
+ request.stdinFile = stdinFile;
556
+ }
557
+ if (Object.prototype.hasOwnProperty.call(config, "globoff")) {
558
+ request.globoff = config.globoff;
559
+ }
560
+ if (Object.prototype.hasOwnProperty.call(config, "disallow-username-in-url")) {
561
+ request.disallowUsernameInUrl = config["disallow-username-in-url"];
562
+ }
563
+ if (Object.prototype.hasOwnProperty.call(config, "path-as-is")) {
564
+ request.pathAsIs = config["path-as-is"];
565
+ }
566
+ if (refererAuto) {
567
+ request.refererAuto = true;
568
+ }
569
+ if (cookies) {
570
+ // generators that use .cookies need to do
571
+ // deleteHeader(request, 'cookie')
572
+ request.cookies = cookies;
573
+ }
574
+ if (cookieFiles.length) {
575
+ request.cookieFiles = cookieFiles;
576
+ }
577
+ if (config["cookie-jar"]) {
578
+ request.cookieJar = config["cookie-jar"];
579
+ }
580
+ if (Object.prototype.hasOwnProperty.call(config, "junk-session-cookies")) {
581
+ request.junkSessionCookies = config["junk-session-cookies"];
582
+ }
583
+ if (Object.prototype.hasOwnProperty.call(config, "compressed")) {
584
+ request.compressed = config.compressed;
585
+ }
586
+ if (Object.prototype.hasOwnProperty.call(config, "tr-encoding")) {
587
+ request.transferEncoding = config["tr-encoding"];
588
+ }
589
+ if (config.include) {
590
+ request.include = true;
591
+ }
592
+ if (config.json) {
593
+ headers.setIfMissing("Content-Type", "application/json");
594
+ headers.setIfMissing("Accept", "application/json");
595
+ }
596
+ else if (config.data) {
597
+ headers.setIfMissing("Content-Type", "application/x-www-form-urlencoded");
598
+ }
599
+ else if (config.form) {
600
+ // TODO: warn when details (;filename=, etc.) are not supported
601
+ // by each converter.
602
+ request.multipartUploads = parseForm(config.form, global_.warnings);
603
+ //headers.setIfMissing("Content-Type", "multipart/form-data");
604
+ }
605
+ const contentType = headers.getContentType();
606
+ const exactContentType = headers.get("Content-Type");
607
+ if (config.data &&
608
+ !dataStrReadsFile &&
609
+ dataStr &&
610
+ dataStr.isString() &&
611
+ !config.form &&
612
+ !request.multipartUploads &&
613
+ contentType === "multipart/form-data" &&
614
+ exactContentType &&
615
+ exactContentType.isString()) {
616
+ const boundary = parseBoundary(exactContentType.toString());
617
+ if (boundary) {
618
+ const form = parseRawForm(dataStr.toString(), boundary);
619
+ if (form) {
620
+ const [parsedForm, roundtrip] = form;
621
+ request.multipartUploads = parsedForm;
622
+ if (!roundtrip) {
623
+ request.multipartUploadsDoesntRoundtrip = true;
624
+ }
625
+ }
626
+ }
627
+ }
628
+ if (Object.prototype.hasOwnProperty.call(config, "form-escape")) {
629
+ request.formEscape = config["form-escape"];
630
+ }
631
+ if (config["aws-sigv4"]) {
632
+ // https://github.com/curl/curl/blob/curl-7_86_0/lib/setopt.c#L678-L679
633
+ request.authType = "aws-sigv4";
634
+ request.awsSigV4 = config["aws-sigv4"];
635
+ }
636
+ if (request.authType === "bearer" && config["oauth2-bearer"]) {
637
+ const bearer = config["oauth2-bearer"].prepend("Bearer ");
638
+ headers.setIfMissing("Authorization", bearer);
639
+ request.oauth2Bearer = config["oauth2-bearer"];
640
+ }
641
+ if (config.delegation) {
642
+ request.delegation = config.delegation;
643
+ }
644
+ if (config.krb) {
645
+ request.krb = config.krb;
646
+ }
647
+ if (config["sasl-authzid"]) {
648
+ request.saslAuthzid = config["sasl-authzid"];
649
+ }
650
+ if (Object.prototype.hasOwnProperty.call(config, "sasl-ir")) {
651
+ request.saslIr = config["sasl-ir"];
652
+ }
653
+ if (config.negotiate) {
654
+ request.authType = "negotiate";
655
+ }
656
+ if (config["service-name"]) {
657
+ request.serviceName = config["service-name"];
658
+ }
659
+ // TODO: ideally we should generate code that explicitly unsets the header too
660
+ // no HTTP libraries allow that.
661
+ headers.clearNulls();
662
+ if (config.data && config.data.length) {
663
+ request.data = dataStr;
664
+ if (dataStrReadsFile) {
665
+ request.dataReadsFile = dataStrReadsFile;
666
+ }
667
+ request.dataArray = data;
668
+ // TODO: remove these
669
+ request.isDataRaw = false;
670
+ request.isDataBinary = (data || []).some((d) => !(d instanceof Word) && d.filetype === "binary");
671
+ }
672
+ if (queryArray) {
673
+ // If we have to generate code that reads from a file, we
674
+ // need to do it once for all URLs.
675
+ request.queryArray = queryArray;
676
+ }
677
+ if (Object.prototype.hasOwnProperty.call(config, "ipv4")) {
678
+ request["ipv4"] = config["ipv4"];
679
+ }
680
+ if (Object.prototype.hasOwnProperty.call(config, "ipv6")) {
681
+ request["ipv6"] = config["ipv6"];
682
+ }
683
+ if (config.proto) {
684
+ // TODO: parse
685
+ request.proto = config.proto;
686
+ }
687
+ if (config["proto-redir"]) {
688
+ // TODO: parse
689
+ request.protoRedir = config["proto-redir"];
690
+ }
691
+ if (config["proto-default"]) {
692
+ request.protoDefault = config["proto-default"];
693
+ }
694
+ if (config["tcp-fastopen"]) {
695
+ request.tcpFastopen = config["tcp-fastopen"];
696
+ }
697
+ if (config["local-port"]) {
698
+ // TODO: check the range
699
+ const [start, end] = config["local-port"].split("-", 1);
700
+ if (end && end.toBool()) {
701
+ request.localPort = [start, end];
702
+ }
703
+ else {
704
+ request.localPort = [config["local-port"], null];
705
+ }
706
+ }
707
+ if (Object.prototype.hasOwnProperty.call(config, "ignore-content-length")) {
708
+ request.ignoreContentLength = config["ignore-content-length"];
709
+ }
710
+ if (config.interface) {
711
+ request.interface = config.interface;
712
+ }
713
+ if (config.ciphers) {
714
+ request.ciphers = config.ciphers;
715
+ }
716
+ if (config.curves) {
717
+ request.curves = config.curves;
718
+ }
719
+ if (config.insecure) {
720
+ request.insecure = true;
721
+ }
722
+ if (Object.prototype.hasOwnProperty.call(config, "cert-status")) {
723
+ request.certStatus = config["cert-status"];
724
+ }
725
+ // TODO: if the URL doesn't start with https://, curl doesn't verify
726
+ // certificates, etc.
727
+ if (config.cert) {
728
+ if (config.cert.startsWith("pkcs11:") || !config.cert.match(/[:\\]/)) {
729
+ request.cert = [config.cert, null];
730
+ }
731
+ else {
732
+ // TODO: curl does more complex processing
733
+ // find un-backslash-escaped colon, backslash might also be escaped with a backslash
734
+ let colon = -1;
735
+ try {
736
+ // Safari versions older than 16.4 don't support negative lookbehind
737
+ colon = config.cert.search(/(?<!\\)(?:\\\\)*:/);
738
+ }
739
+ catch (_c) {
740
+ colon = config.cert.search(/:/);
741
+ }
742
+ if (colon === -1) {
743
+ request.cert = [config.cert, null];
744
+ }
745
+ else {
746
+ const cert = config.cert.slice(0, colon);
747
+ const password = config.cert.slice(colon + 1);
748
+ if (password.toBool()) {
749
+ request.cert = [cert, password];
750
+ }
751
+ else {
752
+ request.cert = [cert, null];
753
+ }
754
+ }
755
+ }
756
+ }
757
+ if (config["cert-type"]) {
758
+ const certType = config["cert-type"];
759
+ request.certType = certType;
760
+ if (certType.isString() &&
761
+ !["PEM", "DER", "ENG", "P12"].includes(certType.toString().toUpperCase())) {
762
+ warnf(global_, [
763
+ "cert-type-unknown",
764
+ "not supported file type " +
765
+ JSON.stringify(certType.toString()) +
766
+ " for certificate",
767
+ ]);
768
+ }
769
+ }
770
+ if (config.key) {
771
+ request.key = config.key;
772
+ }
773
+ if (config["key-type"]) {
774
+ request.keyType = config["key-type"];
775
+ }
776
+ if (config.pass) {
777
+ request.pass = config.pass;
778
+ }
779
+ if (config.cacert) {
780
+ request.cacert = config.cacert;
781
+ }
782
+ if (Object.prototype.hasOwnProperty.call(config, "ca-native")) {
783
+ request.caNative = config["ca-native"];
784
+ }
785
+ if (Object.prototype.hasOwnProperty.call(config, "ssl-allow-beast")) {
786
+ request.sslAllowBeast = config["ssl-allow-beast"];
787
+ }
788
+ if (config.capath) {
789
+ request.capath = config.capath;
790
+ }
791
+ if (config.crlfile) {
792
+ request.crlfile = config.crlfile;
793
+ }
794
+ if (config.pinnedpubkey) {
795
+ request.pinnedpubkey = config.pinnedpubkey;
796
+ }
797
+ if (config["random-file"]) {
798
+ request.randomFile = config["random-file"];
799
+ }
800
+ if (config["egd-file"]) {
801
+ request.egdFile = config["egd-file"];
802
+ }
803
+ if (config.hsts) {
804
+ request.hsts = config.hsts;
805
+ }
806
+ if (Object.prototype.hasOwnProperty.call(config, "alpn")) {
807
+ request.alpn = config.alpn;
808
+ }
809
+ if (config.tlsVersion) {
810
+ request.tlsVersion = config.tlsVersion;
811
+ }
812
+ if (config["tls-max"]) {
813
+ request.tlsMax = config["tls-max"];
814
+ }
815
+ if (config["tls13-ciphers"]) {
816
+ request.tls13Ciphers = config["tls13-ciphers"];
817
+ }
818
+ if (config["tlsauthtype"]) {
819
+ request.tlsauthtype = config["tlsauthtype"];
820
+ }
821
+ if (config["tlspassword"]) {
822
+ request.tlspassword = config["tlspassword"];
823
+ }
824
+ if (config["tlsuser"]) {
825
+ request.tlsuser = config["tlsuser"];
826
+ }
827
+ if (Object.prototype.hasOwnProperty.call(config, "ssl-allow-beast")) {
828
+ request.sslAllowBeast = config["ssl-allow-beast"];
829
+ }
830
+ if (Object.prototype.hasOwnProperty.call(config, "ssl-auto-client-cert")) {
831
+ request.sslAutoClientCert = config["ssl-auto-client-cert"];
832
+ }
833
+ if (Object.prototype.hasOwnProperty.call(config, "ssl-no-revoke")) {
834
+ request.sslNoRevoke = config["ssl-no-revoke"];
835
+ }
836
+ if (Object.prototype.hasOwnProperty.call(config, "ssl-reqd")) {
837
+ request.sslReqd = config["ssl-reqd"];
838
+ }
839
+ if (Object.prototype.hasOwnProperty.call(config, "ssl-revoke-best-effort")) {
840
+ request.sslRevokeBestEffort = config["ssl-revoke-best-effort"];
841
+ }
842
+ if (Object.prototype.hasOwnProperty.call(config, "ssl")) {
843
+ request.ssl = config["ssl"];
844
+ }
845
+ if (Object.prototype.hasOwnProperty.call(config, "sslv2")) {
846
+ request.sslv2 = config["sslv2"];
847
+ }
848
+ if (Object.prototype.hasOwnProperty.call(config, "sslv3")) {
849
+ request.sslv3 = config["sslv3"];
850
+ }
851
+ if (config["doh-url"]) {
852
+ request.dohUrl = config["doh-url"];
853
+ }
854
+ if (Object.prototype.hasOwnProperty.call(config, "doh-insecure")) {
855
+ request.dohInsecure = config["doh-insecure"];
856
+ }
857
+ if (Object.prototype.hasOwnProperty.call(config, "doh-cert-status")) {
858
+ request.dohCertStatus = config["doh-cert-status"];
859
+ }
860
+ if (config.proxy) {
861
+ // https://github.com/curl/curl/blob/e498a9b1fe5964a18eb2a3a99dc52160d2768261/lib/url.c#L2388-L2390
862
+ request.proxy = config.proxy;
863
+ if (request.proxyType && request.proxyType !== "http2") {
864
+ delete request.proxyType;
865
+ }
866
+ if (config["proxy-user"]) {
867
+ request.proxyAuth = config["proxy-user"];
868
+ }
869
+ }
870
+ if (Object.prototype.hasOwnProperty.call(config, "proxytunnel")) {
871
+ request.proxytunnel = config.proxytunnel;
872
+ }
873
+ if (config.noproxy) {
874
+ request.noproxy = config.noproxy;
875
+ }
876
+ if (config.preproxy) {
877
+ request.preproxy = config.preproxy;
878
+ }
879
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-anyauth")) {
880
+ request.proxyAnyauth = config["proxy-anyauth"];
881
+ }
882
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-basic")) {
883
+ request.proxyBasic = config["proxy-basic"];
884
+ }
885
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-digest")) {
886
+ request.proxyDigest = config["proxy-digest"];
887
+ }
888
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-negotiate")) {
889
+ request.proxyNegotiate = config["proxy-negotiate"];
890
+ }
891
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-ntlm")) {
892
+ request.proxyNtlm = config["proxy-ntlm"];
893
+ }
894
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-ca-native")) {
895
+ request.proxyCaNative = config["proxy-ca-native"];
896
+ }
897
+ if (config["proxy-cacert"]) {
898
+ request.proxyCacert = config["proxy-cacert"];
899
+ }
900
+ if (config["proxy-capath"]) {
901
+ request.proxyCapath = config["proxy-capath"];
902
+ }
903
+ if (config["proxy-cert-type"]) {
904
+ request.proxyCertType = config["proxy-cert-type"];
905
+ }
906
+ if (config["proxy-cert"]) {
907
+ request.proxyCert = config["proxy-cert"];
908
+ }
909
+ if (config["proxy-ciphers"]) {
910
+ request.proxyCiphers = config["proxy-ciphers"];
911
+ }
912
+ if (config["proxy-crlfile"]) {
913
+ request.proxyCrlfile = config["proxy-crlfile"];
914
+ }
915
+ if (config["proxy-http2"]) {
916
+ request.proxyType = "http2";
917
+ }
918
+ if (config["proxy1.0"]) {
919
+ request.proxy = config["proxy1.0"];
920
+ request.proxyType = "http1";
921
+ }
922
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-insecure")) {
923
+ request.proxyInsecure = config["proxy-insecure"];
924
+ }
925
+ if (config["proxy-key"]) {
926
+ request.proxyKey = config["proxy-key"];
927
+ }
928
+ if (config["proxy-key-type"]) {
929
+ request.proxyKeyType = config["proxy-key-type"];
930
+ }
931
+ if (config["proxy-pass"]) {
932
+ request.proxyPass = config["proxy-pass"];
933
+ }
934
+ if (config["proxy-pinnedpubkey"]) {
935
+ request.proxyPinnedpubkey = config["proxy-pinnedpubkey"];
936
+ }
937
+ if (config["proxy-pinnedpubkey"]) {
938
+ request.proxyPinnedpubkey = config["proxy-pinnedpubkey"];
939
+ }
940
+ if (config["proxy-service-name"]) {
941
+ request.proxyServiceName = config["proxy-service-name"];
942
+ }
943
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-ssl-allow-beast")) {
944
+ request.proxySslAllowBeast = config["proxy-ssl-allow-beast"];
945
+ }
946
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-ssl-auto-client-cert")) {
947
+ request.proxySslAutoClientCert = config["proxy-ssl-auto-client-cert"];
948
+ }
949
+ if (config["proxy-tls13-ciphers"]) {
950
+ request.proxyTls13Ciphers = config["proxy-tls13-ciphers"];
951
+ }
952
+ if (config["proxy-tlsauthtype"]) {
953
+ request.proxyTlsauthtype = config["proxy-tlsauthtype"];
954
+ if (request.proxyTlsauthtype.isString() &&
955
+ !eq(request.proxyTlsauthtype, "SRP")) {
956
+ warnf(global_, [
957
+ "proxy-tlsauthtype",
958
+ "proxy-tlsauthtype is not supported: " + request.proxyTlsauthtype,
959
+ ]);
960
+ }
961
+ }
962
+ if (config["proxy-tlspassword"]) {
963
+ request.proxyTlspassword = config["proxy-tlspassword"];
964
+ }
965
+ if (config["proxy-tlsuser"]) {
966
+ request.proxyTlsuser = config["proxy-tlsuser"];
967
+ }
968
+ if (Object.prototype.hasOwnProperty.call(config, "proxy-tlsv1")) {
969
+ request.proxyTlsv1 = config["proxy-tlsv1"];
970
+ }
971
+ if (config["proxy-user"]) {
972
+ request.proxyUser = config["proxy-user"];
973
+ }
974
+ if (Object.prototype.hasOwnProperty.call(config, "proxytunnel")) {
975
+ request.proxytunnel = config["proxytunnel"];
976
+ }
977
+ if (config["socks4"]) {
978
+ request.proxy = config["socks4"];
979
+ request.proxyType = "socks4";
980
+ }
981
+ if (config["socks4a"]) {
982
+ request.proxy = config["socks4a"];
983
+ request.proxyType = "socks4a";
984
+ }
985
+ if (config["socks5"]) {
986
+ request.proxy = config["socks5"];
987
+ request.proxyType = "socks5";
988
+ }
989
+ if (config["socks5-hostname"]) {
990
+ request.proxy = config["socks5-hostname"];
991
+ request.proxyType = "socks5-hostname";
992
+ }
993
+ if (Object.prototype.hasOwnProperty.call(config, "socks5-basic")) {
994
+ request.socks5Basic = config["socks5-basic"];
995
+ }
996
+ if (Object.prototype.hasOwnProperty.call(config, "socks5-gssapi-nec")) {
997
+ request.socks5GssapiNec = config["socks5-gssapi-nec"];
998
+ }
999
+ if (config["socks5-gssapi-service"]) {
1000
+ request.socks5GssapiService = config["socks5-gssapi-service"];
1001
+ }
1002
+ if (Object.prototype.hasOwnProperty.call(config, "socks5-gssapi")) {
1003
+ request.socks5Gssapi = config["socks5-gssapi"];
1004
+ }
1005
+ if (config["haproxy-clientip"]) {
1006
+ request.haproxyClientIp = config["haproxy-clientip"];
1007
+ }
1008
+ if (Object.prototype.hasOwnProperty.call(config, "haproxy-protocol")) {
1009
+ request.haproxyProtocol = config["haproxy-protocol"];
1010
+ }
1011
+ if (config["max-time"]) {
1012
+ request.timeout = config["max-time"];
1013
+ if (config["max-time"].isString() &&
1014
+ // TODO: parseFloat() like curl
1015
+ isNaN(parseFloat(config["max-time"].toString()))) {
1016
+ warnf(global_, [
1017
+ "max-time-not-number",
1018
+ "option --max-time: expected a proper numerical parameter: " +
1019
+ JSON.stringify(config["max-time"].toString()),
1020
+ ]);
1021
+ }
1022
+ }
1023
+ if (config["connect-timeout"]) {
1024
+ request.connectTimeout = config["connect-timeout"];
1025
+ if (config["connect-timeout"].isString() &&
1026
+ isNaN(parseFloat(config["connect-timeout"].toString()))) {
1027
+ warnf(global_, [
1028
+ "connect-timeout-not-number",
1029
+ "option --connect-timeout: expected a proper numerical parameter: " +
1030
+ JSON.stringify(config["connect-timeout"].toString()),
1031
+ ]);
1032
+ }
1033
+ }
1034
+ if (config["expect100-timeout"]) {
1035
+ request.expect100Timeout = config["expect100-timeout"];
1036
+ if (config["expect100-timeout"].isString() &&
1037
+ isNaN(parseFloat(config["expect100-timeout"].toString()))) {
1038
+ warnf(global_, [
1039
+ "expect100-timeout-not-number",
1040
+ "option --expect100-timeout: expected a proper numerical parameter: " +
1041
+ JSON.stringify(config["expect100-timeout"].toString()),
1042
+ ]);
1043
+ }
1044
+ }
1045
+ if (config["happy-eyeballs-timeout-ms"]) {
1046
+ request.happyEyeballsTimeoutMs = config["happy-eyeballs-timeout-ms"];
1047
+ }
1048
+ if (config["speed-limit"]) {
1049
+ request.speedLimit = config["speed-limit"];
1050
+ }
1051
+ if (config["speed-time"]) {
1052
+ request.speedTime = config["speed-time"];
1053
+ }
1054
+ if (config["limit-rate"]) {
1055
+ request.limitRate = config["limit-rate"];
1056
+ }
1057
+ if (config["max-filesize"]) {
1058
+ request.maxFilesize = config["max-filesize"];
1059
+ }
1060
+ if (Object.prototype.hasOwnProperty.call(config, "keepalive")) {
1061
+ request.keepAlive = config.keepalive;
1062
+ }
1063
+ if (config["keepalive-time"]) {
1064
+ request.keepAliveTime = config["keepalive-time"];
1065
+ }
1066
+ if (config["alt-svc"]) {
1067
+ request.altSvc = config["alt-svc"];
1068
+ }
1069
+ if (Object.prototype.hasOwnProperty.call(config, "location")) {
1070
+ request.followRedirects = config.location;
1071
+ }
1072
+ if (config["location-trusted"]) {
1073
+ request.followRedirectsTrusted = config["location-trusted"];
1074
+ }
1075
+ if (config["max-redirs"]) {
1076
+ request.maxRedirects = config["max-redirs"].trim();
1077
+ if (config["max-redirs"].isString() &&
1078
+ !isInt(config["max-redirs"].toString())) {
1079
+ warnf(global_, [
1080
+ "max-redirs-not-int",
1081
+ "option --max-redirs: expected a proper numerical parameter: " +
1082
+ JSON.stringify(config["max-redirs"].toString()),
1083
+ ]);
1084
+ }
1085
+ }
1086
+ if (Object.prototype.hasOwnProperty.call(config, "post301")) {
1087
+ request.post301 = config.post301;
1088
+ }
1089
+ if (Object.prototype.hasOwnProperty.call(config, "post302")) {
1090
+ request.post302 = config.post302;
1091
+ }
1092
+ if (Object.prototype.hasOwnProperty.call(config, "post303")) {
1093
+ request.post303 = config.post303;
1094
+ }
1095
+ if (config.fail) {
1096
+ request.fail = config.fail;
1097
+ }
1098
+ if (config.retry) {
1099
+ request.retry = config.retry;
1100
+ }
1101
+ if (config["retry-max-time"]) {
1102
+ request.retryMaxTime = config["retry-max-time"];
1103
+ }
1104
+ if (Object.prototype.hasOwnProperty.call(config, "ftp-skip-pasv-ip")) {
1105
+ request.ftpSkipPasvIp = config["ftp-skip-pasv-ip"];
1106
+ }
1107
+ if (config.httpVersion) {
1108
+ if (config.httpVersion === "2" ||
1109
+ config.httpVersion === "2-prior-knowledge") {
1110
+ request.http2 = true;
1111
+ }
1112
+ if (config.httpVersion === "3" || config.httpVersion === "3-only") {
1113
+ request.http3 = true;
1114
+ }
1115
+ request.httpVersion = config.httpVersion;
1116
+ }
1117
+ if (Object.prototype.hasOwnProperty.call(config, "http0.9")) {
1118
+ request.http0_9 = config["http0.9"];
1119
+ }
1120
+ if (config.resolve && config.resolve.length) {
1121
+ request.resolve = config.resolve;
1122
+ }
1123
+ if (config["connect-to"] && config["connect-to"].length) {
1124
+ request.connectTo = config["connect-to"];
1125
+ }
1126
+ if (config["unix-socket"]) {
1127
+ request.unixSocket = config["unix-socket"];
1128
+ }
1129
+ if (config["abstract-unix-socket"]) {
1130
+ request.abstractUnixSocket = config["abstract-unix-socket"];
1131
+ }
1132
+ if (config["netrc-optional"]) {
1133
+ request.netrc = "optional";
1134
+ }
1135
+ else if (config.netrc || config["netrc-file"]) {
1136
+ request.netrc = "required";
1137
+ }
1138
+ else if (config.netrc === false) {
1139
+ // TODO || config["netrc-optional"] === false ?
1140
+ request.netrc = "ignored";
1141
+ }
1142
+ if (config["netrc-file"]) {
1143
+ request.netrcFile = config["netrc-file"];
1144
+ }
1145
+ if (config["use-ascii"]) {
1146
+ request.useAscii = config["use-ascii"];
1147
+ }
1148
+ if (config["continue-at"]) {
1149
+ request.continueAt = config["continue-at"];
1150
+ }
1151
+ if (Object.prototype.hasOwnProperty.call(config, "crlf")) {
1152
+ request.crlf = config.crlf;
1153
+ }
1154
+ if (Object.prototype.hasOwnProperty.call(config, "clobber")) {
1155
+ request.clobber = config.clobber;
1156
+ }
1157
+ if (Object.prototype.hasOwnProperty.call(config, "remote-time")) {
1158
+ request.remoteTime = config["remote-time"];
1159
+ }
1160
+ // Global options
1161
+ if (Object.prototype.hasOwnProperty.call(global_, "verbose")) {
1162
+ request.verbose = global_.verbose;
1163
+ }
1164
+ if (Object.prototype.hasOwnProperty.call(global_, "silent")) {
1165
+ request.silent = global_.silent;
1166
+ }
1167
+ return request;
1168
+ }
1169
+ export function buildRequests(global_, stdin, stdinFile) {
1170
+ if (!global_.configs.length) {
1171
+ // shouldn't happen
1172
+ warnf(global_, ["no-configs", "got empty config object"]);
1173
+ }
1174
+ return global_.configs.map((config) => buildRequest(global_, config, stdin, stdinFile));
1175
+ }
1176
+ export function getFirst(requests, warnings, support) {
1177
+ if (requests.length > 1) {
1178
+ warnings.push([
1179
+ "next",
1180
+ // TODO: better message, we might have two requests because of
1181
+ // --next or because of multiple curl commands or both
1182
+ "got " +
1183
+ requests.length +
1184
+ " curl requests, only converting the first one",
1185
+ ]);
1186
+ }
1187
+ const request = requests[0];
1188
+ warnIfPartsIgnored(request, warnings, support);
1189
+ return request;
1190
+ }
1191
+ //# sourceMappingURL=Request.js.map