Replace duplicate code in http.server with call to http.client.parse_headers().

This commit is contained in:
Jeremy Hylton 2009-03-27 17:14:18 +00:00
parent ef9f48e578
commit e6fdd04b37
2 changed files with 2 additions and 17 deletions

View file

@ -237,8 +237,6 @@ def parse_headers(fp):
to parse. to parse.
""" """
# XXX: Copied from http.server.BaseHTTPRequestHandler.parse_request,
# maybe we can just call this function from there.
headers = [] headers = []
while True: while True:
line = fp.readline() line = fp.readline()

View file

@ -88,6 +88,7 @@ import io
import os import os
import sys import sys
import cgi import cgi
import http.client
import time import time
import socket # For gethostbyaddr() import socket # For gethostbyaddr()
import shutil import shutil
@ -312,20 +313,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
self.command, self.path, self.request_version = command, path, version self.command, self.path, self.request_version = command, path, version
# Examine the headers and look for a Connection directive. # Examine the headers and look for a Connection directive.
self.headers = http.client.parse_headers(self.rfile)
# MessageClass wants to see strings rather than bytes.
# But a TextIOWrapper around self.rfile would buffer too many bytes
# from the stream, bytes which we later need to read as bytes.
# So we read the correct bytes here, as bytes, then use StringIO
# to make them look like strings for MessageClass to parse.
headers = []
while True:
line = self.rfile.readline()
headers.append(line)
if line in (b'\r\n', b'\n', b''):
break
hfile = io.StringIO(b''.join(headers).decode('iso-8859-1'))
self.headers = email.parser.Parser(_class=self.MessageClass).parse(hfile)
conntype = self.headers.get('Connection', "") conntype = self.headers.get('Connection', "")
if conntype.lower() == 'close': if conntype.lower() == 'close':
@ -524,7 +512,6 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
protocol_version = "HTTP/1.0" protocol_version = "HTTP/1.0"
# MessageClass used to parse headers # MessageClass used to parse headers
import http.client
MessageClass = http.client.HTTPMessage MessageClass = http.client.HTTPMessage
# Table mapping response codes to messages; entries have the # Table mapping response codes to messages; entries have the