Example #1
0
def parse_encapsulated_field(raw_field):
    """Parse an Encapsulated header, `raw_field`, and return it as an instance
    of `collections.OrderedDict`, according to RFC3507.

    Will raise :exc:`InvalidEncapsulatedHeadersError` if `raw_field` is not a
    valid ICAP Encapsulated request header, according to RFC3507 section 4.4.1.

    >>> from icap.utils import parse_encapsulated_field
    >>> parse_encapsulated_field('req-hdr=0, req-body=749')
    OrderedDict([('req-hdr', 0), ('req-body', 749)])

    :return: `collections.OrderedDict` containing the parsed encapsulated
             sections.
    """

    parsed = parse_dict_header(raw_field, cls=OrderedDict)

    keys = ' '.join(parsed)

    for regex in list(encapsulated_input_orders.values()):
        if regex.match(keys):
            return OrderedDict(
                (key, int(value)) for (key, value) in parsed.items())
    else:
        raise InvalidEncapsulatedHeadersError(raw_field)
Example #2
0
def parse_encapsulated_field(raw_field):
    """Parse an Encapsulated header, `raw_field`, and return it as an instance
    of `collections.OrderedDict`, according to RFC3507.

    Will raise :exc:`InvalidEncapsulatedHeadersError` if `raw_field` is not a
    valid ICAP Encapsulated request header, according to RFC3507 section 4.4.1.

    >>> from icap.utils import parse_encapsulated_field
    >>> parse_encapsulated_field('req-hdr=0, req-body=749')
    OrderedDict([('req-hdr', 0), ('req-body', 749)])

    :return: `collections.OrderedDict` containing the parsed encapsulated
             sections.
    """

    parsed = parse_dict_header(raw_field, cls=OrderedDict)

    keys = ' '.join(parsed)

    for regex in list(encapsulated_input_orders.values()):
        if regex.match(keys):
            return OrderedDict((key, int(value)) for (key, value)
                               in parsed.items())
    else:
        raise InvalidEncapsulatedHeadersError(raw_field)
Example #3
0
    def add_entry(self, uri, hsts_header):
        parsed = parse_dict_header(hsts_header)
        max_age, *rest = parsed['max-age'].split(';', 1)

        include_subdomains = False
        if rest:
            include_subdomains = 'includesubdomains' in rest[0].lower()

        domain = urlparse.urlparse(uri).netloc
        if include_subdomains:
            domain = '.' + domain
        max_age = int(max_age)

        expiry = datetime.datetime.now() + datetime.timedelta(seconds=max_age)

        with self.get_hsts_db() as conn:
            cursor = conn.cursor()
            cursor.execute('insert or replace into hsts (domain, expiry) '
                           'values (?, ?)', (domain, expiry))
            conn.commit()