From e6e4240c0e6dbd847696c781f025726968447a44 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Fri, 18 Nov 2016 10:22:03 +0000 Subject: [PATCH] Implement IProtocolNegotiationFactory. --- daphne/http_protocol.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 63f63a7..e466219 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -5,7 +5,10 @@ import six import time import traceback +from zope.interface import implementer + from six.moves.urllib_parse import unquote, unquote_plus +from twisted.internet.interfaces import IProtocolNegotiationFactory from twisted.protocols.policies import ProtocolWrapper from twisted.web import http @@ -283,6 +286,7 @@ class WebRequest(http.Request): }) +@implementer(IProtocolNegotiationFactory) class HTTPFactory(http.HTTPFactory): """ Factory which takes care of tracking which protocol @@ -381,3 +385,18 @@ class HTTPFactory(http.HTTPFactory): # Ping check else: protocol.check_ping() + + # IProtocolNegotiationFactory + def acceptableProtocols(self): + """ + Protocols this server can speak after ALPN negotiation. Currently that + is HTTP/1.1 and optionally HTTP/2. Websockets cannot be negotiated + using ALPN, so that doesn't go here: anyone wanting websockets will + negotiate HTTP/1.1 and then do the upgrade dance. + """ + baseProtocols = [b'http/1.1'] + + if http.H2_ENABLED: + baseProtocols.insert(0, b'h2') + + return baseProtocols