1
0
mirror of https://github.com/django/django.git synced 2024-12-23 09:36:06 +00:00
django/tests/servers/views.py
Florian Apolloner 934acf1126 Fixed keep-alive support in manage.py runserver.
Ticket #25619 changed the default protocol to HTTP/1.1 but did not
properly implement keep-alive. As a "fix" keep-alive was disabled in
ticket #28440 to prevent clients from hanging (they expect the server to
send more data if the connection is not closed and there is no content
length set).

The combination of those two fixes resulted in yet another problem:
HTTP/1.1 by default allows a client to assume that keep-alive is
supported unless the server disables it via 'Connection: close' -- see
RFC2616 8.1.2.1 for details on persistent connection negotiation. Now if
the client receives a response from Django without 'Connection: close'
and immediately sends a new request (on the same tcp connection) before
our server closes the tcp connection, it will error out at some point
because the connection does get closed a few milli seconds later.

This patch fixes the mentioned issues by always sending 'Connection:
close' if we cannot determine a content length. The code is inefficient
in the sense that it does not allow for persistent connections when
chunked responses are used, but that should not really cause any
problems (Django does not generate those) and it only affects the
development server anyways.

Refs #25619, #28440.
2018-11-10 13:54:45 +01:00

45 lines
1.2 KiB
Python

from urllib.request import urlopen
from django.http import HttpResponse, StreamingHttpResponse
from .models import Person
def example_view(request):
return HttpResponse('example view')
def streaming_example_view(request):
return StreamingHttpResponse((b'I', b'am', b'a', b'stream'))
def model_view(request):
people = Person.objects.all()
return HttpResponse('\n'.join(person.name for person in people))
def create_model_instance(request):
person = Person(name='emily')
person.save()
return HttpResponse('')
def environ_view(request):
return HttpResponse("\n".join("%s: %r" % (k, v) for k, v in request.environ.items()))
def subview(request):
return HttpResponse('subview')
def subview_calling_view(request):
with urlopen(request.GET['url'] + '/subview/') as response:
return HttpResponse('subview calling view: {}'.format(response.read().decode()))
def check_model_instance_from_subview(request):
with urlopen(request.GET['url'] + '/create_model_instance/'):
pass
with urlopen(request.GET['url'] + '/model_view/') as response:
return HttpResponse('subview calling view: {}'.format(response.read().decode()))