Skip to content

Commit

Permalink
Convert date elements to strings so they print correctly
Browse files Browse the repository at this point in the history
Non-numeric elements of dates were printed with a b'' wrapper (since
they are bytes), e.g.

    Date: b'Wed', 12 b'Jun' 2019 08:28:30 GMT

This change converts the elements to a string representation to remove
the b'' wrapper.
  • Loading branch information
Ricardo E. Gonzalez authored and netom committed Aug 25, 2019
1 parent 73ff8e4 commit 1e7b89b
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions pyicap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
For the ICAP specification, see RFC 3507
"""

from six import PY3, binary_type, text_type
import sys
import time
import random
Expand Down Expand Up @@ -30,6 +31,27 @@
__all__ = ['ICAPServer', 'BaseICAPRequestHandler', 'ICAPError']


def native(s):
"""
Convert :py:class:`bytes` or :py:class:`unicode` to the native
:py:class:`str` type, using UTF-8 encoding if conversion is necessary.
:raise UnicodeError: The input string is not UTF-8 decodeable.
:raise TypeError: The input is neither :py:class:`bytes` nor
:py:class:`unicode`.
"""
if not isinstance(s, (binary_type, text_type)):
raise TypeError("%r is neither bytes nor unicode" % s)
if PY3:
if isinstance(s, binary_type):
return s.decode("utf-8")
else:
if isinstance(s, text_type):
return s.encode("utf-8")
return s


class ICAPError(Exception):
"""Signals a protocol error"""
def __init__(self, code=500, message=None):
Expand Down Expand Up @@ -497,7 +519,8 @@ def handle_one_request(self):
self.log_error("Request timed out: %r", e)
self.close_connection = 1
except ICAPError as e:
self.send_error(e.code, e.message[0])
msg = e.message[0] if isinstance(e.message, tuple) else e.message
self.send_error(e.code, msg)
#except:
# self.send_error(500)

Expand Down Expand Up @@ -619,8 +642,8 @@ def date_time_bytes(self, timestamp=None):
timestamp = time.time()
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
self._weekdayname[wd],
day, self._monthname[month], year,
native(self._weekdayname[wd]),
day, native(self._monthname[month]), year,
hh, mm, ss)
return s.encode('utf-8')

Expand All @@ -629,7 +652,7 @@ def log_date_time_string(self):
now = time.time()
year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
s = "%02d/%3s/%04d %02d:%02d:%02d" % (
day, self._monthname[month], year, hh, mm, ss)
day, native(self._monthname[month]), year, hh, mm, ss)
return s

def address_string(self):
Expand Down

0 comments on commit 1e7b89b

Please sign in to comment.