FreeBSD Bugzilla – Attachment 154629 Details for
Bug 198772
Problem with pkg behind a chunking proxy
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Python proxy to confirm the bug or be used as a workaround
PkgProxy.py (text/plain), 5.19 KB, created by
sg-ball
on 2015-03-21 14:03:40 UTC
(
hide
)
Description:
Python proxy to confirm the bug or be used as a workaround
Filename:
MIME Type:
Creator:
sg-ball
Created:
2015-03-21 14:03:40 UTC
Size:
5.19 KB
patch
obsolete
>#! /usr/bin/env python ># -*- coding: latin1 -*- > >import urllib2 >import BaseHTTPServer >import logging >import urlparse >import os.path > >PKG_DIR = '/var/cache/pkg_proxy' > >class Handler(BaseHTTPServer.BaseHTTPRequestHandler): > HEADERS = [ 'if-modified-since', 'host', 'user-agent', 'accept', > 'connection' ] > def do_GET(self): > logging.debug("Got a request %s", self.path) > if self.server.debug >= 3: > logging.debug("Original headers %s", str(self.headers)) > req = urllib2.Request(self.path) > for (key, value) in self.headers.items(): > if self.server.debug >= 5: > logging.debug('%s: %s', key, value) > if key in Handler.HEADERS: > req.add_header(key, value) > if self.server.keep and (Handler.HEADERS[0] in self.headers): > logging.debug("Keeping") > self.send_response(304, self.responses[304][0]) > self.end_headers() > self.wfile.write(b"\r\n") > return > if self.server.debug >= 4: > logging.debug("Send headers %s", str(req.headers)) > fname = os.path.join(PKG_DIR, os.path.basename( > urlparse.urlparse(self.path).path)) > if self.server.debug >= 5: > logging.debug("Looking for %s in cache", fname) > if os.path.exists(fname): > logging.debug("sending cached file") > size = os.path.getsize(fname) > self.send_response(200) > self.send_header('Content-Length', size) > self.end_headers() > if self.server.debug >= 5: > logging.debug("Content-Length : %d", size) > data = b'' > with open(fname) as f: > while True: > buff = f.read() > if len(buff) == 0: > break > data += buff > if self.server.debug >= 5: > logging.debug("Sending %d bytes", len(data)) > self.wfile.write(data) > return > > r = self.server.opener.open(req) > code = r.getcode() > logging.debug('Code %d', code) > if self.server.debug >= 3: > logging.debug("Received headers %s", str(r.headers)) > if code == 200: > fcache = open(fname, 'w') > if code == 200 and self.server.chunk: > self.send_response(code) > self.send_header('Transfer-Encoding', 'chunked') > self.end_headers() > while True: > t = r.read(4095) > logging.debug('Chunk : %d', len(t)) > self.wfile.write(("%03x\r\n" % (len(t),)).encode()) > if len(t) == 0: > self.wfile.write(b"\r\n") > break > else: > self.wfile.write(t) > self.wfile.write(b"\r\n") > fcache.write(t) > else: > t = r.read() > #print(t) > self.send_response(code) > logging.debug('Content-Length %d', len(t)) > self.send_header('Content-Length', len(t)) > self.end_headers() > self.wfile.write(t) > fcache.write(t) > fcache.close() > > def do_HEAD(self): > logging.debug("Got a HEAD request %s", self.path) > > >class MiniProxy(BaseHTTPServer.HTTPServer): > def __init__(self, port=8080, relay=None, chunk=False, debug=0, keep=False): > BaseHTTPServer.HTTPServer.__init__(self, ('', port), Handler) > self.port = port > self.relay = relay > self.chunk = chunk > self.debug= debug > self.keep = keep > if relay is not None: > logging.info("relay set to", relay) > self.proxyHandler = urllib2.ProxyHandler( > {'http': relay}) > self.opener = urllib2.build_opener(self.proxyHandler) > else: > self.opener = urllib2.build_opener() > >if __name__ == '__main__': > import sys > import argparse > parser = argparse.ArgumentParser(description = > "Simple HTTP proxy to chunk or unchunk responses") > parser.add_argument('-c', '--chunk', dest = 'chunk', action='store_true') > parser.add_argument('-C', '--no-chunk', dest = 'chunk', action='store_false') > parser.add_argument('-r', '--relay') > parser.add_argument('-p', '--port', type=int, default = 8080) > parser.add_argument('-d', '--debug', type=int, default = 0) > parser.add_argument('-k', '--keep', dest = 'keep', action='store_true') > parser.add_argument('-K', '--no-keep', dest = 'keep', action='store_false') > params = parser.parse_args() > proxy = MiniProxy(port = params.port, relay = params.relay, > chunk = params.chunk, debug = params.debug, > keep = params.keep) > > logging_format = '%(levelname)s:%(message)s' > if params.debug == 1: > logging.basicConfig(format=logging_format, level=logging.INFO) > elif params.debug >= 2: > logging.basicConfig(format=logging_format, level=logging.DEBUG) > else: > logging.basicConfig(format=logging_format) > try: > print (sys.argv[0], "listen on", proxy.port) > proxy.serve_forever() > except KeyboardInterrupt: > print('Stop') > #proxy.shutdown()
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 198772
: 154629