summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xproxy.py26
1 files changed, 12 insertions, 14 deletions
diff --git a/proxy.py b/proxy.py
index b79672c..44c7365 100755
--- a/proxy.py
+++ b/proxy.py
@@ -16,7 +16,7 @@ ENDPOINTS = [
kB = 1024
#minimum entity size to start a paralel fetch
-THRESHOLD = 0 * kB #512 * kB
+THRESHOLD = 512 * kB
#first fetch-range blocksize
INIT_BLOCKSIZE = 512 * kB
#lower bound of fetch-range blocksize optimization
@@ -53,7 +53,7 @@ class Fetcher(asynchat.async_chat):
def handle_connect (self):
print self, "Start"
self.send("GET http://%s:%s%s HTTP/1.0\r\n" % ( self.url.hostname, self.url.port or 80, self.url.path ))
- for key in filter(lambda k: k not in ("range"), self.headers.keys()): #origin request headers
+ for key in filter(lambda k: k not in ("range"), self.headers.keys()): #send origin request headers
self.send("%s: %s\r\n" % (key, self.headers[key]))
self.send("Range: bytes=%s-%s\r\n" % (self.range[0], self.range[1]))
self.send("\r\n")
@@ -75,8 +75,8 @@ class Fetcher(asynchat.async_chat):
if self.pos >= self.range[1]:
self.stop_time = time.time()
print self, "finished"
- #XXX just to make sure the next fetcher will be started
- self.reader.handle_incoming_data(self, "")
+ #make sure the next fetcher will be started
+ self.reader.handle_incoming_data(self)
self.close()
elif self.state ==1: #header
self.http_header += data
@@ -112,8 +112,8 @@ class MultipleProxyReader(object):
for proxy in ENDPOINTS:
self.fetchers.append( Fetcher(self, proxy, self.url, self.header, self.next_range(INIT_BLOCKSIZE)) )
- def handle_incoming_data(self, fetcher, data):
- if len(data) == 0:
+ def handle_incoming_data(self, fetcher, data=None):
+ if not data:
self.fetchers = filter(lambda f: f != fetcher, self.fetchers)
else:
heappush(self.blocks, (fetcher.pos, data))
@@ -223,11 +223,9 @@ class HTTPProxyServer(asyncore.dispatcher):
def handle_request(self, channel, method, path, headers):
url = urlparse.urlparse(path)
- print method, path
if method != "GET" or url.query != "":
- print "non-GET or query, bypass"
#do not handle non-GET or GET with Query (?foo=bla) requests
- return self._bypass_request(channel, method, url)
+ return self._bypass_request(channel, method, url, headers)
#check for content-length header with a HEAD request
conn = httplib.HTTPConnection(url.hostname, url.port or 80)
@@ -237,24 +235,24 @@ class HTTPProxyServer(asyncore.dispatcher):
if len( content_length ) == 0:
# no content length given, bypass this request
print "missing content-length, bypass"
- self._bypass_request(channel, "GET", url)
+ self._bypass_request(channel, "GET", url, headers)
else:
content_length = int(content_length[0][1])
if content_length < THRESHOLD:
- self._bypass_request(channel, "GET", url)
+ self._bypass_request(channel, "GET", url, headers)
else:
MultipleProxyReader(channel, url, headers, content_length)
- def _bypass_request(self, channel, method, url):
- print "_bypass request hostname=%s port=%s path=%s" % (url.hostname, url.port or 80, url.path)
+ def _bypass_request(self, channel, method, url, headers):
+ print "_bypass request: %s %s" % (method, urlparse.urlunparse(url))
#XXX hier sollte nicht proxy gespielt werden sondern
#die daten 1-zu-1 durchgereicht werden.
#Weiterhin sollte sichergestellt werden, dass die requests
#zu Host X1 immer ueber Proxy Y1 geroutet werden
# etwa proxy=proxies[ stuff(hostname) % len(proxies) ]
conn = httplib.HTTPConnection(url.hostname, url.port or 80)
- conn.request(method, url.path)
+ conn.request(method, url.path, body="", headers=headers)
resp = conn.getresponse()
channel.push("HTTP/1.0 200 OK\r\nX-Proxy: Magicproxy (superpower disabled)\r\n")
channel.push( "\r\n".join(map(lambda k: "%s: %s" % (k[0],k[1]), resp.getheaders())) )