Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #28 #36

Merged
merged 1 commit into from
Aug 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -604,8 +627,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 @@ -614,7 +637,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