Samesite - proxy that can cache partial transfers

Annotation For samesite.py
anonymous

Annotation For samesite.py

Origin for each line in samesite.py from check-in 80f8e3804a:

08ae38b6ce 2010-06-25 c.kworr@b84a3: #!/usr/bin/env python3.1
08ae38b6ce 2010-06-25 c.kworr@b84a3: 
08ae38b6ce 2010-06-25 c.kworr@b84a3: import datetime, http.cookiejar, optparse, os, sys, shelve, re, urllib.request
08ae38b6ce 2010-06-25 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: from spacemap import SpaceMap
80f8e3804a 2010-08-20 c.kworr@b84a3: 
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser = optparse.OptionParser()
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-v', '--verbose', action = 'store_true', dest = 'verbose', help = 'turns on verbose status notifications', metavar = 'bool', default = False)
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-d', '--dir', action = 'store', dest = 'dir', help = 'specify directory where the files should be stored', metavar = 'string', default = None)
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-r', '--root', action = 'store', dest = 'root', help = 'specify a site from which data should be mirrored', metavar = 'string', default = None)
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-l', '--log', action = 'store', dest = 'log', help = 'specify a log file to process', metavar = 'string', default = None)
38b25713eb 2010-07-26 c.kworr@b84a3: parser.add_option('-e', '--skip-etag', action = 'store_true', dest = 'noetag', help = 'do not process etags', metavar = 'bool', default = False)
80f8e3804a 2010-08-20 c.kworr@b84a3: parser.add_option('-p', '--port', action = 'store', dest = 'port', help = 'listen on this port for incoming connections', metavar = 'integer', default = None)
80f8e3804a 2010-08-20 c.kworr@b84a3: parser.add_option('-n', '--no-update', action = 'store_true', dest = 'noupdate', help = 'do not update already downloaded files', metavar = 'bool', default = 'False')
08ae38b6ce 2010-06-25 c.kworr@b84a3: (options, args) = parser.parse_args()
08ae38b6ce 2010-06-25 c.kworr@b84a3: 
38b25713eb 2010-07-26 c.kworr@b84a3: assert options.dir, 'Directory not specified'
38b25713eb 2010-07-26 c.kworr@b84a3: assert options.root, 'Server not specified'
80f8e3804a 2010-08-20 c.kworr@b84a3: assert options.log or options.port, 'Log file or port not specified'
80f8e3804a 2010-08-20 c.kworr@b84a3: assert options.port or os.access(options.log, os.R_OK), 'Log file unreadable'
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: optionsDirWithSep = re.compile('^(.*?)/?$').match(options.dir)
80f8e3804a 2010-08-20 c.kworr@b84a3: if optionsDirWithSep:
80f8e3804a 2010-08-20 c.kworr@b84a3: 	options.dir = optionsDirWithSep.group(1)
08ae38b6ce 2010-06-25 c.kworr@b84a3: 
08ae38b6ce 2010-06-25 c.kworr@b84a3: # this is file index - everything is stored in this file
80f8e3804a 2010-08-20 c.kworr@b84a3: # _parts - list of stored parts of file
80f8e3804a 2010-08-20 c.kworr@b84a3: # _time - last time the file was checked
80f8e3804a 2010-08-20 c.kworr@b84a3: # everything else is just the headers
80f8e3804a 2010-08-20 c.kworr@b84a3: index = shelve.open(options.dir + os.sep + '.index')
38b25713eb 2010-07-26 c.kworr@b84a3: desc_fields = ('Content-Length', 'Pragma', 'Last-Modified')
38b25713eb 2010-07-26 c.kworr@b84a3: ignore_fields = ('Accept-Ranges', 'Age', 'Cache-Control', 'Connection', 'Content-Type', 'Date', 'Expires', 'Server', 'Via', 'X-Cache', 'X-Cache-Lookup', 'X-Powered-By')
38b25713eb 2010-07-26 c.kworr@b84a3: 
38b25713eb 2010-07-26 c.kworr@b84a3: if not options.noetag:
38b25713eb 2010-07-26 c.kworr@b84a3: 	desc_fields += 'ETag',
38b25713eb 2010-07-26 c.kworr@b84a3: else:
38b25713eb 2010-07-26 c.kworr@b84a3: 	ignore_fields += 'ETag',
38b25713eb 2010-07-26 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: block_size = 4096
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: temp_file_name = options.dir + os.sep + '.tmp'
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: '''
80f8e3804a 2010-08-20 c.kworr@b84a3: # later, kqueue would be good but later
80f8e3804a 2010-08-20 c.kworr@b84a3: class Connection:
80f8e3804a 2010-08-20 c.kworr@b84a3: 	__slots__ = frozenset(('__address', '__input', '__socket', '__status', 'error', 'method', 'url', 'http_version'))
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 	def __init__(self, socket, address):
80f8e3804a 2010-08-20 c.kworr@b84a3: 		self.__address = address
80f8e3804a 2010-08-20 c.kworr@b84a3: 		self.__input = b''
80f8e3804a 2010-08-20 c.kworr@b84a3: 		self.__socket = socket
80f8e3804a 2010-08-20 c.kworr@b84a3: 		self.__status = 0
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 	def read(self, kev):
80f8e3804a 2010-08-20 c.kworr@b84a3: 		buffer = self.__socket.recv(kev.data)
80f8e3804a 2010-08-20 c.kworr@b84a3: 		exhausted = False
80f8e3804a 2010-08-20 c.kworr@b84a3: 		if len(buffer) == 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 			eof = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 		else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 			self.__input += buffer
80f8e3804a 2010-08-20 c.kworr@b84a3: 			while not exhausted:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if self.__status == -1:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					exhausted = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 				elif self.__status == 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					endstring = self.__input.find(b'\n')
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if endstring > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Processing request line.')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						line = self.__input[:endstring].decode('ascii')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						self.__input = self.__input[endstring + 1:]
80f8e3804a 2010-08-20 c.kworr@b84a3: 						isRequest = re.compile('(GET) ([^ ]+) HTTP/(1\.0)').match(line)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if not isRequest:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.error = 'Not a HTTP connection.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.__status = -1
80f8e3804a 2010-08-20 c.kworr@b84a3: 						else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.method = isRequest.group(1)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.url = isRequest.group(2)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.http_version = isRequest.group(3)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.__status = 1
80f8e3804a 2010-08-20 c.kworr@b84a3: 					else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						exhausted = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 				elif self.__status == 1:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					endstring = self.__input.find(b'\n')
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if endstring > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Processing header line.' + repr(self.__input))
80f8e3804a 2010-08-20 c.kworr@b84a3: 						line = self.__input[:endstring].decode('ascii')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						self.__input = self.__input[endstring + 1:]
80f8e3804a 2010-08-20 c.kworr@b84a3: 						isHeader = re.compile('([^:]*): +(.*)').match(line)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if not isHeader:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.error = 'Bad header.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 							return(False)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						# process header here
80f8e3804a 2010-08-20 c.kworr@b84a3: 					elif endstring == 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						self.__status = 2
80f8e3804a 2010-08-20 c.kworr@b84a3: 					else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						exhausted = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 	def write(self, kev):
80f8e3804a 2010-08-20 c.kworr@b84a3: 		pass
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: if options.port:
80f8e3804a 2010-08-20 c.kworr@b84a3: 	import select, socket
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
80f8e3804a 2010-08-20 c.kworr@b84a3: 	try:
80f8e3804a 2010-08-20 c.kworr@b84a3: 		sock.bind(('127.0.0.1', int(options.port)))
80f8e3804a 2010-08-20 c.kworr@b84a3: 		sock.listen(-1)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		kq = select.kqueue()
80f8e3804a 2010-08-20 c.kworr@b84a3: 		assert kq.fileno() != -1, "Fatal error: can't initialise kqueue."
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		kq.control([select.kevent(sock, select.KQ_FILTER_READ, select.KQ_EV_ADD)], 0)
80f8e3804a 2010-08-20 c.kworr@b84a3: 		timeout = None
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		connections = {sock.fileno(): None}
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		while True:
80f8e3804a 2010-08-20 c.kworr@b84a3: 			kevs = kq.control(None, 1, timeout)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			for kev in kevs:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if type(connections[kev.ident]) == Connection:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					print(kev.ident, kev.data, kev.filter, kev.flags)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					assert kev.data != 0, 'No data available.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if kev.filter == select.KQ_FILTER_READ:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						connections[kev.ident].read(kev)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					elif kev.filter == select.KQ_FILTER_WRITE:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						connections[kev.ident].write(kev)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						assert kev.filter in (select.KQ_FILTER_READ, select.KQ_FILTER_WRITE), 'Do we support other filters?'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					(conn, addr) = sock.accept()
80f8e3804a 2010-08-20 c.kworr@b84a3: 					print('Connection from ' + repr(addr))
80f8e3804a 2010-08-20 c.kworr@b84a3: 					kq.control([select.kevent(conn, select.KQ_FILTER_READ, select.KQ_EV_ADD)], 0)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					connections[conn.fileno()] = Connection(conn, addr)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if kev.flags >> 15 == 1:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					kq.control([select.kevent(kev.ident, select.KQ_FILTER_READ, select.KQ_EV_DELETE)], 0)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					kq.control([select.kevent(kev.ident, select.KQ_FILTER_WRITE, select.KQ_EV_DELETE)], 0)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					del(connections[kev.ident])
80f8e3804a 2010-08-20 c.kworr@b84a3: 	finally:
80f8e3804a 2010-08-20 c.kworr@b84a3: 		sock.close()
80f8e3804a 2010-08-20 c.kworr@b84a3: '''
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: if options.port:
80f8e3804a 2010-08-20 c.kworr@b84a3: 	import http.server
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 	class MyRequestHandler(http.server.BaseHTTPRequestHandler):
80f8e3804a 2010-08-20 c.kworr@b84a3: 		def __process(self):
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# reload means file needs to be reloaded to serve request
80f8e3804a 2010-08-20 c.kworr@b84a3: 			reload = False
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# recheck means file needs to be checked, this also means that if file hav been modified we can serve older copy
80f8e3804a 2010-08-20 c.kworr@b84a3: 			recheck = False
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# file_stat means file definitely exists
80f8e3804a 2010-08-20 c.kworr@b84a3: 			file_stat = None
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# requested_ranges holds data about any range requested
80f8e3804a 2010-08-20 c.kworr@b84a3: 			requested_ranges = None
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# records holds data from index locally, should be written back upon successfull completion
80f8e3804a 2010-08-20 c.kworr@b84a3: 			record = None
80f8e3804a 2010-08-20 c.kworr@b84a3: 			info = 'Checking file: ' + self.path
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			proxy_ignored = ('Accept', 'Accept-Encoding',
80f8e3804a 2010-08-20 c.kworr@b84a3: 				'Cache-Control', 'Connection',
80f8e3804a 2010-08-20 c.kworr@b84a3: 				'Host',
80f8e3804a 2010-08-20 c.kworr@b84a3: 				'User-Agent',
80f8e3804a 2010-08-20 c.kworr@b84a3: 				'Via',
80f8e3804a 2010-08-20 c.kworr@b84a3: 				'X-Forwarded-For',
80f8e3804a 2010-08-20 c.kworr@b84a3: 			)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			print('Command:', self.command)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			for header in self.headers:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if header in proxy_ignored:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					pass
80f8e3804a 2010-08-20 c.kworr@b84a3: 				elif header in ('Range'):
80f8e3804a 2010-08-20 c.kworr@b84a3: 					isRange = re.compile('bytes=(\d+)-(\d+)').match(self.headers[header])
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if isRange:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						requested_ranges = SpaceMap({int(isRange.group(1)): int(isRange.group(2)) + 1})
80f8e3804a 2010-08-20 c.kworr@b84a3: 					else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						return()
80f8e3804a 2010-08-20 c.kworr@b84a3: 				else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					print('Unknown header - ', header, ': ', self.headers[header], sep='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 					return()
80f8e3804a 2010-08-20 c.kworr@b84a3: 				print(header, self.headers[header])
80f8e3804a 2010-08-20 c.kworr@b84a3: 			print(self.path)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# creating empty placeholder in index
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# if there's no space map and there's no file in real directory - we have no file
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# if there's an empty space map - file is full
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# space map generally covers every bit of file we don't posess currently
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not self.path in index:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				info += '\nThis one is new.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 				record = {'_parts': None}
80f8e3804a 2010-08-20 c.kworr@b84a3: 			else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				record = index[self.path]
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if '_parts' in index[self.path]:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					print(record['_parts'])
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if index[self.path]['_parts'] == {0: -1}:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						index[self.path]['_parts'] = None
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# creating file name from self.path
80f8e3804a 2010-08-20 c.kworr@b84a3: 			file_name = options.dir + os.sep + re.compile('%20').sub(' ', self.path)
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# partial file or unfinished download
80f8e3804a 2010-08-20 c.kworr@b84a3: 			temp_name = options.dir + os.sep + '.parts' + re.compile('%20').sub(' ', self.path)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# forcibly checking file if no file present
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if os.access(file_name, os.R_OK):
80f8e3804a 2010-08-20 c.kworr@b84a3: 				file_stat = os.stat(file_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 			elif '_parts' in record and os.access(temp_name, os.R_OK):
80f8e3804a 2010-08-20 c.kworr@b84a3: 				file_stat = os.stat(temp_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 			elif not reload:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				info += '\nFile not found or inaccessible.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# forcibly checking file if file size doesn't match with index data
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not reload:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if '_parts' in record and record['_parts'] == SpaceMap():
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if 'Content-Length' in record and file_stat and file_stat.st_size != int(record['Content-Length']):
80f8e3804a 2010-08-20 c.kworr@b84a3: 						info += '\nFile size is {} and stored file size is {}.'.format(file_stat.st_size, record['Content-Length'])
80f8e3804a 2010-08-20 c.kworr@b84a3: 						reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# forcibly checking file if index holds Pragma header
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not reload and 'Pragma' in record and record['Pragma'] == 'no-cache':
80f8e3804a 2010-08-20 c.kworr@b84a3: 				info +='\nPragma on: recheck imminent.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				recheck = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# skipping file processing if there's no need to recheck it and we have checked it at least 4 hours ago
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not recheck and not reload and '_time' in record and (datetime.datetime.now() - datetime.timedelta(hours = 4) - record['_time']).days < 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				recheck = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			print(info)
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if reload or recheck:
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 				try:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					request = options.root + self.path
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if requested_ranges != None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if '_parts' in record and record['_parts'] != None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							needed = record['_parts'] & requested_ranges
80f8e3804a 2010-08-20 c.kworr@b84a3: 						else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							needed = requested_ranges
80f8e3804a 2010-08-20 c.kworr@b84a3: 						ranges = ()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Requesting ranges:', ranges)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Not stored ranges:', record['_parts'])
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Requested ranges:', requested_ranges)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Needed ranges:', needed)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						needed.rewind()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						while True:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							range = needed.pop()
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if range[0] == None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								break
80f8e3804a 2010-08-20 c.kworr@b84a3: 							ranges += '{}-{}'.format(range[0], range[1] - 1),
80f8e3804a 2010-08-20 c.kworr@b84a3: 						request = urllib.request.Request(request, headers = {'Range': 'bytes=' + ','.join(ranges)})
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 					with urllib.request.urlopen(request) as source:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						new_record = {}
80f8e3804a 2010-08-20 c.kworr@b84a3: 						new_record['_parts'] = record['_parts']
80f8e3804a 2010-08-20 c.kworr@b84a3: 						headers = source.info()
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						# stripping unneeded headers (XXX make this inplace?)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						for header in headers:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if header in desc_fields:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								#if header == 'Pragma' and headers[header] != 'no-cache':
80f8e3804a 2010-08-20 c.kworr@b84a3: 								print(header, headers[header])
80f8e3804a 2010-08-20 c.kworr@b84a3: 								if header == 'Content-Length':
80f8e3804a 2010-08-20 c.kworr@b84a3: 									if 'Content-Range' not in headers:
80f8e3804a 2010-08-20 c.kworr@b84a3: 										new_record[header] = headers[header]
80f8e3804a 2010-08-20 c.kworr@b84a3: 								else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 									new_record[header] = headers[header]
80f8e3804a 2010-08-20 c.kworr@b84a3: 							elif header == 'Content-Range':
80f8e3804a 2010-08-20 c.kworr@b84a3: 								range = re.compile('^bytes (\d+)-(\d+)/(\d+)$').match(headers[header])
80f8e3804a 2010-08-20 c.kworr@b84a3: 								if range:
80f8e3804a 2010-08-20 c.kworr@b84a3: 									new_record['Content-Length'] = range.group(3)
80f8e3804a 2010-08-20 c.kworr@b84a3: 								else:	
80f8e3804a 2010-08-20 c.kworr@b84a3: 									assert False, 'Content-Range unrecognized.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 							elif not header in ignore_fields:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								print('Undefined header "', header, '": ', headers[header], sep='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if new_record['_parts'] == None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							new_record['_parts'] = SpaceMap({0: int(new_record['Content-Length'])})
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print(new_record)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						# comparing headers with data found in index
80f8e3804a 2010-08-20 c.kworr@b84a3: 						# if any header has changed (except Pragma) file is fully downloaded
80f8e3804a 2010-08-20 c.kworr@b84a3: 						# same if we get more or less headers
80f8e3804a 2010-08-20 c.kworr@b84a3: 						old_keys = set(record.keys())
80f8e3804a 2010-08-20 c.kworr@b84a3: 						old_keys.discard('_time')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						old_keys.discard('Pragma')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						more_keys = set(new_record.keys()) - old_keys
80f8e3804a 2010-08-20 c.kworr@b84a3: 						more_keys.discard('Pragma')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						less_keys = old_keys - set(new_record.keys())
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if len(more_keys) > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if not len(old_keys) == 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								print('More headers appear:', more_keys)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 						elif len(less_keys) > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							print('Less headers appear:', less_keys)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							for key in record.keys():
80f8e3804a 2010-08-20 c.kworr@b84a3: 								if key[0] != '_' and key != 'Pragma' and not record[key] == new_record[key]:
80f8e3804a 2010-08-20 c.kworr@b84a3: 									print('Header "', key, '" changed from [', record[key], '] to [', new_record[key], ']', sep='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 									reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if reload:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							print('Reloading.')
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if os.access(temp_name, os.R_OK):
80f8e3804a 2010-08-20 c.kworr@b84a3: 								os.unlink(temp_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if os.access(file_name, os.R_OK):
80f8e3804a 2010-08-20 c.kworr@b84a3: 								os.unlink(file_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						# downloading file or segment
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if 'Content-Length' in new_record:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if requested_ranges == None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								requested_ranges = new_record['_parts']
80f8e3804a 2010-08-20 c.kworr@b84a3: 							else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								if len(requested_ranges) > 1:
80f8e3804a 2010-08-20 c.kworr@b84a3: 									print("Multipart requests currently not supported.")
80f8e3804a 2010-08-20 c.kworr@b84a3: 									assert False, 'Skip this one for now.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 						else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							assert False, 'No Content-Length or Content-Range header.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if reload:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							new_record['_time'] = datetime.datetime.now()
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if self.command not in ('HEAD'):
80f8e3804a 2010-08-20 c.kworr@b84a3: 								# file is created at temporary location and moved in place only when download completes
80f8e3804a 2010-08-20 c.kworr@b84a3: 								if not os.access(temp_name, os.R_OK):
80f8e3804a 2010-08-20 c.kworr@b84a3: 									empty_name = options.dir + os.sep + '.tmp'
80f8e3804a 2010-08-20 c.kworr@b84a3: 									with open(empty_name, 'w+b') as some_file:
80f8e3804a 2010-08-20 c.kworr@b84a3: 										pass
80f8e3804a 2010-08-20 c.kworr@b84a3: 									os.renames(empty_name, temp_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 								temp_file = open(temp_name, 'r+b')
80f8e3804a 2010-08-20 c.kworr@b84a3: 								requested_ranges.rewind()
80f8e3804a 2010-08-20 c.kworr@b84a3: 								while True:
80f8e3804a 2010-08-20 c.kworr@b84a3: 									(start, end) = requested_ranges.pop()
80f8e3804a 2010-08-20 c.kworr@b84a3: 									if start == None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 										break
80f8e3804a 2010-08-20 c.kworr@b84a3: 									stream_last = start
80f8e3804a 2010-08-20 c.kworr@b84a3: 									old_record = new_record
80f8e3804a 2010-08-20 c.kworr@b84a3: 									if end - start < block_size:
80f8e3804a 2010-08-20 c.kworr@b84a3: 										req_block_size = end - start
80f8e3804a 2010-08-20 c.kworr@b84a3: 									else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 										req_block_size = block_size
80f8e3804a 2010-08-20 c.kworr@b84a3: 									buffer = source.read(req_block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 									print(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 									length = len(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 									while length > 0 and stream_last < end:
80f8e3804a 2010-08-20 c.kworr@b84a3: 										stream_pos = stream_last + length
80f8e3804a 2010-08-20 c.kworr@b84a3: 										assert not stream_pos > end, 'Received more data then requested: pos:{} start:{} end:{}.'.format(stream_pos, start, end)
80f8e3804a 2010-08-20 c.kworr@b84a3: 										print('Writing', length, 'bytes to temp file at position', stream_last)
80f8e3804a 2010-08-20 c.kworr@b84a3: 										temp_file.seek(stream_last)
80f8e3804a 2010-08-20 c.kworr@b84a3: 										temp_file.write(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 										new_record['_parts'] = new_record['_parts'] - SpaceMap({stream_last: stream_pos})
80f8e3804a 2010-08-20 c.kworr@b84a3: 										print(new_record)
80f8e3804a 2010-08-20 c.kworr@b84a3: 										index[self.path] = old_record
80f8e3804a 2010-08-20 c.kworr@b84a3: 										index.sync()
80f8e3804a 2010-08-20 c.kworr@b84a3: 										old_record = new_record
80f8e3804a 2010-08-20 c.kworr@b84a3: 										stream_last = stream_pos
80f8e3804a 2010-08-20 c.kworr@b84a3: 										if end - stream_last < block_size:
80f8e3804a 2010-08-20 c.kworr@b84a3: 											req_block_size = end - stream_last
80f8e3804a 2010-08-20 c.kworr@b84a3: 										buffer = source.read(req_block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 										print(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 										length = len(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 								print(new_record)
80f8e3804a 2010-08-20 c.kworr@b84a3: 								index[self.path] = new_record
80f8e3804a 2010-08-20 c.kworr@b84a3: 								index.sync()
80f8e3804a 2010-08-20 c.kworr@b84a3: 								temp_file.close()
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 								# moving downloaded data to real file
80f8e3804a 2010-08-20 c.kworr@b84a3: 								if new_record['_parts'] == SpaceMap():
80f8e3804a 2010-08-20 c.kworr@b84a3: 									if type(request) != str:
80f8e3804a 2010-08-20 c.kworr@b84a3: 										# just moving
80f8e3804a 2010-08-20 c.kworr@b84a3: 										# drop old dirs XXX
80f8e3804a 2010-08-20 c.kworr@b84a3: 										print('Moving temporary file to new destination.')
80f8e3804a 2010-08-20 c.kworr@b84a3: 										os.renames(temp_name, file_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 				except urllib.error.HTTPError as error:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# in case of error we don't need to do anything actually,
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# if file download stalls or fails the file would not be moved to it's location
80f8e3804a 2010-08-20 c.kworr@b84a3: 					print(error)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if self.command == 'HEAD':
80f8e3804a 2010-08-20 c.kworr@b84a3: 				self.send_response(200)
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if 'Content-Length' in index[self.path]:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					self.send_header('Content-Length', index[self.path]['Content-Length'])
80f8e3804a 2010-08-20 c.kworr@b84a3: 				self.send_header('Accept-Ranges', 'bytes')
80f8e3804a 2010-08-20 c.kworr@b84a3: 				self.send_header('Content-Type', 'application/octet-stream')
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if 'Last-Modified' in index[self.path]:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					self.send_header('Last-Modified', index[self.path]['Last-Modified'])
80f8e3804a 2010-08-20 c.kworr@b84a3: 				self.end_headers()
80f8e3804a 2010-08-20 c.kworr@b84a3: 			else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if index[self.path]['_parts'] != SpaceMap():
80f8e3804a 2010-08-20 c.kworr@b84a3: 					file_name = temp_name
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 				with open(file_name, 'rb') as real_file:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					file_stat = os.stat(file_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					self.send_response(200)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					self.send_header('Last-Modified', index[self.path]['Last-Modified'])
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if requested_ranges != None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						ranges = ()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						requested_ranges.rewind()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						while True:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							pair = requested_ranges.pop()
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if pair[0] == None:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								break
80f8e3804a 2010-08-20 c.kworr@b84a3: 							ranges += '{}-{}'.format(pair[0], str(pair[1] - 1)),
80f8e3804a 2010-08-20 c.kworr@b84a3: 						self.send_header('Content-Range', 'bytes ' + ','.join(ranges) + '/' + index[self.path]['Content-Length'])
80f8e3804a 2010-08-20 c.kworr@b84a3: 					else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						self.send_header('Content-Length', str(file_stat.st_size))
80f8e3804a 2010-08-20 c.kworr@b84a3: 						requested_ranges = SpaceMap({0: file_stat.st_size})
80f8e3804a 2010-08-20 c.kworr@b84a3: 					self.send_header('Content-Type', 'application/octet-stream')
80f8e3804a 2010-08-20 c.kworr@b84a3: 					self.end_headers()
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if self.command in ('GET'):
80f8e3804a 2010-08-20 c.kworr@b84a3: 						requested_ranges.rewind()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						(start, end) = requested_ranges.pop()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Seeking file to position', start)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						real_file.seek(start)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if block_size > end - start:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							req_block_size = end - start
80f8e3804a 2010-08-20 c.kworr@b84a3: 						else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							req_block_size = block_size
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('block_size is', req_block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						buffer = real_file.read(req_block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						length = len(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						while length > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							self.wfile.write(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							start += len(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if req_block_size > end - start:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								req_block_size = end - start
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if req_block_size == 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								break
80f8e3804a 2010-08-20 c.kworr@b84a3: 							print('block_size is', req_block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							buffer = real_file.read(req_block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							length = len(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					
80f8e3804a 2010-08-20 c.kworr@b84a3: 		def do_HEAD(self):
80f8e3804a 2010-08-20 c.kworr@b84a3: 			return self.__process()
80f8e3804a 2010-08-20 c.kworr@b84a3: 		def do_GET(self):
80f8e3804a 2010-08-20 c.kworr@b84a3: 			return self.__process()
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 	server = http.server.HTTPServer(('127.0.0.1', int(options.port)), MyRequestHandler)
80f8e3804a 2010-08-20 c.kworr@b84a3: 	server.serve_forever()
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 	while True:
80f8e3804a 2010-08-20 c.kworr@b84a3: 		unchecked_files = set()
80f8e3804a 2010-08-20 c.kworr@b84a3: 		checked_files = 0
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		# reading log and storing found urls for processing
80f8e3804a 2010-08-20 c.kworr@b84a3: 		# check file mtime XXX
80f8e3804a 2010-08-20 c.kworr@b84a3: 		with open(options.log, 'r') as log_file:
80f8e3804a 2010-08-20 c.kworr@b84a3: 			log_line = re.compile('^[^ ]+ - - \[.*] "(GET|HEAD) (.*?)(\?.*)? HTTP/1.1" (\d+) \d+ "(.*)" "(.*)"$')
80f8e3804a 2010-08-20 c.kworr@b84a3: 			for line in log_file:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				this_line = log_line.match(line.strip())
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if this_line:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					unchecked_files.add(this_line.group(2))
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		for url in unchecked_files:
80f8e3804a 2010-08-20 c.kworr@b84a3: 			reload = False
80f8e3804a 2010-08-20 c.kworr@b84a3: 			recheck = False
80f8e3804a 2010-08-20 c.kworr@b84a3: 			info = 'Checking file: ' + url
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# creating empty placeholder in index
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not url in index:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				info += '\nThis one is new.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				index[url] = {}
80f8e3804a 2010-08-20 c.kworr@b84a3: 				reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# creating file name from url
80f8e3804a 2010-08-20 c.kworr@b84a3: 			file_name = options.dir + re.compile('%20').sub(' ', url)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# forcibly checking file if no file present
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not reload and not os.access(file_name, os.R_OK):
80f8e3804a 2010-08-20 c.kworr@b84a3: 				info += '\nFile not found or inaccessible.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# forcibly checking file if file size doesn't match with index data
80f8e3804a 2010-08-20 c.kworr@b84a3: 			elif not reload and 'Content-Length' in index[url] and os.stat(file_name).st_size != int(index[url]['Content-Length']):
80f8e3804a 2010-08-20 c.kworr@b84a3: 				info += '\nFile size is ' + os.stat(file_name).st_size + ' and stored file size is ' + index[url]['Content-Length'] + '.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# forcibly checking file if index hods Pragma header
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not reload and 'Pragma' in index[url] and index[url]['Pragma'] == 'no-cache':
80f8e3804a 2010-08-20 c.kworr@b84a3: 				info +='\nPragma on: recheck imminent.'
80f8e3804a 2010-08-20 c.kworr@b84a3: 				recheck = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			# skipping file processing if there's no need to recheck it and we have checked it at least 4 hours ago
80f8e3804a 2010-08-20 c.kworr@b84a3: 			if not recheck and not reload and (options.noupdate or ('_time' in index[url] and (datetime.datetime.now() - datetime.timedelta(hours = 4) - index[url]['_time']).days < 0)):
80f8e3804a 2010-08-20 c.kworr@b84a3: 				if options.verbose:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					print(info)
80f8e3804a 2010-08-20 c.kworr@b84a3: 				continue
80f8e3804a 2010-08-20 c.kworr@b84a3: 			else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				print(info)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			try:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				with urllib.request.urlopen(options.root + url) as source:
80f8e3804a 2010-08-20 c.kworr@b84a3: 					new_headers = {}
80f8e3804a 2010-08-20 c.kworr@b84a3: 					headers = source.info()
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# stripping unneeded headers (XXX make this inplace?)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					for header in headers:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if header in desc_fields:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if header == 'Pragma' and headers[header] != 'no-cache':
80f8e3804a 2010-08-20 c.kworr@b84a3: 								print('Pragma:', headers[header])
80f8e3804a 2010-08-20 c.kworr@b84a3: 							new_headers[header] = headers[header]
80f8e3804a 2010-08-20 c.kworr@b84a3: 						elif not header in ignore_fields:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							print('Undefined header "', header, '": ', headers[header], sep='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# comparing headers with data found in index
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# if any header has changed (except Pragma) file is fully downloaded
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# same if we get more or less headers
80f8e3804a 2010-08-20 c.kworr@b84a3: 					old_keys = set(index[url].keys())
80f8e3804a 2010-08-20 c.kworr@b84a3: 					old_keys.discard('_time')
80f8e3804a 2010-08-20 c.kworr@b84a3: 					old_keys.discard('Pragma')
80f8e3804a 2010-08-20 c.kworr@b84a3: 					more_keys = set(new_headers.keys()) - old_keys
80f8e3804a 2010-08-20 c.kworr@b84a3: 					more_keys.discard('Pragma')
80f8e3804a 2010-08-20 c.kworr@b84a3: 					less_keys = old_keys - set(new_headers.keys())
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if len(more_keys) > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if not len(old_keys) == 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							print('More headers appear:', more_keys)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 					elif len(less_keys) > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print('Less headers appear:', less_keys)
80f8e3804a 2010-08-20 c.kworr@b84a3: 					else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						for key in index[url].keys():
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if key[0] != '_' and key != 'Pragma' and not index[url][key] == new_headers[key]:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								print('Header "', key, '" changed from [', index[url][key], '] to [', new_headers[key], ']', sep='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 								reload = True
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# downloading file
80f8e3804a 2010-08-20 c.kworr@b84a3: 					if reload:
80f8e3804a 2010-08-20 c.kworr@b84a3: 						if 'Content-Length' in headers:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							print('Downloading', headers['Content-Length'], 'bytes [', end='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							print('Downloading [', end='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						sys.stdout.flush()
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						# file is created at temporary location and moved in place only when download completes
80f8e3804a 2010-08-20 c.kworr@b84a3: 						temp_file = open(options.dir + os.sep + '.tmp', 'wb')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						buffer = source.read(block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 						megablocks = 0
80f8e3804a 2010-08-20 c.kworr@b84a3: 						blocks = 0
80f8e3804a 2010-08-20 c.kworr@b84a3: 						megs = 0
80f8e3804a 2010-08-20 c.kworr@b84a3: 						while len(buffer) > 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 							temp_file.write(buffer)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							buffer = source.read(block_size)
80f8e3804a 2010-08-20 c.kworr@b84a3: 							blocks += 1
80f8e3804a 2010-08-20 c.kworr@b84a3: 							if blocks > 102400/block_size:
80f8e3804a 2010-08-20 c.kworr@b84a3: 								megablocks += 1
80f8e3804a 2010-08-20 c.kworr@b84a3: 								if megablocks > 10:
80f8e3804a 2010-08-20 c.kworr@b84a3: 									megablocks = megablocks - 10
80f8e3804a 2010-08-20 c.kworr@b84a3: 									megs += 1
80f8e3804a 2010-08-20 c.kworr@b84a3: 									print('{}Mb'.format(megs), end='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 								else:
80f8e3804a 2010-08-20 c.kworr@b84a3: 									print('.', end='')
80f8e3804a 2010-08-20 c.kworr@b84a3: 								blocks = blocks - 102400/block_size
80f8e3804a 2010-08-20 c.kworr@b84a3: 							sys.stdout.flush()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						temp_file.close()
80f8e3804a 2010-08-20 c.kworr@b84a3: 						print(']')
80f8e3804a 2010-08-20 c.kworr@b84a3: 						os.renames(options.dir + os.sep + '.tmp', file_name)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 						checked_files += 1
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 					# storing new time mark and storing new headers
80f8e3804a 2010-08-20 c.kworr@b84a3: 					new_headers['_time'] = datetime.datetime.now()
80f8e3804a 2010-08-20 c.kworr@b84a3: 					index[url] = new_headers
80f8e3804a 2010-08-20 c.kworr@b84a3: 					index.sync()
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 			except urllib.error.HTTPError as error:
80f8e3804a 2010-08-20 c.kworr@b84a3: 				# in case of error we don't need to do anything actually,
80f8e3804a 2010-08-20 c.kworr@b84a3: 				# if file download stalls or fails the file would not be moved to it's location
80f8e3804a 2010-08-20 c.kworr@b84a3: 				print(error)
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		if options.verbose:
80f8e3804a 2010-08-20 c.kworr@b84a3: 			print('[', len(unchecked_files), '/', checked_files, ']')
80f8e3804a 2010-08-20 c.kworr@b84a3: 
80f8e3804a 2010-08-20 c.kworr@b84a3: 		# checking if there were any files downloaded, if yes - restarting sequence
80f8e3804a 2010-08-20 c.kworr@b84a3: 		if checked_files == 0:
80f8e3804a 2010-08-20 c.kworr@b84a3: 			break