Skip to content

Commit

Permalink
Unbreak Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Oct 21, 2017
1 parent 030b452 commit 5bf442a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/zope/server/http/httprequestparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ def parse_header(self, header_plus):
lines = self.get_header_lines()
headers = self.headers
for line in lines:
index = line.find(b':')
if not isinstance(line, str):
line = line.decode('latin1')
index = line.find(':')
if index > 0:
key = line[:index].decode('latin1')
value = line[index + 1:].strip().decode('latin1')
key = line[:index]
value = line[index + 1:].strip()
key1 = key.upper().replace('-', '_')
# If a header already exists, we append subsequent values
# seperated by a comma. Applications already need to handle
Expand Down Expand Up @@ -170,19 +172,21 @@ def get_header_lines(self):
return r

first_line_re = re.compile(
b'([^ ]+) ((?:[^ :?#]+://[^ ?#/]*(?:[0-9]{1,5})?)?[^ ]+)'
b'(( HTTP/([0-9.]+))$|$)')
'([^ ]+) ((?:[^ :?#]+://[^ ?#/]*(?:[0-9]{1,5})?)?[^ ]+)'
'(( HTTP/([0-9.]+))$|$)')

def crack_first_line(self):
r = self.first_line
if not isinstance(r, str):
r = r.decode('latin1')
m = self.first_line_re.match(r)
if m is not None and m.end() == len(r):
if m.group(3):
version = m.group(5).decode('latin1')
version = m.group(5)
else:
version = None
method = m.group(1).upper().decode('latin1')
uri = m.group(2).decode('latin1')
method = m.group(1).upper()
uri = m.group(2)
return (method, uri, version)
else:
return None, None, None
Expand Down
4 changes: 3 additions & 1 deletion src/zope/server/linereceiver/linecommandparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def received(self, data):
return len(s)

def parseLine(self, line):
parts = line.decode('latin-1').split(' ', 1)
if not isinstance(line, str):
line = line.decode('utf-8')
parts = line.split(' ', 1)
if len(parts) == 2:
self.cmd, self.args = parts
else:
Expand Down

0 comments on commit 5bf442a

Please sign in to comment.