home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import struct
- import time
- import cherrypy
-
- def decode(encoding = None, default_encoding = 'utf-8'):
- if not encoding:
- ct = cherrypy.request.headers.elements('Content-Type')
- if ct:
- ct = ct[0]
- encoding = ct.params.get('charset', None)
- if not encoding and ct.value.lower().startswith('text/'):
- encoding = 'ISO-8859-1'
-
-
- if not encoding:
- encoding = default_encoding
-
-
-
- try:
- decode_params(encoding)
- except UnicodeDecodeError:
- decode_params('ISO-8859-1')
-
-
-
- def decode_params(encoding):
- decoded_params = { }
- for key, value in cherrypy.request.params.items():
- if not hasattr(value, 'file'):
- if isinstance(value, list):
- value = [ v.decode(encoding) for v in value ]
- elif isinstance(value, str):
- value = value.decode(encoding)
-
-
- decoded_params[key] = value
-
- cherrypy.request.params = decoded_params
-
-
- def encode(encoding = None, errors = 'strict', text_only = True, add_charset = True):
- if getattr(cherrypy.request, '_encoding_attempted', False):
- return None
- cherrypy.request._encoding_attempted = True
- ct = cherrypy.response.headers.elements('Content-Type')
- if ct:
- ct = ct[0]
- if not text_only or ct.value.lower().startswith('text/'):
- ct.params['charset'] = find_acceptable_charset(encoding, errors = errors)
- if add_charset:
- cherrypy.response.headers['Content-Type'] = str(ct)
-
-
-
-
-
- def encode_stream(encoding, errors = 'strict'):
-
- def encoder(body):
- for chunk in body:
- if isinstance(chunk, unicode):
- chunk = chunk.encode(encoding, errors)
-
- yield chunk
-
-
- cherrypy.response.body = encoder(cherrypy.response.body)
- return True
-
-
- def encode_string(encoding, errors = 'strict'):
-
- try:
- body = []
- for chunk in cherrypy.response.body:
- if isinstance(chunk, unicode):
- chunk = chunk.encode(encoding, errors)
-
- body.append(chunk)
-
- cherrypy.response.body = body
- except (LookupError, UnicodeError):
- return False
-
- return True
-
-
- def find_acceptable_charset(encoding = None, default_encoding = 'utf-8', errors = 'strict'):
- response = cherrypy.response
- if cherrypy.response.stream:
- encoder = encode_stream
- else:
- response.collapse_body()
- encoder = encode_string
- if response.headers.has_key('Content-Length'):
- del response.headers['Content-Length']
-
- encs = cherrypy.request.headers.elements('Accept-Charset')
- charsets = [ enc.value.lower() for enc in encs ]
- attempted_charsets = []
- if encoding is not None:
- encoding = encoding.lower()
- if not charsets and '*' in charsets or encoding in charsets:
- if encoder(encoding, errors):
- return encoding
-
- elif not encs:
- if encoder(default_encoding, errors):
- return default_encoding
- raise cherrypy.HTTPError(500, failmsg % default_encoding)
- elif '*' not in charsets:
- iso = 'iso-8859-1'
- if iso not in charsets:
- attempted_charsets.append(iso)
- if encoder(iso, errors):
- return iso
-
-
- for element in encs:
- if element.qvalue > 0:
- pass
- None if element.value == '*' else encoding not in attempted_charsets
-
- ac = cherrypy.request.headers.get('Accept-Charset')
- if ac is None:
- msg = 'Your client did not send an Accept-Charset header.'
- else:
- msg = 'Your client sent this Accept-Charset header: %s.' % ac
- msg += ' We tried these charsets: %s.' % ', '.join(attempted_charsets)
- raise cherrypy.HTTPError(406, msg)
-
-
- def compress(body, compress_level):
- import zlib
- yield '\x1f\x8b'
- yield '\x08'
- yield '\x00'
- yield struct.pack('<L', long(time.time()))
- yield '\x02'
- yield '\xff'
- crc = zlib.crc32('')
- size = 0
- zobj = zlib.compressobj(compress_level, zlib.DEFLATED, -(zlib.MAX_WBITS), zlib.DEF_MEM_LEVEL, 0)
- for line in body:
- size += len(line)
- crc = zlib.crc32(line, crc)
- yield zobj.compress(line)
-
- yield zobj.flush()
- yield struct.pack('<l', crc)
- yield struct.pack('<L', size & 0xFFFFFFFFL)
-
-
- def decompress(body):
- import gzip
- import StringIO
- zbuf = StringIO.StringIO()
- zbuf.write(body)
- zbuf.seek(0)
- zfile = gzip.GzipFile(mode = 'rb', fileobj = zbuf)
- data = zfile.read()
- zfile.close()
- return data
-
-
- def gzip(compress_level = 9, mime_types = [
- 'text/html',
- 'text/plain']):
- response = cherrypy.response
- if not response.body:
- return None
- if getattr(cherrypy.request, 'cached', False):
- return None
- acceptable = cherrypy.request.headers.elements('Accept-Encoding')
- if not acceptable:
- return None
- ct = response.headers.get('Content-Type', '').split(';')[0]
- for coding in acceptable:
- if coding.value == 'identity' and coding.qvalue != 0:
- return None
- if coding.value in ('gzip', 'x-gzip'):
- if coding.qvalue == 0:
- return None
- return None
-
- cherrypy.HTTPError(406, 'identity, gzip').set_response()
-
-