浏览代码

Sync with websockify.

Pull c33f0b52e7 from websockify.

- Fix for python2.4 with URL parsing.
- Set binaryType earlier in Opera 12.10 to avoid receiving a blob.
- Re-order client and target processing so that pending client data
  has an opportunity to be sent when the target closes.
Joel Martin 12 年之前
父节点
当前提交
705c54edb6
共有 3 个文件被更改,包括 32 次插入24 次删除
  1. 3 3
      include/websock.js
  2. 5 0
      utils/websocket.py
  3. 24 21
      utils/websockify

+ 3 - 3
include/websock.js

@@ -331,6 +331,9 @@ function open(uri, protocols) {
         websocket = {};
     } else {
         websocket = new WebSocket(uri, protocols);
+        if (protocols.indexOf('binary') >= 0) {
+            websocket.binaryType = 'arraybuffer';
+        }
     }
 
     websocket.onmessage = recv_message;
@@ -343,9 +346,6 @@ function open(uri, protocols) {
             mode = 'base64';
             Util.Error("Server select no sub-protocol!: " + websocket.protocol);
         }
-        if (mode === 'binary') {
-            websocket.binaryType = 'arraybuffer';
-        }
         eventHandlers.open();
         Util.Debug("<< WebSock.onopen");
     };

+ 5 - 0
utils/websocket.py

@@ -941,6 +941,11 @@ Sec-WebSocket-Accept: %s\r
                 if startsock:
                     startsock.close()
 
+        # Close listen port
+        self.vmsg("Closing socket listening at %s:%s"
+                % (self.listen_host, self.listen_port))
+        lsock.close()
+
 
 # HTTP handler with WebSocket upgrade support
 class WSRequestHandler(SimpleHTTPRequestHandler):

+ 24 - 21
utils/websockify

@@ -14,8 +14,11 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
 import signal, socket, optparse, time, os, sys, subprocess
 from select import select
 import websocket
-try:    from urllib.parse import parse_qs, urlparse
-except: from urlparse import parse_qs, urlparse
+try:
+    from urllib.parse import parse_qs, urlparse
+except:
+    from cgi import parse_qs
+    from urlparse import urlparse
 
 class WebSocketProxy(websocket.WebSocketServer):
     """
@@ -205,7 +208,7 @@ Traffic Legend:
         # Extract the token parameter from url
         args = parse_qs(urlparse(path)[4]) # 4 is the query from url
 
-        if not len(args['token']):
+        if not args.has_key('token') or not len(args['token']):
             raise self.EClose("Token not present")
 
         token = args['token'][0].rstrip('\n')
@@ -249,6 +252,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 +294,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