home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import re
- from types import StringType
- from types import UnicodeType
- STRING_TYPES = (StringType, UnicodeType)
- from _util import http2time
- import _rfc3986
-
- def is_html(ct_headers, url, allow_xhtml = False):
- if not ct_headers:
- ext = os.path.splitext(_rfc3986.urlsplit(url)[2])[1]
- html_exts = [
- '.htm',
- '.html']
- if allow_xhtml:
- html_exts += [
- '.xhtml']
-
- return ext in html_exts
- ct = split_header_words(ct_headers)[0][0][0]
- html_types = [
- 'text/html']
- if allow_xhtml:
- html_types += [
- 'text/xhtml',
- 'text/xml',
- 'application/xml',
- 'application/xhtml+xml']
-
- return ct in html_types
-
-
- def unmatched(match):
- (start, end) = match.span(0)
- return match.string[:start] + match.string[end:]
-
- token_re = re.compile('^\\s*([^=\\s;,]+)')
- quoted_value_re = re.compile('^\\s*=\\s*\\"([^\\"\\\\]*(?:\\\\.[^\\"\\\\]*)*)\\"')
- value_re = re.compile('^\\s*=\\s*([^\\s;,]*)')
- escape_re = re.compile('\\\\(.)')
-
- def split_header_words(header_values):
- result = []
- for text in header_values:
- orig_text = text
- pairs = []
- while text:
- m = token_re.search(text)
- if m:
- text = unmatched(m)
- name = m.group(1)
- m = quoted_value_re.search(text)
- if m:
- text = unmatched(m)
- value = m.group(1)
- value = escape_re.sub('\\1', value)
- else:
- m = value_re.search(text)
- if m:
- text = unmatched(m)
- value = m.group(1)
- value = value.rstrip()
- else:
- value = None
- pairs.append((name, value))
- continue
- if text.lstrip().startswith(','):
- text = text.lstrip()[1:]
- if pairs:
- result.append(pairs)
-
- pairs = []
- continue
- (non_junk, nr_junk_chars) = re.subn('^[=\\s;]*', '', text)
- text = non_junk
- if pairs:
- result.append(pairs)
- continue
-
- return result
-
- join_escape_re = re.compile('([\\"\\\\])')
-
- def join_header_words(lists):
- headers = []
- for pairs in lists:
- attr = []
- for k, v in pairs:
- if v is not None:
- if not re.search('^\\w+$', v):
- v = join_escape_re.sub('\\\\\\1', v)
- v = '"%s"' % v
-
- if k is None:
- k = v
- else:
- k = '%s=%s' % (k, v)
-
- attr.append(k)
-
- if attr:
- headers.append('; '.join(attr))
- continue
-
- return ', '.join(headers)
-
-
- def strip_quotes(text):
- if text.startswith('"'):
- text = text[1:]
-
- if text.endswith('"'):
- text = text[:-1]
-
- return text
-
-
- def parse_ns_headers(ns_headers):
- known_attrs = ('expires', 'domain', 'path', 'secure', 'version', 'port', 'max-age')
- result = []
- for ns_header in ns_headers:
- pairs = []
- version_set = False
- params = re.split(';\\s*', ns_header)
- for ii in range(len(params)):
- param = params[ii]
- param = param.rstrip()
- if param == '':
- continue
-
- if '=' not in param:
- k = param
- v = None
- else:
- (k, v) = re.split('\\s*=\\s*', param, 1)
- k = k.lstrip()
- if ii != 0:
- lc = k.lower()
- if lc in known_attrs:
- k = lc
-
- if k == 'version':
- v = strip_quotes(v)
- version_set = True
-
- if k == 'expires':
- v = http2time(strip_quotes(v))
-
-
- pairs.append((k, v))
-
- if pairs:
- if not version_set:
- pairs.append(('version', '0'))
-
- result.append(pairs)
- continue
-
- return result
-
-
- def _test():
- import doctest
- import _headersutil
- return doctest.testmod(_headersutil)
-
- if __name__ == '__main__':
- _test()
-
-