mirror of https://github.com/django/django.git
Removed custom WSGIRequestHandler.get_environ
We probably historically customized it for good reasons, but currently, the differences with upstream Python are not significant any longer. Also fixes #19075 for which a test has been added.
This commit is contained in:
parent
3084b1cfd6
commit
681550ca6d
|
@ -14,9 +14,8 @@ import socket
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
try:
|
try:
|
||||||
from urllib.parse import unquote, urljoin
|
from urllib.parse import urljoin
|
||||||
except ImportError: # Python 2
|
except ImportError: # Python 2
|
||||||
from urllib import unquote
|
|
||||||
from urlparse import urljoin
|
from urlparse import urljoin
|
||||||
from django.utils.six.moves import socketserver
|
from django.utils.six.moves import socketserver
|
||||||
from wsgiref import simple_server
|
from wsgiref import simple_server
|
||||||
|
@ -139,37 +138,6 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):
|
||||||
self.style = color_style()
|
self.style = color_style()
|
||||||
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
|
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def get_environ(self):
|
|
||||||
env = self.server.base_environ.copy()
|
|
||||||
env['SERVER_PROTOCOL'] = self.request_version
|
|
||||||
env['REQUEST_METHOD'] = self.command
|
|
||||||
if '?' in self.path:
|
|
||||||
path,query = self.path.split('?',1)
|
|
||||||
else:
|
|
||||||
path,query = self.path,''
|
|
||||||
|
|
||||||
env['PATH_INFO'] = unquote(path)
|
|
||||||
env['QUERY_STRING'] = query
|
|
||||||
env['REMOTE_ADDR'] = self.client_address[0]
|
|
||||||
env['CONTENT_TYPE'] = self.headers.get('content-type', 'text/plain')
|
|
||||||
|
|
||||||
length = self.headers.get('content-length')
|
|
||||||
if length:
|
|
||||||
env['CONTENT_LENGTH'] = length
|
|
||||||
|
|
||||||
for key, value in self.headers.items():
|
|
||||||
key = key.replace('-','_').upper()
|
|
||||||
value = value.strip()
|
|
||||||
if key in env:
|
|
||||||
# Skip content length, type, etc.
|
|
||||||
continue
|
|
||||||
if 'HTTP_' + key in env:
|
|
||||||
# Comma-separate multiple headers
|
|
||||||
env['HTTP_' + key] += ',' + value
|
|
||||||
else:
|
|
||||||
env['HTTP_' + key] = value
|
|
||||||
return env
|
|
||||||
|
|
||||||
def log_message(self, format, *args):
|
def log_message(self, format, *args):
|
||||||
# Don't bother logging requests for admin images or the favicon.
|
# Don't bother logging requests for admin images or the favicon.
|
||||||
if (self.path.startswith(self.admin_static_prefix)
|
if (self.path.startswith(self.admin_static_prefix)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
|
# -*- encoding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Tests for django.core.servers.
|
Tests for django.core.servers.
|
||||||
"""
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import os
|
import os
|
||||||
try:
|
try:
|
||||||
from urllib.request import urlopen, HTTPError
|
from urllib.request import urlopen, HTTPError
|
||||||
|
@ -11,6 +14,7 @@ from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.test import LiveServerTestCase
|
from django.test import LiveServerTestCase
|
||||||
from django.core.servers.basehttp import WSGIServerException
|
from django.core.servers.basehttp import WSGIServerException
|
||||||
from django.test.utils import override_settings
|
from django.test.utils import override_settings
|
||||||
|
from django.utils.http import urlencode
|
||||||
|
|
||||||
from .models import Person
|
from .models import Person
|
||||||
|
|
||||||
|
@ -134,6 +138,10 @@ class LiveServerViews(LiveServerBase):
|
||||||
f = self.urlopen('/media/example_media_file.txt')
|
f = self.urlopen('/media/example_media_file.txt')
|
||||||
self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file')
|
self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file')
|
||||||
|
|
||||||
|
def test_environ(self):
|
||||||
|
f = self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))
|
||||||
|
self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
|
||||||
|
|
||||||
|
|
||||||
class LiveServerDatabase(LiveServerBase):
|
class LiveServerDatabase(LiveServerBase):
|
||||||
|
|
||||||
|
|
|
@ -9,4 +9,5 @@ urlpatterns = patterns('',
|
||||||
url(r'^example_view/$', views.example_view),
|
url(r'^example_view/$', views.example_view),
|
||||||
url(r'^model_view/$', views.model_view),
|
url(r'^model_view/$', views.model_view),
|
||||||
url(r'^create_model_instance/$', views.create_model_instance),
|
url(r'^create_model_instance/$', views.create_model_instance),
|
||||||
)
|
url(r'^environ_view/$', views.environ_view),
|
||||||
|
)
|
||||||
|
|
|
@ -14,4 +14,8 @@ def model_view(request):
|
||||||
def create_model_instance(request):
|
def create_model_instance(request):
|
||||||
person = Person(name='emily')
|
person = Person(name='emily')
|
||||||
person.save()
|
person.save()
|
||||||
return HttpResponse('')
|
return HttpResponse('')
|
||||||
|
|
||||||
|
|
||||||
|
def environ_view(request):
|
||||||
|
return HttpResponse("\n".join(["%s: %r" % (k, v) for k, v in request.environ.items()]))
|
||||||
|
|
Loading…
Reference in New Issue