@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,1230 @@
1
+ import { Word, eq, mergeWords } from "../shell/Word.js";
2
+ import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../parse.js";
3
+ export const supportedArgs = new Set([
4
+ ...COMMON_SUPPORTED_ARGS,
5
+ "disallow-username-in-url",
6
+ "path-as-is",
7
+ "alt-svc",
8
+ "unix-socket",
9
+ "abstract-unix-socket",
10
+ "compressed",
11
+ "no-compressed",
12
+ "verbose",
13
+ "no-verbose",
14
+ "proxy",
15
+ "proxy-user",
16
+ "proxytunnel",
17
+ "noproxy",
18
+ "preproxy",
19
+ "proxy-anyauth",
20
+ "proxy-basic",
21
+ "proxy-digest",
22
+ "proxy-negotiate",
23
+ "proxy-ntlm",
24
+ "proxy-ca-native",
25
+ "proxy-cacert",
26
+ "proxy-capath",
27
+ "proxy-cert-type",
28
+ "proxy-cert",
29
+ "proxy-ciphers",
30
+ "proxy-crlfile",
31
+ "proxy-header",
32
+ "proxy-http2",
33
+ "proxy-insecure",
34
+ "proxy-key",
35
+ "proxy-key-type",
36
+ "proxy-pass",
37
+ "proxy-pinnedpubkey",
38
+ "proxy-service-name",
39
+ "proxy-ssl-allow-beast",
40
+ "proxy-ssl-auto-client-cert",
41
+ "proxy-tls13-ciphers",
42
+ "proxy-tlsuser",
43
+ "proxy-tlspassword",
44
+ "proxy-tlsauthtype",
45
+ "proxy-tlsv1",
46
+ "socks4",
47
+ "socks4a",
48
+ "socks5",
49
+ "socks5-hostname",
50
+ "socks5-basic",
51
+ "socks5-gssapi",
52
+ "socks5-gssapi-nec",
53
+ "socks5-gssapi-service",
54
+ "haproxy-clientip",
55
+ "haproxy-protocol",
56
+ "interface",
57
+ "netrc",
58
+ "netrc-file",
59
+ "netrc-optional",
60
+ // "anyauth",
61
+ // "no-anyauth",
62
+ "digest",
63
+ "no-digest",
64
+ "negotiate",
65
+ "no-negotiate",
66
+ "service-name",
67
+ "ntlm",
68
+ "no-ntlm",
69
+ "ntlm-wb",
70
+ "no-ntlm-wb",
71
+ "aws-sigv4",
72
+ "delegation",
73
+ "oauth2-bearer",
74
+ "ipv4",
75
+ "ipv6",
76
+ "local-port",
77
+ "ftp-skip-pasv-ip",
78
+ "sasl-authzid",
79
+ "sasl-ir",
80
+ "tr-encoding",
81
+ "tcp-fastopen",
82
+ // Not parsed properly
83
+ // "proto",
84
+ // "proto-redir",
85
+ "proto-default",
86
+ "ignore-content-length",
87
+ "no-ignore-content-length",
88
+ "remote-time",
89
+ // "time-cond",
90
+ // requires that the underlying libcurl was built to support c-ares
91
+ // "dns-interface",
92
+ // "dns-ipv4-addr",
93
+ // "dns-ipv6-addr",
94
+ // "dns-servers",
95
+ // No effect
96
+ // "dump-header",
97
+ // "include",
98
+ // "engine",
99
+ // Adds a If-None-Match header
100
+ // "etag-compare",
101
+ // "etag-save", // No effect
102
+ "fail",
103
+ // "fail-with-body",
104
+ // "fail-early",
105
+ "continue-at",
106
+ "speed-limit",
107
+ "speed-time",
108
+ "limit-rate",
109
+ "max-filesize",
110
+ "http0.9",
111
+ "http1.0",
112
+ "http1.1",
113
+ "http2",
114
+ "http2-prior-knowledge",
115
+ "http3",
116
+ "http3-only",
117
+ "cookie-jar",
118
+ "junk-session-cookies",
119
+ "crlf",
120
+ "no-crlf",
121
+ "use-ascii",
122
+ // "write-out",
123
+ "pass",
124
+ "cacert",
125
+ "capath",
126
+ "crlfile",
127
+ "pinnedpubkey",
128
+ "curves",
129
+ "cert",
130
+ "cert-status",
131
+ "cert-type",
132
+ "key",
133
+ "key-type",
134
+ "ca-native",
135
+ "ssl-allow-beast",
136
+ "ciphers",
137
+ "tls13-ciphers",
138
+ "tlsuser",
139
+ "tlspassword",
140
+ "tlsauthtype",
141
+ "ssl",
142
+ "ssl-auto-client-cert",
143
+ "ssl-no-revoke",
144
+ "ssl-reqd",
145
+ "ssl-revoke-best-effort",
146
+ // "sslv2", // ignored
147
+ // "sslv3", // ignored
148
+ "tlsv1",
149
+ "tlsv1.0",
150
+ "tlsv1.1",
151
+ "tlsv1.2",
152
+ "tlsv1.3",
153
+ "tls-max",
154
+ // "false-start",
155
+ "hsts",
156
+ "alpn",
157
+ "no-alpn",
158
+ "form",
159
+ "form-string",
160
+ "form-escape",
161
+ "no-form-escape",
162
+ "doh-url",
163
+ "doh-cert-status",
164
+ // "doh-insecure", // has no effect
165
+ "location",
166
+ "no-location",
167
+ "location-trusted",
168
+ "no-location-trusted",
169
+ "max-redirs",
170
+ "post301",
171
+ "post302",
172
+ "post303",
173
+ "max-time",
174
+ "connect-timeout",
175
+ "expect100-timeout",
176
+ "happy-eyeballs-timeout-ms",
177
+ "resolve",
178
+ "connect-to",
179
+ "keepalive",
180
+ "no-keepalive",
181
+ "keepalive-time",
182
+ "insecure",
183
+ "no-insecure",
184
+ ]);
185
+ const regexEscape = /"|\\|\p{C}|[^ \P{Z}]/gu;
186
+ export function reprStr(s) {
187
+ return ('"' +
188
+ s.replace(regexEscape, (c) => {
189
+ switch (c) {
190
+ case "\x07":
191
+ return "\\a";
192
+ case "\b":
193
+ return "\\b";
194
+ case "\f":
195
+ return "\\f";
196
+ case "\n":
197
+ return "\\n";
198
+ case "\r":
199
+ return "\\r";
200
+ case "\t":
201
+ return "\\t";
202
+ case "\v":
203
+ return "\\v";
204
+ case "\\":
205
+ return "\\\\";
206
+ case '"':
207
+ return '\\"';
208
+ }
209
+ const hex = c.codePointAt(0).toString(16);
210
+ if (hex.length <= 2) {
211
+ return "\\x" + hex.padStart(2, "0");
212
+ }
213
+ if (hex.length <= 4) {
214
+ return "\\u" + hex.padStart(4, "0");
215
+ }
216
+ return "\\U" + hex.padStart(8, "0");
217
+ }) +
218
+ '"');
219
+ }
220
+ export function repr(word, imports) {
221
+ const reprs = [];
222
+ for (const t of word.tokens) {
223
+ if (typeof t === "string") {
224
+ reprs.push(reprStr(t));
225
+ }
226
+ else if (t.type === "variable") {
227
+ reprs.push("getenv(" + reprStr(t.value) + ")");
228
+ imports.add("stdlib.h");
229
+ }
230
+ else if (t.type === "command") {
231
+ // TODO: read the FILE contents
232
+ reprs.push("popen(" + reprStr(t.value) + ', "r")');
233
+ imports.add("stdio.h");
234
+ }
235
+ }
236
+ // TODO: C can't concatenate strings
237
+ return reprs.join(" + ");
238
+ }
239
+ export function atof1000(word, imports) {
240
+ if (word.isString()) {
241
+ // TODO: check it's actually a valid float
242
+ const asFloat = parseFloat(word.toString());
243
+ if (!Number.isNaN(asFloat)) {
244
+ return Math.floor(asFloat * 1000) + "L";
245
+ }
246
+ }
247
+ return "(long)(atof(" + repr(word, imports) + ") * 1000)";
248
+ }
249
+ export function atoi(word, imports) {
250
+ if (word.isString()) {
251
+ // TODO: check it's actually a valid int
252
+ return word.toString();
253
+ }
254
+ return "atoi(" + repr(word, imports) + ")";
255
+ }
256
+ export function atol(word, imports) {
257
+ if (word.isString()) {
258
+ // TODO: check it's actually a valid int
259
+ return word.toString() + "L";
260
+ }
261
+ return "atol(" + repr(word, imports) + ")";
262
+ }
263
+ const AUTH_TO_VAR = {
264
+ basic: "CURLAUTH_BASIC",
265
+ negotiate: "CURLAUTH_NEGOTIATE",
266
+ // technically what --libcurl generates but older
267
+ // negotiate: "CURLAUTH_GSSNEGOTIATE",
268
+ digest: "CURLAUTH_DIGEST",
269
+ ntlm: "CURLAUTH_NTLM",
270
+ "ntlm-wb": "CURLAUTH_NTLM_WB",
271
+ bearer: "CURLAUTH_BEARER",
272
+ "aws-sigv4": "CURLAUTH_AWS_SIGV4",
273
+ none: "CURLAUTH_NONE",
274
+ };
275
+ function requestToC(request, warnings = [], imports) {
276
+ let preamble = "";
277
+ preamble += "int main(int argc, char *argv[])\n";
278
+ preamble += "{\n";
279
+ preamble += " CURLcode ret;\n";
280
+ preamble += " CURL *hnd;\n";
281
+ let vars = "";
282
+ let code = "";
283
+ code += " hnd = curl_easy_init();\n";
284
+ let cleanup = "";
285
+ cleanup += " curl_easy_cleanup(hnd);\n";
286
+ cleanup += " hnd = NULL;\n";
287
+ if (request.tcpFastopen) {
288
+ code += " curl_easy_setopt(hnd, CURLOPT_TCP_FASTOPEN, 1L);\n";
289
+ }
290
+ const bufferSize = request.limitRate
291
+ ? atol(request.limitRate, imports) // TODO: parse
292
+ : "102400L";
293
+ code += " curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, " + bufferSize + ");\n";
294
+ if (request.localPort) {
295
+ const [start, end] = request.localPort;
296
+ code +=
297
+ " curl_easy_setopt(hnd, CURLOPT_LOCALPORT, " +
298
+ atol(start, imports) +
299
+ ");\n";
300
+ let range = "1L";
301
+ if (end) {
302
+ range = atol(end, imports) + "-" + atol(start, imports);
303
+ }
304
+ code += " curl_easy_setopt(hnd, CURLOPT_LOCALPORTRANGE, " + range + ");\n";
305
+ }
306
+ // TODO: if it doesn't have a query string from --data, it's better to
307
+ // do originalUrl because it doesn't need to have the http[s]://
308
+ const url = request.urls[0].url;
309
+ code += " curl_easy_setopt(hnd, CURLOPT_URL, " + repr(url, imports) + ");\n";
310
+ code += " curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);\n";
311
+ if (request.oauth2Bearer) {
312
+ code +=
313
+ " curl_easy_setopt(hnd, CURLOPT_XOAUTH2_BEARER, " +
314
+ repr(request.oauth2Bearer, imports) +
315
+ ");\n";
316
+ }
317
+ if (request.proxy) {
318
+ const proxy = repr(request.proxy, imports);
319
+ code += " curl_easy_setopt(hnd, CURLOPT_PROXY, " + proxy + ");\n";
320
+ }
321
+ if (request.proxyType) {
322
+ if (request.proxyType === "http2") {
323
+ code += " curl_easy_setopt(hnd, CURLOPT_PROXYTYPE, 3L);\n";
324
+ }
325
+ else if (request.proxyType === "http1") {
326
+ code +=
327
+ " curl_easy_setopt(hnd, CURLOPT_PROXYTYPE, (long)CURLPROXY_HTTP_1_0);\n";
328
+ }
329
+ else if (request.proxyType === "socks4") {
330
+ code +=
331
+ " curl_easy_setopt(hnd, CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS4);\n";
332
+ }
333
+ else if (request.proxyType === "socks4a") {
334
+ code +=
335
+ " curl_easy_setopt(hnd, CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS4A);\n";
336
+ }
337
+ else if (request.proxyType === "socks5") {
338
+ code +=
339
+ " curl_easy_setopt(hnd, CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS5);\n";
340
+ }
341
+ else if (request.proxyType === "socks5-hostname") {
342
+ code +=
343
+ " curl_easy_setopt(hnd, CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS5_HOSTNAME);\n";
344
+ }
345
+ }
346
+ if (request.proxyAuth) {
347
+ const proxyUserpwd = repr(request.proxyAuth, imports);
348
+ code +=
349
+ " curl_easy_setopt(hnd, CURLOPT_PROXYUSERPWD, " + proxyUserpwd + ");\n";
350
+ }
351
+ if (request.proxytunnel) {
352
+ code += " curl_easy_setopt(hnd, CURLOPT_HTTPPROXYTUNNEL, 1L);\n";
353
+ }
354
+ if (request.proxyAuth) {
355
+ const proxyAuth = AUTH_TO_VAR[request.proxyAuthType];
356
+ code +=
357
+ " curl_easy_setopt(hnd, CURLOPT_PROXYAUTH, (long)" + proxyAuth + ");\n";
358
+ }
359
+ if (request.preproxy) {
360
+ const preproxy = repr(request.preproxy, imports);
361
+ code += " curl_easy_setopt(hnd, CURLOPT_PRE_PROXY, " + preproxy + ");\n";
362
+ }
363
+ if (request.noproxy) {
364
+ const noproxy = repr(request.noproxy, imports);
365
+ code += " curl_easy_setopt(hnd, CURLOPT_NOPROXY, " + noproxy + ");\n";
366
+ }
367
+ if (request.fail) {
368
+ code += " curl_easy_setopt(hnd, CURLOPT_FAILONERROR, 1L);\n";
369
+ }
370
+ if (request.netrc) {
371
+ const netrc = {
372
+ optional: "CURL_NETRC_OPTIONAL",
373
+ required: "CURL_NETRC_REQUIRED",
374
+ ignored: "CURL_NETRC_IGNORED",
375
+ }[request.netrc || "ignored"];
376
+ code += " curl_easy_setopt(hnd, CURLOPT_NETRC, (long)" + netrc + ");\n";
377
+ }
378
+ if (request.netrcFile) {
379
+ const netrcFile = repr(request.netrcFile, imports);
380
+ code += " curl_easy_setopt(hnd, CURLOPT_NETRC_FILE, " + netrcFile + ");\n";
381
+ }
382
+ if (request.useAscii) {
383
+ code += " curl_easy_setopt(hnd, CURLOPT_TRANSFERTEXT, 1L);\n";
384
+ }
385
+ if (request.urls[0].auth) {
386
+ const [username, password] = request.urls[0].auth;
387
+ const userpwd = repr(mergeWords(username, ":", password), imports);
388
+ code += " curl_easy_setopt(hnd, CURLOPT_USERPWD, " + userpwd + ");\n";
389
+ }
390
+ if (request.timeout) {
391
+ code +=
392
+ " curl_easy_setopt(hnd, CURLOPT_TIMEOUT_MS, " +
393
+ atof1000(request.timeout, imports) +
394
+ ");\n";
395
+ }
396
+ if (request.urls[0].uploadFile) {
397
+ // TODO
398
+ }
399
+ else if (request.multipartUploads) {
400
+ preamble += " curl_mime *mime1;\n";
401
+ preamble += " curl_mimepart *part1;\n";
402
+ vars += " mime1 = NULL;\n";
403
+ code += " mime1 = curl_mime_init(hnd);\n";
404
+ for (const m of request.multipartUploads) {
405
+ code += " part1 = curl_mime_addpart(mime1);\n";
406
+ if ("contentFile" in m && m.contentFile) {
407
+ code +=
408
+ " curl_mime_filedata(part1, " +
409
+ repr(m.contentFile, imports) +
410
+ ");\n";
411
+ }
412
+ else if ("content" in m && m.content) {
413
+ code +=
414
+ " curl_mime_data(part1, " +
415
+ repr(m.content, imports) +
416
+ ", CURL_ZERO_TERMINATED);\n";
417
+ }
418
+ if ("encoder" in m && m.encoder) {
419
+ code +=
420
+ " curl_mime_encoder(part1, " + repr(m.encoder, imports) + ");\n";
421
+ }
422
+ if ("filename" in m &&
423
+ m.filename &&
424
+ !("contentFile" in m && m.contentFile && eq(m.filename, m.contentFile))) {
425
+ code +=
426
+ " curl_mime_filename(part1, " + repr(m.filename, imports) + ");\n";
427
+ }
428
+ else if (!m.filename && "contentFile" in m && m.contentFile) {
429
+ code += " curl_mime_filename(part1, NULL);\n";
430
+ }
431
+ code += " curl_mime_name(part1, " + repr(m.name, imports) + ");\n";
432
+ if ("contentType" in m && m.contentType) {
433
+ code +=
434
+ " curl_mime_type(part1, " + repr(m.contentType, imports) + ");\n";
435
+ }
436
+ if ("headers" in m && m.headers) {
437
+ warnings.push([
438
+ "multipart-headers",
439
+ "multipart headers are not supported: " +
440
+ m.headers.map((h) => h.toString()).join(", "),
441
+ ]);
442
+ }
443
+ if ("headerFiles" in m && m.headerFiles) {
444
+ warnings.push([
445
+ "multipart-headers",
446
+ "multipart header files are not supported: " +
447
+ m.headerFiles.map((h) => h.toString()).join(", "),
448
+ ]);
449
+ }
450
+ }
451
+ code += " curl_easy_setopt(hnd, CURLOPT_MIMEPOST, mime1);\n";
452
+ cleanup += " curl_mime_free(mime1);\n";
453
+ cleanup += " mime1 = NULL;\n";
454
+ }
455
+ else if (request.data) {
456
+ code +=
457
+ " curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, " +
458
+ repr(request.data, imports) +
459
+ ");\n";
460
+ // this isn't correct if .data reads files
461
+ code +=
462
+ " curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)" +
463
+ request.data.length.toString() +
464
+ ");\n";
465
+ }
466
+ if (request.formEscape) {
467
+ code += " curl_easy_setopt(hnd, CURLOPT_MIME_OPTIONS, 1L);\n";
468
+ }
469
+ if (request.urls[0].auth) {
470
+ const curlAuth = AUTH_TO_VAR[request.authType];
471
+ code +=
472
+ " curl_easy_setopt(hnd, CURLOPT_HTTPAUTH, (long)" + curlAuth + ");\n";
473
+ }
474
+ const headerLines = [];
475
+ for (const [headerName, headerValue] of request.headers) {
476
+ const h = headerName.toLowerCase();
477
+ if (eq(h, "user-agent") || eq(h, "referer")) {
478
+ continue;
479
+ }
480
+ if (headerValue === null) {
481
+ headerLines.push(" headers = curl_slist_append(headers, " +
482
+ repr(mergeWords(headerName, ":"), imports) +
483
+ ");\n");
484
+ }
485
+ else if (eq(headerValue, "")) {
486
+ headerLines.push(" headers = curl_slist_append(headers, " +
487
+ repr(mergeWords(headerName, ";"), imports) +
488
+ ");\n");
489
+ }
490
+ else {
491
+ headerLines.push(" headers = curl_slist_append(headers, " +
492
+ repr(mergeWords(headerName, ": ", headerValue), imports) +
493
+ ");\n");
494
+ }
495
+ }
496
+ if (headerLines.length) {
497
+ preamble += " struct curl_slist *headers;\n";
498
+ vars += " headers = NULL;\n";
499
+ vars += headerLines.join("");
500
+ code += " curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);\n";
501
+ cleanup += " curl_slist_free_all(headers);\n";
502
+ cleanup += " headers = NULL;\n";
503
+ }
504
+ const referer = request.headers.get("referer");
505
+ if (referer) {
506
+ code +=
507
+ " curl_easy_setopt(hnd, CURLOPT_REFERER, " +
508
+ repr(referer, imports) +
509
+ ");\n";
510
+ // TODO: only if passed with --referer and not -H
511
+ }
512
+ const userAgent = request.headers.get("user-agent");
513
+ if (userAgent) {
514
+ code +=
515
+ " curl_easy_setopt(hnd, CURLOPT_USERAGENT, " +
516
+ repr(userAgent, imports) +
517
+ ");\n";
518
+ }
519
+ else if (userAgent === undefined) {
520
+ // TODO: needs to be kept up-to-date with VERSION in cli.ts
521
+ code += ' curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/8.2.1");\n';
522
+ }
523
+ if (request.followRedirects) {
524
+ code += " curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 1L);\n";
525
+ if (request.followRedirectsTrusted) {
526
+ code += " curl_easy_setopt(hnd, CURLOPT_UNRESTRICTED_AUTH, 1L);\n";
527
+ }
528
+ }
529
+ if (request.awsSigV4) {
530
+ const awsSig = repr(request.awsSigV4, imports);
531
+ code += " curl_easy_setopt(hnd, CURLOPT_AWS_SIGV4, " + awsSig + ");\n";
532
+ }
533
+ if (request.refererAuto) {
534
+ code += " curl_easy_setopt(hnd, CURLOPT_AUTOREFERER, 1L);\n";
535
+ }
536
+ if (request.proxyHeaders.length) {
537
+ // TODO: camelCase snake_case orjustoneword?
538
+ preamble += " struct curl_slist *proxy_headers;\n";
539
+ if (vars) {
540
+ vars += "\n";
541
+ }
542
+ vars += " proxy_headers = NULL;\n";
543
+ for (const [headerName, headerValue] of request.proxyHeaders) {
544
+ if (headerValue === null) {
545
+ vars +=
546
+ " proxy_headers = curl_slist_append(proxy_headers, " +
547
+ repr(mergeWords(headerName, ":"), imports) +
548
+ ");\n";
549
+ }
550
+ else if (eq(headerValue, "")) {
551
+ vars +=
552
+ " proxy_headers = curl_slist_append(proxy_headers, " +
553
+ repr(mergeWords(headerName, ";"), imports) +
554
+ ");\n";
555
+ }
556
+ else {
557
+ vars +=
558
+ " proxy_headers = curl_slist_append(proxy_headers, " +
559
+ repr(mergeWords(headerName, ": ", headerValue), imports) +
560
+ ");\n";
561
+ }
562
+ }
563
+ code += " curl_easy_setopt(hnd, CURLOPT_PROXYHEADER, proxy_headers);\n";
564
+ code +=
565
+ " curl_easy_setopt(hnd, CURLOPT_HEADEROPT, (long)CURLHEADER_SEPARATE);\n";
566
+ cleanup += " curl_slist_free_all(proxy_headers);\n";
567
+ cleanup += " proxy_headers = NULL;\n";
568
+ }
569
+ const maxRedirs = request.maxRedirects || new Word("50");
570
+ code +=
571
+ " curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, " +
572
+ atol(maxRedirs, imports) +
573
+ ");\n";
574
+ let httpVersion = "CURL_HTTP_VERSION_2TLS";
575
+ if (request.httpVersion) {
576
+ httpVersion = {
577
+ "1.0": "CURL_HTTP_VERSION_1_0",
578
+ "1.1": "CURL_HTTP_VERSION_1_1",
579
+ "2": "CURL_HTTP_VERSION_2_0",
580
+ "2-prior-knowledge": "CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE",
581
+ "3": "CURL_HTTP_VERSION_3",
582
+ "3-only": "CURL_HTTP_VERSION_3ONLY",
583
+ }[request.httpVersion];
584
+ }
585
+ code +=
586
+ " curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)" +
587
+ httpVersion +
588
+ ");\n";
589
+ if (request.http0_9) {
590
+ code += " curl_easy_setopt(hnd, CURLOPT_HTTP09_ALLOWED, 1L);\n";
591
+ }
592
+ if (request.post301 || request.post302 || request.post303) {
593
+ const postRedir = [];
594
+ if (request.post301) {
595
+ postRedir.push("CURL_REDIR_POST_301");
596
+ }
597
+ if (request.post302) {
598
+ postRedir.push("CURL_REDIR_POST_302");
599
+ }
600
+ if (request.post303) {
601
+ postRedir.push("CURL_REDIR_POST_303");
602
+ }
603
+ code +=
604
+ " curl_easy_setopt(hnd, CURLOPT_POSTREDIR, " +
605
+ postRedir.join(" | ") +
606
+ ");\n";
607
+ }
608
+ if (request.compressed) {
609
+ code += ' curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "");\n';
610
+ }
611
+ if (request.transferEncoding) {
612
+ code += " curl_easy_setopt(hnd, CURLOPT_TRANSFER_ENCODING, 1L);\n";
613
+ }
614
+ if (request.speedLimit || request.speedTime) {
615
+ const speedLimit = atol(request.speedLimit || new Word("1"), imports);
616
+ const speedTime = atol(request.speedTime || new Word("30"), imports);
617
+ code +=
618
+ " curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_LIMIT, " + speedLimit + ");\n";
619
+ code +=
620
+ " curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_TIME, " + speedTime + ");\n";
621
+ }
622
+ if (request.limitRate) {
623
+ // TODO: parse
624
+ const limitRate = repr(request.limitRate, imports);
625
+ code +=
626
+ " curl_easy_setopt(hnd, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t)" +
627
+ limitRate +
628
+ ");\n";
629
+ code +=
630
+ " curl_easy_setopt(hnd, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)" +
631
+ limitRate +
632
+ ");\n";
633
+ }
634
+ if (request.continueAt) {
635
+ if (!eq(request.continueAt, "-")) {
636
+ const continueAt = atoi(request.continueAt, imports);
637
+ code +=
638
+ " curl_easy_setopt(hnd, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)" +
639
+ continueAt +
640
+ ");\n";
641
+ }
642
+ }
643
+ if (request.pass) {
644
+ const pass = repr(request.pass, imports);
645
+ // TODO: --cert can also set this
646
+ code += " curl_easy_setopt(hnd, CURLOPT_KEYPASSWD, " + pass + ");\n";
647
+ }
648
+ if (request.proxyPass) {
649
+ // TODO: --proxy-cert can also set this
650
+ const proxyPass = repr(request.proxyPass, imports);
651
+ code +=
652
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_KEYPASSWD, " + proxyPass + ");\n";
653
+ }
654
+ if (request.cacert) {
655
+ const cacert = repr(request.cacert, imports);
656
+ code += " curl_easy_setopt(hnd, CURLOPT_CAINFO, " + cacert + ");\n";
657
+ }
658
+ if (request.proxyCacert) {
659
+ const proxyCacert = repr(request.proxyCacert, imports);
660
+ code +=
661
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_CAINFO, " + proxyCacert + ");\n";
662
+ }
663
+ if (request.capath || request.proxyCapath) {
664
+ if (request.capath) {
665
+ const capath = repr(request.capath, imports);
666
+ code += " curl_easy_setopt(hnd, CURLOPT_CAPATH, " + capath + ");\n";
667
+ const proxyCapath = repr(request.proxyCapath || request.capath, imports);
668
+ code +=
669
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_CAPATH, " + proxyCapath + ");\n";
670
+ }
671
+ else if (request.proxyCapath) {
672
+ // placate type checker
673
+ const proxyCapath = repr(request.proxyCapath, imports);
674
+ code +=
675
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_CAPATH, " + proxyCapath + ");\n";
676
+ }
677
+ }
678
+ if (request.crlfile || request.proxyCrlfile) {
679
+ if (request.crlfile) {
680
+ const crlfile = repr(request.crlfile, imports);
681
+ code += " curl_easy_setopt(hnd, CURLOPT_CRLFILE, " + crlfile + ");\n";
682
+ const proxyCrlfile = repr(request.proxyCrlfile || request.crlfile, imports);
683
+ code +=
684
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_CRLFILE, " +
685
+ proxyCrlfile +
686
+ ");\n";
687
+ }
688
+ else if (request.proxyCrlfile) {
689
+ // placate type checker
690
+ const proxyCrlfile = repr(request.proxyCrlfile, imports);
691
+ code +=
692
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_CRLFILE, " +
693
+ proxyCrlfile +
694
+ ");\n";
695
+ }
696
+ }
697
+ if (request.pinnedpubkey) {
698
+ const pinnedpubkey = repr(request.pinnedpubkey, imports);
699
+ code +=
700
+ " curl_easy_setopt(hnd, CURLOPT_PINNEDPUBLICKEY, " +
701
+ pinnedpubkey +
702
+ ");\n";
703
+ }
704
+ // TODO: --proxy-pinnedpubkey ?
705
+ if (request.curves) {
706
+ const curves = repr(request.curves, imports);
707
+ code += " curl_easy_setopt(hnd, CURLOPT_SSL_EC_CURVES, " + curves + ");\n";
708
+ }
709
+ if (request.cert) {
710
+ const [cert, pass] = request.cert;
711
+ if (pass) {
712
+ code +=
713
+ " curl_easy_setopt(hnd, CURLOPT_KEYPASSWD, " +
714
+ repr(pass, imports) +
715
+ ");\n";
716
+ }
717
+ code +=
718
+ " curl_easy_setopt(hnd, CURLOPT_SSLCERT, " +
719
+ repr(cert, imports) +
720
+ ");\n";
721
+ }
722
+ if (request.proxyCert) {
723
+ // TODO: split
724
+ const proxyCert = repr(request.proxyCert, imports);
725
+ code +=
726
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SSLCERT, " + proxyCert + ");\n";
727
+ }
728
+ if (request.certType) {
729
+ const certType = repr(request.certType, imports);
730
+ code += " curl_easy_setopt(hnd, CURLOPT_SSLCERTTYPE, " + certType + ");\n";
731
+ }
732
+ if (request.proxyCertType) {
733
+ const proxyCertType = repr(request.proxyCertType, imports);
734
+ code +=
735
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SSLCERTTYPE, " +
736
+ proxyCertType +
737
+ ");\n";
738
+ }
739
+ if (request.key) {
740
+ const key = repr(request.key, imports);
741
+ code += " curl_easy_setopt(hnd, CURLOPT_SSLKEY, " + key + ");\n";
742
+ }
743
+ if (request.proxyKey) {
744
+ const proxyKey = repr(request.proxyKey, imports);
745
+ code +=
746
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SSLKEY, " + proxyKey + ");\n";
747
+ }
748
+ if (request.keyType) {
749
+ const keyType = repr(request.keyType, imports);
750
+ code += " curl_easy_setopt(hnd, CURLOPT_SSLKEYTYPE, " + keyType + ");\n";
751
+ }
752
+ if (request.proxyKeyType) {
753
+ const proxyKeyType = repr(request.proxyKeyType, imports);
754
+ code +=
755
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SSLKEYTYPE, " +
756
+ proxyKeyType +
757
+ ");\n";
758
+ }
759
+ if (request.insecure) {
760
+ code += " curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);\n";
761
+ code += " curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);\n";
762
+ }
763
+ if (request.proxyInsecure) {
764
+ code += " curl_easy_setopt(hnd, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);\n";
765
+ code += " curl_easy_setopt(hnd, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);\n";
766
+ }
767
+ if (request.certStatus) {
768
+ code += " curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYSTATUS, 1L);\n";
769
+ }
770
+ if (request.dohCertStatus) {
771
+ code += " curl_easy_setopt(hnd, CURLOPT_DOH_SSL_VERIFYSTATUS, 1L);\n";
772
+ }
773
+ let tlsVersion = null;
774
+ if (request.tlsVersion) {
775
+ tlsVersion = {
776
+ "1": "CURL_SSLVERSION_TLSv1",
777
+ "1.0": "CURL_SSLVERSION_TLSv1_0",
778
+ "1.1": "CURL_SSLVERSION_TLSv1_1",
779
+ "1.2": "CURL_SSLVERSION_TLSv1_2",
780
+ "1.3": "CURL_SSLVERSION_TLSv1_3",
781
+ }[request.tlsVersion];
782
+ }
783
+ let tlsMax = null;
784
+ if (request.tlsMax) {
785
+ if (request.tlsMax.isString()) {
786
+ const tlsMaxVal = request.tlsMax.toString();
787
+ switch (tlsMaxVal) {
788
+ case "1.0":
789
+ tlsMax = "CURL_SSLVERSION_MAX_TLSv1_0";
790
+ break;
791
+ case "1.1":
792
+ tlsMax = "CURL_SSLVERSION_MAX_TLSv1_1";
793
+ break;
794
+ case "1.2":
795
+ tlsMax = "CURL_SSLVERSION_MAX_TLSv1_2";
796
+ break;
797
+ case "1.3":
798
+ tlsMax = "CURL_SSLVERSION_MAX_TLSv1_3";
799
+ break;
800
+ case "default":
801
+ tlsMax = "CURL_SSLVERSION_MAX_DEFAULT";
802
+ break;
803
+ default:
804
+ warnings.push([
805
+ "tls-max",
806
+ "unknown value for --tls-max: " + JSON.stringify(tlsMaxVal),
807
+ ]);
808
+ }
809
+ }
810
+ else {
811
+ warnings.push([
812
+ "tls-max",
813
+ "unparseable value for --tls-max: " +
814
+ JSON.stringify(request.tlsMax.toString()),
815
+ ]);
816
+ }
817
+ }
818
+ if (tlsVersion || tlsMax) {
819
+ if (!tlsVersion) {
820
+ // not really necessary since it's 0
821
+ tlsVersion = "CURL_SSLVERSION_DEFAULT";
822
+ }
823
+ code += " curl_easy_setopt(hnd, CURLOPT_SSLVERSION, (long)";
824
+ if (tlsMax) {
825
+ code += "(" + tlsVersion + " | " + tlsMax + ")";
826
+ }
827
+ else {
828
+ code += tlsVersion;
829
+ }
830
+ code += ");\n";
831
+ }
832
+ if (request.proxyTlsv1) {
833
+ code +=
834
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SSLVERSION, (long)CURL_SSLVERSION_TLSv1);\n";
835
+ }
836
+ if (request.sslAllowBeast ||
837
+ request.sslNoRevoke ||
838
+ request.sslRevokeBestEffort ||
839
+ request.caNative ||
840
+ request.sslAutoClientCert) {
841
+ const sslOptions = [];
842
+ if (request.sslAllowBeast) {
843
+ sslOptions.push("CURLSSLOPT_ALLOW_BEAST");
844
+ }
845
+ if (request.sslNoRevoke) {
846
+ sslOptions.push("CURLSSLOPT_NO_REVOKE");
847
+ }
848
+ if (request.sslRevokeBestEffort) {
849
+ sslOptions.push("CURLSSLOPT_REVOKE_BEST_EFFORT");
850
+ }
851
+ if (request.caNative) {
852
+ sslOptions.push("CURLSSLOPT_NATIVE_CA");
853
+ }
854
+ if (request.sslAutoClientCert) {
855
+ sslOptions.push("CURLSSLOPT_AUTO_CLIENT_CERT");
856
+ }
857
+ code +=
858
+ " curl_easy_setopt(hnd, CURLOPT_SSL_OPTIONS, (long)" +
859
+ (sslOptions.length > 1 ? "(" : "") +
860
+ sslOptions.join(" | ") +
861
+ (sslOptions.length > 1 ? ")" : "") +
862
+ ");\n";
863
+ }
864
+ if (request.proxySslAllowBeast ||
865
+ request.proxyCaNative ||
866
+ request.proxySslAutoClientCert) {
867
+ const sslOptions = [];
868
+ if (request.proxySslAllowBeast) {
869
+ sslOptions.push("CURLSSLOPT_ALLOW_BEAST");
870
+ }
871
+ if (request.proxyCaNative) {
872
+ sslOptions.push("CURLSSLOPT_NATIVE_CA");
873
+ }
874
+ if (request.proxySslAutoClientCert) {
875
+ sslOptions.push("CURLSSLOPT_AUTO_CLIENT_CERT");
876
+ }
877
+ code +=
878
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SSL_OPTIONS, (long)" +
879
+ sslOptions.join(" | (long)") +
880
+ ");\n";
881
+ }
882
+ if (request.pathAsIs) {
883
+ code += " curl_easy_setopt(hnd, CURLOPT_PATH_AS_IS, 1L);\n";
884
+ }
885
+ if (request.remoteTime) {
886
+ code += " curl_easy_setopt(hnd, CURLOPT_FILETIME, 1L);\n";
887
+ }
888
+ if (request.crlf) {
889
+ code += " curl_easy_setopt(hnd, CURLOPT_CRLF, 1L);\n";
890
+ }
891
+ if (request.cookieFiles) {
892
+ for (const cookieFile of request.cookieFiles) {
893
+ // TODO: why can curl set this more than once?
894
+ const cookieFile_ = repr(cookieFile, imports);
895
+ code +=
896
+ " curl_easy_setopt(hnd, CURLOPT_COOKIEFILE, " + cookieFile_ + ");\n";
897
+ }
898
+ }
899
+ if (request.cookieJar) {
900
+ const cookieJar = repr(request.cookieJar, imports);
901
+ code += " curl_easy_setopt(hnd, CURLOPT_COOKIEJAR, " + cookieJar + ");\n";
902
+ }
903
+ if (request.junkSessionCookies) {
904
+ code += " curl_easy_setopt(hnd, CURLOPT_COOKIESESSION, 1L);\n";
905
+ }
906
+ // TODO: this is more complicated.
907
+ // --head just sets CURLOPT_NOBODY and CURLOPT_FILETIME
908
+ // --upload-file just sets CURLOPT_UPLOAD and CURLOPT_INFILESIZE_LARGE
909
+ let expectedMethod = "GET";
910
+ if (request.data || request.multipartUploads) {
911
+ expectedMethod = "POST";
912
+ }
913
+ if (!eq(request.urls[0].method, expectedMethod)) {
914
+ code +=
915
+ " curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, " +
916
+ repr(request.urls[0].method, imports) +
917
+ ");\n";
918
+ }
919
+ if (request.interface) {
920
+ const interface_ = repr(request.interface, imports);
921
+ code += " curl_easy_setopt(hnd, CURLOPT_INTERFACE, " + interface_ + ");\n";
922
+ }
923
+ if (request.krb) {
924
+ const krb = repr(request.krb, imports);
925
+ code += " curl_easy_setopt(hnd, CURLOPT_KRBLEVEL, " + krb + ");\n";
926
+ }
927
+ if (request.connectTimeout) {
928
+ code +=
929
+ " curl_easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT_MS, " +
930
+ atof1000(request.connectTimeout, imports) +
931
+ ");\n";
932
+ }
933
+ if (request.dohUrl) {
934
+ const dohUrl = repr(request.dohUrl, imports);
935
+ code += " curl_easy_setopt(hnd, CURLOPT_DOH_URL, " + dohUrl + ");\n";
936
+ }
937
+ if (request.ciphers) {
938
+ const ciphers = repr(request.ciphers, imports);
939
+ code +=
940
+ " curl_easy_setopt(hnd, CURLOPT_SSL_CIPHER_LIST, " + ciphers + ");\n";
941
+ }
942
+ if (request.proxyCiphers) {
943
+ const proxyCiphers = repr(request.proxyCiphers, imports);
944
+ code +=
945
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SSL_CIPHER_LIST, " +
946
+ proxyCiphers +
947
+ ");\n";
948
+ }
949
+ if (request.tls13Ciphers) {
950
+ const tls13Ciphers = repr(request.tls13Ciphers, imports);
951
+ code +=
952
+ " curl_easy_setopt(hnd, CURLOPT_TLS13_CIPHERS, " + tls13Ciphers + ");\n";
953
+ }
954
+ if (request.proxyTls13Ciphers) {
955
+ const proxyTls13Ciphers = repr(request.proxyTls13Ciphers, imports);
956
+ code +=
957
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_TLS13_CIPHERS, " +
958
+ proxyTls13Ciphers +
959
+ ");\n";
960
+ }
961
+ if (request.verbose) {
962
+ code += " curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);\n";
963
+ }
964
+ if (request.maxFilesize) {
965
+ // TODO: parse
966
+ const maxFilesize = atol(request.maxFilesize, imports);
967
+ code +=
968
+ " curl_easy_setopt(hnd, CURLOPT_MAXFILESIZE_LARGE, (curl_off_t)" +
969
+ maxFilesize +
970
+ ");\n";
971
+ }
972
+ // TODO: these should be mutually exclusive
973
+ if (request.ipv6) {
974
+ code += " curl_easy_setopt(hnd, CURLOPT_IPRESOLVE, 2L);\n";
975
+ }
976
+ else if (request.ipv4) {
977
+ code += " curl_easy_setopt(hnd, CURLOPT_IPRESOLVE, 1L);\n";
978
+ }
979
+ if (request.sslReqd || request.ssl) {
980
+ if (request.sslReqd) {
981
+ code +=
982
+ " curl_easy_setopt(hnd, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);\n";
983
+ }
984
+ else if (request.ssl) {
985
+ code +=
986
+ " curl_easy_setopt(hnd, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);\n";
987
+ }
988
+ }
989
+ if (request.socks5GssapiNec) {
990
+ code += " curl_easy_setopt(hnd, CURLOPT_SOCKS5_GSSAPI_NEC, 1L);\n";
991
+ }
992
+ if (request.socks5Basic) {
993
+ code +=
994
+ " curl_easy_setopt(hnd, CURLOPT_SOCKS5_AUTH, (long)CURLAUTH_BASIC);\n";
995
+ }
996
+ else if (request.socks5Gssapi) {
997
+ code +=
998
+ " curl_easy_setopt(hnd, CURLOPT_SOCKS5_AUTH, (long)CURLAUTH_GSSNEGOTIATE);\n";
999
+ }
1000
+ if (request.socks5GssapiService) {
1001
+ const socks5GssapiService = repr(request.socks5GssapiService, imports);
1002
+ code +=
1003
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SERVICE_NAME, " +
1004
+ socks5GssapiService +
1005
+ ");\n";
1006
+ }
1007
+ if (request.serviceName) {
1008
+ const serviceName = repr(request.serviceName, imports);
1009
+ code +=
1010
+ " curl_easy_setopt(hnd, CURLOPT_SERVICE_NAME, " + serviceName + ");\n";
1011
+ }
1012
+ if (request.proxyServiceName) {
1013
+ const proxyServiceName = repr(request.proxyServiceName, imports);
1014
+ code +=
1015
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_SERVICE_NAME, " +
1016
+ proxyServiceName +
1017
+ ");\n";
1018
+ }
1019
+ // TODO: check
1020
+ if (request.ignoreContentLength) {
1021
+ code += " curl_easy_setopt(hnd, CURLOPT_IGNORE_CONTENT_LENGTH, 1L);\n";
1022
+ }
1023
+ if (request.ftpSkipPasvIp !== false) {
1024
+ code += " curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);\n";
1025
+ }
1026
+ if (request.keepAlive !== false) {
1027
+ code += " curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);\n";
1028
+ if (request.keepAliveTime) {
1029
+ const keepAliveTime = atol(request.keepAliveTime, imports);
1030
+ code +=
1031
+ " curl_easy_setopt(hnd, CURLOPT_TCP_KEEPIDLE, " +
1032
+ keepAliveTime +
1033
+ ");\n";
1034
+ code +=
1035
+ " curl_easy_setopt(hnd, CURLOPT_TCP_KEEPINTVL, " +
1036
+ keepAliveTime +
1037
+ ");\n";
1038
+ }
1039
+ }
1040
+ if (request.proto) {
1041
+ // TODO: parse
1042
+ const proto = repr(request.proto, imports);
1043
+ code += " curl_easy_setopt(hnd, CURLOPT_PROTOCOLS_STR, " + proto + ");\n";
1044
+ }
1045
+ if (request.protoRedir) {
1046
+ // TODO: parse
1047
+ const protoRedir = repr(request.protoRedir, imports);
1048
+ code +=
1049
+ " curl_easy_setopt(hnd, CURLOPT_REDIR_PROTOCOLS_STR, " +
1050
+ protoRedir +
1051
+ ");\n";
1052
+ }
1053
+ if (request.resolve) {
1054
+ preamble += " struct curl_slist *resolve;\n";
1055
+ if (vars) {
1056
+ vars += "\n";
1057
+ }
1058
+ vars += " resolve = NULL;\n";
1059
+ for (const line of request.resolve) {
1060
+ vars +=
1061
+ " resolve = curl_slist_append(resolve, " +
1062
+ repr(line, imports) +
1063
+ ");\n";
1064
+ }
1065
+ code += " curl_easy_setopt(hnd, CURLOPT_RESOLVE, resolve);\n";
1066
+ cleanup += " curl_slist_free_all(resolve);\n";
1067
+ cleanup += " resolve = NULL;\n";
1068
+ }
1069
+ if (request.connectTo) {
1070
+ // TODO: camelCase snake_case orjustoneword?
1071
+ preamble += " struct curl_slist *connect_to;\n";
1072
+ if (vars) {
1073
+ vars += "\n";
1074
+ }
1075
+ vars += " connect_to = NULL;\n";
1076
+ for (const line of request.connectTo) {
1077
+ vars +=
1078
+ " connect_to = curl_slist_append(connect_to, " +
1079
+ repr(line, imports) +
1080
+ ");\n";
1081
+ }
1082
+ code += " curl_easy_setopt(hnd, CURLOPT_CONNECT_TO, connect_to);\n";
1083
+ cleanup += " curl_slist_free_all(connect_to);\n";
1084
+ cleanup += " connect_to = NULL;\n";
1085
+ }
1086
+ if (request.tlsuser) {
1087
+ const tlsuser = repr(request.tlsuser, imports);
1088
+ code +=
1089
+ " curl_easy_setopt(hnd, CURLOPT_TLSAUTH_USERNAME, " + tlsuser + ");\n";
1090
+ }
1091
+ if (request.tlspassword) {
1092
+ const tlspassword = repr(request.tlspassword, imports);
1093
+ code +=
1094
+ " curl_easy_setopt(hnd, CURLOPT_TLSAUTH_PASSWORD, " +
1095
+ tlspassword +
1096
+ ");\n";
1097
+ }
1098
+ if (request.tlsauthtype) {
1099
+ const tlsauthtype = repr(request.tlsauthtype, imports);
1100
+ code +=
1101
+ " curl_easy_setopt(hnd, CURLOPT_TLSAUTH_TYPE, " + tlsauthtype + ");\n";
1102
+ }
1103
+ if (request.proxyTlsuser) {
1104
+ const proxyTlsuser = repr(request.proxyTlsuser, imports);
1105
+ code +=
1106
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_TLSAUTH_USERNAME, " +
1107
+ proxyTlsuser +
1108
+ ");\n";
1109
+ }
1110
+ if (request.proxyTlspassword) {
1111
+ const proxyTlspassword = repr(request.proxyTlspassword, imports);
1112
+ code +=
1113
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_TLSAUTH_PASSWORD, " +
1114
+ proxyTlspassword +
1115
+ ");\n";
1116
+ }
1117
+ if (request.proxyTlsauthtype) {
1118
+ const proxyTlsauthtype = repr(request.proxyTlsauthtype, imports);
1119
+ code +=
1120
+ " curl_easy_setopt(hnd, CURLOPT_PROXY_TLSAUTH_TYPE, " +
1121
+ proxyTlsauthtype +
1122
+ ");\n";
1123
+ }
1124
+ if (request.delegation) {
1125
+ if (eq(request.delegation, "always")) {
1126
+ code += " curl_easy_setopt(hnd, CURLOPT_GSSAPI_DELEGATION, 2L);\n";
1127
+ }
1128
+ else if (eq(request.delegation, "policy")) {
1129
+ code += " curl_easy_setopt(hnd, CURLOPT_GSSAPI_DELEGATION, 1L);\n";
1130
+ }
1131
+ }
1132
+ if (request.saslAuthzid) {
1133
+ const zid = repr(request.saslAuthzid, imports);
1134
+ code += " curl_easy_setopt(hnd, CURLOPT_SASL_AUTHZID, " + zid + ");\n";
1135
+ }
1136
+ if (request.saslIr) {
1137
+ code += " curl_easy_setopt(hnd, CURLOPT_SASL_IR, 1L);\n";
1138
+ }
1139
+ if (request.alpn === false) {
1140
+ code += " curl_easy_setopt(hnd, CURLOPT_SSL_ENABLE_ALPN, 0L);\n";
1141
+ }
1142
+ if (request.unixSocket) {
1143
+ const socket = repr(request.unixSocket, imports);
1144
+ code +=
1145
+ " curl_easy_setopt(hnd, CURLOPT_UNIX_SOCKET_PATH, " + socket + ");\n";
1146
+ }
1147
+ if (request.abstractUnixSocket) {
1148
+ const socket = repr(request.abstractUnixSocket, imports);
1149
+ code +=
1150
+ " curl_easy_setopt(hnd, CURLOPT_ABSTRACT_UNIX_SOCKET, " +
1151
+ socket +
1152
+ ");\n";
1153
+ }
1154
+ if (request.protoDefault) {
1155
+ const protoDefault = repr(request.protoDefault, imports);
1156
+ code +=
1157
+ " curl_easy_setopt(hnd, CURLOPT_DEFAULT_PROTOCOL, " +
1158
+ protoDefault +
1159
+ ");\n";
1160
+ }
1161
+ if (request.expect100Timeout) {
1162
+ code +=
1163
+ " curl_easy_setopt(hnd, CURLOPT_EXPECT_100_TIMEOUT_MS, " +
1164
+ atof1000(request.expect100Timeout, imports) +
1165
+ ");\n";
1166
+ }
1167
+ if (request.happyEyeballsTimeoutMs) {
1168
+ code +=
1169
+ " curl_easy_setopt(hnd, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, " +
1170
+ atol(request.happyEyeballsTimeoutMs, imports) +
1171
+ ");\n";
1172
+ }
1173
+ if (request.haproxyClientIp) {
1174
+ const haproxyClientIp = repr(request.haproxyClientIp, imports);
1175
+ code +=
1176
+ " curl_easy_setopt(hnd, CURLOPT_HAPROXY_CLIENT_IP, " +
1177
+ haproxyClientIp +
1178
+ ");\n";
1179
+ }
1180
+ if (request.haproxyProtocol) {
1181
+ code += " curl_easy_setopt(hnd, CURLOPT_HAPROXYPROTOCOL, 1L);\n";
1182
+ }
1183
+ if (request.disallowUsernameInUrl) {
1184
+ code += " curl_easy_setopt(hnd, CURLOPT_DISALLOW_USERNAME_IN_URL, 1L);\n";
1185
+ }
1186
+ if (request.altSvc) {
1187
+ const altSvc = repr(request.altSvc, imports);
1188
+ code += " curl_easy_setopt(hnd, CURLOPT_ALTSVC, " + altSvc + ");\n";
1189
+ }
1190
+ if (request.hsts) {
1191
+ // TODO: warn that files aren't read?
1192
+ const hsts = repr(request.hsts[request.hsts.length - 1], imports);
1193
+ code += " curl_easy_setopt(hnd, CURLOPT_HSTS, " + hsts + ");\n";
1194
+ }
1195
+ code += "\n";
1196
+ code += " ret = curl_easy_perform(hnd);\n";
1197
+ let end = "";
1198
+ end += " return (int)ret;\n";
1199
+ end += "}\n";
1200
+ return (preamble +
1201
+ (vars ? "\n" + vars : "") +
1202
+ "\n" +
1203
+ code +
1204
+ "\n" +
1205
+ cleanup +
1206
+ "\n" +
1207
+ end);
1208
+ }
1209
+ export function printImports(imps) {
1210
+ let s = "";
1211
+ for (const imp of Array.from(imps).sort()) {
1212
+ s += "#include <" + imp + ">\n";
1213
+ }
1214
+ return s;
1215
+ }
1216
+ export function _toC(requests, warnings = []) {
1217
+ const imports = new Set(["curl/curl.h"]);
1218
+ const request = getFirst(requests, warnings, { cookieFiles: true });
1219
+ const code = requestToC(request, warnings, imports);
1220
+ return printImports(imports) + "\n" + code;
1221
+ }
1222
+ export function toCWarn(curlCommand, warnings = []) {
1223
+ const requests = parse(curlCommand, supportedArgs, warnings);
1224
+ const c = _toC(requests, warnings);
1225
+ return [c, warnings];
1226
+ }
1227
+ export function toC(curlCommand) {
1228
+ return toCWarn(curlCommand)[0];
1229
+ }
1230
+ //# sourceMappingURL=c.js.map