Skip to content

Commit

Permalink
proper route cleaning for websockets
Browse files Browse the repository at this point in the history
adding a route cleaning method that ensures `REDIS_PUBSUB["websocket_url_prefix"]` is prefixed using the `os.path.join` method. prior to this, if a route began with a slash and `REDIS_PUBSUB["websocket_url_prefix"]` was set, then the prefix would not be applied. additionally added a check to see if a clients settings include the `APPEND_SLASH` variable. This way websocket urls will match that of the DJANGO project.
  • Loading branch information
nd-young committed Feb 12, 2016
1 parent da8912b commit 378aa8e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions redis_pubsub/contrib/websockets/util.py
Expand Up @@ -3,6 +3,7 @@
import logging
import os

from django.conf import settings
from django.utils.module_loading import import_string

from aiohttp.web import WebSocketResponse, HTTPForbidden, Application
Expand All @@ -28,6 +29,17 @@ def handle_auth(token):
if user is None:
raise HTTPForbidden(body=b"invalid token")
return user


def _clean_route(route):
""" ensure that the route can be prefixed with os.path.join and ends with a slash if.
"""
if route.startswith("/"):
route = route[1:]
route_ = os.path.join(REDIS_PUBSUB["websocket_url_prefix"], route)
if not route_.endswith("/") and settings.APPEND_SLASH:
route_ = route_ + "/"
return route_


def websocket(route, authenticate=False):
Expand All @@ -54,7 +66,8 @@ def wrapper(request):

return ws

route_ = os.path.join(REDIS_PUBSUB["websocket_url_prefix"], route)
# cleanup the route
route_ = _clean_route(route)
wrapper.route = ("GET", route_, wrapper)
return wrapper
return inner
Expand Down Expand Up @@ -95,7 +108,8 @@ def wrapper(request):

return ws

route_ = os.path.join(REDIS_PUBSUB["websocket_url_prefix"], route)
# cleanup the route
route_ = _clean_route(route)
wrapper.route = ("GET", route_, wrapper)
return wrapper
return inner

0 comments on commit 378aa8e

Please sign in to comment.