Twisted HTTP interface now serves basic requests

This commit is contained in:
Andrew Godwin 2015-11-10 17:28:06 -08:00
parent 17e42a85fb
commit bf9a423211
3 changed files with 23 additions and 5 deletions

View File

@ -1,8 +1,8 @@
from django.core.management import BaseCommand, CommandError from django.core.management import BaseCommand, CommandError
from channels import channel_backends, DEFAULT_CHANNEL_BACKEND
from channels import DEFAULT_CHANNEL_BACKEND, channel_backends
from channels.log import setup_logger from channels.log import setup_logger
from channels.adapters import UrlConsumer
from channels.worker import Worker 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" "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." "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 # Launch a worker
self.logger.info("Running worker against backend %s", channel_backend) self.logger.info("Running worker against backend %s", channel_backend)
# Optionally provide an output callback # Optionally provide an output callback

View File

@ -12,7 +12,11 @@ def encode_request(request):
"get": dict(request.GET.lists()), "get": dict(request.GET.lists()),
"post": dict(request.POST.lists()), "post": dict(request.POST.lists()),
"cookies": request.COOKIES, "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, "path": request.path,
"method": request.method, "method": request.method,
"reply_channel": request.reply_channel, "reply_channel": request.reply_channel,
@ -28,10 +32,20 @@ def decode_request(value):
request.GET = CustomQueryDict(value['get']) request.GET = CustomQueryDict(value['get'])
request.POST = CustomQueryDict(value['post']) request.POST = CustomQueryDict(value['post'])
request.COOKIES = value['cookies'] request.COOKIES = value['cookies']
request.META = value['meta']
request.path = value['path'] request.path = value['path']
request.method = value['method'] request.method = value['method']
request.reply_channel = value['reply_channel'] 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 # We don't support non-/ script roots
request.path_info = value['path'] request.path_info = value['path']
return request return request

View File

@ -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 that ships with Django; just make sure you've installed the latest release
of ``autobahn`` first:: of ``autobahn`` first::
pip install -U autobahn pip install -U autobahn[twisted]
python manage.py runwsserver python manage.py runwsserver
Run that alongside ``runserver`` and you'll have two interface servers, a Run that alongside ``runserver`` and you'll have two interface servers, a