Skip to content

Commit

Permalink
Replace duplicate code in http.server with call to http.client.parse_…
Browse files Browse the repository at this point in the history
…headers().
  • Loading branch information
jeremyhylton committed Mar 27, 2009
1 parent ef9f48e commit e6fdd04
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 17 deletions.
2 changes: 0 additions & 2 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ def parse_headers(fp):
to parse.
"""
# XXX: Copied from http.server.BaseHTTPRequestHandler.parse_request,
# maybe we can just call this function from there.
headers = []
while True:
line = fp.readline()
Expand Down
17 changes: 2 additions & 15 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import os
import sys
import cgi
import http.client
import time
import socket # For gethostbyaddr()
import shutil
Expand Down Expand Up @@ -312,20 +313,7 @@ def parse_request(self):
self.command, self.path, self.request_version = command, path, version

# Examine the headers and look for a Connection directive.

# 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)
self.headers = http.client.parse_headers(self.rfile)

conntype = self.headers.get('Connection', "")
if conntype.lower() == 'close':
Expand Down Expand Up @@ -524,7 +512,6 @@ def address_string(self):
protocol_version = "HTTP/1.0"

# MessageClass used to parse headers
import http.client
MessageClass = http.client.HTTPMessage

# Table mapping response codes to messages; entries have the
Expand Down

0 comments on commit e6fdd04

Please sign in to comment.