forked from curlconverter/curlconverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
1051 lines (1011 loc) · 39.1 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import URL from 'url'
import cookie from 'cookie'
import nunjucks from 'nunjucks'
import querystring from 'query-string'
import parser from './parser.js'
const env = nunjucks.configure(['templates/'], { // set folders with templates
autoescape: false
})
env.addFilter('isArr', something => Array.isArray(something))
env.addFilter('isString', something => typeof something === 'string')
const has = (obj, prop) => {
return Object.prototype.hasOwnProperty.call(obj, prop)
}
const pushProp = (obj, prop, value) => {
if (!has(obj, prop)) {
obj[prop] = []
}
obj[prop].push(value)
return obj
}
// BEGIN GENERATED CURL OPTIONS
const curlLongOpts = {
url: { type: 'string' },
'dns-ipv4-addr': { type: 'string' },
'dns-ipv6-addr': { type: 'string' },
'random-file': { type: 'string' },
'egd-file': { type: 'string' },
'oauth2-bearer': { type: 'string' },
'connect-timeout': { type: 'string' },
'doh-url': { type: 'string' },
ciphers: { type: 'string' },
'dns-interface': { type: 'string' },
'disable-epsv': { type: 'bool', name: 'epsv' },
'no-disable-epsv': { type: 'bool', name: 'epsv', expand: false },
'disallow-username-in-url': { type: 'bool' },
'no-disallow-username-in-url': { type: 'bool', name: 'disallow-username-in-url', expand: false },
epsv: { type: 'bool' },
'no-epsv': { type: 'bool', name: 'epsv', expand: false },
'dns-servers': { type: 'string' },
trace: { type: 'string' },
npn: { type: 'bool' },
'no-npn': { type: 'bool', name: 'npn', expand: false },
'trace-ascii': { type: 'string' },
alpn: { type: 'bool' },
'no-alpn': { type: 'bool', name: 'alpn', expand: false },
'limit-rate': { type: 'string' },
compressed: { type: 'bool' },
'no-compressed': { type: 'bool', name: 'compressed', expand: false },
'tr-encoding': { type: 'bool' },
'no-tr-encoding': { type: 'bool', name: 'tr-encoding', expand: false },
digest: { type: 'bool' },
'no-digest': { type: 'bool', name: 'digest', expand: false },
negotiate: { type: 'bool' },
'no-negotiate': { type: 'bool', name: 'negotiate', expand: false },
ntlm: { type: 'bool' },
'no-ntlm': { type: 'bool', name: 'ntlm', expand: false },
'ntlm-wb': { type: 'bool' },
'no-ntlm-wb': { type: 'bool', name: 'ntlm-wb', expand: false },
basic: { type: 'bool' },
'no-basic': { type: 'bool', name: 'basic', expand: false },
anyauth: { type: 'bool' },
'no-anyauth': { type: 'bool', name: 'anyauth', expand: false },
wdebug: { type: 'bool' },
'no-wdebug': { type: 'bool', name: 'wdebug', expand: false },
'ftp-create-dirs': { type: 'bool' },
'no-ftp-create-dirs': { type: 'bool', name: 'ftp-create-dirs', expand: false },
'create-dirs': { type: 'bool' },
'no-create-dirs': { type: 'bool', name: 'create-dirs', expand: false },
'create-file-mode': { type: 'string' },
'max-redirs': { type: 'string' },
'proxy-ntlm': { type: 'bool' },
'no-proxy-ntlm': { type: 'bool', name: 'proxy-ntlm', expand: false },
crlf: { type: 'bool' },
'no-crlf': { type: 'bool', name: 'crlf', expand: false },
stderr: { type: 'string' },
'aws-sigv4': { type: 'string' },
interface: { type: 'string' },
krb: { type: 'string' },
krb4: { type: 'string', name: 'krb' },
'haproxy-protocol': { type: 'bool' },
'no-haproxy-protocol': { type: 'bool', name: 'haproxy-protocol', expand: false },
'max-filesize': { type: 'string' },
'disable-eprt': { type: 'bool', name: 'eprt' },
'no-disable-eprt': { type: 'bool', name: 'eprt', expand: false },
eprt: { type: 'bool' },
'no-eprt': { type: 'bool', name: 'eprt', expand: false },
xattr: { type: 'bool' },
'no-xattr': { type: 'bool', name: 'xattr', expand: false },
'ftp-ssl': { type: 'bool', name: 'ssl' },
'no-ftp-ssl': { type: 'bool', name: 'ssl', expand: false },
ssl: { type: 'bool' },
'no-ssl': { type: 'bool', name: 'ssl', expand: false },
'ftp-pasv': { type: 'bool' },
'no-ftp-pasv': { type: 'bool', name: 'ftp-pasv', expand: false },
socks5: { type: 'string' },
'tcp-nodelay': { type: 'bool' },
'no-tcp-nodelay': { type: 'bool', name: 'tcp-nodelay', expand: false },
'proxy-digest': { type: 'bool' },
'no-proxy-digest': { type: 'bool', name: 'proxy-digest', expand: false },
'proxy-basic': { type: 'bool' },
'no-proxy-basic': { type: 'bool', name: 'proxy-basic', expand: false },
retry: { type: 'string' },
'retry-connrefused': { type: 'bool' },
'no-retry-connrefused': { type: 'bool', name: 'retry-connrefused', expand: false },
'retry-delay': { type: 'string' },
'retry-max-time': { type: 'string' },
'proxy-negotiate': { type: 'bool' },
'no-proxy-negotiate': { type: 'bool', name: 'proxy-negotiate', expand: false },
'ftp-account': { type: 'string' },
'proxy-anyauth': { type: 'bool' },
'no-proxy-anyauth': { type: 'bool', name: 'proxy-anyauth', expand: false },
'trace-time': { type: 'bool' },
'no-trace-time': { type: 'bool', name: 'trace-time', expand: false },
'ignore-content-length': { type: 'bool' },
'no-ignore-content-length': { type: 'bool', name: 'ignore-content-length', expand: false },
'ftp-skip-pasv-ip': { type: 'bool' },
'no-ftp-skip-pasv-ip': { type: 'bool', name: 'ftp-skip-pasv-ip', expand: false },
'ftp-method': { type: 'string' },
'local-port': { type: 'string' },
socks4: { type: 'string' },
socks4a: { type: 'string' },
'ftp-alternative-to-user': { type: 'string' },
'ftp-ssl-reqd': { type: 'bool', name: 'ssl-reqd' },
'no-ftp-ssl-reqd': { type: 'bool', name: 'ssl-reqd', expand: false },
'ssl-reqd': { type: 'bool' },
'no-ssl-reqd': { type: 'bool', name: 'ssl-reqd', expand: false },
sessionid: { type: 'bool' },
'no-sessionid': { type: 'bool', name: 'sessionid', expand: false },
'ftp-ssl-control': { type: 'bool' },
'no-ftp-ssl-control': { type: 'bool', name: 'ftp-ssl-control', expand: false },
'ftp-ssl-ccc': { type: 'bool' },
'no-ftp-ssl-ccc': { type: 'bool', name: 'ftp-ssl-ccc', expand: false },
'ftp-ssl-ccc-mode': { type: 'string' },
libcurl: { type: 'string' },
raw: { type: 'bool' },
'no-raw': { type: 'bool', name: 'raw', expand: false },
post301: { type: 'bool' },
'no-post301': { type: 'bool', name: 'post301', expand: false },
keepalive: { type: 'bool' },
'no-keepalive': { type: 'bool', name: 'keepalive', expand: false },
'socks5-hostname': { type: 'string' },
'keepalive-time': { type: 'string' },
post302: { type: 'bool' },
'no-post302': { type: 'bool', name: 'post302', expand: false },
noproxy: { type: 'string' },
'socks5-gssapi-nec': { type: 'bool' },
'no-socks5-gssapi-nec': { type: 'bool', name: 'socks5-gssapi-nec', expand: false },
'proxy1.0': { type: 'string' },
'tftp-blksize': { type: 'string' },
'mail-from': { type: 'string' },
'mail-rcpt': { type: 'string' },
'ftp-pret': { type: 'bool' },
'no-ftp-pret': { type: 'bool', name: 'ftp-pret', expand: false },
proto: { type: 'string' },
'proto-redir': { type: 'string' },
resolve: { type: 'string' },
delegation: { type: 'string' },
'mail-auth': { type: 'string' },
post303: { type: 'bool' },
'no-post303': { type: 'bool', name: 'post303', expand: false },
metalink: { type: 'bool' },
'no-metalink': { type: 'bool', name: 'metalink', expand: false },
'sasl-authzid': { type: 'string' },
'sasl-ir': { type: 'bool' },
'no-sasl-ir': { type: 'bool', name: 'sasl-ir', expand: false },
'test-event': { type: 'bool' },
'no-test-event': { type: 'bool', name: 'test-event', expand: false },
'unix-socket': { type: 'string' },
'path-as-is': { type: 'bool' },
'no-path-as-is': { type: 'bool', name: 'path-as-is', expand: false },
'socks5-gssapi-service': { type: 'string', name: 'proxy-service-name' },
'proxy-service-name': { type: 'string' },
'service-name': { type: 'string' },
'proto-default': { type: 'string' },
'expect100-timeout': { type: 'string' },
'tftp-no-options': { type: 'bool' },
'no-tftp-no-options': { type: 'bool', name: 'tftp-no-options', expand: false },
'connect-to': { type: 'string' },
'abstract-unix-socket': { type: 'string' },
'tls-max': { type: 'string' },
'suppress-connect-headers': { type: 'bool' },
'no-suppress-connect-headers': { type: 'bool', name: 'suppress-connect-headers', expand: false },
'compressed-ssh': { type: 'bool' },
'no-compressed-ssh': { type: 'bool', name: 'compressed-ssh', expand: false },
'happy-eyeballs-timeout-ms': { type: 'string' },
'retry-all-errors': { type: 'bool' },
'no-retry-all-errors': { type: 'bool', name: 'retry-all-errors', expand: false },
'http1.0': { type: 'bool' },
'http1.1': { type: 'bool' },
http2: { type: 'bool' },
'http2-prior-knowledge': { type: 'bool' },
http3: { type: 'bool' },
'http0.9': { type: 'bool' },
'no-http0.9': { type: 'bool', name: 'http0.9', expand: false },
tlsv1: { type: 'bool' },
'tlsv1.0': { type: 'bool' },
'tlsv1.1': { type: 'bool' },
'tlsv1.2': { type: 'bool' },
'tlsv1.3': { type: 'bool' },
'tls13-ciphers': { type: 'string' },
'proxy-tls13-ciphers': { type: 'string' },
sslv2: { type: 'bool' },
sslv3: { type: 'bool' },
ipv4: { type: 'bool' },
ipv6: { type: 'bool' },
append: { type: 'bool' },
'no-append': { type: 'bool', name: 'append', expand: false },
'user-agent': { type: 'string' },
cookie: { type: 'string' },
'alt-svc': { type: 'string' },
hsts: { type: 'string' },
'use-ascii': { type: 'bool' },
'no-use-ascii': { type: 'bool', name: 'use-ascii', expand: false },
'cookie-jar': { type: 'string' },
'continue-at': { type: 'string' },
data: { type: 'string' },
'data-raw': { type: 'string' },
'data-ascii': { type: 'string' },
'data-binary': { type: 'string' },
'data-urlencode': { type: 'string' },
'dump-header': { type: 'string' },
referer: { type: 'string' },
cert: { type: 'string' },
cacert: { type: 'string' },
'cert-type': { type: 'string' },
key: { type: 'string' },
'key-type': { type: 'string' },
pass: { type: 'string' },
engine: { type: 'string' },
capath: { type: 'string' },
pubkey: { type: 'string' },
hostpubmd5: { type: 'string' },
crlfile: { type: 'string' },
tlsuser: { type: 'string' },
tlspassword: { type: 'string' },
tlsauthtype: { type: 'string' },
'ssl-allow-beast': { type: 'bool' },
'no-ssl-allow-beast': { type: 'bool', name: 'ssl-allow-beast', expand: false },
'ssl-auto-client-cert': { type: 'bool' },
'no-ssl-auto-client-cert': { type: 'bool', name: 'ssl-auto-client-cert', expand: false },
'proxy-ssl-auto-client-cert': { type: 'bool' },
'no-proxy-ssl-auto-client-cert': { type: 'bool', name: 'proxy-ssl-auto-client-cert', expand: false },
pinnedpubkey: { type: 'string' },
'proxy-pinnedpubkey': { type: 'string' },
'cert-status': { type: 'bool' },
'no-cert-status': { type: 'bool', name: 'cert-status', expand: false },
'doh-cert-status': { type: 'bool' },
'no-doh-cert-status': { type: 'bool', name: 'doh-cert-status', expand: false },
'false-start': { type: 'bool' },
'no-false-start': { type: 'bool', name: 'false-start', expand: false },
'ssl-no-revoke': { type: 'bool' },
'no-ssl-no-revoke': { type: 'bool', name: 'ssl-no-revoke', expand: false },
'ssl-revoke-best-effort': { type: 'bool' },
'no-ssl-revoke-best-effort': { type: 'bool', name: 'ssl-revoke-best-effort', expand: false },
'tcp-fastopen': { type: 'bool' },
'no-tcp-fastopen': { type: 'bool', name: 'tcp-fastopen', expand: false },
'proxy-tlsuser': { type: 'string' },
'proxy-tlspassword': { type: 'string' },
'proxy-tlsauthtype': { type: 'string' },
'proxy-cert': { type: 'string' },
'proxy-cert-type': { type: 'string' },
'proxy-key': { type: 'string' },
'proxy-key-type': { type: 'string' },
'proxy-pass': { type: 'string' },
'proxy-ciphers': { type: 'string' },
'proxy-crlfile': { type: 'string' },
'proxy-ssl-allow-beast': { type: 'bool' },
'no-proxy-ssl-allow-beast': { type: 'bool', name: 'proxy-ssl-allow-beast', expand: false },
'login-options': { type: 'string' },
'proxy-cacert': { type: 'string' },
'proxy-capath': { type: 'string' },
'proxy-insecure': { type: 'bool' },
'no-proxy-insecure': { type: 'bool', name: 'proxy-insecure', expand: false },
'proxy-tlsv1': { type: 'bool' },
'socks5-basic': { type: 'bool' },
'no-socks5-basic': { type: 'bool', name: 'socks5-basic', expand: false },
'socks5-gssapi': { type: 'bool' },
'no-socks5-gssapi': { type: 'bool', name: 'socks5-gssapi', expand: false },
'etag-save': { type: 'string' },
'etag-compare': { type: 'string' },
curves: { type: 'string' },
fail: { type: 'bool' },
'no-fail': { type: 'bool', name: 'fail', expand: false },
'fail-early': { type: 'bool' },
'no-fail-early': { type: 'bool', name: 'fail-early', expand: false },
'styled-output': { type: 'bool' },
'no-styled-output': { type: 'bool', name: 'styled-output', expand: false },
'mail-rcpt-allowfails': { type: 'bool' },
'no-mail-rcpt-allowfails': { type: 'bool', name: 'mail-rcpt-allowfails', expand: false },
'fail-with-body': { type: 'bool' },
'no-fail-with-body': { type: 'bool', name: 'fail-with-body', expand: false },
form: { type: 'string' },
'form-string': { type: 'string' },
globoff: { type: 'bool' },
'no-globoff': { type: 'bool', name: 'globoff', expand: false },
get: { type: 'bool' },
'request-target': { type: 'string' },
help: { type: 'bool' },
'no-help': { type: 'bool', name: 'help', expand: false },
header: { type: 'string' },
'proxy-header': { type: 'string' },
include: { type: 'bool' },
'no-include': { type: 'bool', name: 'include', expand: false },
head: { type: 'bool' },
'no-head': { type: 'bool', name: 'head', expand: false },
'junk-session-cookies': { type: 'bool' },
'no-junk-session-cookies': { type: 'bool', name: 'junk-session-cookies', expand: false },
'remote-header-name': { type: 'bool' },
'no-remote-header-name': { type: 'bool', name: 'remote-header-name', expand: false },
insecure: { type: 'bool' },
'no-insecure': { type: 'bool', name: 'insecure', expand: false },
'doh-insecure': { type: 'bool' },
'no-doh-insecure': { type: 'bool', name: 'doh-insecure', expand: false },
config: { type: 'string' },
'list-only': { type: 'bool' },
'no-list-only': { type: 'bool', name: 'list-only', expand: false },
location: { type: 'bool' },
'no-location': { type: 'bool', name: 'location', expand: false },
'location-trusted': { type: 'bool' },
'no-location-trusted': { type: 'bool', name: 'location-trusted', expand: false },
'max-time': { type: 'string' },
manual: { type: 'bool' },
'no-manual': { type: 'bool', name: 'manual', expand: false },
netrc: { type: 'bool' },
'no-netrc': { type: 'bool', name: 'netrc', expand: false },
'netrc-optional': { type: 'bool' },
'no-netrc-optional': { type: 'bool', name: 'netrc-optional', expand: false },
'netrc-file': { type: 'string' },
buffer: { type: 'bool' },
'no-buffer': { type: 'bool', name: 'buffer', expand: false },
output: { type: 'string' },
'remote-name': { type: 'bool' },
'remote-name-all': { type: 'bool' },
'no-remote-name-all': { type: 'bool', name: 'remote-name-all', expand: false },
'output-dir': { type: 'string' },
proxytunnel: { type: 'bool' },
'no-proxytunnel': { type: 'bool', name: 'proxytunnel', expand: false },
'ftp-port': { type: 'string' },
disable: { type: 'bool' },
'no-disable': { type: 'bool', name: 'disable', expand: false },
quote: { type: 'string' },
range: { type: 'string' },
'remote-time': { type: 'bool' },
'no-remote-time': { type: 'bool', name: 'remote-time', expand: false },
silent: { type: 'bool' },
'no-silent': { type: 'bool', name: 'silent', expand: false },
'show-error': { type: 'bool' },
'no-show-error': { type: 'bool', name: 'show-error', expand: false },
'telnet-option': { type: 'string' },
'upload-file': { type: 'string' },
user: { type: 'string' },
'proxy-user': { type: 'string' },
verbose: { type: 'bool' },
'no-verbose': { type: 'bool', name: 'verbose', expand: false },
version: { type: 'bool' },
'no-version': { type: 'bool', name: 'version', expand: false },
'write-out': { type: 'string' },
proxy: { type: 'string' },
preproxy: { type: 'string' },
request: { type: 'string' },
'speed-limit': { type: 'string' },
'speed-time': { type: 'string' },
'time-cond': { type: 'string' },
parallel: { type: 'bool' },
'no-parallel': { type: 'bool', name: 'parallel', expand: false },
'parallel-max': { type: 'string' },
'parallel-immediate': { type: 'bool' },
'no-parallel-immediate': { type: 'bool', name: 'parallel-immediate', expand: false },
'progress-bar': { type: 'bool' },
'no-progress-bar': { type: 'bool', name: 'progress-bar', expand: false },
'progress-meter': { type: 'bool' },
'no-progress-meter': { type: 'bool', name: 'progress-meter', expand: false },
next: { type: 'bool' }
}
const curlShortOpts = {
0: 'http1.0',
1: 'tlsv1',
2: 'sslv2',
3: 'sslv3',
4: 'ipv4',
6: 'ipv6',
a: 'append',
A: 'user-agent',
b: 'cookie',
B: 'use-ascii',
c: 'cookie-jar',
C: 'continue-at',
d: 'data',
D: 'dump-header',
e: 'referer',
E: 'cert',
f: 'fail',
F: 'form',
g: 'globoff',
G: 'get',
h: 'help',
H: 'header',
i: 'include',
I: 'head',
j: 'junk-session-cookies',
J: 'remote-header-name',
k: 'insecure',
K: 'config',
l: 'list-only',
L: 'location',
m: 'max-time',
M: 'manual',
n: 'netrc',
N: 'no-buffer',
o: 'output',
O: 'remote-name',
p: 'proxytunnel',
P: 'ftp-port',
q: 'disable',
Q: 'quote',
r: 'range',
R: 'remote-time',
s: 'silent',
S: 'show-error',
t: 'telnet-option',
T: 'upload-file',
u: 'user',
U: 'proxy-user',
v: 'verbose',
V: 'version',
w: 'write-out',
x: 'proxy',
X: 'request',
Y: 'speed-limit',
y: 'speed-time',
z: 'time-cond',
Z: 'parallel',
'#': 'progress-bar',
':': 'next'
}
// END GENERATED CURL OPTIONS
// These are options that curl used to have.
// Those that don't conflict with the current options are supported by curlconverter.
// TODO: curl's --long-options can be shortened.
// For example if curl used to only have a single option, "--blah" then
// "--bla" "--bl" and "--b" all used to be valid options as well. If later
// "--blaz" was added, suddenly those 3 shortened options are removed (because
// they are now ambiguous).
// https://github.com/curlconverter/curlconverter/pull/280#issuecomment-931241328
const removedLongOpts = {
'ftp-ascii': { type: 'bool', name: 'use-ascii', removed: '7.10.7' },
port: { type: 'string', removed: '7.3' },
upload: { type: 'bool', removed: '7.7' },
continue: { type: 'bool', removed: '7.9' },
'3p-url': { type: 'string', removed: '7.16.0' },
'3p-user': { type: 'string', removed: '7.16.0' },
'3p-quote': { type: 'string', removed: '7.16.0' },
'http2.0': { type: 'bool', name: 'http2', removed: '7.36.0' },
'no-http2.0': { type: 'bool', name: 'http2', removed: '7.36.0' },
'telnet-options': { type: 'string', name: 'telnet-option', removed: '7.49.0' },
'http-request': { type: 'string', name: 'request', removed: '7.49.0' },
socks: { type: 'string', name: 'socks5', removed: '7.49.0' },
'capath ': { type: 'string', name: 'capath', removed: '7.49.0' }, // trailing space
ftpport: { type: 'string', name: 'ftp-port', removed: '7.49.0' },
environment: { type: 'bool', removed: '7.54.1' },
// These --no-<option> flags were automatically generated and never had any effect
'no-tlsv1': { type: 'bool', name: 'tlsv1', removed: '7.54.1' },
'no-tlsv1.2': { type: 'bool', name: 'tlsv1.2', removed: '7.54.1' },
'no-http2-prior-knowledge': { type: 'bool', name: 'http2-prior-knowledge', removed: '7.54.1' },
'no-ipv6': { type: 'bool', name: 'ipv6', removed: '7.54.1' },
'no-ipv4': { type: 'bool', name: 'ipv4', removed: '7.54.1' },
'no-sslv2': { type: 'bool', name: 'sslv2', removed: '7.54.1' },
'no-tlsv1.0': { type: 'bool', name: 'tlsv1.0', removed: '7.54.1' },
'no-tlsv1.1': { type: 'bool', name: 'tlsv1.1', removed: '7.54.1' },
'no-remote-name': { type: 'bool', name: 'remote-name', removed: '7.54.1' },
'no-sslv3': { type: 'bool', name: 'sslv3', removed: '7.54.1' },
'no-get': { type: 'bool', name: 'get', removed: '7.54.1' },
'no-http1.0': { type: 'bool', name: 'http1.0', removed: '7.54.1' },
'no-next': { type: 'bool', name: 'next', removed: '7.54.1' },
'no-tlsv1.3': { type: 'bool', name: 'tlsv1.3', removed: '7.54.1' },
'no-environment': { type: 'bool', name: 'environment', removed: '7.54.1' },
'no-http1.1': { type: 'bool', name: 'http1.1', removed: '7.54.1' },
'no-proxy-tlsv1': { type: 'bool', name: 'proxy-tlsv1', removed: '7.54.1' },
'no-http2': { type: 'bool', name: 'http2', removed: '7.54.1' }
}
for (const [opt, val] of Object.entries(removedLongOpts)) {
if (!has(val, 'name')) {
val.name = opt
}
}
// TODO: use this to warn users when they specify a short option that
// used to be for something else?
const changedShortOpts = {
p: 'used to be short for --port <port> (a since-deleted flag) until curl 7.3',
// TODO: some of these might be renamed options
t: 'used to be short for --upload (a since-deleted boolean flag) until curl 7.7',
c: 'used to be short for --continue (a since-deleted boolean flag) until curl 7.9',
// TODO: did -@ actually work?
'@': 'used to be short for --create-dirs until curl 7.10.7',
Z: 'used to be short for --max-redirs <num> until curl 7.10.7',
9: 'used to be short for --crlf until curl 7.10.8',
8: 'used to be short for --stderr <file> until curl 7.10.8',
7: 'used to be short for --interface <name> until curl 7.10.8',
6: 'used to be short for --krb <level> (which itself used to be --krb4 <level>) until curl 7.10.8',
// TODO: did these short options ever actually work?
5: 'used to be another way to specify the url until curl 7.10.8',
'*': 'used to be another way to specify the url until curl 7.49.0',
'~': 'used to be short for --xattr until curl 7.49.0'
}
// These options can be specified more than once, they
// are always returned as a list.
// Normally, if you specify some option more than once,
// curl will just take the last one.
// TODO: extract this from curl's source code?
const canBeList = new Set([
// TODO: unlike curl, we don't support multiple
// URLs and just take the last one.
'url',
'header', 'proxy-header',
'form',
'data', 'data-binary', 'data-ascii', 'data-raw', 'data-urlencode',
'mail-rcpt',
'resolve',
'connect-to',
// TODO: support multiple cookies
// https://github.com/curlconverter/curlconverter/issues/161
// 'cookie',
'quote',
'telnet-option'
])
const shortened = {}
for (const [opt, val] of Object.entries(curlLongOpts)) {
if (!has(val, 'name')) {
val.name = opt
}
// curl lets you not type the full argument as long as it's unambiguous.
// So --sil instead of --silent is okay, --s is not.
// This doesn't apply to options starting with --no-
// Default 'expand' to true if not specified
const shouldExpand = !has(val, 'expand') || val.expand
delete val.expand
if (shouldExpand) {
for (let i = 1; i < opt.length; i++) {
const shortenedOpt = opt.slice(0, i)
pushProp(shortened, shortenedOpt, val)
}
}
}
for (const [shortenedOpt, vals] of Object.entries(shortened)) {
if (!has(curlLongOpts, shortenedOpt)) {
if (vals.length === 1) {
curlLongOpts[shortenedOpt] = vals[0]
} else if (vals.length > 1) {
// More than one option shortens to this, it's ambiguous
curlLongOpts[shortenedOpt] = null
}
}
}
for (const [removedOpt, val] of Object.entries(removedLongOpts)) {
if (!has(curlLongOpts, removedOpt)) {
curlLongOpts[removedOpt] = val
} else if (curlLongOpts[removedOpt] === null) {
// This happens with --socks because it became --socks5 and there are multiple options
// that start with "--socks"
// console.error("couldn't add removed option --" + removedOpt + " to curlLongOpts because it's already ambiguous")
// TODO: do we want to do this?
// curlLongOpts[removedOpt] = val
} else {
// Almost certainly a shortened form of a still-existing option
// This happens with --continue (now short for --continue-at)
// and --upload (now short for --upload-file)
// console.error("couldn't add removed option --" + removedOpt + ' to curlLongOpts because it already exists')
}
}
const toBoolean = opt => {
if (opt.startsWith('no-disable-')) {
return true
}
if (opt.startsWith('disable-') || opt.startsWith('no-')) {
return false
}
return true
}
// NOTE: this bash string parsing is probably not entirely correct.
// We get the text as it appears in the bash source code,
// which might have escaped newlines (if the string spans
// multiple lines) and escaped quotes and maybe other
// things I don't know about.
const parseSingleQuoteString = (str) => {
const BACKSLASHES = /\\(\n|')/gs
const unescapeChar = (m) => m.charAt(1) === '\n' ? '' : m.charAt(1)
return str.slice(1, -1).replace(BACKSLASHES, unescapeChar)
}
const parseDoubleQuoteString = (str) => {
const BACKSLASHES = /\\(\n|\\|")/gs
const unescapeChar = (m) => m.charAt(1) === '\n' ? '' : m.charAt(1)
return str.slice(1, -1).replace(BACKSLASHES, unescapeChar)
}
// ANSI-C quoted strings look $'like this'.
// Not all shells have them but bash does
// https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
//
// https://git.savannah.gnu.org/cgit/bash.git/tree/lib/sh/strtrans.c
const parseAnsiCString = (str) => {
const ANSI_BACKSLASHES = /\\(\\|a|b|e|E|f|n|r|t|v|'|"|\?|[0-7]{1,3}|x[0-9A-Fa-f]{1,2}|u[0-9A-Fa-f]{1,4}|U[0-9A-Fa-f]{1,8}|c.)/gs
const unescapeChar = (m) => {
switch (m.charAt(1)) {
case '\\':
return '\\'
case 'a':
return '\a' // eslint-disable-line
case 'b':
return '\b'
case 'e':
case 'E':
return '\x1B'
case 'f':
return '\f'
case 'n':
return '\n'
case 'r':
return '\r'
case 't':
return '\t'
case 'v':
return '\v'
case "'":
return "'"
case '"':
return '"'
case '?':
return '?'
case 'c':
// bash handles all characters by considering the first byte
// of its UTF-8 input and can produce invalid UTF-8, whereas
// JavaScript stores strings in UTF-16
if (m.codePointAt(2) > 127) {
throw Error("non-ASCII control character in ANSI-C quoted string: '\\u{" + m.codePointAt(2).toString(16) + "}'")
}
// If this produces a 0x00 (null) character, it will cause bash to
// terminate the string at that character, but we return the null
// character in the result.
return m[2] === '?' ? '\x7F' : String.fromCodePoint(m[2].toUpperCase().codePointAt(0) & 0b00011111)
case 'x':
case 'u':
case 'U':
// Hexadecimal character literal
// Unlike bash, this will error if the the code point is greater than 10FFFF
return String.fromCodePoint(parseInt(m.slice(2), 16))
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
// Octal character literal
return String.fromCodePoint(parseInt(m.slice(1), 8) % 256)
default:
// There must be a mis-match between ANSI_BACKSLASHES and the switch statement
throw Error('unhandled character in ANSI-C escape code: ' + JSON.stringify(m))
}
}
return str.slice(2, -1).replace(ANSI_BACKSLASHES, unescapeChar)
}
const tokenizeBashStr = (curlCommand) => {
const curlArgs = parser.parse(curlCommand)
// The AST must be in a nice format, i.e.
// (program
// (command
// name: (command_name (word))
// argument+: (
// word |
// string ('') |
// raw_string ("") |
// ansii_c_string ($'') |
// simple_expansion (variable_name))))
//
// TODO: support strings with variable expansions inside
// TODO: support prefixed variables, e.g. "MY_VAR=hello curl example.com"
// TODO: get only named children?
if (curlArgs.rootNode.type !== 'program') {
// TODO: better error message.
throw "expected a 'program' AST node, got " + curlArgs.rootNode.type + ' instead'
}
if (curlArgs.rootNode.childCount < 1) {
// TODO: better error message.
throw 'empty "program" node'
} else if (curlArgs.rootNode.childCount > 1) {
// TODO: warn that everything after the first "command" node is ignored
} else if (curlArgs.rootNode.firstChild.type !== 'command') {
// TODO: better error message.
throw "expected a 'command' AST node, got " + curlArgs.rootNode.firstChild.type + ' instead'
}
const command = curlArgs.rootNode.firstChild
if (command.childCount < 1) {
// TODO: better error message.
throw 'empty "command" node'
}
// TODO: add childrenForFieldName to tree-sitter node/web bindings and then
// use that here instead
// TODO: you can have variable_assignment before the actual command
// MY_VAR=foo curl example.com
const [cmdName, ...args] = command.children
if (cmdName.type !== 'command_name') {
// TODO: better error message.
throw "expected a 'command_name' AST node, got " + cmdName.type + ' instead'
}
const toVal = (node) => {
switch (node.type) {
case 'word':
return node.text
case 'string':
return parseDoubleQuoteString(node.text)
case 'raw_string':
return parseSingleQuoteString(node.text)
case 'ansii_c_string':
return parseAnsiCString(node.text)
case 'simple_expansion':
return node.text // TODO: handle variables properly downstream
case 'concatenation':
// item[]=1 turns into item=1 if we don't do this
// https://github.com/tree-sitter/tree-sitter-bash/issues/104
if (node.children.every(n => n.type === 'word')) {
return node.text
}
return node.children.map(toVal).join('')
default:
// console.error(curlCommand)
// console.error(curlArgs.rootNode.toString())
throw 'unexpected argument type ' + JSON.stringify(node.type) + '. Must be one of "word", "string", "raw_string", "ascii_c_string", "simple_expansion" or "concatenation"'
}
}
return [cmdName.text.trim(), ...args.map(toVal)]
}
const parseArgs = (args, opts) => {
const [longOpts, shortOpts] = opts || [curlLongOpts, curlShortOpts]
const parsedArguments = {}
for (let i = 0, stillflags = true; i < args.length; i++) {
const arg = args[i]
if (stillflags && arg.startsWith('-')) {
if (arg === '--') {
/* This indicates the end of the flags and thus enables the
following (URL) argument to start with -. */
stillflags = false
} else if (arg.startsWith('--')) {
const longArg = longOpts[arg.slice(2)]
if (longArg === null) {
throw 'option ' + arg + ': is ambiguous'
}
if (typeof longArg === 'undefined') {
// TODO: extract a list of deleted arguments to check here
throw 'option ' + arg + ': is unknown'
}
if (longArg.type === 'string') {
if (i + 1 < args.length) {
i++
pushProp(parsedArguments, longArg.name, args[i])
} else {
throw 'option ' + arg + ': requires parameter'
}
} else {
parsedArguments[longArg.name] = toBoolean(arg.slice(2)) // TODO: all shortened args work correctly?
}
} else {
// Short option. These can look like
// -X POST -> {request: 'POST'}
// or
// -XPOST -> {request: 'POST'}
// or multiple options
// -ABCX POST
// -> {A: true, B: true, C: true, request: 'POST'}
// or multiple options and a value for the last one
// -ABCXPOST
// -> {A: true, B: true, C: true, request: 'POST'}
// "-" on its own raises error
if (arg.length === 1) {
throw 'option ' + arg + ': is unknown'
}
for (let j = 1; j < arg.length; j++) {
if (!has(shortOpts, arg[j])) {
if (has(changedShortOpts, arg[j])) {
throw 'option ' + arg + ': ' + changedShortOpts[arg[j]]
}
// TODO: there are a few deleted short options we could report
throw 'option ' + arg + ': is unknown'
}
const shortFor = shortOpts[arg[j]]
const longArg = longOpts[shortFor]
if (longArg.type === 'string') {
let val
if (j + 1 < arg.length) {
// treat -XPOST as -X POST
val = arg.slice(j + 1)
j = arg.length
} else if (i + 1 < args.length) {
i++
val = args[i]
} else {
throw 'option ' + arg + ': requires parameter'
}
pushProp(parsedArguments, longArg.name, val)
} else {
// Use shortFor because -N is short for --no-buffer
// and we want to end up with {buffer: false}
parsedArguments[longArg.name] = toBoolean(shortFor)
}
}
}
} else {
pushProp(parsedArguments, 'url', arg)
}
}
for (const [arg, values] of Object.entries(parsedArguments)) {
if (Array.isArray(values) && !canBeList.has(arg)) {
parsedArguments[arg] = values[values.length - 1]
}
}
return parsedArguments
}
const buildRequest = parsedArguments => {
// TODO: handle multiple URLs
if (!parsedArguments.url || !parsedArguments.url.length) {
// TODO: better error message (could be parsing fail)
throw 'no URL specified!'
}
let url = parsedArguments.url[parsedArguments.url.length - 1]
let headers
let cookieString
if (parsedArguments.header) {
if (!headers) {
headers = {}
}
parsedArguments.header.forEach(header => {
if (header.indexOf('Cookie') !== -1) {
cookieString = header
} else {
const components = header.split(/:(.*)/s)
if (components[1]) {
headers[components[0]] = components[1].trim()
}
}
})
}
if (parsedArguments['user-agent']) {
if (!headers) {
headers = {}
}
headers['User-Agent'] = parsedArguments['user-agent']
}
if (parsedArguments.cookie) {
cookieString = parsedArguments.cookie
}
let multipartUploads
if (parsedArguments.form) {
multipartUploads = {}
parsedArguments.form.forEach(multipartArgument => {
// input looks like key=value. value could be json or a file path prepended with an @
// TODO: what if multipartArgument is empty string?
// TODO: if string has more than one '=', this throws away data
const [key, value] = multipartArgument.split('=', 2)
multipartUploads[key] = value
})
}
let cookies
if (cookieString) {
const cookieParseOptions = { decode: (s) => s }
// separate out cookie headers into separate data structure
// note: cookie is case insensitive
cookies = cookie.parse(cookieString.replace(/^Cookie: /gi, ''), cookieParseOptions)
}
// TODO: don't lower case method,
// curl expects you to uppercase always, if you do -X PoSt, that's
// what it will put as the method and we should do the same.
// TODO: read curl's source to figure out precedence rules.
let method
if (parsedArguments.head) {
method = 'head'
} else if (has(parsedArguments, 'request') &&
parsedArguments.request !== 'null') { // Safari adds `-Xnull` if it can't determine the request type
method = parsedArguments.request.toLowerCase()
} else if (parsedArguments['upload-file']) { // --upload-file '' doesn't do anything.
method = 'put'
} else if ((has(parsedArguments, 'data') ||
has(parsedArguments, 'data-ascii') ||
has(parsedArguments, 'data-binary') ||
has(parsedArguments, 'data-raw') ||
has(parsedArguments, 'form')) && !(parsedArguments.get)) {
method = 'post'
} else {
method = 'get'
}
const urlObject = URL.parse(url) // eslint-disable-line
// if GET request with data, convert data to query string
// NB: the -G flag does not change the http verb. It just moves the data into the url.
if (parsedArguments.get) {
urlObject.query = urlObject.query ? urlObject.query : ''
if (has(parsedArguments, 'data')) {
let urlQueryString = ''
if (url.indexOf('?') < 0) {
url += '?'
} else {
urlQueryString += '&'
}
urlQueryString += parsedArguments.data.join('&')
urlObject.query += urlQueryString
url += urlQueryString
delete parsedArguments.data
}
}
if (urlObject.query && urlObject.query.endsWith('&')) {
urlObject.query = urlObject.query.slice(0, -1)
}
const query = querystring.parse(urlObject.query, { sort: false })
for (const param in query) {
if (query[param] === null) {
query[param] = ''
}
}
urlObject.search = null // Clean out the search/query portion.
const request = {
url: url,
urlWithoutQuery: URL.format(urlObject)
}
if (parsedArguments.compressed) {
request.compressed = true
}
if (Object.keys(query).length > 0) {
request.query = query
}
if (headers) {
request.headers = headers
}
request.method = method
if (cookies) {
request.cookies = cookies
request.cookieString = cookieString.replace('Cookie: ', '')
}
if (multipartUploads) {
request.multipartUploads = multipartUploads
}
// TODO: all of these could be specified in the same command.
// They also need to maintain order.
// TODO: do all of these allow @file?
// TODO: set Content-Type downstream for some of these
if (parsedArguments.data) {
request.data = parsedArguments.data
} else if (parsedArguments['data-binary']) {
request.data = parsedArguments['data-binary']
request.isDataBinary = true
} else if (parsedArguments['data-ascii']) {
request.data = parsedArguments['data-ascii']
} else if (parsedArguments['data-raw']) {
request.data = parsedArguments['data-raw']
request.isDataRaw = true
} else if (parsedArguments['data-urlencode']) {
// TODO: this doesn't exactly match curl
// all '&' and all but the first '=' need to be escaped
request.data = parsedArguments['data-urlencode']
}
if (parsedArguments.user) {
request.auth = parsedArguments.user
}
if (has(request, 'data')) {
if (request.data.length > 1) {
request.dataArray = request.data
request.data = request.data.join('&')
} else {
request.data = request.data[0]
}