Ticket #19075: 0001-Removed-custom-WSGIRequestHandler.get_environ.patch

File 0001-Removed-custom-WSGIRequestHandler.get_environ.patch, 4.8 KB (added by Claude Paroz, 12 years ago)

Removed custom WSGIRequestHandler.get_environ

  • django/core/servers/basehttp.py

    From 53e0659287692975afd570cb645b75d060fcffbc Mon Sep 17 00:00:00 2001
    From: Claude Paroz <claude@2xlibre.net>
    Date: Sat, 20 Oct 2012 12:34:50 +0200
    Subject: [PATCH] Removed custom WSGIRequestHandler.get_environ
    
    We probably historically customized it for good reasons, but
    currently, the differences are not significant any longer.
    Also adding a test for #19075.
    ---
     django/core/servers/basehttp.py        |   34 +-------------------------------
     tests/regressiontests/servers/tests.py |    8 +++++++
     tests/regressiontests/servers/urls.py  |    3 +-
     tests/regressiontests/servers/views.py |    6 ++++-
     4 files changed, 16 insertions(+), 35 deletions(-)
    
    diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
    index 19b287a..a7004f2 100644
    a b import socket  
    1414import sys
    1515import traceback
    1616try:
    17     from urllib.parse import unquote, urljoin
     17    from urllib.parse import urljoin
    1818except ImportError:     # Python 2
    19     from urllib import unquote
    2019    from urlparse import urljoin
    2120from django.utils.six.moves import socketserver
    2221from wsgiref import simple_server
    class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):  
    139138        self.style = color_style()
    140139        super(WSGIRequestHandler, self).__init__(*args, **kwargs)
    141140
    142     def get_environ(self):
    143         env = self.server.base_environ.copy()
    144         env['SERVER_PROTOCOL'] = self.request_version
    145         env['REQUEST_METHOD'] = self.command
    146         if '?' in self.path:
    147             path,query = self.path.split('?',1)
    148         else:
    149             path,query = self.path,''
    150 
    151         env['PATH_INFO'] = unquote(path)
    152         env['QUERY_STRING'] = query
    153         env['REMOTE_ADDR'] = self.client_address[0]
    154         env['CONTENT_TYPE'] = self.headers.get('content-type', 'text/plain')
    155 
    156         length = self.headers.get('content-length')
    157         if length:
    158             env['CONTENT_LENGTH'] = length
    159 
    160         for key, value in self.headers.items():
    161             key = key.replace('-','_').upper()
    162             value = value.strip()
    163             if key in env:
    164                 # Skip content length, type, etc.
    165                 continue
    166             if 'HTTP_' + key in env:
    167                 # Comma-separate multiple headers
    168                 env['HTTP_' + key] += ',' + value
    169             else:
    170                 env['HTTP_' + key] = value
    171         return env
    172 
    173141    def log_message(self, format, *args):
    174142        # Don't bother logging requests for admin images or the favicon.
    175143        if (self.path.startswith(self.admin_static_prefix)
  • tests/regressiontests/servers/tests.py

    diff --git a/tests/regressiontests/servers/tests.py b/tests/regressiontests/servers/tests.py
    index c90c785..f54e34c 100644
    a b  
     1# -*- encoding: utf-8 -*-
    12"""
    23Tests for django.core.servers.
    34"""
     5from __future__ import unicode_literals
     6
    47import os
    58try:
    69    from urllib.request import urlopen, HTTPError
    from django.core.exceptions import ImproperlyConfigured  
    1114from django.test import LiveServerTestCase
    1215from django.core.servers.basehttp import WSGIServerException
    1316from django.test.utils import override_settings
     17from django.utils.http import urlencode
    1418
    1519from .models import Person
    1620
    class LiveServerViews(LiveServerBase):  
    134138        f = self.urlopen('/media/example_media_file.txt')
    135139        self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file')
    136140
     141    def test_environ(self):
     142        f = self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))
     143        self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
     144
    137145
    138146class LiveServerDatabase(LiveServerBase):
    139147
  • tests/regressiontests/servers/urls.py

    diff --git a/tests/regressiontests/servers/urls.py b/tests/regressiontests/servers/urls.py
    index c8ca1ac..a857c45 100644
    a b urlpatterns = patterns('',  
    99    url(r'^example_view/$', views.example_view),
    1010    url(r'^model_view/$', views.model_view),
    1111    url(r'^create_model_instance/$', views.create_model_instance),
    12 )
    13  No newline at end of file
     12    url(r'^environ_view/$', views.environ_view),
     13)
  • tests/regressiontests/servers/views.py

    diff --git a/tests/regressiontests/servers/views.py b/tests/regressiontests/servers/views.py
    index 94a4f2d..00baf4b 100644
    a b def model_view(request):  
    1414def create_model_instance(request):
    1515    person = Person(name='emily')
    1616    person.save()
    17     return HttpResponse('')
    18  No newline at end of file
     17    return HttpResponse('')
     18
     19
     20def environ_view(request):
     21    return HttpResponse("\n".join(["%s: %r" % (k, v) for k, v in request.environ.items()]))
Back to Top