From e6065c7b8363202c5eb13ba10c97a8c24d014b45 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 31 Oct 2015 00:38:34 +0100 Subject: [PATCH] Fixed #25619 -- Made runserver serve with HTTP 1.1 protocol Thanks Tim Graham for the review. --- django/core/servers/basehttp.py | 12 +++++++++++- docs/releases/2.0.txt | 2 +- tests/servers/tests.py | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py index 8ef39e98fc9..d7252411700 100644 --- a/django/core/servers/basehttp.py +++ b/django/core/servers/basehttp.py @@ -78,6 +78,8 @@ class ThreadedWSGIServer(socketserver.ThreadingMixIn, WSGIServer): class ServerHandler(simple_server.ServerHandler): + http_version = '1.1' + def handle_error(self): # Ignore broken pipe errors, otherwise pass on if not is_broken_pipe_error(): @@ -85,6 +87,8 @@ class ServerHandler(simple_server.ServerHandler): class WSGIRequestHandler(simple_server.WSGIRequestHandler): + protocol_version = 'HTTP/1.1' + def address_string(self): # Short-circuit parent method to not call socket.getfqdn return self.client_address[0] @@ -131,8 +135,14 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler): return super().get_environ() 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) if len(self.raw_requestline) > 65536: self.requestline = '' diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 8670e332f07..ad78c84fe5f 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -173,7 +173,7 @@ Models Requests and Responses ~~~~~~~~~~~~~~~~~~~~~~ -* ... +* The :djadmin:`runserver` Web server supports HTTP 1.1. Serialization ~~~~~~~~~~~~~ diff --git a/tests/servers/tests.py b/tests/servers/tests.py index b114b968dfa..cb911316072 100644 --- a/tests/servers/tests.py +++ b/tests/servers/tests.py @@ -4,11 +4,13 @@ Tests for django.core.servers. import errno import os import socket +from http.client import HTTPConnection from urllib.error import HTTPError from urllib.parse import urlencode from urllib.request import urlopen from django.test import LiveServerTestCase, override_settings +from django.test.utils import captured_stdout from .models import Person @@ -50,6 +52,20 @@ class LiveServerAddress(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): with self.assertRaises(HTTPError) as err: self.urlopen('/')