import http.server, functools, json, os, hashlib, shutil, mimetypes from pathlib import Path from urllib.parse import unquote,urlsplit root=Path(__file__).resolve().parent repo=root.parents[2] candidate=repo/'docs/design-system/candidates/7b11bf22fd24' manifest=json.loads((candidate/'build-output.json').read_text()) static=Path('/private/tmp/progcode-ds-frozen-7b11bf22fd24') if not static.is_dir():static=repo/'web/out' for item in manifest['files']: assert hashlib.sha256((static/item['path']).read_bytes()).hexdigest()==item['sha256'],item['path'] source=json.loads((candidate/'source-inputs.json').read_text()) snapshot=root/'reference-source' for item in source['files']: target=snapshot/item['path'];target.parent.mkdir(parents=True,exist_ok=True) if not target.exists():shutil.copyfile(repo/'web'/item['path'],target) assert hashlib.sha256(target.read_bytes()).hexdigest()==item['sha256'],item['path'] class Handler(http.server.SimpleHTTPRequestHandler): def __init__(self,*a,**kw):super().__init__(*a,directory=str(static),**kw) def translate_path(self,path): route=unquote(urlsplit(path).path) if route.startswith('/review/'): rel=route[len('/review/'):] if '..' in Path(rel).parts:return str(root/'_blocked') return str(root/rel) return super().translate_path(path) def guess_type(self,path): if '/reference-source/' in str(path):return 'text/plain; charset=utf-8' return super().guess_type(path) def end_headers(self): self.send_header('Content-Security-Policy',"default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; frame-src 'self'; object-src 'none'") self.send_header('X-Review-Source',source['sourceHash']) self.send_header('X-Review-Build',manifest['buildHash']) self.send_header('Cache-Control','no-store') super().end_headers() def send_error(self,code,message=None,explain=None): if code==404 and not self.path.startswith('/review/'): body=(static/'404.html').read_bytes();self.send_response(404);self.send_header('Content-Type','text/html; charset=utf-8');self.send_header('Content-Length',str(len(body)));self.end_headers() if self.command!='HEAD':self.wfile.write(body) else:super().send_error(code,message,explain) s=http.server.ThreadingHTTPServer(('127.0.0.1',0),Handler) (root/'local-service.json').write_text(json.dumps({'pid':os.getpid(),'port':s.server_port,'url':f'http://127.0.0.1:{s.server_port}/review/','referencePagesOrigin':f'http://127.0.0.1:{s.server_port}','staticRoot':str(static),'sourceHash':source['sourceHash'],'buildHash':manifest['buildHash'],'sourceFilesVerified':len(source['files']),'outputFilesVerified':len(manifest['files']),'scope':'documentation matrix + byte-verified F6 actual pages; F6 creativeREJECTED; V3 separateproposal; externalanalytics blocked by viewer CSP, not product change'},indent=2)+'\n') s.serve_forever()