Fixed #25619 -- Made runserver serve with HTTP 1.1 protocol

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2015-10-31 00:38:34 +01:00
parent 007d4e030c
commit e6065c7b83
3 changed files with 28 additions and 2 deletions

View File

@ -78,6 +78,8 @@ class ThreadedWSGIServer(socketserver.ThreadingMixIn, WSGIServer):
class ServerHandler(simple_server.ServerHandler): class ServerHandler(simple_server.ServerHandler):
http_version = '1.1'
def handle_error(self): def handle_error(self):
# Ignore broken pipe errors, otherwise pass on # Ignore broken pipe errors, otherwise pass on
if not is_broken_pipe_error(): if not is_broken_pipe_error():
@ -85,6 +87,8 @@ class ServerHandler(simple_server.ServerHandler):
class WSGIRequestHandler(simple_server.WSGIRequestHandler): class WSGIRequestHandler(simple_server.WSGIRequestHandler):
protocol_version = 'HTTP/1.1'
def address_string(self): def address_string(self):
# Short-circuit parent method to not call socket.getfqdn # Short-circuit parent method to not call socket.getfqdn
return self.client_address[0] return self.client_address[0]
@ -131,8 +135,14 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler):
return super().get_environ() return super().get_environ()
def handle(self): def handle(self):
"""Copy of WSGIRequestHandler, but with different ServerHandler""" """Handle multiple requests if necessary."""
self.close_connection = 1
self.handle_one_request()
while not self.close_connection:
self.handle_one_request()
def handle_one_request(self):
"""Copy of WSGIRequestHandler.handle() but with different ServerHandler"""
self.raw_requestline = self.rfile.readline(65537) self.raw_requestline = self.rfile.readline(65537)
if len(self.raw_requestline) > 65536: if len(self.raw_requestline) > 65536:
self.requestline = '' self.requestline = ''

View File

@ -173,7 +173,7 @@ Models
Requests and Responses Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
* ... * The :djadmin:`runserver` Web server supports HTTP 1.1.
Serialization Serialization
~~~~~~~~~~~~~ ~~~~~~~~~~~~~

View File

@ -4,11 +4,13 @@ Tests for django.core.servers.
import errno import errno
import os import os
import socket import socket
from http.client import HTTPConnection
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.parse import urlencode from urllib.parse import urlencode
from urllib.request import urlopen from urllib.request import urlopen
from django.test import LiveServerTestCase, override_settings from django.test import LiveServerTestCase, override_settings
from django.test.utils import captured_stdout
from .models import Person from .models import Person
@ -50,6 +52,20 @@ class LiveServerAddress(LiveServerBase):
class LiveServerViews(LiveServerBase): class LiveServerViews(LiveServerBase):
def test_protocol(self):
"""Launched server serves with HTTP 1.1."""
with captured_stdout() as debug_output:
conn = HTTPConnection(LiveServerViews.server_thread.host, LiveServerViews.server_thread.port)
try:
conn.set_debuglevel(1)
conn.request('GET', '/example_view/', headers={"Connection": "keep-alive"})
conn.getresponse().read()
conn.request('GET', '/example_view/', headers={"Connection": "close"})
conn.getresponse()
finally:
conn.close()
self.assertEqual(debug_output.getvalue().count("reply: 'HTTP/1.1 200 OK"), 2)
def test_404(self): def test_404(self):
with self.assertRaises(HTTPError) as err: with self.assertRaises(HTTPError) as err:
self.urlopen('/') self.urlopen('/')