def handle(self, msock, msg, addr): # data=msg.decode('ascii') # data=bz2.decompress(msg).decode('ascii') print('HTTP Proxyback message from '+encodeAddress(addr)+': ') print(msg) packet=ProxybackMessage() packet.decodeProxybackMessage(msg) print(packet) q=self.qs[packet.reqid] q.put(packet.data) if packet.fin: print('EOF') q.put(None) del self.qs[packet.reqid] print(q.qsize())
def handle(self, msock, msg, addr): # data=msg.decode('ascii') # data=bz2.decompress(msg).decode('ascii') # print('HTTP Proxyback message from '+encodeAddress(addr)+': ') # print(msg) packet=ProxybackMessage() packet.decodeProxybackMessage(msg) # print(packet) print('reqid: '+encode(packet.reqid)) print('current: '+str(packet.seq)) if not encode(packet.reqid) in self.stats: print('Unknown reqid '+encode(packet.reqid)) print('Dropping packet with seq '+str(packet.seq)) return q=self.qs[encode(packet.reqid)] progress, last, chunks=self.stats[encode(packet.reqid)] if not packet.seq in chunks: # Store new chunks by seq number chunks[packet.seq]=packet.data if progress==None and packet.seq==0: # First packet arrived progress=0 if packet.fin: # If fin flag set, this is the last packet in the sequence last=packet.seq done=False print('reqid: '+encode(packet.reqid)) print('progress: '+str(progress)) print('last: '+str(last)) print('current: '+str(packet.seq)) if progress!=None: if last!=None: done=True for x in range(progress, last+1): # Send all sequential packets from last sent to new packet try: chunk=chunks[x] print('putting chunk '+encode(packet.reqid)+':'+str(x)+'/'+str(last)) q.put(chunk) progress=x+1 except: done=False break else: for x in range(progress, packet.seq+1): # Send all sequential packets from last sent to new packet try: chunk=chunks[x] print('putting chunk '+encode(packet.reqid)+':'+str(x)+'/?') q.put(chunk) progress=x+1 except: break if done: q.put(None) del self.qs[encode(packet.reqid)] del self.stats[encode(packet.reqid)] else: self.stats[encode(packet.reqid)]=[progress, last, chunks] print(q.qsize())
def handle(self, msock, msg, addr): packet=ProxyMessage() packet.decodeProxyMessage(msg) reqid=packet.reqid data=packet.data.decode('ascii') print('HTTP Proxy message from '+encodeAddress(addr)+':') print(data) parts=data.split(' ') print('parts: '+str(parts)) url=parts[1] print('url: '+str(url)) try: print('connecting to '+str(urlparse(url).netloc)) parts=urlparse(url) print(parts) print(parts.username) print(parts.password) host=parts.hostname port=parts.port if not port: port=80 if parts.username and parts.password: auth=base64.b64encode(parts.username+':'+parts.password) else: auth=None headers={'Connection': 'close'} if auth: headers['Authentication']=auth print(headers) conn=httplib.HTTPConnection(host, port) conn.request('GET', url, headers=headers) resp=conn.getresponse() if resp.version==10: result=b"HTTP/1.0" elif resp.version==11: result=b"HTTP/1.1" else: result=b"HTTP/1.1" result=result+b' '+bytes(str(resp.status), 'ascii')+b' '+bytes(resp.reason, 'ascii')+b"\r\n" for key, value in resp.getheaders(): if key!='Connection': result=result+bytes(key, 'ascii')+b': '+bytes(value, 'ascii')+b"\r\n" result=result+b"Connection: close\r\n" result=result+b"Proxy-Connection: close\r\n" result=result+b"\r\n" result=result+resp.read() except Exception as e: print(e) traceback.print_exc() result=b"HTTP/1.1 500 Error\r\n\r\n" # comp=bz2.compress(result) chunkSize=512 n=int(math.ceil(len(result)/chunkSize)) for i in range(n): start=chunkSize*i end=chunkSize*(i+1) print('sending '+str(i)) packet=ProxybackMessage() packet.createProxybackMessage(reqid, i, (i==n-1), result[start:end]) self.router.sendto(packet.msg, addr, service='httpProxyback')