Skip to content

Commit

Permalink
wsgiserver optimization: no need for mimetools' subclass of rfc822.Me…
Browse files Browse the repository at this point in the history
…ssage. Also inlined header transform into a single pass.
  • Loading branch information
aminusfu committed Dec 22, 2006
1 parent 4ef5c12 commit 20f3aa0
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions cherrypy/wsgiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def my_crazy_app(environ, start_response):


import base64
import mimetools # todo: use email
import Queue
import os
import re
Expand Down Expand Up @@ -75,15 +74,12 @@ def my_crazy_app(environ, start_response):
socket_errors_to_ignore = dict.fromkeys(socket_errors_to_ignore).keys()
socket_errors_to_ignore.append("timed out")

# These are lowercase because mimetools.Message uses lowercase keys.
comma_separated_headers = [
'accept', 'accept-charset', 'accept-encoding', 'accept-language',
'accept-ranges', 'allow', 'cache-control', 'connection', 'content-encoding',
'content-language', 'expect', 'if-match', 'if-none-match', 'pragma',
'proxy-authenticate', 'te', 'trailer', 'transfer-encoding', 'upgrade',
'vary', 'via', 'warning', 'www-authenticate',
]

comma_separated_headers = ['ACCEPT', 'ACCEPT-CHARSET', 'ACCEPT-ENCODING',
'ACCEPT-LANGUAGE', 'ACCEPT-RANGES', 'ALLOW', 'CACHE-CONTROL',
'CONNECTION', 'CONTENT-ENCODING', 'CONTENT-LANGUAGE', 'EXPECT',
'IF-MATCH', 'IF-NONE-MATCH', 'PRAGMA', 'PROXY-AUTHENTICATE', 'TE',
'TRAILER', 'TRANSFER-ENCODING', 'UPGRADE', 'VARY', 'VIA', 'WARNING',
'WWW-AUTHENTICATE']

class HTTPRequest(object):
"""An HTTP Request (and response).
Expand Down Expand Up @@ -223,7 +219,7 @@ def parse_request(self):
self.environ["SERVER_NAME"] = location

# then all the http headers
headers = mimetools.Message(self.rfile)
headers = rfc822.Message(self.rfile)
self.environ.update(self.parse_headers(headers))

creds = headers.getheader("Authorization", "").split(" ", 1)
Expand Down Expand Up @@ -293,27 +289,27 @@ def parse_request(self):
def parse_headers(self, headers):
"""Parse the given HTTP request message-headers."""
environ = {}
ct = headers.getheader("Content-type", "")
ct = headers.dict.get("content-type")
if ct:
environ["CONTENT_TYPE"] = ct
cl = headers.getheader("Content-length") or ""
cl = headers.dict.get("content-length")
if cl:
environ["CONTENT_LENGTH"] = cl

# Must use keys() here for Python 2.3 (rfc822.Message had no __iter__).
for k in headers.keys():
if k in ('transfer-encoding', 'content-type', 'content-length'):
continue
for line in headers.headers:
if line[:1].isspace():
v = line.strip()
else:
k, v = line.split(":", 1)
k, v = k.strip().upper(), v.strip()
envname = "HTTP_" + k.replace("-", "_")

envname = "HTTP_" + k.upper().replace("-", "_")
if k in comma_separated_headers:
existing = environ.get(envname)
if existing:
environ[envname] = ", ".join([existing] + headers.getheaders(k))
else:
environ[envname] = ", ".join(headers.getheaders(k))
else:
environ[envname] = headers[k]
v = ", ".join((existing, v))
environ[envname] = v

return environ

def decode_chunked(self):
Expand All @@ -336,7 +332,7 @@ def decode_chunked(self):
return

# Grab any trailer headers
headers = mimetools.Message(self.rfile)
headers = rfc822.Message(self.rfile)
self.environ.update(self.parse_headers(headers))

data.seek(0)
Expand Down

0 comments on commit 20f3aa0

Please sign in to comment.