From bf9a4232112a3c74d1d8b77ddb71a43f6a225786 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Tue, 10 Nov 2015 17:28:06 -0800 Subject: [PATCH] Twisted HTTP interface now serves basic requests --- channels/management/commands/runworker.py | 8 ++++++-- channels/request.py | 18 ++++++++++++++++-- docs/getting-started.rst | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/channels/management/commands/runworker.py b/channels/management/commands/runworker.py index 2c157bc..32692e9 100644 --- a/channels/management/commands/runworker.py +++ b/channels/management/commands/runworker.py @@ -1,8 +1,8 @@ from django.core.management import BaseCommand, CommandError - -from channels import DEFAULT_CHANNEL_BACKEND, channel_backends +from channels import channel_backends, DEFAULT_CHANNEL_BACKEND from channels.log import setup_logger +from channels.adapters import UrlConsumer from channels.worker import Worker @@ -18,6 +18,10 @@ class Command(BaseCommand): "You have a process-local channel backend configured, and so cannot run separate workers.\n" "Configure a network-based backend in CHANNEL_BACKENDS to use this command." ) + # Check a handler is registered for http reqs + if not channel_backend.registry.consumer_for_channel("http.request"): + # Register the default one + channel_backend.registry.add_consumer(UrlConsumer(), ["http.request"]) # Launch a worker self.logger.info("Running worker against backend %s", channel_backend) # Optionally provide an output callback diff --git a/channels/request.py b/channels/request.py index 4060335..0c5de55 100644 --- a/channels/request.py +++ b/channels/request.py @@ -12,7 +12,11 @@ def encode_request(request): "get": dict(request.GET.lists()), "post": dict(request.POST.lists()), "cookies": request.COOKIES, - "meta": {k: v for k, v in request.META.items() if not k.startswith("wsgi")}, + "headers": { + k[5:].lower(): v + for k, v in request.META.items() + if k.lower().startswith("http_") + }, "path": request.path, "method": request.method, "reply_channel": request.reply_channel, @@ -28,10 +32,20 @@ def decode_request(value): request.GET = CustomQueryDict(value['get']) request.POST = CustomQueryDict(value['post']) request.COOKIES = value['cookies'] - request.META = value['meta'] request.path = value['path'] request.method = value['method'] request.reply_channel = value['reply_channel'] + # Channels requests are more high-level than the dumping ground that is + # META; re-combine back into it + request.META = { + "REQUEST_METHOD": value["method"], + "SERVER_NAME": value["server"][0], + "SERVER_PORT": value["server"][1], + "REMOTE_ADDR": value["client"][0], + "REMOTE_HOST": value["client"][0], # Not the DNS name, hopefully fine. + } + for header, header_value in value.get("headers", {}).items(): + request.META["HTTP_%s" % header.upper()] = header_value # We don't support non-/ script roots request.path_info = value['path'] return request diff --git a/docs/getting-started.rst b/docs/getting-started.rst index e11b0db..283cdcf 100644 --- a/docs/getting-started.rst +++ b/docs/getting-started.rst @@ -203,7 +203,7 @@ The easiest way to do this is to use the ``runwsserver`` management command that ships with Django; just make sure you've installed the latest release of ``autobahn`` first:: - pip install -U autobahn + pip install -U autobahn[twisted] python manage.py runwsserver Run that alongside ``runserver`` and you'll have two interface servers, a