62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""仅为安装验收控制下载边界;工具链内容仍来自官方源。"""
|
|
|
|
from functools import partial
|
|
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
|
from pathlib import Path
|
|
import threading
|
|
import time
|
|
|
|
|
|
WORK = Path('/work')
|
|
lock = threading.Lock()
|
|
active = 0
|
|
|
|
|
|
class Handler(SimpleHTTPRequestHandler):
|
|
def do_GET(self):
|
|
global active
|
|
environment = self.path.startswith(('/dist/', '/dever-native-'))
|
|
if environment and (WORK / 'reject-environment').exists():
|
|
self.send_error(404, 'intentional preparation failure')
|
|
return
|
|
archive = environment and self.path.endswith('.tar.xz')
|
|
if archive:
|
|
with lock:
|
|
active += 1
|
|
if active >= 2:
|
|
(WORK / 'parallel-downloads').touch()
|
|
try:
|
|
while (WORK / 'hold-environment').exists():
|
|
time.sleep(0.1)
|
|
self.download()
|
|
finally:
|
|
with lock:
|
|
active -= 1
|
|
else:
|
|
self.download()
|
|
|
|
def download(self):
|
|
if self.path.startswith('/dist/'):
|
|
self.send_response(302)
|
|
self.send_header('Location', 'https://static.rust-lang.org' + self.path)
|
|
self.end_headers()
|
|
return
|
|
resume = self.headers.get('Range')
|
|
if resume and self.path.startswith('/dever-native-'):
|
|
offset = int(resume.removeprefix('bytes=').removesuffix('-'))
|
|
path = Path(self.translate_path(self.path))
|
|
size = path.stat().st_size
|
|
(WORK / 'resumed-offset').write_text(str(offset))
|
|
self.send_response(206)
|
|
self.send_header('Content-Range', f'bytes {offset}-{size - 1}/{size}')
|
|
self.send_header('Content-Length', str(size - offset))
|
|
self.end_headers()
|
|
with path.open('rb') as source:
|
|
source.seek(offset)
|
|
self.copyfile(source, self.wfile)
|
|
return
|
|
super().do_GET()
|
|
|
|
|
|
ThreadingHTTPServer(('127.0.0.1', 18080), partial(Handler, directory='/work/assets')).serve_forever()
|