From 5e16b38524509d105600d501ecbc7e4e73781345 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Mon, 29 Oct 2012 18:12:54 -0500 Subject: [PATCH] websocketproxy.py: put client socket handling first. Should at least mostly address this issue: https://github.com/kanaka/websockify/issues/63 The problem is that the target in the test case often immediately closed the target after sending the data. In this case the last data received from the target is never sent to the client because the client is not in the list of sockets being selected against (because it is only added if their is pending data). By moving the client conditionals first, we give the client socket a chance to be sent data before the target is detected as close and we terminate the loop. --- websockify/websocketproxy.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/websockify/websocketproxy.py b/websockify/websocketproxy.py index 060125d..6a073e0 100755 --- a/websockify/websocketproxy.py +++ b/websockify/websocketproxy.py @@ -249,6 +249,24 @@ Traffic Legend: ins, outs, excepts = select(rlist, wlist, [], 1) if excepts: raise Exception("Socket exception") + if self.client in outs: + # Send queued target data to the client + c_pend = self.send_frames(cqueue) + + cqueue = [] + + if self.client in ins: + # Receive client data, decode it, and queue for target + bufs, closed = self.recv_frames() + tqueue.extend(bufs) + + if closed: + # TODO: What about blocking on client socket? + self.vmsg("%s:%s: Client closed connection" %( + self.target_host, self.target_port)) + raise self.CClose(closed['code'], closed['reason']) + + if target in outs: # Send queued client data to the target dat = tqueue.pop(0) @@ -273,24 +291,6 @@ Traffic Legend: self.traffic("{") - if self.client in outs: - # Send queued target data to the client - c_pend = self.send_frames(cqueue) - - cqueue = [] - - - if self.client in ins: - # Receive client data, decode it, and queue for target - bufs, closed = self.recv_frames() - tqueue.extend(bufs) - - if closed: - # TODO: What about blocking on client socket? - self.vmsg("%s:%s: Client closed connection" %( - self.target_host, self.target_port)) - raise self.CClose(closed['code'], closed['reason']) - def _subprocess_setup(): # Python installs a SIGPIPE handler by default. This is usually not what