Commit 7d146027 authored by Joel Martin's avatar Joel Martin

Pull websockify 284ef3cc1a54

Including HyBi-07 support and refactor of send/recv.
parent 5210330a
This diff is collapsed.
...@@ -133,7 +133,7 @@ Traffic Legend: ...@@ -133,7 +133,7 @@ Traffic Legend:
# will be run in a separate forked process for each connection. # will be run in a separate forked process for each connection.
# #
def new_client(self, client): def new_client(self):
""" """
Called after a new WebSocket connection has been established. Called after a new WebSocket connection has been established.
""" """
...@@ -156,9 +156,9 @@ Traffic Legend: ...@@ -156,9 +156,9 @@ Traffic Legend:
if self.verbose and not self.daemon: if self.verbose and not self.daemon:
print self.traffic_legend print self.traffic_legend
# Stat proxying # Start proxying
try: try:
self.do_proxy(client, tsock) self.do_proxy(tsock)
except: except:
if tsock: if tsock:
tsock.close() tsock.close()
...@@ -169,14 +169,14 @@ Traffic Legend: ...@@ -169,14 +169,14 @@ Traffic Legend:
self.rec.close() self.rec.close()
raise raise
def do_proxy(self, client, target): def do_proxy(self, target):
""" """
Proxy client WebSocket to normal target socket. Proxy client WebSocket to normal target socket.
""" """
cqueue = [] cqueue = []
cpartial = "" c_pend = 0
tqueue = [] tqueue = []
rlist = [client, target] rlist = [self.client, target]
tstart = int(time.time()*1000) tstart = int(time.time()*1000)
while True: while True:
...@@ -184,7 +184,7 @@ Traffic Legend: ...@@ -184,7 +184,7 @@ Traffic Legend:
tdelta = int(time.time()*1000) - tstart tdelta = int(time.time()*1000) - tstart
if tqueue: wlist.append(target) if tqueue: wlist.append(target)
if cqueue: wlist.append(client) if cqueue or c_pend: wlist.append(self.client)
ins, outs, excepts = select(rlist, wlist, [], 1) ins, outs, excepts = select(rlist, wlist, [], 1)
if excepts: raise Exception("Socket exception") if excepts: raise Exception("Socket exception")
...@@ -199,53 +199,40 @@ Traffic Legend: ...@@ -199,53 +199,40 @@ Traffic Legend:
tqueue.insert(0, dat[sent:]) tqueue.insert(0, dat[sent:])
self.traffic(".>") self.traffic(".>")
if client in outs:
# Send queued target data to the client
dat = cqueue.pop(0)
sent = client.send(dat)
if sent == len(dat):
self.traffic("<")
if self.rec:
self.rec.write("%s,\n" %
repr("{%s{" % tdelta + dat[1:-1]))
else:
cqueue.insert(0, dat[sent:])
self.traffic("<.")
if target in ins: if target in ins:
# Receive target data, encode it and queue for client # Receive target data, encode it and queue for client
buf = target.recv(self.buffer_size) buf = target.recv(self.buffer_size)
if len(buf) == 0: raise self.EClose("Target closed") if len(buf) == 0: raise self.EClose("Target closed")
cqueue.append(self.encode(buf)) cqueue.append(buf)
self.traffic("{") self.traffic("{")
if client in ins:
if self.client in outs:
# Send queued target data to the client
c_pend = self.send_frames(cqueue)
cqueue = []
#if self.rec:
# self.rec.write("%s,\n" %
# repr("{%s{" % tdelta + dat[1:-1]))
if self.client in ins:
# Receive client data, decode it, and queue for target # Receive client data, decode it, and queue for target
buf = client.recv(self.buffer_size) bufs, closed = self.recv_frames()
if len(buf) == 0: raise self.EClose("Client closed") tqueue.extend(bufs)
if buf == '\xff\x00': #if self.rec:
raise self.EClose("Client sent orderly close frame") # for b in bufs:
elif buf[-1] == '\xff': # self.rec.write(
if buf.count('\xff') > 1: # repr("}%s}%s" % (tdelta, b)) + ",\n")
self.traffic(str(buf.count('\xff')))
self.traffic("}") if closed:
if self.rec: # TODO: What about blocking on client socket?
self.rec.write("%s,\n" % self.send_close()
(repr("}%s}" % tdelta + buf[1:-1]))) raise self.EClose(closed)
if cpartial:
# Prepend saved partial and decode frame(s)
tqueue.extend(self.decode(cpartial + buf))
cpartial = ""
else:
# decode frame(s)
tqueue.extend(self.decode(buf))
else:
# Save off partial WebSockets frame
self.traffic(".}")
cpartial = cpartial + buf
if __name__ == '__main__': if __name__ == '__main__':
usage = "\n %prog [options]" usage = "\n %prog [options]"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment