Permalink
Browse files

Merge pull request #3 from jontho/master

struct.pack format should use unsigned ints.
  • Loading branch information...
2 parents d5920b9 + 29747e1 commit 67335c7ae66e0164477947660b91c29d86b37d17 @gleicon committed Nov 18, 2011
Showing with 14 additions and 12 deletions.
  1. +1 −1 examples/interact.py
  2. +13 −11 txwebsockets/__init__.py
View
@@ -24,7 +24,7 @@ def hexify(self, sto):
if __name__ == '__main__':
from twisted.internet import reactor
- mo = MyOperations()
+ mo = MyOperations
factory = WebSocketFactory(mo)
View
@@ -48,16 +48,17 @@ def _out(self, str):
def after_connection(self):
pass
-class WebSocketServer(LineReceiver):
+class WebSocketConnection(LineReceiver):
HDR_ORIGIN = re.compile('Origin\:\s+(.*)')
HDR_LOCATION = re.compile('GET\s+(.*)\s+HTTP\/1.1', re.I)
HDR_HOST = re.compile('Host\:\s+(.*)')
SEC_WS_KEY1 = re.compile('Sec-WebSocket-Key1\:\s+(.*)')
SEC_WS_KEY2 = re.compile('Sec-WebSocket-Key2\:\s+(.*)')
- def __init__(self):
-
+ def __init__(self,oper):
+ self.oper = oper
+
self.old_hdr = '''HTTP/1.1 101 Web Socket Protocol Handshake\r
Upgrade: WebSocket\r
Connection: Upgrade\r
@@ -74,10 +75,10 @@ def __init__(self):
def connectionMade(self):
self.setRawMode()
- self.factory.oper.on_connect()
+ self.oper.on_connect()
def lineReceived(self, line):
- self.factory.oper.on_read(line)
+ self.oper.on_read(line)
def rawDataReceived(self, line):
origin, location, host, token = self._parseHeaders(line)
@@ -88,11 +89,11 @@ def rawDataReceived(self, line):
self.sendLine(self.hdr % (origin, host, location, token))
self.delimiter='\xff'
self.setLineMode()
- self.factory.oper.setWriteHandler(self.sendLine)
- self.factory.oper.after_connection()
+ self.oper.setWriteHandler(self.sendLine)
+ self.oper.after_connection()
def connectionLost(self, reason):
- self.factory.oper.on_close(reason)
+ self.oper.on_close(reason)
def _parseHeaders(self, buf):
if buf == None:
@@ -126,7 +127,7 @@ def _parseHeaders(self, buf):
return o, l, h, None
def _calculate_token(self, k1, k2, k3):
- token = struct.pack('>ii8s', self._filterella(k1), self._filterella(k2), k3)
+ token = struct.pack('>II8s', self._filterella(k1), self._filterella(k2), k3)
return md5(token).digest()
def _filterella(self, w):
@@ -139,9 +140,10 @@ def _filterella(self, w):
return x
class WebSocketFactory(Factory):
- protocol = WebSocketServer
+ protocol = WebSocketConnection
def __init__(self, oper=BasicOperations):
self.oper=oper
-
+ def buildProtocol(self, addr):
+ return self.protocol(self.oper())

0 comments on commit 67335c7

Please sign in to comment.