Commits

Jeffrey Gelens  committed 76074dd

Now chooses only one protocol during the handshake (thanks Daniel Swarbrick)
Renamed MessageResource to Resource

  • Participants
  • Parent commits fa55130

Comments (0)

Files changed (4)

File examples/wamp.py

 from geventwebsocket.server import WebSocketServer
-from geventwebsocket.resource import MessageResource, WebSocketApplication
+from geventwebsocket.resource import Resource, WebSocketApplication
 from geventwebsocket.protocols.wamp import WampProtocol, export_rpc
 
 
         return self.wamp
 
     @classmethod
-    def supported_protocols(cls):
-        return [WampProtocol.PROTOCOL_NAME]
+    def protocol(cls):
+        return WampProtocol.PROTOCOL_NAME
 
 
 if __name__ == "__main__":
-    resource = MessageResource(apps={
+    resource = Resource(apps={
         '/': WampApplication
     })
 
-    server = WebSocketServer(("", 8000), resource, protocols=resource.supported_protocols,  debug=True)
+    server = WebSocketServer(("", 8000), resource, debug=True)
     server.serve_forever()

File geventwebsocket/handler.py

             return [msg]
 
         # Check for WebSocket Protocols
-        requested_protocols = set(self.environ.get('HTTP_SEC_WEBSOCKET_PROTOCOL', '').split(','))
-        allowed_protocols = requested_protocols & set(self.server.protocols)
-        self.logger.debug("Protocols allowed: {0}".format(", ".join(allowed_protocols)))
+        requested_protocols = self.environ.get('HTTP_SEC_WEBSOCKET_PROTOCOL', '')
+
+        if hasattr(self.application, 'app_protocol'):
+            allowed_protocol = self.application.app_protocol(self.environ['PATH_INFO'])
+            if allowed_protocol in requested_protocols:
+                protocol = allowed_protocol
+                self.logger.debug("Protocol allowed: {0}".format(protocol))
+            else:
+                protocol = ''
+        else:
+            protocol = ''
 
         self.websocket = WebSocket(self.environ, Stream(self), self)
         self.environ.update({
         headers = [
             ("Upgrade", "websocket"),
             ("Connection", "Upgrade"),
-            ("Sec-WebSocket-Protocol", ", ".join(allowed_protocols)),
+            ("Sec-WebSocket-Protocol", protocol),
             ("Sec-WebSocket-Accept", base64.b64encode(
                 hashlib.sha1(key + self.GUID).digest())),
         ]

File geventwebsocket/resource.py

     def on_message(self, message, *args, **kwargs):
         self.ws.send(message, **kwargs)
 
-    def supported_protocols(self):
-        return []
+    @classmethod
+    def protocol(self):
+        return ''
 
 
-class MessageResource(object):
+class Resource(object):
     def __init__(self, environ=None, apps=[]):
         self.environ = environ
         self.ws = None
         self.apps = apps
         self.current_app = None
 
-    @property
-    def supported_protocols(self):
-        protocols = []
-
-        for app in self.apps.values():
-            protocols.extend(app.supported_protocols())
-
-        return protocols
+    def app_protocol(self, path):
+        if path in self.apps:
+            return self.apps[path].protocol()
+        else:
+            return ''
 
     def listen(self):
         self.ws = self.environ['wsgi.websocket']

File geventwebsocket/server.py

 from gevent.pywsgi import WSGIServer
 
+from .resource import Resource
 from .handler import WebSocketHandler
 from .logging import create_logger
 
     def __init__(self, *args, **kwargs):
         self.debug = kwargs.pop('debug', False)
         self.pre_start_hook = kwargs.pop('pre_start_hook', None)
-        self.protocols = kwargs.pop('protocols', [])
-
         self._logger = None
 
         kwargs['handler_class'] = WebSocketHandler