Samesite - proxy that can cache partial transfers

Hex Artifact Content
anonymous

Hex Artifact Content

Artifact 873110a573b70aeeb1696d56a723941d5a3dc77fe6481695775f6265759ccf1b:


0000: 23 21 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20 70  #!/usr/bin/env p
0010: 79 74 68 6f 6e 33 2e 31 0a 0a 69 6d 70 6f 72 74  ython3.1..import
0020: 20 64 61 74 65 74 69 6d 65 2c 20 68 74 74 70 2e   datetime, http.
0030: 63 6f 6f 6b 69 65 6a 61 72 2c 20 6f 70 74 70 61  cookiejar, optpa
0040: 72 73 65 2c 20 6f 73 2c 20 73 79 73 2c 20 73 68  rse, os, sys, sh
0050: 65 6c 76 65 2c 20 72 65 2c 20 75 72 6c 6c 69 62  elve, re, urllib
0060: 2e 72 65 71 75 65 73 74 0a 0a 70 61 72 73 65 72  .request..parser
0070: 20 3d 20 6f 70 74 70 61 72 73 65 2e 4f 70 74 69   = optparse.Opti
0080: 6f 6e 50 61 72 73 65 72 28 29 0a 70 61 72 73 65  onParser().parse
0090: 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 27 2d 76  r.add_option('-v
00a0: 27 2c 20 27 2d 2d 76 65 72 62 6f 73 65 27 2c 20  ', '--verbose', 
00b0: 61 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72 65 5f  action = 'store_
00c0: 74 72 75 65 27 2c 20 64 65 73 74 20 3d 20 27 76  true', dest = 'v
00d0: 65 72 62 6f 73 65 27 2c 20 68 65 6c 70 20 3d 20  erbose', help = 
00e0: 27 74 75 72 6e 73 20 6f 6e 20 76 65 72 62 6f 73  'turns on verbos
00f0: 65 20 73 74 61 74 75 73 20 6e 6f 74 69 66 69 63  e status notific
0100: 61 74 69 6f 6e 73 27 2c 20 6d 65 74 61 76 61 72  ations', metavar
0110: 20 3d 20 27 62 6f 6f 6c 27 2c 20 64 65 66 61 75   = 'bool', defau
0120: 6c 74 20 3d 20 46 61 6c 73 65 29 0a 70 61 72 73  lt = False).pars
0130: 65 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 27 2d  er.add_option('-
0140: 64 27 2c 20 27 2d 2d 64 69 72 27 2c 20 61 63 74  d', '--dir', act
0150: 69 6f 6e 20 3d 20 27 73 74 6f 72 65 27 2c 20 64  ion = 'store', d
0160: 65 73 74 20 3d 20 27 64 69 72 27 2c 20 68 65 6c  est = 'dir', hel
0170: 70 20 3d 20 27 73 70 65 63 69 66 79 20 64 69 72  p = 'specify dir
0180: 65 63 74 6f 72 79 20 77 68 65 72 65 20 74 68 65  ectory where the
0190: 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 62 65   files should be
01a0: 20 73 74 6f 72 65 64 27 2c 20 6d 65 74 61 76 61   stored', metava
01b0: 72 20 3d 20 27 73 74 72 69 6e 67 27 2c 20 64 65  r = 'string', de
01c0: 66 61 75 6c 74 20 3d 20 4e 6f 6e 65 29 0a 70 61  fault = None).pa
01d0: 72 73 65 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28  rser.add_option(
01e0: 27 2d 72 27 2c 20 27 2d 2d 72 6f 6f 74 27 2c 20  '-r', '--root', 
01f0: 61 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72 65 27  action = 'store'
0200: 2c 20 64 65 73 74 20 3d 20 27 72 6f 6f 74 27 2c  , dest = 'root',
0210: 20 68 65 6c 70 20 3d 20 27 73 70 65 63 69 66 79   help = 'specify
0220: 20 61 20 73 69 74 65 20 66 72 6f 6d 20 77 68 69   a site from whi
0230: 63 68 20 64 61 74 61 20 73 68 6f 75 6c 64 20 62  ch data should b
0240: 65 20 6d 69 72 72 6f 72 65 64 27 2c 20 6d 65 74  e mirrored', met
0250: 61 76 61 72 20 3d 20 27 73 74 72 69 6e 67 27 2c  avar = 'string',
0260: 20 64 65 66 61 75 6c 74 20 3d 20 4e 6f 6e 65 29   default = None)
0270: 0a 70 61 72 73 65 72 2e 61 64 64 5f 6f 70 74 69  .parser.add_opti
0280: 6f 6e 28 27 2d 6c 27 2c 20 27 2d 2d 6c 6f 67 27  on('-l', '--log'
0290: 2c 20 61 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72  , action = 'stor
02a0: 65 27 2c 20 64 65 73 74 20 3d 20 27 6c 6f 67 27  e', dest = 'log'
02b0: 2c 20 68 65 6c 70 20 3d 20 27 73 70 65 63 69 66  , help = 'specif
02c0: 79 20 61 20 6c 6f 67 20 66 69 6c 65 20 74 6f 20  y a log file to 
02d0: 70 72 6f 63 65 73 73 27 2c 20 6d 65 74 61 76 61  process', metava
02e0: 72 20 3d 20 27 73 74 72 69 6e 67 27 2c 20 64 65  r = 'string', de
02f0: 66 61 75 6c 74 20 3d 20 4e 6f 6e 65 29 0a 28 6f  fault = None).(o
0300: 70 74 69 6f 6e 73 2c 20 61 72 67 73 29 20 3d 20  ptions, args) = 
0310: 70 61 72 73 65 72 2e 70 61 72 73 65 5f 61 72 67  parser.parse_arg
0320: 73 28 29 0a 0a 69 66 20 6e 6f 74 20 6f 70 74 69  s()..if not opti
0330: 6f 6e 73 2e 64 69 72 3a 0a 09 70 72 69 6e 74 28  ons.dir:..print(
0340: 27 44 69 72 65 63 74 6f 72 79 20 6e 6f 74 20 73  'Directory not s
0350: 70 65 63 69 66 69 65 64 27 29 0a 09 65 78 69 74  pecified')..exit
0360: 28 31 29 0a 0a 69 66 20 6e 6f 74 20 6f 70 74 69  (1)..if not opti
0370: 6f 6e 73 2e 72 6f 6f 74 3a 0a 09 70 72 69 6e 74  ons.root:..print
0380: 28 27 53 65 72 76 65 72 20 6e 6f 74 20 73 70 65  ('Server not spe
0390: 63 69 66 69 65 64 27 29 0a 09 65 78 69 74 28 31  cified')..exit(1
03a0: 29 0a 0a 69 66 20 6e 6f 74 20 6f 70 74 69 6f 6e  )..if not option
03b0: 73 2e 6c 6f 67 3a 0a 09 70 72 69 6e 74 28 27 4c  s.log:..print('L
03c0: 6f 67 20 66 69 6c 65 20 6e 6f 74 20 73 70 65 63  og file not spec
03d0: 69 66 69 65 64 27 29 0a 09 65 78 69 74 28 31 29  ified')..exit(1)
03e0: 0a 0a 69 66 20 6e 6f 74 20 6f 73 2e 61 63 63 65  ..if not os.acce
03f0: 73 73 28 6f 70 74 69 6f 6e 73 2e 6c 6f 67 2c 20  ss(options.log, 
0400: 6f 73 2e 52 5f 4f 4b 29 3a 0a 09 70 72 69 6e 74  os.R_OK):..print
0410: 28 27 4c 6f 67 20 66 69 6c 65 20 75 6e 72 65 61  ('Log file unrea
0420: 64 61 62 6c 65 27 29 0a 09 65 78 69 74 28 31 29  dable')..exit(1)
0430: 0a 0a 23 20 74 68 69 73 20 69 73 20 66 69 6c 65  ..# this is file
0440: 20 69 6e 64 65 78 20 2d 20 65 76 65 72 79 74 68   index - everyth
0450: 69 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ing is stored in
0460: 20 74 68 69 73 20 66 69 6c 65 0a 69 6e 64 65 78   this file.index
0470: 20 3d 20 73 68 65 6c 76 65 2e 6f 70 65 6e 28 6f   = shelve.open(o
0480: 70 74 69 6f 6e 73 2e 64 69 72 20 2b 20 27 2f 2e  ptions.dir + '/.
0490: 69 6e 64 65 78 27 29 0a 64 65 73 63 5f 66 69 65  index').desc_fie
04a0: 6c 64 73 20 3d 20 28 27 43 6f 6e 74 65 6e 74 2d  lds = ('Content-
04b0: 4c 65 6e 67 74 68 27 2c 20 27 45 54 61 67 27 2c  Length', 'ETag',
04c0: 20 27 50 72 61 67 6d 61 27 2c 20 27 4c 61 73 74   'Pragma', 'Last
04d0: 2d 4d 6f 64 69 66 69 65 64 27 29 0a 69 67 6e 6f  -Modified').igno
04e0: 72 65 5f 66 69 65 6c 64 73 20 3d 20 28 27 41 63  re_fields = ('Ac
04f0: 63 65 70 74 2d 52 61 6e 67 65 73 27 2c 20 27 41  cept-Ranges', 'A
0500: 67 65 27 2c 20 27 43 61 63 68 65 2d 43 6f 6e 74  ge', 'Cache-Cont
0510: 72 6f 6c 27 2c 20 27 43 6f 6e 6e 65 63 74 69 6f  rol', 'Connectio
0520: 6e 27 2c 20 27 43 6f 6e 74 65 6e 74 2d 54 79 70  n', 'Content-Typ
0530: 65 27 2c 20 27 44 61 74 65 27 2c 20 27 45 78 70  e', 'Date', 'Exp
0540: 69 72 65 73 27 2c 20 27 53 65 72 76 65 72 27 2c  ires', 'Server',
0550: 20 27 56 69 61 27 2c 20 27 58 2d 43 61 63 68 65   'Via', 'X-Cache
0560: 27 2c 20 27 58 2d 43 61 63 68 65 2d 4c 6f 6f 6b  ', 'X-Cache-Look
0570: 75 70 27 29 0a 0a 77 68 69 6c 65 20 54 72 75 65  up')..while True
0580: 3a 0a 09 75 6e 63 68 65 63 6b 65 64 5f 66 69 6c  :..unchecked_fil
0590: 65 73 20 3d 20 73 65 74 28 29 0a 09 63 68 65 63  es = set()..chec
05a0: 6b 65 64 5f 66 69 6c 65 73 20 3d 20 30 0a 0a 09  ked_files = 0...
05b0: 23 20 72 65 61 64 69 6e 67 20 6c 6f 67 20 61 6e  # reading log an
05c0: 64 20 73 74 6f 72 69 6e 67 20 66 6f 75 6e 64 20  d storing found 
05d0: 75 72 6c 73 20 66 6f 72 20 70 72 6f 63 65 73 73  urls for process
05e0: 69 6e 67 0a 09 23 20 63 68 65 63 6b 20 66 69 6c  ing..# check fil
05f0: 65 20 6d 74 69 6d 65 20 58 58 58 0a 09 77 69 74  e mtime XXX..wit
0600: 68 20 6f 70 65 6e 28 6f 70 74 69 6f 6e 73 2e 6c  h open(options.l
0610: 6f 67 2c 20 27 72 27 29 20 61 73 20 6c 6f 67 5f  og, 'r') as log_
0620: 66 69 6c 65 3a 0a 09 09 6c 6f 67 5f 6c 69 6e 65  file:...log_line
0630: 20 3d 20 72 65 2e 63 6f 6d 70 69 6c 65 28 27 5e   = re.compile('^
0640: 5b 5e 20 5d 2b 20 2d 20 2d 20 5c 5b 2e 2a 5d 20  [^ ]+ - - \[.*] 
0650: 22 47 45 54 20 28 2e 2a 3f 29 28 5c 3f 2e 2a 29  "GET (.*?)(\?.*)
0660: 3f 20 48 54 54 50 2f 31 2e 31 22 20 28 5c 64 2b  ? HTTP/1.1" (\d+
0670: 29 20 5c 64 2b 20 22 28 2e 2a 29 22 20 22 28 2e  ) \d+ "(.*)" "(.
0680: 2a 29 22 24 27 29 0a 09 09 66 6f 72 20 6c 69 6e  *)"$')...for lin
0690: 65 20 69 6e 20 6c 6f 67 5f 66 69 6c 65 3a 0a 09  e in log_file:..
06a0: 09 09 74 68 69 73 5f 6c 69 6e 65 20 3d 20 6c 6f  ..this_line = lo
06b0: 67 5f 6c 69 6e 65 2e 6d 61 74 63 68 28 6c 69 6e  g_line.match(lin
06c0: 65 2e 73 74 72 69 70 28 29 29 0a 09 09 09 69 66  e.strip())....if
06d0: 20 74 68 69 73 5f 6c 69 6e 65 3a 0a 09 09 09 09   this_line:.....
06e0: 75 6e 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 2e  unchecked_files.
06f0: 61 64 64 28 74 68 69 73 5f 6c 69 6e 65 2e 67 72  add(this_line.gr
0700: 6f 75 70 28 31 29 29 0a 0a 09 66 6f 72 20 75 72  oup(1))...for ur
0710: 6c 20 69 6e 20 75 6e 63 68 65 63 6b 65 64 5f 66  l in unchecked_f
0720: 69 6c 65 73 3a 0a 0a 09 09 23 20 63 72 65 61 74  iles:....# creat
0730: 69 6e 67 20 65 6d 70 74 79 20 70 6c 61 63 65 68  ing empty placeh
0740: 6f 6c 64 65 72 20 69 6e 20 69 6e 64 65 78 0a 09  older in index..
0750: 09 69 66 20 6e 6f 74 20 75 72 6c 20 69 6e 20 69  .if not url in i
0760: 6e 64 65 78 3a 0a 09 09 09 69 6e 64 65 78 5b 75  ndex:....index[u
0770: 72 6c 5d 20 3d 20 7b 7d 0a 09 09 72 65 6c 6f 61  rl] = {}...reloa
0780: 64 20 3d 20 46 61 6c 73 65 0a 0a 09 09 23 20 63  d = False....# c
0790: 72 65 61 74 69 6e 67 20 66 69 6c 65 20 6e 61 6d  reating file nam
07a0: 65 20 66 72 6f 6d 20 75 72 6c 0a 09 09 66 69 6c  e from url...fil
07b0: 65 5f 6e 61 6d 65 20 3d 20 6f 70 74 69 6f 6e 73  e_name = options
07c0: 2e 64 69 72 20 2b 20 72 65 2e 63 6f 6d 70 69 6c  .dir + re.compil
07d0: 65 28 27 25 32 30 27 29 2e 73 75 62 28 27 20 27  e('%20').sub(' '
07e0: 2c 20 75 72 6c 29 0a 0a 09 09 23 20 66 6f 72 63  , url)....# forc
07f0: 69 62 6c 79 20 63 68 65 63 6b 69 6e 67 20 66 69  ibly checking fi
0800: 6c 65 20 69 66 20 6e 6f 20 66 69 6c 65 20 70 72  le if no file pr
0810: 65 73 65 6e 74 0a 09 09 69 66 20 6e 6f 74 20 6f  esent...if not o
0820: 73 2e 61 63 63 65 73 73 28 66 69 6c 65 5f 6e 61  s.access(file_na
0830: 6d 65 2c 20 6f 73 2e 52 5f 4f 4b 29 3a 0a 09 09  me, os.R_OK):...
0840: 09 72 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a 0a  .reload = True..
0850: 09 09 23 20 66 6f 72 63 69 62 6c 79 20 63 68 65  ..# forcibly che
0860: 63 6b 69 6e 67 20 66 69 6c 65 20 69 66 20 66 69  cking file if fi
0870: 6c 65 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20  le size doesn't 
0880: 6d 61 74 63 68 20 77 69 74 68 20 69 6e 64 65 78  match with index
0890: 20 64 61 74 61 0a 09 09 65 6c 69 66 20 27 43 6f   data...elif 'Co
08a0: 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 27 20 69 6e  ntent-Length' in
08b0: 20 69 6e 64 65 78 5b 75 72 6c 5d 20 61 6e 64 20   index[url] and 
08c0: 6f 73 2e 73 74 61 74 28 66 69 6c 65 5f 6e 61 6d  os.stat(file_nam
08d0: 65 29 2e 73 74 5f 73 69 7a 65 20 21 3d 20 69 6e  e).st_size != in
08e0: 74 28 69 6e 64 65 78 5b 75 72 6c 5d 5b 27 43 6f  t(index[url]['Co
08f0: 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 27 5d 29 3a  ntent-Length']):
0900: 0a 09 09 09 70 72 69 6e 74 28 27 46 69 6c 65 20  ....print('File 
0910: 73 69 7a 65 20 69 73 27 2c 20 6f 73 2e 73 74 61  size is', os.sta
0920: 74 28 66 69 6c 65 5f 6e 61 6d 65 29 2e 73 74 5f  t(file_name).st_
0930: 73 69 7a 65 2c 20 27 61 6e 64 20 73 74 6f 72 65  size, 'and store
0940: 64 20 66 69 6c 65 20 73 69 7a 65 20 69 73 27 2c  d file size is',
0950: 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 27 43 6f 6e   index[url]['Con
0960: 74 65 6e 74 2d 4c 65 6e 67 74 68 27 5d 29 0a 09  tent-Length'])..
0970: 09 09 72 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a  ..reload = True.
0980: 0a 09 09 23 20 66 6f 72 63 69 62 6c 79 20 63 68  ...# forcibly ch
0990: 65 63 6b 69 6e 67 20 66 69 6c 65 20 69 66 20 69  ecking file if i
09a0: 6e 64 65 78 20 68 6f 64 73 20 50 72 61 67 6d 61  ndex hods Pragma
09b0: 20 68 65 61 64 65 72 0a 09 09 69 66 20 27 50 72   header...if 'Pr
09c0: 61 67 6d 61 27 20 69 6e 20 69 6e 64 65 78 5b 75  agma' in index[u
09d0: 72 6c 5d 20 61 6e 64 20 69 6e 64 65 78 5b 75 72  rl] and index[ur
09e0: 6c 5d 5b 27 50 72 61 67 6d 61 27 5d 20 3d 3d 20  l]['Pragma'] == 
09f0: 27 6e 6f 2d 63 61 63 68 65 27 3a 0a 09 09 09 72  'no-cache':....r
0a00: 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a 0a 09 09  eload = True....
0a10: 23 20 73 6b 69 70 70 69 6e 67 20 66 69 6c 65 20  # skipping file 
0a20: 70 72 6f 63 65 73 73 69 6e 67 20 69 66 20 74 68  processing if th
0a30: 65 72 65 27 73 20 6e 6f 20 6e 65 65 64 20 74 6f  ere's no need to
0a40: 20 72 65 63 68 65 63 6b 20 69 74 20 61 6e 64 20   recheck it and 
0a50: 77 65 20 68 61 76 65 20 63 68 65 63 6b 65 64 20  we have checked 
0a60: 69 74 20 61 74 20 6c 65 61 73 74 20 34 20 68 6f  it at least 4 ho
0a70: 75 72 73 20 61 67 6f 0a 09 09 69 66 20 6e 6f 74  urs ago...if not
0a80: 20 72 65 6c 6f 61 64 20 61 6e 64 20 27 5f 5f 74   reload and '__t
0a90: 69 6d 65 5f 5f 27 20 69 6e 20 69 6e 64 65 78 5b  ime__' in index[
0aa0: 75 72 6c 5d 20 61 6e 64 20 28 64 61 74 65 74 69  url] and (dateti
0ab0: 6d 65 2e 64 61 74 65 74 69 6d 65 2e 6e 6f 77 28  me.datetime.now(
0ac0: 29 20 2d 20 64 61 74 65 74 69 6d 65 2e 74 69 6d  ) - datetime.tim
0ad0: 65 64 65 6c 74 61 28 68 6f 75 72 73 20 3d 20 34  edelta(hours = 4
0ae0: 29 20 2d 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 27  ) - index[url]['
0af0: 5f 5f 74 69 6d 65 5f 5f 27 5d 29 2e 64 61 79 73  __time__']).days
0b00: 20 3c 20 30 3a 0a 09 09 09 63 6f 6e 74 69 6e 75   < 0:....continu
0b10: 65 0a 09 09 70 72 69 6e 74 28 27 43 68 65 63 6b  e...print('Check
0b20: 69 6e 67 20 66 69 6c 65 3a 27 2c 20 75 72 6c 29  ing file:', url)
0b30: 0a 09 09 74 72 79 3a 0a 09 09 09 77 69 74 68 20  ...try:....with 
0b40: 75 72 6c 6c 69 62 2e 72 65 71 75 65 73 74 2e 75  urllib.request.u
0b50: 72 6c 6f 70 65 6e 28 6f 70 74 69 6f 6e 73 2e 72  rlopen(options.r
0b60: 6f 6f 74 20 2b 20 75 72 6c 29 20 61 73 20 73 6f  oot + url) as so
0b70: 75 72 63 65 3a 0a 09 09 09 09 6e 65 77 5f 68 65  urce:.....new_he
0b80: 61 64 65 72 73 20 3d 20 7b 7d 0a 09 09 09 09 68  aders = {}.....h
0b90: 65 61 64 65 72 73 20 3d 20 73 6f 75 72 63 65 2e  eaders = source.
0ba0: 69 6e 66 6f 28 29 0a 0a 09 09 09 09 23 20 73 74  info()......# st
0bb0: 72 69 70 70 69 6e 67 20 75 6e 6e 65 65 64 65 64  ripping unneeded
0bc0: 20 68 65 61 64 65 72 73 20 28 58 58 58 20 6d 61   headers (XXX ma
0bd0: 6b 65 20 74 68 69 73 20 69 6e 70 6c 61 63 65 3f  ke this inplace?
0be0: 29 0a 09 09 09 09 66 6f 72 20 68 65 61 64 65 72  ).....for header
0bf0: 20 69 6e 20 68 65 61 64 65 72 73 3a 0a 09 09 09   in headers:....
0c00: 09 09 69 66 20 68 65 61 64 65 72 20 69 6e 20 64  ..if header in d
0c10: 65 73 63 5f 66 69 65 6c 64 73 3a 0a 09 09 09 09  esc_fields:.....
0c20: 09 09 69 66 20 68 65 61 64 65 72 20 3d 3d 20 27  ..if header == '
0c30: 50 72 61 67 6d 61 27 20 61 6e 64 20 68 65 61 64  Pragma' and head
0c40: 65 72 73 5b 68 65 61 64 65 72 5d 20 21 3d 20 27  ers[header] != '
0c50: 6e 6f 2d 63 61 63 68 65 27 3a 0a 09 09 09 09 09  no-cache':......
0c60: 09 09 70 72 69 6e 74 28 27 50 72 61 67 6d 61 3a  ..print('Pragma:
0c70: 27 2c 20 68 65 61 64 65 72 73 5b 68 65 61 64 65  ', headers[heade
0c80: 72 5d 29 0a 09 09 09 09 09 09 6e 65 77 5f 68 65  r]).......new_he
0c90: 61 64 65 72 73 5b 68 65 61 64 65 72 5d 20 3d 20  aders[header] = 
0ca0: 68 65 61 64 65 72 73 5b 68 65 61 64 65 72 5d 0a  headers[header].
0cb0: 09 09 09 09 09 65 6c 69 66 20 6e 6f 74 20 68 65  .....elif not he
0cc0: 61 64 65 72 20 69 6e 20 69 67 6e 6f 72 65 5f 66  ader in ignore_f
0cd0: 69 65 6c 64 73 3a 0a 09 09 09 09 09 09 70 72 69  ields:.......pri
0ce0: 6e 74 28 27 55 6e 64 65 66 69 6e 65 64 20 68 65  nt('Undefined he
0cf0: 61 64 65 72 27 2c 20 68 65 61 64 65 72 2c 20 27  ader', header, '
0d00: 3a 27 2c 20 68 65 61 64 65 72 73 5b 68 65 61 64  :', headers[head
0d10: 65 72 5d 29 0a 0a 09 09 09 09 23 20 63 6f 6d 70  er])......# comp
0d20: 61 72 69 6e 67 20 68 65 61 64 65 72 73 20 77 69  aring headers wi
0d30: 74 68 20 64 61 74 61 20 66 6f 75 6e 64 20 69 6e  th data found in
0d40: 20 69 6e 64 65 78 0a 09 09 09 09 23 20 69 66 20   index.....# if 
0d50: 61 6e 79 20 68 65 61 64 65 72 20 68 61 73 20 63  any header has c
0d60: 68 61 6e 67 65 64 20 28 65 78 63 65 70 74 20 50  hanged (except P
0d70: 72 61 67 6d 61 29 20 66 69 6c 65 20 69 73 20 66  ragma) file is f
0d80: 75 6c 6c 79 20 64 6f 77 6e 6c 6f 61 64 65 64 0a  ully downloaded.
0d90: 09 09 09 09 23 20 73 61 6d 65 20 69 66 20 77 65  ....# same if we
0da0: 20 67 65 74 20 6d 6f 72 65 20 6f 72 20 6c 65 73   get more or les
0db0: 73 20 68 65 61 64 65 72 73 0a 09 09 09 09 6f 6c  s headers.....ol
0dc0: 64 5f 6b 65 79 73 20 3d 20 73 65 74 28 69 6e 64  d_keys = set(ind
0dd0: 65 78 5b 75 72 6c 5d 2e 6b 65 79 73 28 29 29 0a  ex[url].keys()).
0de0: 09 09 09 09 6f 6c 64 5f 6b 65 79 73 2e 64 69 73  ....old_keys.dis
0df0: 63 61 72 64 28 27 5f 5f 74 69 6d 65 5f 5f 27 29  card('__time__')
0e00: 0a 09 09 09 09 6f 6c 64 5f 6b 65 79 73 2e 64 69  .....old_keys.di
0e10: 73 63 61 72 64 28 27 50 72 61 67 6d 61 27 29 0a  scard('Pragma').
0e20: 09 09 09 09 6d 6f 72 65 5f 6b 65 79 73 20 3d 20  ....more_keys = 
0e30: 73 65 74 28 6e 65 77 5f 68 65 61 64 65 72 73 2e  set(new_headers.
0e40: 6b 65 79 73 28 29 29 20 2d 20 6f 6c 64 5f 6b 65  keys()) - old_ke
0e50: 79 73 0a 09 09 09 09 6d 6f 72 65 5f 6b 65 79 73  ys.....more_keys
0e60: 2e 64 69 73 63 61 72 64 28 27 50 72 61 67 6d 61  .discard('Pragma
0e70: 27 29 0a 09 09 09 09 6c 65 73 73 5f 6b 65 79 73  ').....less_keys
0e80: 20 3d 20 6f 6c 64 5f 6b 65 79 73 20 2d 20 73 65   = old_keys - se
0e90: 74 28 6e 65 77 5f 68 65 61 64 65 72 73 2e 6b 65  t(new_headers.ke
0ea0: 79 73 28 29 29 0a 09 09 09 09 69 66 20 6c 65 6e  ys()).....if len
0eb0: 28 6d 6f 72 65 5f 6b 65 79 73 29 20 3e 20 30 3a  (more_keys) > 0:
0ec0: 0a 09 09 09 09 09 70 72 69 6e 74 28 27 4d 6f 72  ......print('Mor
0ed0: 65 20 68 65 61 64 65 72 73 20 61 70 70 65 61 72  e headers appear
0ee0: 3a 27 2c 20 6d 6f 72 65 5f 6b 65 79 73 29 0a 09  :', more_keys)..
0ef0: 09 09 09 09 72 65 6c 6f 61 64 20 3d 20 54 72 75  ....reload = Tru
0f00: 65 0a 09 09 09 09 65 6c 69 66 20 6c 65 6e 28 6c  e.....elif len(l
0f10: 65 73 73 5f 6b 65 79 73 29 20 3e 20 30 3a 0a 09  ess_keys) > 0:..
0f20: 09 09 09 09 70 72 69 6e 74 28 27 4c 65 73 73 20  ....print('Less 
0f30: 68 65 61 64 65 72 73 20 61 70 70 65 61 72 3a 27  headers appear:'
0f40: 2c 20 6c 65 73 73 5f 6b 65 79 73 29 0a 09 09 09  , less_keys)....
0f50: 09 09 72 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a  ..reload = True.
0f60: 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 09 66  ....else:......f
0f70: 6f 72 20 6b 65 79 20 69 6e 20 69 6e 64 65 78 5b  or key in index[
0f80: 75 72 6c 5d 2e 6b 65 79 73 28 29 3a 0a 09 09 09  url].keys():....
0f90: 09 09 09 69 66 20 6b 65 79 20 6e 6f 74 20 69 6e  ...if key not in
0fa0: 20 28 27 5f 5f 74 69 6d 65 5f 5f 27 2c 20 27 50   ('__time__', 'P
0fb0: 72 61 67 6d 61 27 29 20 61 6e 64 20 6e 6f 74 20  ragma') and not 
0fc0: 69 6e 64 65 78 5b 75 72 6c 5d 5b 6b 65 79 5d 20  index[url][key] 
0fd0: 3d 3d 20 6e 65 77 5f 68 65 61 64 65 72 73 5b 6b  == new_headers[k
0fe0: 65 79 5d 3a 0a 09 09 09 09 09 09 09 70 72 69 6e  ey]:........prin
0ff0: 74 28 27 48 65 61 64 65 72 27 2c 20 6b 65 79 2c  t('Header', key,
1000: 20 27 63 68 61 6e 67 65 64 20 66 72 6f 6d 27 2c   'changed from',
1010: 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 6b 65 79 5d   index[url][key]
1020: 2c 20 27 74 6f 27 2c 20 6e 65 77 5f 68 65 61 64  , 'to', new_head
1030: 65 72 73 5b 6b 65 79 5d 29 0a 09 09 09 09 09 09  ers[key]).......
1040: 09 72 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a 0a  .reload = True..
1050: 09 09 09 09 23 20 64 6f 77 6e 6c 6f 61 64 69 6e  ....# downloadin
1060: 67 20 66 69 6c 65 0a 09 09 09 09 69 66 20 72 65  g file.....if re
1070: 6c 6f 61 64 3a 0a 09 09 09 09 09 69 66 20 27 43  load:......if 'C
1080: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 27 20 69  ontent-Length' i
1090: 6e 20 68 65 61 64 65 72 73 3a 0a 09 09 09 09 09  n headers:......
10a0: 09 70 72 69 6e 74 28 27 44 6f 77 6e 6c 6f 61 64  .print('Download
10b0: 69 6e 67 27 2c 20 68 65 61 64 65 72 73 5b 27 43  ing', headers['C
10c0: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 27 5d 2c  ontent-Length'],
10d0: 20 27 62 79 74 65 73 20 5b 27 2c 20 65 6e 64 3d   'bytes [', end=
10e0: 27 27 29 0a 09 09 09 09 09 65 6c 73 65 3a 0a 09  '')......else:..
10f0: 09 09 09 09 09 70 72 69 6e 74 28 27 44 6f 77 6e  .....print('Down
1100: 6c 6f 61 64 69 6e 67 20 5b 27 2c 20 65 6e 64 3d  loading [', end=
1110: 27 27 29 0a 09 09 09 09 09 73 79 73 2e 73 74 64  '')......sys.std
1120: 6f 75 74 2e 66 6c 75 73 68 28 29 0a 0a 09 09 09  out.flush().....
1130: 09 09 23 20 66 69 6c 65 20 69 73 20 63 72 65 61  ..# file is crea
1140: 74 65 64 20 61 74 20 74 65 6d 70 6f 72 61 72 79  ted at temporary
1150: 20 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 6f   location and mo
1160: 76 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 6e 6c  ved in place onl
1170: 79 20 77 68 65 6e 20 64 6f 77 6e 6c 6f 61 64 20  y when download 
1180: 63 6f 6d 70 6c 65 74 65 73 0a 09 09 09 09 09 74  completes......t
1190: 65 6d 70 5f 66 69 6c 65 20 3d 20 6f 70 65 6e 28  emp_file = open(
11a0: 6f 70 74 69 6f 6e 73 2e 64 69 72 20 2b 20 27 2f  options.dir + '/
11b0: 2e 74 6d 70 27 2c 20 27 77 62 27 29 0a 09 09 09  .tmp', 'wb')....
11c0: 09 09 62 75 66 66 65 72 20 3d 20 73 6f 75 72 63  ..buffer = sourc
11d0: 65 2e 72 65 61 64 28 34 30 39 36 29 0a 09 09 09  e.read(4096)....
11e0: 09 09 77 68 69 6c 65 20 6c 65 6e 28 62 75 66 66  ..while len(buff
11f0: 65 72 29 20 3e 20 30 3a 0a 09 09 09 09 09 09 74  er) > 0:.......t
1200: 65 6d 70 5f 66 69 6c 65 2e 77 72 69 74 65 28 62  emp_file.write(b
1210: 75 66 66 65 72 29 0a 09 09 09 09 09 09 70 72 69  uffer).......pri
1220: 6e 74 28 27 2e 27 2c 20 65 6e 64 3d 27 27 29 0a  nt('.', end='').
1230: 09 09 09 09 09 09 73 79 73 2e 73 74 64 6f 75 74  ......sys.stdout
1240: 2e 66 6c 75 73 68 28 29 0a 09 09 09 09 09 09 62  .flush().......b
1250: 75 66 66 65 72 20 3d 20 73 6f 75 72 63 65 2e 72  uffer = source.r
1260: 65 61 64 28 34 30 39 36 29 0a 09 09 09 09 09 74  ead(4096)......t
1270: 65 6d 70 5f 66 69 6c 65 2e 63 6c 6f 73 65 28 29  emp_file.close()
1280: 0a 09 09 09 09 09 70 72 69 6e 74 28 27 5d 27 29  ......print(']')
1290: 0a 09 09 09 09 09 6f 73 2e 72 65 6e 61 6d 65 73  ......os.renames
12a0: 28 6f 70 74 69 6f 6e 73 2e 64 69 72 20 2b 20 27  (options.dir + '
12b0: 2f 2e 74 6d 70 27 2c 20 66 69 6c 65 5f 6e 61 6d  /.tmp', file_nam
12c0: 65 29 0a 0a 09 09 09 09 63 68 65 63 6b 65 64 5f  e)......checked_
12d0: 66 69 6c 65 73 20 2b 3d 20 31 0a 0a 09 09 09 09  files += 1......
12e0: 23 20 73 74 6f 72 69 6e 67 20 6e 65 77 20 74 69  # storing new ti
12f0: 6d 65 20 6d 61 72 6b 20 61 6e 64 20 73 74 6f 72  me mark and stor
1300: 69 6e 67 20 6e 65 77 20 68 65 61 64 65 72 73 0a  ing new headers.
1310: 09 09 09 09 6e 65 77 5f 68 65 61 64 65 72 73 5b  ....new_headers[
1320: 27 5f 5f 74 69 6d 65 5f 5f 27 5d 20 3d 20 64 61  '__time__'] = da
1330: 74 65 74 69 6d 65 2e 64 61 74 65 74 69 6d 65 2e  tetime.datetime.
1340: 6e 6f 77 28 29 0a 09 09 09 09 69 6e 64 65 78 5b  now().....index[
1350: 75 72 6c 5d 20 3d 20 6e 65 77 5f 68 65 61 64 65  url] = new_heade
1360: 72 73 0a 0a 09 09 65 78 63 65 70 74 20 75 72 6c  rs....except url
1370: 6c 69 62 2e 65 72 72 6f 72 2e 48 54 54 50 45 72  lib.error.HTTPEr
1380: 72 6f 72 20 61 73 20 65 72 72 6f 72 3a 0a 09 09  ror as error:...
1390: 09 23 20 69 6e 20 63 61 73 65 20 6f 66 20 65 72  .# in case of er
13a0: 72 6f 72 20 77 65 20 64 6f 6e 27 74 20 6e 65 65  ror we don't nee
13b0: 64 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67  d to do anything
13c0: 20 61 63 74 75 61 6c 6c 79 2c 0a 09 09 09 23 20   actually,....# 
13d0: 69 66 20 66 69 6c 65 20 64 6f 77 6e 6c 6f 61 64  if file download
13e0: 20 73 74 61 6c 6c 73 20 6f 72 20 66 61 69 6c 73   stalls or fails
13f0: 20 74 68 65 20 66 69 6c 65 20 77 6f 75 6c 64 20   the file would 
1400: 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 74 6f 20  not be moved to 
1410: 69 74 27 73 20 6c 6f 63 61 74 69 6f 6e 0a 09 09  it's location...
1420: 09 70 72 69 6e 74 28 65 72 72 6f 72 29 0a 0a 09  .print(error)...
1430: 70 72 69 6e 74 28 27 5b 27 2c 20 6c 65 6e 28 75  print('[', len(u
1440: 6e 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 29 2c  nchecked_files),
1450: 20 27 2f 27 2c 20 63 68 65 63 6b 65 64 5f 66 69   '/', checked_fi
1460: 6c 65 73 2c 20 27 5d 27 29 0a 0a 09 23 20 63 68  les, ']')...# ch
1470: 65 63 6b 69 6e 67 20 69 66 20 74 68 65 72 65 20  ecking if there 
1480: 77 65 72 65 20 61 6e 79 20 66 69 6c 65 73 20 64  were any files d
1490: 6f 77 6e 6c 6f 61 64 65 64 2c 20 69 66 20 79 65  ownloaded, if ye
14a0: 73 20 2d 20 72 65 73 74 61 72 74 69 6e 67 20 73  s - restarting s
14b0: 65 71 75 65 6e 63 65 0a 09 69 66 20 63 68 65 63  equence..if chec
14c0: 6b 65 64 5f 66 69 6c 65 73 20 3d 3d 20 30 3a 0a  ked_files == 0:.
14d0: 09 09 62 72 65 61 6b 0a                          ..break.