-
Notifications
You must be signed in to change notification settings - Fork 0
/
metacurl
executable file
·1226 lines (1093 loc) · 47 KB
/
metacurl
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
#!/usr/bin/env python3
import subprocess, os, hashlib
import tempfile
import urllib.parse
import re
import datetime, time
import dateutil.parser
import platform, sys
import argparse
import json
import sqlite3
import base64, binascii
import urllib.request, urllib.parse, urllib.error
import html.parser
import io
import codecs
###########################################################################
###########################################################################
# nonessential module
try:
import xattr
except ImportError:
pass # just ignore it for now
def set_extended_attr(fno,aname,avalue):
# pyxattr API should work with python3
try:
xattr.set(fno,'user.%s'%(aname,),avalue.encode('utf-8'))
return
except:
try:
# XXX This is for Linux, OS X has a different module
# and not with each installed python version - need to figure it out
xattr._fsetxattr(fno,'user.%s'%(aname,),avalue.encode('utf-8'))
except:
# print("fsetxattr %s failed"%(aname,))
pass
# environment name for user's meta filelist directory
FL = 'FLMETADIR'
METAFILE = 'METAFILE'
METACOOKIES = 'METACOOKIES'
N_ALL = [
'file', 'size', 'stamp', 'md5', 'sha1', 'sha256', 'githash', 'mimetype',
'charset', 'encoding', 'url', 'stored', 'server', 'etag'
]
class MetaFLJSONEncoder(json.JSONEncoder):
def encode(self, o):
if isinstance(o, dict):
return "{" + \
",".join(["%s:%s"%(self.encode(i),self.encode(o[i] if i in o else None)) \
for i in N_ALL]) + \
"}"
else:
return json.JSONEncoder.encode(self, o)
###########################################################################
###########################################################################
def tokensplit(s):
w = s.encode('UTF-8').strip(b'\x20\x09').decode('UTF-8')
t = []
while len(w)>0:
m=re.match('^[\x20\x09]*([A-Za-z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2e\x5e\x5f\x60\x7c\x7e]+)[\x20\x09]*(;|=|$)[\x20\x09]*(.*?)$'
,w)
if m:
# end of token (or separated by ; from the next)
if m.group(2)==';' or m.group(2) == '':
t.append(m.group(1))
w=m.group(3)
else:
(a,b)=(m.group(1),m.group(3))
if b[0]=='"':
begp=1
endp=b.find('"',1)
else:
begp=0
endp=b.find(';')
if endp>=0:
t.append("%s=%s"%(a,b[begp:endp]))
w=b[endp+1:]
else:
t.append("%s=%s"%(a,b[begp:]))
w=''
else:
t.append('%s'%(w,))
w=''
return t
class http_headerblock(object):
"""
HTTP header block, parsed from a pipe
http_headerblock(pipe). Uses iteration,
which cannot be then intermixed with plain
read()
Args:
pipe (handle): an input file handle
Attributes:
http_ver (str): HTTP version token
lines (list): list of header fields
"""
def get_all_headers(self, name):
"""Get all headers matching the name, tokenized"""
## expects: values stripped of ows
## Returns:
## lines in a list
## containing token value pairs in a list
## [firsttoken, firstvalue or None, repeat of: token, value ]
res = []
try:
for h in self.headers[name.upper()]:
# review rfc6266
# XXX handle semicolons in quotes ;token="what;ever"
# XXX handle quotes?
#okens = [t.strip('\x20\x09') for t in h.split(';')]
tokens = tokensplit(h)
line = []
for t in tokens:
z = t.split('=', 1)
z.append(None)
line.extend(z[:2])
res.append(line)
except KeyError:
pass
return res
def get_single_header(self, name):
"""Get a simple header, fail if multiple found"""
try:
allvalues = self.headers[name.upper()]
if len(allvalues) != 1:
return None
return allvalues[0]
except (KeyError):
return None
def get_duplicated_header(self, name):
"""Get a single header, if there are multiple headers, return
only if they all match"""
try:
allvalues = self.headers[name.upper()]
cl = allvalues[0]
for s in allvalues[1:]:
if s != cl:
# XXX write stderr fmt
print("!! ambiguous header %s (%s:%s)", name, s, cl)
return None
return cl
except KeyError:
return None
# content-length - may have several occurrences, but all those must match
def get_content_length(self):
a = self.get_all_headers('Content-Length')
b = self.get_all_headers('X-Archive-Orig-Content-Length')
if len(b)>0:
a=a+b
val = None
try:
val = int(a[0][0])
except (ValueError, TypeError, IndexError):
return None
try:
for l in a[1:]:
if val != int(l[0]):
return None
except (ValueError, TypeError):
return None
return val
def get_chrono_header(self, name):
"""Retrieve time-specific field value"""
# Thu, 05 Sep 2002 08:34:38 GMT
# September, 08-Sun-02 13:51:43 GMT
# Thursday, 19-Sep-02 13:41:10 GMT
# November, 14-Thu-02 14:01:04 GMT
# Monday, 25-Nov-102 12:49:26 GMT
# Thu, 18 Dec 2003 01:32:51 GMT
# Fri, 13 Apr 2018 19:07:57 GMT
n = self.get_duplicated_header(name)
try:
tzi = {"UTC": 0, "GMT": 0}
d = dateutil.parser.parse(n,tzinfos=tzi)
t = int(d.strftime("%s"))
return t
except (ValueError, AttributeError, TypeError):
return None
def __str__(self):
if self.http_ver:
return "* [%s] %s %s %s\n" % (len(self.lines), self.http_ver,
self.status_code, self.reason)
else:
return "?"
def __init__(self, pipe):
self.lines = None #: list of header lines
self.headers = {}
self.http_ver = None #: HTTP version token
self.status_code = None #: HTTP status code (3 digit)
self.reason = None #: HTTP reason phrase
# https://tools.ietf.org/html/rfc7230#section-3.1.2
# status-line = HTTP-version SP status-code SP reason-phrase CRLF
l = ''
for l in pipe:
if l: l = l.decode('UTF-8').rstrip("\x20\x09\r\n")
break
else:
return None
try:
(v, c, r) = l.split(None, 2)
except ValueError:
(v, c) = l.split(None, 1)
r = 'None given'
(self_http_ver, self.status_code, self.reason) = (v, int(c), r.strip())
# https://tools.ietf.org/html/rfc7230#section-3.1.2
# header-field = field-name ":" OWS field-value OWS
lines = []
for l in pipe:
if l: l = l.rstrip(b"\r\n").decode('UTF-8')
if l == '':
break
# XXX folding not well tested
if l[0] in "\x20\x09":
l = l.strip("\x20\x09")
if len(lines) > 0:
# not perfect for traditional Set-Cookie (no testing available)
lines[-1] = "%s %s" % (lines[-1], l)
else:
# replicate bogus field
lines.append(" " + l)
else:
l = l.rstrip("\x20\x09")
lines.append(l)
# now parse the lines to header fields
h = {}
for l in lines:
try:
(f, v) = l.split(':', 1)
# field-name shouldn't have any, but anyway
f = f.strip("\x20\x09").upper()
## provides: values stripped of ows
v = v.strip("\x20\x09")
try:
h[f].append(v)
except KeyError:
h[f] = [v]
except ValueError:
# XXX really bad, but stay robust
pass
self.lines = lines
self.headers = h
return
###########################################################################
###########################################################################
class singlefile(object):
"""
Retrieve a single file by using curl and inspecting the output
"""
def __init__(self, url, curlopts, debug=False, text=False, auth=None):
self.error = "not initialized"
self.progress = "downloading"
self.orig_url = ""
self.filename = None
self.filesize = None
self.hashes = [] # md5,sha1,sha256,*githash
self.server = None
self.etag = None
self.mimetype = None
self.charset = None
self.encoding = None
self.tmpname = None
self.expected_md5 = None
self.expected_sha1 = None
self.expected_sha256 = None
self.refer = None
self.ext_url = None
self.ext_args = []
self.allow_text = text
if url[-5:] == '.html':
self.allow_text = True
if debug:
ANSI_OFF = ''
ANSI_RED = ''
ANSI_GREEN = ''
ANSI_YEL = ''
ANSI_EL = ''
CR = '\n'
else:
ANSI_OFF = '\033[m\033[K'
ANSI_RED = '\033[31m'
ANSI_GREEN = '\033[32m'
ANSI_YEL = '\033[38;5;11m'
ANSI_EL = '\033[K' # tput el
CR = '\r'
if '#' in url:
## TODO parse fragment for hash spec
[url, frag] = url.split('#', 1)
url = url.split('#', 1)[0]
else:
frag = ''
# check if any fixing is needed
url, frag = self.canonical_url(url, frag)
m = re.search('(md5|sha1|sha256)[A-Za-z]*=([A-Za-z0-9+/=]+)', frag, re.I)
if m:
if m.group(1).upper() == 'MD5':
self.expected_md5 = m.group(2).lower()
elif m.group(1).upper() == 'SHA1':
self.expected_sha1 = m.group(2).lower()
elif m.group(1).upper() == 'SHA256':
hash = m.group(2)
if len(hash)<64:
self.expected_sha256=binascii.hexlify(base64.b64decode(hash)).decode('ASCII').lower()
else:
self.expected_sha256=hash.lower()
# XXX also - multihash qm
# save original url and extract possible filename (without parameters)
if not self.orig_url:
self.orig_url = url
self.ext_url = url
parts = url.split('?', 1)[0].split('/')
# [:8] is new XXX
if parts[-1] == '' or parts[-1][0] in '&?#' or parts[-1][:8] == 'download':
hunk = parts[-2]
else:
hunk = parts[-1]
try:
x = urllib.parse.unquote(hunk)
if x != hunk:
hunk = x
except:
pass
self.filename = hunk
self.mimetype = None
conn = None
# initially, cursor is at the start of the line
# progress is expected to keep the cursor mid-line (right after stats)
while True:
url, frag = self.canonical_url(url, frag)
###
if self.orig_url != url and re.match(r'https?://t\.co/.*|.*kernel\.org/.*|.*downloads.sourceforge.net/.*',self.orig_url):
print("forget",self.orig_url,url)
self.orig_url = url
sys.stderr.write(("-- %s %s %s\n"
if debug else "\r-- %s %s %s \r") % (
self.progress,
url,
ANSI_OFF, ))
all_args = curlopts[:]
all_args.extend(self.ext_args)
if self.refer:
all_args.extend([
'-H',
'Referer: %s' % (self.refer),
])
if METACOOKIES in os.environ:
cookie=os.environ[METACOOKIES]
all_args.append('-b')
all_args.append(cookie)
all_args.append('-c')
all_args.append(cookie)
### ftp has metadata, but curl does not pass them - fake them here
if url[:3] == 'ftp':
ftpconv = ''
import ftplib
import urllib.parse
u = urllib.parse.urlparse(url)
netloc = u.netloc
auth_user = 'ftp'
auth_pass = 'ftp@'
if '@' in netloc:
(a,l) = netloc.rsplit('@',1)
netloc = l
if ':' in a:
(auth_user,auth_pass) = a.rsplit(':',1)
else:
auth_user = a
# ParseResult(scheme='ftp', netloc='atrey.karlin.mff.cuni.cz:21', path='/pub/linux/pci/pciutils-3.5.6.tar.gz.sign', params='', query='', fragment='')
all_args.append('-u')
all_args.append('%s:%s'%(auth_user,auth_pass))
url = 'ftp://%s%s'%(netloc,u.path.replace(' ','%20'))
try:
ftpconn = ftplib.FTP(netloc)
res = ftpconn.login(auth_user, auth_pass)
print('* ftp',res.split("\n")[0])
# 230 Login successful.
if res[0] == '2':
firstline = res[4:].split('\n')[0].strip("\r")
ftpconv = ftpconv + 'HTTP/1.0 200 ' + firstline + '\r\n'
# 550 SIZE not allowed in ASCII mode
ftpconn.voidcmd('TYPE I')
ftpsz = ftpconn.size(u.path)
# res=ftpconn.sendcmd("SIZE %s"%(u.path))
print('* ftp size 213',ftpsz)
# 213 811
#if res[0] == '2':
ftpconv = ftpconv + 'Content-Length: ' + str(ftpsz) + '\r\n'
ftptm=ftpconn.sendcmd("MDTM %s"%(u.path))
print('* ftp time',ftptm)
# 213 20171117140512
if ftptm[0] == '2':
tm = ftptm[4:]
matchtm=re.match('^(\d+)(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}(\.\d+|))$',tm)
z = datetime.datetime(int(matchtm.group(1)),int(matchtm.group(2)),int(matchtm.group(3)),
int(matchtm.group(4)),int(matchtm.group(5)),int(matchtm.group(6)),0,None)
ftpconv = ftpconv + 'Last-Modified: ' + z.strftime("%a, %d %b %Y %T GMT") + '\r\n'
ftpconn.quit()
ftpconn = None
ftpconv = ftpconv + '\r\n'
ftpconv = ftpconv.encode('UTF-8')
except ftplib.all_errors as e:
print('* ftp',e)
ftpstr = io.BytesIO(ftpconv)
hdr = http_headerblock(ftpstr)
else:
hdr = None
all_args.append('--')
all_args.append(url)
run_curl = subprocess.Popen(all_args, stdout=subprocess.PIPE)
#sys.stderr.write("\n"+' | '.join(all_args)) # XXX
if hdr is None:
hdr = http_headerblock(run_curl.stdout)
# if via proxy, there might be a first HTTP response about the connection
if hdr.lines is not None and hdr.status_code is not None and len(hdr.lines) == 0 and hdr.status_code == 200:
hdr = http_headerblock(run_curl.stdout)
elif hdr.status_code is not None and (int(hdr.status_code) == 200 or int(hdr.status_code) == 150):
if not debug:
sys.stderr.write("\r-- %s -- %s%s%s\n" %
(url, ANSI_GREEN, str(hdr.status_code),
ANSI_OFF)) # XXX
elif hdr.status_code is not None and int(hdr.status_code) > 305:
sys.stderr.write("\r-- %s -- %s%s%s\n" % (
url, ANSI_RED, str(hdr.status_code), ANSI_OFF)) # XXX
else:
sys.stderr.write("\r-- %s -- %s%s%s\n" % (
url, ANSI_OFF, str(hdr.status_code), ANSI_OFF)) # XXX
if None == hdr.lines:
## XXX kill first?
res = run_curl.wait()
if res == 35:
self.error = "curl returned err %d, try without -k" % (
res, )
elif res == 60:
self.error = "curl returned err %d, try with -k" % (res, )
else:
self.error = "curl returned err %d" % (res, )
break
## check if redirect
if hdr.status_code >= 300 and hdr.status_code < 400:
self.error = "redirecting"
self.ext_args = []
run_curl.kill()
# TODO handle failure
loc = hdr.headers['LOCATION'][0]
self.refer, url = self.canonical_redirect(url, loc)
self.progress = 'redirected to'
# content not seen yet, restarting from new url
continue
## prepare to process body
expected_size = hdr.get_content_length()
if expected_size == 0: expected_size = None
#CONTENT-DISPOSITION inline; filename="WWW.pdf"
# ['inline',None,'filename','"WWW.pdf"']
# XXX RFC6266 / RFC5987
# filename="EURO rates";
# filename*=utf-8''%e2%82%ac%20rates
# foo: bar; title*=iso-8859-1'en'%A3%20rates
# inline; filename -- botched
try:
for l in hdr.get_all_headers('Content-Disposition'):
z = None
if len(l) >= 4 and l[2].upper().strip() == 'FILENAME':
z = l[3].strip('\x27\x22')
if len(l) >= 4 and l[2].upper().strip() == 'FILENAME*':
arr = l[3].strip().split("'")
if len(arr) > 2:
z = arr[2].strip('\x27\x22\x09\x20')
# z = bytearray(z).decode('utf-8')
if z:
z.replace('%20','_')
self.filename = z
except (KeyError, TypeError, AttributeError,UnicodeError):
# AttributeError when filename is not specified (None)
pass
## debugging to seek for new headers containing information
for n in sorted(hdr.headers):
if ('LANGUAGE' in n) or \
(n[0:6] in ['ACCESS','ACCEPT','CACHE-','X-GUPL','X-CACH','PROXY-','X-FRAM','X-TIME']) or \
(n in ['SERVER','AGE','ALT-SVC','CONNECTION','VARY','CF-RAY','PRAGMA','VIA']) or \
(n in ['DATE','LAST-MODIFIED','EXPIRES']) or \
(n in ['CONTENT-LENGTH','CONTENT-TYPE','ETAG','SET-COOKIE','P3P']) or \
(n[0:10] in ['X-GOOG-MET','X-GOOG-STO','X-GOOG-GEN','STRICT-TRA','X-GOOG-HAS','X-AMZ-REQU','X-AMZ-ID-2']) or \
(n[0:10] in ['CF-CACHE-S','X-XSS-PROT','X-CONTENT-','X-POWERED-','X-UA-COMPA','TIMING-ALL','X-GITHUB-R']) or \
(n[0:10] in ['X-FASTLY-R','X-GEO-BLOC','CONTENT-SE','X-SERVED-B','SOURCE-AGE','TRANSFER-E']) or \
False:
continue
if ('CONTENT-' in n) or \
('LAST-' in n) or \
('GOOG-' in n) or \
True:
sys.stderr.write("%s %s\n" % (n, ":".join(hdr.headers[n])))
## -- http://central.maven.org/maven2/com/lowagie/itext/2.1.7/itext-2.1.7.jar -- 200
# X-CHECKSUM-SHA1 892bfb3e97074a61123b3b2d7caa2db112750864
# X-CHECKSUM-MD5 7587a618197a065eac4a453d173d4ed6
# XXX sha256
# X-CHECKSUM-SHA2 712913a083f07dbacfdf5686364127ba8d457418458df0f6b899de8efa3d91af
# X-AMZ-META-S3CMD-ATTRS atime:1554763479/ctime:1554763036/gid:100/gname:users/md5:b5beced41ccb9db2cd567e97d18cf5bf/mode:33188/mtime:1554762731/uid:1000/uname:andy
if 'X-CHECKSUM-SHA1' in hdr.headers:
d = hdr.get_single_header('X-Checksum-SHA1').lower()
if self.expected_sha1:
if self.expected_sha1 != d:
sys.stderr.write(
"!! different SHA1 digests to check\n")
else:
self.expected_sha1 = d
if 'X-CHECKSUM-MD5' in hdr.headers:
d = hdr.get_single_header('X-Checksum-MD5').lower()
if self.expected_md5:
if self.expected_md5 != d:
sys.stderr.write("!! different MD5 digests to check\n")
else:
self.expected_md5 = d
## http://download.system-cfg.com/f.php?h=2Z0Khs23&d=1
## CONTENT-MD5 6c3156d38333f715ad5cc0ed73feba19
# XXX -- http://hackage.haskell.org/package/xmonad-0.15/xmonad-0.15.tar.gz -- 200
# CONTENT-MD5 OkX/s6RkgtqNlIc8SKed+g==
if 'CONTENT-MD5' in hdr.headers:
d = hdr.get_single_header('Content-MD5')
if len(d) == 32:
dig = d.lower()
else:
dig = binascii.hexlify(base64.b64decode(d)).decode('ASCII').lower()
if self.expected_md5:
if self.expected_md5 != dig:
sys.stderr.write("!! different MD5 digests to check\n")
else:
self.expected_md5 = dig
## x-goog-hash: md5=<base64 of binary>
try:
for h in hdr.headers['X-GOOG-HASH']:
(t, d) = h.split('=', 1)
if t.strip().upper() == 'MD5':
dig = binascii.hexlify(base64.b64decode(d.strip())).decode('ASCII').lower()
if self.expected_md5:
if self.expected_md5 != dig:
sys.stderr.write(
"!! different MD5 digests to check\n")
else:
self.expected_md5 = dig
except (KeyError, ValueError, TypeError):
pass
# hack - sometimes MD5 is part of the etag
# < Last-Modified: Tue, 18 Sep 2018 06:30:07 GMT
# < ETag: "eaf82392603b92dae632cc0f356b08aa:1537252207"
# XXX also try fetching stamp if not in last-modified
try:
self.etag = hdr.get_single_header('Etag').strip("\x22\x27")
m = re.match('([0-9a-fA-F]+)($|:)', self.etag)
if m and len(m.group(1)) == 32:
dig = m.group(1).lower()
if self.expected_md5 and self.expected_md5 != dig:
sys.stderr.write(
"!! different MD5 digests to check\n")
else:
self.expected_md5 = dig
except (AttributeError, TypeError):
self.etag = None
# https://github-production-release-asset-2e65be.s3.amazonaws.com/1234783/d5b56b20-6e78-11e7-8593-067daae6ef3b?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20170909%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170909T090834Z&X-Amz-Expires=300&X-Amz-Signature=7870d67107a365117aa8b10ab65625a87bf634b1021bea07e5ba1dfaf0e077b6&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dmosh-1.3.2.pkg&response-content-type=application%2Foctet-stream
# correct filename if needed
self.filename = self.canonical_filename(self.filename, url)
## try to resume? XXX allow-resume
## -C, --continue-at <offset>
## prepare hashing (git hash needs size beforehand)
hash_md5 = hashlib.md5()
hash_sha1 = hashlib.sha1()
hash_sha256 = hashlib.sha256()
hash_githash = None
if expected_size:
hash_githash = hashlib.sha1()
hash_githash.update(b"blob %d\0" % (expected_size))
## start downloading to a temporary file
if hdr.status_code != 200:
self.error = "HTTP status %d %s" % (hdr.status_code,
hdr.reason)
sys.stderr.write("!! %s%s%s" %
(ANSI_RED, self.error, ANSI_OFF))
f = tempfile.mkstemp(
prefix='.err%d' % (hdr.status_code), dir='.')
else:
self.error = "downloading"
f = tempfile.mkstemp(prefix='.dl_', dir='.')
self.tmpname = f[1]
f = os.fdopen(f[0], "wb")
set_extended_attr(f.fileno(),'origurl',self.orig_url)
## TODO progress
counted_size, i, x = 0, 0, 99
shortened_url = self.orig_url[0:100]
if self.filename not in shortened_url:
shortened_name = ' .../' + self.filename[-95:]
shortened_url = shortened_url[:120 - len(shortened_name
)] + shortened_name
if not debug:
sys.stderr.write("\r %15s %s%s \r" %
('', shortened_url, ANSI_OFF))
## in 2.x it's not possible to switch to non-line delimited reads
## XXX try to find a way to make the transfer efficient
for buf in run_curl.stdout:
f.write(buf)
counted_size = counted_size + len(buf)
z = time.gmtime().tm_sec
if x != z and not debug:
i = i + 1
x = z
if expected_size:
perc = '%3d%%|%-9d' % (100.0 * counted_size /
expected_size, expected_size)
else:
perc = '%-14d' % (counted_size)
sys.stderr.write("\r %s%s \r" % ("_oOo" [i % 4], perc))
hash_md5.update(buf)
hash_sha1.update(buf)
hash_sha256.update(buf)
if hash_githash: hash_githash.update(buf)
# XXX closing - set xattr before, while we have the handle
# XXX needs to get the hashes finalised before too
# set_extended_attr(f.fileno(),'origurl',self.orig_url) # XXX xattr
f.close()
## need to have the timestamp before renaming
## LAST-MODIFIED Wed, 21 Dec 2016 03:44:53 GMT
## 1482291893
## X-GOOG-GENERATION 1482291893643481
## MEMENTO-DATETIME Thu, 16 Oct 2008 15:18:32 GMT
## X-ARCHIVE-ORIG-DATE Thu, 16 Oct 2008 15:18:32 GMT
## X-ARCHIVE-ORIG-ETAG "2967404-3200700"
## X-ARCHIVE-ORIG-LAST-MODIFIED Wed, 06 Feb 2008 03:08:53 GMT
## X-ARCHIVE-ORIG-CONTENT-LENGTH 12062338
try:
self.stamp = hdr.get_chrono_header('Last-Modified')
except:
self.stamp = None
if self.stamp == -1: self.stamp = None
# this overrides the naive stamp
z = hdr.get_chrono_header('X-Archive-Orig-Last-Modified')
if z:
self.stamp = z
# if it's coming from G* with a higher resolution time
try:
z = float(hdr.get_single_header('X-GOOG-Generation'))
if abs(z / 1000000.0 - self.stamp) < 5:
self.stamp = z / 1000000.0
except (ValueError, TypeError):
pass
self.stored = int(time.time())
try:
## save server's idea of the time if not too far off
d = hdr.get_chrono_header('Date')
if abs(d - self.stored) < 7200:
self.stored = d
except:
pass
# check if download is complete
if hdr.status_code == 200:
statbuf = os.stat(self.tmpname)
if statbuf.st_size != counted_size:
counted_size = statbuf.st_size
if expected_size is None or counted_size == expected_size:
self.filesize = int(counted_size)
self.error = None
## success
elif expected_size is not None:
if counted_size < expected_size:
self.error = "loaded only %d bytes of %d" % (
counted_size, expected_size)
os.remove(self.tmpname)
self.tmpname = None
break
else:
# XXX hack (help force)
self.error = None
self.filesize = counted_size
sys.stderr.write("?? read more (%d of %d)\n" %
(counted_size, expected_size))
else:
# XXX did remove, but may need for redirect parsing
break
self.server = hdr.get_single_header('Server')
# store Content-Type: mimetype; charset=whatever
try:
for l in hdr.get_all_headers('Content-Type'):
self.mimetype = l[0].strip()
if len(l) > 3 and l[2].upper().strip() == 'CHARSET':
self.charset = l[3].strip()
except (TypeError, IndexError):
self.charset = None
self.encoding = hdr.get_single_header('Content-Encoding')
z = hdr.get_single_header('X-GOOG-Stored-Content-Encoding')
if z and self.encoding is None:
self.encoding = z
self.hashes = [
hash_md5.hexdigest(), hash_sha1.hexdigest(),
hash_sha256.hexdigest(), None
]
if hash_githash: self.hashes[3] = hash_githash.hexdigest()
self.refer, url = self.content_redirect(url, self.tmpname,
self.mimetype)
if url is None:
break
# else start again
return None
def canonical_url(self, url, frag):
return url, frag
def canonical_redirect(self, url, loc):
orig_url = url
# unsure if correct
loc = loc[:]
if ' ' in loc: loc = loc.replace(' ', '%20')
url = urllib.parse.urljoin(url, loc)
return orig_url, url
def canonical_filename(self, filename, url):
if filename[-4:].upper() == '.PDF':
return filename[:-4] + '.pdf'
x = url.split('/')[-1]
if x[-4:].upper() == '.PDF':
return x[:-4] + '.pdf'
return filename
def content_redirect(self, url, tmpname, mimetype):
return None, None
def savefile(self, filename=None, debug=False):
if debug:
ANSI_OFF = ''
ANSI_RED = ''
ANSI_GREEN = ''
ANSI_YEL = ''
ANSI_EL = ''
CR = '\n'
else:
ANSI_OFF = '\033[m\033[K'
ANSI_RED = '\033[31m'
ANSI_GREEN = '\033[32m'
ANSI_YEL = '\033[38;5;11m'
ANSI_EL = '\033[K' # tput el
CR = '\r'
if self.error and not debug:
sys.stderr.write("!! cannot save, %s%s%s\n" %
(ANSI_RED, self.error, ANSI_OFF))
return None
if not self.tmpname:
return None
# step - find a filename (or a duplicate prefix to make it unique)
if filename is None:
filename = self.filename
# XXX todo
# duplicate by hash?
# duplicate by name on fs elsewhere
# duplicate by name in db (case-insensitive)
# XXX todo
# check if name already in use
dupprefix = ''
try:
# break out via OSError if file with that name does not exist
os.stat(dupprefix + filename)
dupprefix = 'dup.'
os.stat(dupprefix + filename)
dupprefix = 'dup.%d.' % (os.getpid(),)
os.stat(dupprefix + filename)
return None
except OSError:
pass
filename = dupprefix+filename
# XXX try to match with constraints instead?
if self.filename[-4:].upper() == '.PHP':
sys.stderr.write("!! not saving, .php extension\n")
return None
first20 = None
with open(self.tmpname,'rb') as checkf:
first20 = checkf.read(20)
try:
if '<!DOCTYPE' in first20.decode('utf-8').upper():
self.mimetype = 'text/html'
except UnicodeDecodeError:
pass
if self.mimetype and self.mimetype.upper() == 'TEXT/HTML' and not self.allow_text:
sys.stderr.write("!! not saving, MIME type is %s%s\n" %
(self.mimetype, ANSI_OFF))
# XXX grep WARNING
return None
if self.mimetype and self.mimetype.upper()[0:5] == 'TEXT/' and not self.allow_text:
# -- https://gnupg.org/ftp/gcrypt/npth/npth-1.5.tar.bz2 -- 200
# !! not saving, MIME type is text/plain (npth-1.5.tar.bz2)
if '/raw.githubusercontent' in self.orig_url:
pass
elif self.filename[-7:].upper() == '.TAR.GZ':
self.mimetype='application/x-gzip'
elif self.filename[-7:].upper() == '.TAR.XZ':
self.mimetype='application/x-xz'
elif self.filename.split('.')[-1].upper() == 'BZ2':
self.mimetype='application/x-bzip2'
elif self.filename.split('.')[-1].upper()[:6] == 'SHA256':
self.mimetype='application/octet-stream'
elif self.filename.split('.')[-1].upper() == 'ADF':
if self.filesize < 819200:
sys.stderr.write("!! not saving, ADF %s size %d\n"%(self.filename,self.filesize,))
self.mimetype='application/octet-stream'
elif self.filename.split('.')[-1].upper() == 'PDF':
if self.filesize < 8192:
sys.stderr.write("!! not saving, PDF %s size %d\n"%(self.filename,self.filesize,))
return None
if '%PDF' in first20.decode('utf-8').upper():
pass
if '<!DOCTYPE' in first20.decode('utf-8').upper():
sys.stderr.write('!! not saving, %s looks HTML instead of PDF\n'%(self.filename,))
return None
checkf.close()
self.mimetype='application/pdf'
elif self.filename.split('.')[-1].upper(
) not in ['SIG', 'SIGN', 'PUB', 'ASC', 'TXT', 'C', 'H', 'CSV', 'S', 'ASM', 'MD5','SHA1','SHA256','SHA512','DIFF','PATCH','SUM','SUMS']:
sys.stderr.write("!! not saving, MIME type is %s (%s) %s\n" %
(self.mimetype, self.filename, ANSI_OFF))
return None
if self.mimetype and self.mimetype[0:5].upper() == 'IMAGE' and '/img/' in self.orig_url:
sys.stderr.write("!! not saving, MIME type is %s (%s) %s\n" %
(self.mimetype, self.filename, ANSI_OFF))
return None
# swift-swift-5.0.0.tar.gz really?
splitfnarr=filename.split('-',2)
if len(splitfnarr)>2:
if splitfnarr[0] == splitfnarr[1]:
filename='-'.join(splitfnarr[1:])
# postponing these - the temp file won't be seen as proper
if self.stamp:
os.utime(self.tmpname, (self.stamp, self.stamp))
os.chmod(self.tmpname, 0o644)
self.filename = filename.replace('/','_')
print(self.filename,self.tmpname,ANSI_EL)
os.rename(self.tmpname, self.filename)
self.tmpname = None
bad = []
good = []
# XXX xattr
# check digests
if self.expected_md5:
if self.expected_md5 == self.hashes[0]:
good.append('MD5')
else:
bad.append('MD5')
if self.expected_sha1:
if self.expected_sha1 == self.hashes[1]:
good.append('SHA1')
else:
bad.append('SHA1')
if self.expected_sha256:
if self.expected_sha256 == self.hashes[2]:
good.append('SHA256')
else:
bad.append('SHA256')
# report digest verficiation
report_color = ANSI_OFF
if len(bad):
report_color = ANSI_RED
ok = 'BAD ' + ' '.join(bad)
else:
report_color = ANSI_GREEN
good.append('OK')
ok = ' '.join(good)
# xxx no LF when debug
if not self.filesize:
self.filesize = 0
sys.stderr.write(
"%s%s%s%s%s %s%s%s (%d)%s\n" %
(CR, ("\n" if len(bad) and not debug else ""), ANSI_YEL, filename, ANSI_OFF, report_color, ok, ANSI_OFF, self.filesize, ANSI_EL))
## 1/4 write .meta as text with some fields
## order cannot be changed - append only (or rather ignore)
## don't store extra precision if not present
if self.stamp:
safestamp = '%f' % (self.stamp, )
else:
safestamp = self.stamp
try:
(i, frac) = safestamp.split('.', 1)
if int(frac) == 0:
safestamp = int(i)
else:
safestamp = float(safestamp.rstrip('0'))
except (ValueError, AttributeError):
pass
try:
self.filename = self.filename.encode('utf-8')
except (UnicodeError):
pass
metafile = '.meta'
if not os.path.isfile(metafile) and METAFILE in os.environ:
metafile = os.environ[METAFILE]
with codecs.open(metafile, encoding='utf-8', mode='a') as f:
f.write("\t".join([str(i) if i else '?' for i in \
[self.filename.decode('UTF-8'),self.filesize,safestamp,\
self.hashes[0],self.mimetype,self.encoding,\
self.orig_url,self.stored,self.server,self.etag]])+"\n")
f.close()
## 2/4 avoid further records if duplicate
## todo - make unique file before saving
conn, cur = None, None
try:
if conn == None: