소스 검색

Make WAYBACK_API disable the PAC bypass instead

RichardG867 5 년 전
부모
커밋
6a08628277
2개의 변경된 파일10개의 추가작업 그리고 9개의 파일을 삭제
  1. 2 2
      config.py
  2. 8 7
      waybackproxy.py

+ 2 - 2
config.py

@@ -23,8 +23,8 @@ QUICK_IMAGES = True
 # snapshot to the desired date, instead of directly requesting that date. Helps
 # in situations where an image returns a server error on the desired date, but
 # is available at an earlier date. As a side effect, pages will take longer to
-# load due to the added API call. This option as no effect when QUICK_IMAGES is
-# used alongside the PAC file.
+# load due to the added API call. If enabled, this option will disable the
+# QUICK_IMAGES bypass mechanism built into the PAC file.
 WAYBACK_API = True
 
 # Allow the Content-Type header to contain an encoding. Some old browsers

+ 8 - 7
waybackproxy.py

@@ -40,7 +40,7 @@ class Handler(socketserver.BaseRequestHandler):
 			ll = line.lower()
 			if ll[:6] == 'host: ':
 				pac_host = request_host = line[6:].rstrip('\r\n')
-				if ':' not in pac_host: # who would run this on port 80 anyway?
+				if ':' not in pac_host: # explicitly specify port if running on port 80
 					pac_host += ':80'
 			elif ll[:21] == 'x-waybackproxy-date: ':
 				# API for a personal project of mine
@@ -83,16 +83,17 @@ class Handler(socketserver.BaseRequestHandler):
 		# effectively handle the request
 		try:
 			if path in pac_file_paths:
-				# PAC file to bypass QUICK_IMAGES requests
+				# PAC file to bypass QUICK_IMAGES requests if WAYBACK_API is not enabled
 				pac  = http_version.encode('ascii', 'ignore') + b''' 200 OK\r\n'''
 				pac += b'''Content-Type: application/x-ns-proxy-autoconfig\r\n'''
 				pac += b'''\r\n'''
 				pac += b'''function FindProxyForURL(url, host)\r\n'''
 				pac += b'''{\r\n'''
-				pac += b'''	if (shExpMatch(url, "http://web.archive.org/web/*") && !shExpMatch(url, "http://web.archive.org/web/??????????????if_/*"))\r\n'''
-				pac += b'''	{\r\n'''
-				pac += b'''		return "DIRECT";\r\n'''
-				pac += b'''	}\r\n'''
+				if not availability_cache:
+					pac += b'''	if (shExpMatch(url, "http://web.archive.org/web/*") && !shExpMatch(url, "http://web.archive.org/web/??????????????if_/*"))\r\n'''
+					pac += b'''	{\r\n'''
+					pac += b'''		return "DIRECT";\r\n'''
+					pac += b'''	}\r\n'''
 				pac += b'''	return "PROXY ''' + pac_host.encode('ascii', 'ignore') + b'''";\r\n'''
 				pac += b'''}\r\n'''
 				self.request.sendall(pac)
@@ -105,7 +106,7 @@ class Handler(socketserver.BaseRequestHandler):
 					else:
 						return self.error_page(http_version, 404, 'Not Found')
 				else:
-					# pass-through requests to web.archive.org
+					# pass requests through to web.archive.org
 					# required for QUICK_IMAGES
 					archived_url = '/'.join(request_url.split('/')[5:])
 					_print('[>] [QI] {0}'.format(archived_url))