cubicweb/web/http_headers.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Tue, 31 Mar 2020 19:15:03 +0200
changeset 12957 0c973204033a
parent 12829 0e9ce897dfdf
permissions -rw-r--r--
[server] prevent returning closed cursor to the database pool In since c8c6ad8 init_repository use repo.internal_cnx() instead of repo.system_source.get_connection() so it use the pool and we should not close cursors from the pool before returning it back. Otherwise we may have "connection already closed" error. This bug only trigger when connection-pool-size = 1. Since we are moving to use a dynamic pooler we need to get this fixed. This does not occur with sqlite since the connection wrapper instantiate new cursor everytime, but this occur with other databases.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     1
# This file has been extracted from the abandoned TwistedWeb2 project
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     2
# http://twistedmatrix.com/trac/wiki/TwistedWeb2
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     3
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     4
10594
6a55853f14c9 Unused imports
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10593
diff changeset
     5
import time
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     6
from calendar import timegm
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     7
import base64
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     8
import re
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11883
diff changeset
     9
from urllib.parse import urlparse
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
    10
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    11
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    12
def dashCapitalize(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    13
    ''' Capitalize a string, making sure to treat - as a word seperator '''
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    14
    return '-'.join([ x.capitalize() for x in s.split('-')])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    15
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    16
# datetime parsing and formatting
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    17
weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    18
weekdayname_lower = [name.lower() for name in weekdayname]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    19
monthname = [None,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    20
             'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    21
             'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    22
monthname_lower = [name and name.lower() for name in monthname]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    23
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    24
# HTTP Header parsing API
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    25
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    26
header_case_mapping = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    27
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    28
def casemappingify(d):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    29
    global header_case_mapping
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    30
    newd = dict([(key.lower(), key) for key in d])
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    31
    header_case_mapping.update(newd)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    32
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    33
def lowerify(d):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    34
    return dict([(key.lower(), value) for key, value in d.items()])
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    35
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    36
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    37
class HeaderHandler(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    38
    """HeaderHandler manages header generating and parsing functions.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    39
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    40
    HTTPParsers = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    41
    HTTPGenerators = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    42
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    43
    def __init__(self, parsers=None, generators=None):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    44
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    45
        @param parsers: A map of header names to parsing functions.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    46
        @type parsers: L{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    47
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    48
        @param generators: A map of header names to generating functions.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    49
        @type generators: L{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    50
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    51
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    52
        if parsers:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    53
            self.HTTPParsers.update(parsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    54
        if generators:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    55
            self.HTTPGenerators.update(generators)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    56
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    57
    def parse(self, name, header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    58
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    59
        Parse the given header based on its given name.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    60
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    61
        @param name: The header name to parse.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    62
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    63
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    64
        @param header: A list of unparsed headers.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    65
        @type header: C{list} of C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    66
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    67
        @return: The return value is the parsed header representation,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    68
            it is dependent on the header.  See the HTTP Headers document.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    69
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    70
        parser = self.HTTPParsers.get(name, None)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    71
        if parser is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    72
            raise ValueError("No header parser for header '%s', either add one or use getHeaderRaw." % (name,))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    73
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    74
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    75
            for p in parser:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    76
                #print "==> Parsing %s: %s(%s)" % (name, repr(p), repr(header))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    77
                header = p(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    78
                # if isinstance(h, types.GeneratorType):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    79
                #     h = list(h)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8313
diff changeset
    80
        except ValueError as v:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    81
            # print v
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    82
            header = None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    83
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    84
        return header
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    85
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    86
    def generate(self, name, header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    87
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    88
        Generate the given header based on its given name.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    89
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    90
        @param name: The header name to generate.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    91
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    92
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    93
        @param header: A parsed header, such as the output of
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    94
            L{HeaderHandler}.parse.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    95
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    96
        @return: C{list} of C{str} each representing a generated HTTP header.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    97
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    98
        generator = self.HTTPGenerators.get(name, None)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    99
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   100
        if generator is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   101
            # print self.generators
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   102
            raise ValueError("No header generator for header '%s', either add one or use setHeaderRaw." % (name,))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   103
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   104
        for g in generator:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   105
            header = g(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   106
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   107
        #self._raw_headers[name] = h
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   108
        return header
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   109
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   110
    def updateParsers(self, parsers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   111
        """Update en masse the parser maps.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   112
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   113
        @param parsers: Map of header names to parser chains.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   114
        @type parsers: C{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   115
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   116
        casemappingify(parsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   117
        self.HTTPParsers.update(lowerify(parsers))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   118
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   119
    def addParser(self, name, value):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   120
        """Add an individual parser chain for the given header.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   121
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   122
        @param name: Name of the header to add
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   123
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   124
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   125
        @param value: The parser chain
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   126
        @type value: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   127
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   128
        self.updateParsers({name: value})
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   129
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   130
    def updateGenerators(self, generators):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   131
        """Update en masse the generator maps.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   132
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   133
        @param parsers: Map of header names to generator chains.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   134
        @type parsers: C{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   135
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   136
        casemappingify(generators)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   137
        self.HTTPGenerators.update(lowerify(generators))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   138
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   139
    def addGenerators(self, name, value):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   140
        """Add an individual generator chain for the given header.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   141
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   142
        @param name: Name of the header to add
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   143
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   144
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   145
        @param value: The generator chain
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   146
        @type value: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   147
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   148
        self.updateGenerators({name: value})
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   149
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   150
    def update(self, parsers, generators):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   151
        """Conveniently update parsers and generators all at once.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   152
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   153
        self.updateParsers(parsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   154
        self.updateGenerators(generators)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   156
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   157
DefaultHTTPHandler = HeaderHandler()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   158
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   159
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   160
## HTTP DateTime parser
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   161
def parseDateTime(dateString):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   162
    """Convert an HTTP date string (one of three formats) to seconds since epoch."""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   163
    parts = dateString.split()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   164
11883
0cb36982db94 [web/http_headers] do not crash when IfModifiedSince is empty string (closes #16527954)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11057
diff changeset
   165
    if parts and not parts[0][0:3].lower() in weekdayname_lower:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   166
        # Weekday is stupid. Might have been omitted.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   167
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   168
            return parseDateTime("Sun, "+dateString)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   169
        except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   170
            # Guess not.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   171
            pass
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   172
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   173
    partlen = len(parts)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   174
    if (partlen == 5 or partlen == 6) and parts[1].isdigit():
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   175
        # 1st date format: Sun, 06 Nov 1994 08:49:37 GMT
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   176
        # (Note: "GMT" is literal, not a variable timezone)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   177
        # (also handles without "GMT")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   178
        # This is the normal format
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   179
        day = parts[1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   180
        month = parts[2]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   181
        year = parts[3]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   182
        time = parts[4]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   183
    elif (partlen == 3 or partlen == 4) and parts[1].find('-') != -1:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   184
        # 2nd date format: Sunday, 06-Nov-94 08:49:37 GMT
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   185
        # (Note: "GMT" is literal, not a variable timezone)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   186
        # (also handles without without "GMT")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   187
        # Two digit year, yucko.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   188
        day, month, year = parts[1].split('-')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   189
        time = parts[2]
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   190
        year = int(year)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   191
        if year < 69:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   192
            year = year + 2000
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   193
        elif year < 100:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   194
            year = year + 1900
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   195
    elif len(parts) == 5:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   196
        # 3rd date format: Sun Nov  6 08:49:37 1994
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   197
        # ANSI C asctime() format.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   198
        day = parts[2]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   199
        month = parts[1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   200
        year = parts[4]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   201
        time = parts[3]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   202
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   203
        raise ValueError("Unknown datetime format %r" % dateString)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   204
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   205
    day = int(day)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   206
    month = int(monthname_lower.index(month.lower()))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   207
    year = int(year)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   208
    hour, min, sec = map(int, time.split(':'))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   209
    return int(timegm((year, month, day, hour, min, sec)))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   210
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   211
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   212
##### HTTP tokenizer
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   213
class Token(str):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   214
    __slots__=[]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   215
    tokens = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   216
    def __new__(self, char):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   217
        token = Token.tokens.get(char)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   218
        if token is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   219
            Token.tokens[char] = token = str.__new__(self, char)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   220
        return token
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   221
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   222
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   223
        return "Token(%s)" % str.__repr__(self)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   224
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   225
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   226
http_tokens = " \t\"()<>@,;:\\/[]?={}"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   227
http_ctls = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   228
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   229
def tokenize(header, foldCase=True):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   230
    """Tokenize a string according to normal HTTP header parsing rules.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   231
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   232
    In particular:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   233
     - Whitespace is irrelevant and eaten next to special separator tokens.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   234
       Its existance (but not amount) is important between character strings.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   235
     - Quoted string support including embedded backslashes.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   236
     - Case is insignificant (and thus lowercased), except in quoted strings.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   237
        (unless foldCase=False)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   238
     - Multiple headers are concatenated with ','
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   239
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   240
    NOTE: not all headers can be parsed with this function.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   241
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   242
    Takes a raw header value (list of strings), and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   243
    Returns a generator of strings and Token class instances.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   244
    """
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   245
    tokens = http_tokens
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   246
    ctls = http_ctls
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   247
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   248
    string = ",".join(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   249
    list = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   250
    start = 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   251
    cur = 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   252
    quoted = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   253
    qpair = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   254
    inSpaces = -1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   255
    qstring = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   256
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   257
    for x in string:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   258
        if quoted:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   259
            if qpair:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   260
                qpair = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   261
                qstring = qstring+string[start:cur-1]+x
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   262
                start = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   263
            elif x == '\\':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   264
                qpair = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   265
            elif x == '"':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   266
                quoted = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   267
                yield qstring+string[start:cur]
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   268
                qstring = None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   269
                start = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   270
        elif x in tokens:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   271
            if start != cur:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   272
                if foldCase:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   273
                    yield string[start:cur].lower()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   274
                else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   275
                    yield string[start:cur]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   276
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   277
            start = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   278
            if x == '"':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   279
                quoted = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   280
                qstring = ""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   281
                inSpaces = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   282
            elif x in " \t":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   283
                if inSpaces is False:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   284
                    inSpaces = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   285
            else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   286
                inSpaces = -1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   287
                yield Token(x)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   288
        elif x in ctls:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   289
            raise ValueError("Invalid control character: %d in header" % ord(x))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   290
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   291
            if inSpaces is True:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   292
                yield Token(' ')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   293
                inSpaces = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   294
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   295
            inSpaces = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   296
        cur = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   297
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   298
    if qpair:
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10067
diff changeset
   299
        raise ValueError("Missing character after '\\'")
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   300
    if quoted:
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10067
diff changeset
   301
        raise ValueError("Missing end quote")
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   302
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   303
    if start != cur:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   304
        if foldCase:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   305
            yield string[start:cur].lower()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   306
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   307
            yield string[start:cur]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   308
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   309
def split(seq, delim):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   310
    """The same as str.split but works on arbitrary sequences.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   311
    Too bad it's not builtin to python!"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   312
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   313
    cur = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   314
    for item in seq:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   315
        if item == delim:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   316
            yield cur
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   317
            cur = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   318
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   319
            cur.append(item)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   320
    yield cur
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   321
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   322
# def find(seq, *args):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   323
#     """The same as seq.index but returns -1 if not found, instead
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   324
#     Too bad it's not builtin to python!"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   325
#     try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   326
#         return seq.index(value, *args)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   327
#     except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   328
#         return -1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   329
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   330
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   331
def filterTokens(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   332
    """Filter out instances of Token, leaving only a list of strings.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   333
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   334
    Used instead of a more specific parsing method (e.g. splitting on commas)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   335
    when only strings are expected, so as to be a little lenient.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   336
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   337
    Apache does it this way and has some comments about broken clients which
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   338
    forget commas (?), so I'm doing it the same way. It shouldn't
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   339
    hurt anything, in any case.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   340
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   341
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   342
    l = []
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   343
    for x in seq:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   344
        if not isinstance(x, Token):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   345
            l.append(x)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   346
    return l
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   347
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   348
##### parser utilities:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   349
def checkSingleToken(tokens):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   350
    if len(tokens) != 1:
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10067
diff changeset
   351
        raise ValueError("Expected single token, not %s." % (tokens,))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   352
    return tokens[0]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   353
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   354
def parseKeyValue(val):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   355
    if len(val) == 1:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   356
        return val[0], None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   357
    elif len(val) == 3 and val[1] == Token('='):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   358
        return val[0], val[2]
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10067
diff changeset
   359
    raise ValueError("Expected key or key=value, but got %s." % (val,))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   360
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   361
def parseArgs(field):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   362
    args = split(field, Token(';'))
10669
155c29e0ed1c [py3k] use next builtin instead of next method
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   363
    val = next(args)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   364
    args = [parseKeyValue(arg) for arg in args]
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   365
    return val, args
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   366
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   367
def listParser(fun):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   368
    """Return a function which applies 'fun' to every element in the
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   369
    comma-separated list"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   370
    def listParserHelper(tokens):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   371
        fields = split(tokens, Token(','))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   372
        for field in fields:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   373
            if len(field) != 0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   374
                yield fun(field)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   375
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   376
    return listParserHelper
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   377
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   378
def last(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   379
    """Return seq[-1]"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   380
    return seq[-1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   381
9569
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   382
def unique(seq):
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   383
    '''if seq is not a string, check it's a sequence of one element and return it'''
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11883
diff changeset
   384
    if isinstance(seq, str):
9569
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   385
        return seq
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   386
    if len(seq) != 1:
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   387
        raise ValueError('single value required, not %s' % seq)
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   388
    return seq[0]
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   389
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   390
def parseHTTPMethod(method):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   391
    """Ensure a HTTP method is valid according the rfc2616, but extension-method ones"""
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   392
    method = method.strip()
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   393
    if method not in ("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE",
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   394
                      "TRACE", "CONNECT"):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   395
        raise ValueError('Unsupported HTTP method %s' % method)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   396
    return method
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   397
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   398
def parseAllowOrigin(origin):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   399
    """Ensure origin is a valid URL-base stuff, or null"""
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   400
    if origin == 'null':
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   401
        return origin
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
   402
    p = urlparse(origin)
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   403
    if p.params or p.query or p.username or p.path not in ('', '/'):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   404
        raise ValueError('Incorrect Accept-Control-Allow-Origin value %s' % origin)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   405
    if p.scheme not in ('http', 'https'):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   406
        raise ValueError('Unsupported Accept-Control-Allow-Origin URL scheme %s' % origin)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   407
    if not p.netloc:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   408
        raise ValueError('Accept-Control-Allow-Origin: host name cannot be unset  (%s)' % origin)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   409
    return origin
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   410
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   411
def parseAllowCreds(cred):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   412
    """Can be "true" """
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   413
    if cred:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   414
        cred = cred.lower()
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   415
    if cred and cred != 'true':
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   416
        raise ValueError('Accept-Control-Allow-Credentials can only be "true" (%s)' % cred)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   417
    return cred
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   418
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   419
##### Generation utilities
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   420
def quoteString(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   421
    return '"%s"' % s.replace('\\', '\\\\').replace('"', '\\"')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   422
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   423
def listGenerator(fun):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   424
    """Return a function which applies 'fun' to every element in
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   425
    the given list, then joins the result with generateList"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   426
    def listGeneratorHelper(l):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   427
        return generateList([fun(e) for e in l])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   428
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   429
    return listGeneratorHelper
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   430
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   431
def generateList(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   432
    return ", ".join(seq)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   433
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   434
def singleHeader(item):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   435
    return [item]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   436
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   437
def generateKeyValues(kvs):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   438
    l = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   439
    # print kvs
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   440
    for k, v in kvs:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   441
        if v is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   442
            l.append('%s' % k)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   443
        else:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   444
            l.append('%s=%s' % (k, v))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   445
    return ";".join(l)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   446
9989
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   447
def generateTrueFalse(value):
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   448
    """
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   449
    Return 'true' or 'false' depending on the value.
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   450
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   451
    *   'true' values are `True`, `1`, `"true"`
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   452
    *   'false' values are `False`, `0`, `"false"`
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   453
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   454
    """
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   455
    if (value in (True, 1) or
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11883
diff changeset
   456
            isinstance(value, str) and value.lower() == 'true'):
9989
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   457
        return 'true'
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   458
    if (value in (False, 0) or
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11883
diff changeset
   459
            isinstance(value, str) and value.lower() == 'false'):
9989
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   460
        return 'false'
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   461
    raise ValueError("Invalid true/false header value: %s" % value)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   462
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   463
class MimeType(object):
10798
3f24adbf971a [web/http_headers] use classmethod decorator
Julien Cristau <julien.cristau@logilab.fr>
parents: 10778
diff changeset
   464
    @classmethod
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   465
    def fromString(klass, mimeTypeString):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   466
        """Generate a MimeType object from the given string.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   467
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   468
        @param mimeTypeString: The mimetype to parse
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   469
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   470
        @return: L{MimeType}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   471
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   472
        return DefaultHTTPHandler.parse('content-type', [mimeTypeString])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   473
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   474
    def __init__(self, mediaType, mediaSubtype, params={}, **kwargs):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   475
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   476
        @type mediaType: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   477
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   478
        @type mediaSubtype: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   479
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   480
        @type params: C{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   481
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   482
        self.mediaType = mediaType
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   483
        self.mediaSubtype = mediaSubtype
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   484
        self.params = dict(params)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   485
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   486
        if kwargs:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   487
            self.params.update(kwargs)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   488
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   489
    def __eq__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   490
        if not isinstance(other, MimeType): return NotImplemented
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   491
        return (self.mediaType == other.mediaType and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   492
                self.mediaSubtype == other.mediaSubtype and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   493
                self.params == other.params)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   494
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   495
    def __ne__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   496
        return not self.__eq__(other)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   497
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   498
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   499
        return "MimeType(%r, %r, %r)" % (self.mediaType, self.mediaSubtype, self.params)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   500
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   501
    def __hash__(self):
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   502
        return hash(self.mediaType)^hash(self.mediaSubtype)^hash(tuple(self.params.items()))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   503
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   504
##### Specific header parsers.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   505
def parseAccept(field):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   506
    type, args = parseArgs(field)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   507
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   508
    if len(type) != 3 or type[1] != Token('/'):
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10067
diff changeset
   509
        raise ValueError("MIME Type "+str(type)+" invalid.")
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   510
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   511
    # okay, this spec is screwy. A 'q' parameter is used as the separator
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   512
    # between MIME parameters and (as yet undefined) additional HTTP
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   513
    # parameters.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   514
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   515
    num = 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   516
    for arg in args:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   517
        if arg[0] == 'q':
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   518
            mimeparams = tuple(args[0:num])
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   519
            params = args[num:]
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   520
            break
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   521
        num = num + 1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   522
    else:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   523
        mimeparams = tuple(args)
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   524
        params = []
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   525
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   526
    # Default values for parameters:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   527
    qval = 1.0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   528
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   529
    # Parse accept parameters:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   530
    for param in params:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   531
        if param[0] == 'q':
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   532
            qval = float(param[1])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   533
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   534
            # Warn? ignored parameter.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   535
            pass
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   536
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   537
    ret = MimeType(type[0], type[2], mimeparams), qval
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   538
    return ret
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   539
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   540
def parseAcceptQvalue(field):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   541
    type, args = parseArgs(field)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   542
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   543
    type = checkSingleToken(type)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   544
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   545
    qvalue = 1.0 # Default qvalue is 1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   546
    for arg in args:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   547
        if arg[0] == 'q':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   548
            qvalue = float(arg[1])
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   549
    return type, qvalue
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   550
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   551
def addDefaultCharset(charsets):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   552
    if charsets.get('*') is None and charsets.get('iso-8859-1') is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   553
        charsets['iso-8859-1'] = 1.0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   554
    return charsets
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   555
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   556
def addDefaultEncoding(encodings):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   557
    if encodings.get('*') is None and encodings.get('identity') is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   558
        # RFC doesn't specify a default value for identity, only that it
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   559
        # "is acceptable" if not mentioned. Thus, give it a very low qvalue.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   560
        encodings['identity'] = .0001
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   561
    return encodings
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   562
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   563
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   564
def parseContentType(header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   565
    # Case folding is disabled for this header, because of use of
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   566
    # Content-Type: multipart/form-data; boundary=CaSeFuLsTuFf
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   567
    # So, we need to explicitly .lower() the type/subtype and arg keys.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   568
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   569
    type, args = parseArgs(header)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   570
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   571
    if len(type) != 3 or type[1] != Token('/'):
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10067
diff changeset
   572
        raise ValueError("MIME Type "+str(type)+" invalid.")
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   573
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   574
    args = [(kv[0].lower(), kv[1]) for kv in args]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   575
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   576
    return MimeType(type[0].lower(), type[2].lower(), tuple(args))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   577
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   578
def parseContentMD5(header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   579
    try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   580
        return base64.decodestring(header)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8313
diff changeset
   581
    except Exception as e:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   582
        raise ValueError(e)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   583
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   584
def parseContentRange(header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   585
    """Parse a content-range header into (kind, start, end, realLength).
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   586
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   587
    realLength might be None if real length is not known ('*').
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   588
    start and end might be None if start, end unspecified (for response code 416)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   589
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   590
    kind, other = header.strip().split()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   591
    if kind.lower() != "bytes":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   592
        raise ValueError("a range of type %r is not supported")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   593
    startend, realLength = other.split("/")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   594
    if startend.strip() == '*':
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   595
        start, end = None, None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   596
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   597
        start, end = map(int, startend.split("-"))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   598
    if realLength == "*":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   599
        realLength = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   600
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   601
        realLength = int(realLength)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   602
    return (kind, start, end, realLength)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   603
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   604
def parseExpect(field):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   605
    type, args = parseArgs(field)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   606
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   607
    type = parseKeyValue(type)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   608
    return (type[0], (lambda *args:args)(type[1], *args))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   609
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   610
def parseExpires(header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   611
    # """HTTP/1.1 clients and caches MUST treat other invalid date formats,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   612
    #    especially including the value 0, as in the past (i.e., "already expired")."""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   613
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   614
    try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   615
        return parseDateTime(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   616
    except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   617
        return 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   618
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   619
def parseIfModifiedSince(header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   620
    # Ancient versions of netscape and *current* versions of MSIE send
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   621
    #   If-Modified-Since: Thu, 05 Aug 2004 12:57:27 GMT; length=123
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   622
    # which is blantantly RFC-violating and not documented anywhere
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   623
    # except bug-trackers for web frameworks.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   624
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   625
    # So, we'll just strip off everything after a ';'.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   626
    return parseDateTime(header.split(';', 1)[0])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   627
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   628
def parseIfRange(headers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   629
    try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   630
        return ETag.parse(tokenize(headers))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   631
    except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   632
        return parseDateTime(last(headers))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   633
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   634
def parseRange(range):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   635
    range = list(range)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   636
    if len(range) < 3 or range[1] != Token('='):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   637
        raise ValueError("Invalid range header format: %s" %(range,))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   638
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   639
    type = range[0]
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   640
    if type != 'bytes':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   641
        raise ValueError("Unknown range unit: %s." % (type,))
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   642
    rangeset = split(range[2:], Token(','))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   643
    ranges = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   644
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   645
    for byterangespec in rangeset:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   646
        if len(byterangespec) != 1:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   647
            raise ValueError("Invalid range header format: %s" % (range,))
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   648
        start, end = byterangespec[0].split('-')
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   649
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   650
        if not start and not end:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   651
            raise ValueError("Invalid range header format: %s" % (range,))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   652
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   653
        if start:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   654
            start = int(start)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   655
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   656
            start = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   657
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   658
        if end:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   659
            end = int(end)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   660
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   661
            end = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   662
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   663
        if start and end and start > end:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   664
            raise ValueError("Invalid range header, start > end: %s" % (range,))
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   665
        ranges.append((start, end))
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   666
    return type, ranges
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   667
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   668
def parseRetryAfter(header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   669
    try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   670
        # delta seconds
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   671
        return time.time() + int(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   672
    except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   673
        # or datetime
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   674
        return parseDateTime(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   675
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   676
# WWW-Authenticate and Authorization
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   677
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   678
def parseWWWAuthenticate(tokenized):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   679
    headers = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   680
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   681
    tokenList = list(tokenized)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   682
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   683
    while tokenList:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   684
        scheme = tokenList.pop(0)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   685
        challenge = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   686
        last = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   687
        kvChallenge = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   688
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   689
        while tokenList:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   690
            token = tokenList.pop(0)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   691
            if token == Token('='):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   692
                kvChallenge = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   693
                challenge[last] = tokenList.pop(0)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   694
                last = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   695
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   696
            elif token == Token(','):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   697
                if kvChallenge:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   698
                    if len(tokenList) > 1 and tokenList[1] != Token('='):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   699
                        break
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   700
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   701
                else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   702
                    break
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   703
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   704
            else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   705
                last = token
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   706
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   707
        if last and scheme and not challenge and not kvChallenge:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   708
            challenge = last
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   709
            last = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   710
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   711
        headers.append((scheme, challenge))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   712
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   713
    if last and last not in (Token('='), Token(',')):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   714
        if headers[-1] == (scheme, challenge):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   715
            scheme = last
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   716
            challenge = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   717
            headers.append((scheme, challenge))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   718
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   719
    return headers
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   720
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   721
def parseAuthorization(header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   722
    scheme, rest = header.split(' ', 1)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   723
    # this header isn't tokenized because it may eat characters
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   724
    # in the unquoted base64 encoded credentials
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   725
    return scheme.lower(), rest
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   726
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   727
#### Header generators
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   728
def generateAccept(accept):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   729
    mimeType, q = accept
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   730
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   731
    out ="%s/%s"%(mimeType.mediaType, mimeType.mediaSubtype)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   732
    if mimeType.params:
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   733
        out+=';'+generateKeyValues(mimeType.params.items())
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   734
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   735
    if q != 1.0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   736
        out+=(';q=%.3f' % (q,)).rstrip('0').rstrip('.')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   737
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   738
    return out
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   739
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   740
def removeDefaultEncoding(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   741
    for item in seq:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   742
        if item[0] != 'identity' or item[1] != .0001:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   743
            yield item
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   744
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   745
def generateAcceptQvalue(keyvalue):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   746
    if keyvalue[1] == 1.0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   747
        return "%s" % keyvalue[0:1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   748
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   749
        return ("%s;q=%.3f" % keyvalue).rstrip('0').rstrip('.')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   750
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   751
def parseCacheControl(kv):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   752
    k, v = parseKeyValue(kv)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   753
    if k == 'max-age' or k == 'min-fresh' or k == 's-maxage':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   754
        # Required integer argument
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   755
        if v is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   756
            v = 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   757
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   758
            v = int(v)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   759
    elif k == 'max-stale':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   760
        # Optional integer argument
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   761
        if v is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   762
            v = int(v)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   763
    elif k == 'private' or k == 'no-cache':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   764
        # Optional list argument
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   765
        if v is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   766
            v = [field.strip().lower() for field in v.split(',')]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   767
    return k, v
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   768
10593
03ce3d24db87 [py3k] tuple unpacking in function args
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10590
diff changeset
   769
def generateCacheControl(args):
03ce3d24db87 [py3k] tuple unpacking in function args
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10590
diff changeset
   770
    k, v = args
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   771
    if v is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   772
        return str(k)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   773
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   774
        if k == 'no-cache' or k == 'private':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   775
            # quoted list of values
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   776
            v = quoteString(generateList(
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   777
                [header_case_mapping.get(name) or dashCapitalize(name) for name in v]))
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   778
        return '%s=%s' % (k, v)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   779
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   780
def generateContentRange(tup):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   781
    """tup is (type, start, end, len)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   782
    len can be None.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   783
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   784
    type, start, end, len = tup
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   785
    if len == None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   786
        len = '*'
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   787
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   788
        len = int(len)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   789
    if start == None and end == None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   790
        startend = '*'
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   791
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   792
        startend = '%d-%d' % (start, end)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   793
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   794
    return '%s %s/%s' % (type, startend, len)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   795
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   796
def generateDateTime(secSinceEpoch):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   797
    """Convert seconds since epoch to HTTP datetime string."""
8081
e8091b415ad8 [http, windows] fix remove_cookie trying to expire a cookie before the Epoch (closes: #2087998)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7855
diff changeset
   798
    # take care gmtime doesn't handle time before epoch (crash on windows at least)
e8091b415ad8 [http, windows] fix remove_cookie trying to expire a cookie before the Epoch (closes: #2087998)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7855
diff changeset
   799
    year, month, day, hh, mm, ss, wd, y, z = time.gmtime(max(0, secSinceEpoch))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   800
    s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   801
        weekdayname[wd],
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   802
        day, monthname[month], year,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   803
        hh, mm, ss)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   804
    return s
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   805
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   806
def generateExpect(item):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   807
    if item[1][0] is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   808
        out = '%s' % (item[0],)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   809
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   810
        out = '%s=%s' % (item[0], item[1][0])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   811
    if len(item[1]) > 1:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   812
        out += ';'+generateKeyValues(item[1][1:])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   813
    return out
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   814
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   815
def generateRange(range):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   816
    def noneOr(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   817
        if s is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   818
            return ''
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   819
        return s
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   820
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   821
    type, ranges = range
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   822
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   823
    if type != 'bytes':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   824
        raise ValueError("Unknown range unit: "+type+".")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   825
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   826
    return (type+'='+
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   827
            ','.join(['%s-%s' % (noneOr(startend[0]), noneOr(startend[1]))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   828
                      for startend in ranges]))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   829
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   830
def generateRetryAfter(when):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   831
    # always generate delta seconds format
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   832
    return str(int(when - time.time()))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   833
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   834
def generateContentType(mimeType):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   835
    out = "%s/%s" % (mimeType.mediaType, mimeType.mediaSubtype)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   836
    if mimeType.params:
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   837
        out += ';' + generateKeyValues(mimeType.params.items())
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   838
    return out
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   839
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   840
def generateIfRange(dateOrETag):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   841
    if isinstance(dateOrETag, ETag):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   842
        return dateOrETag.generate()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   843
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   844
        return generateDateTime(dateOrETag)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   845
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   846
# WWW-Authenticate and Authorization
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   847
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   848
def generateWWWAuthenticate(headers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   849
    _generated = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   850
    for seq in headers:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   851
        scheme, challenge = seq[0], seq[1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   852
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   853
        # If we're going to parse out to something other than a dict
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   854
        # we need to be able to generate from something other than a dict
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   855
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   856
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   857
            l = []
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   858
            for k, v in dict(challenge).items():
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   859
                l.append("%s=%s" % (k, quoteString(v)))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   860
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   861
            _generated.append("%s %s" % (scheme, ", ".join(l)))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   862
        except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   863
            _generated.append("%s %s" % (scheme, challenge))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   864
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   865
    return _generated
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   866
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   867
def generateAuthorization(seq):
10778
04d248305810 [web] accept unicode in parsed Authorization header
Julien Cristau <julien.cristau@logilab.fr>
parents: 10669
diff changeset
   868
    return [' '.join(str(v) for v in seq)]
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   869
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   870
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   871
####
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   872
class ETag(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   873
    def __init__(self, tag, weak=False):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   874
        self.tag = str(tag)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   875
        self.weak = weak
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   876
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   877
    def match(self, other, strongCompare):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   878
        # Sec 13.3.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   879
        # The strong comparison function: in order to be considered equal, both
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   880
        #   validators MUST be identical in every way, and both MUST NOT be weak.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   881
        #
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   882
        # The weak comparison function: in order to be considered equal, both
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   883
        #   validators MUST be identical in every way, but either or both of
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   884
        #   them MAY be tagged as "weak" without affecting the result.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   885
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   886
        if not isinstance(other, ETag) or other.tag != self.tag:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   887
            return False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   888
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   889
        if strongCompare and (other.weak or self.weak):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   890
            return False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   891
        return True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   892
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   893
    def __eq__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   894
        return isinstance(other, ETag) and other.tag == self.tag and other.weak == self.weak
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   895
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   896
    def __ne__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   897
        return not self.__eq__(other)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   898
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   899
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   900
        return "Etag(%r, weak=%r)" % (self.tag, self.weak)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   901
12829
0e9ce897dfdf [mypy/fix] Incompatible types in assignment (expression has type "staticmethod", variable has type "Callable[[ETag], Any]")
Laurent Peuch <cortex@worlddomination.be>
parents: 12567
diff changeset
   902
    @staticmethod
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   903
    def parse(tokens):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   904
        tokens = tuple(tokens)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   905
        if len(tokens) == 1 and not isinstance(tokens[0], Token):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   906
            return ETag(tokens[0])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   907
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   908
        if(len(tokens) == 3 and tokens[0] == "w"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   909
           and tokens[1] == Token('/')):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   910
            return ETag(tokens[2], weak=True)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   911
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   912
        raise ValueError("Invalid ETag.")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   913
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   914
    def generate(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   915
        if self.weak:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   916
            return 'W/'+quoteString(self.tag)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   917
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   918
            return quoteString(self.tag)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   919
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   920
def parseStarOrETag(tokens):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   921
    tokens = tuple(tokens)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   922
    if tokens == ('*',):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   923
        return '*'
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   924
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   925
        return ETag.parse(tokens)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   926
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   927
def generateStarOrETag(etag):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   928
    if etag == '*':
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   929
        return etag
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   930
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   931
        return etag.generate()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   932
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   933
#### Cookies. Blech!
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   934
class Cookie(object):
10001
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   935
    # __slots__ = ['name', 'value', 'path', 'domain', 'ports', 'expires',
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   936
    #              'discard', 'secure', 'httponly', 'comment', 'commenturl',
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   937
    #              'version']
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   938
10001
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   939
    def __init__(self, name, value, path=None, domain=None, ports=None,
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   940
                 expires=None, discard=False, secure=False, httponly=False,
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   941
                 comment=None, commenturl=None, version=0):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   942
        self.name = name
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   943
        self.value = value
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   944
        self.path = path
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   945
        self.domain = domain
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   946
        self.ports = ports
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   947
        self.expires = expires
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   948
        self.discard = discard
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   949
        self.secure = secure
10001
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   950
        self.httponly = httponly
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   951
        self.comment = comment
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   952
        self.commenturl = commenturl
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   953
        self.version = version
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   954
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   955
    def __repr__(self):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   956
        s = "Cookie(%r=%r" % (self.name, self.value)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   957
        if self.path is not None: s+=", path=%r" % (self.path,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   958
        if self.domain is not None: s+=", domain=%r" % (self.domain,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   959
        if self.ports is not None: s+=", ports=%r" % (self.ports,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   960
        if self.expires is not None: s+=", expires=%r" % (self.expires,)
10001
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   961
        if self.secure: s+=", secure"
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
   962
        if self.httponly: s+=", HttpOnly"
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   963
        if self.comment is not None: s+=", comment=%r" % (self.comment,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   964
        if self.commenturl is not None: s+=", commenturl=%r" % (self.commenturl,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   965
        if self.version != 0: s+=", version=%r" % (self.version,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   966
        s+=")"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   967
        return s
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   968
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   969
    def __eq__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   970
        return (isinstance(other, Cookie) and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   971
                other.path == self.path and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   972
                other.domain == self.domain and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   973
                other.ports == self.ports and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   974
                other.expires == self.expires and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   975
                other.secure == self.secure and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   976
                other.comment == self.comment and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   977
                other.commenturl == self.commenturl and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   978
                other.version == self.version)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   979
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   980
    def __ne__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   981
        return not self.__eq__(other)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   982
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   983
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   984
def parseCookie(headers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   985
    """Bleargh, the cookie spec sucks.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   986
    This surely needs interoperability testing.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   987
    There are two specs that are supported:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   988
    Version 0) http://wp.netscape.com/newsref/std/cookie_spec.html
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   989
    Version 1) http://www.faqs.org/rfcs/rfc2965.html
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   990
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   991
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   992
    cookies = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   993
    # There can't really be multiple cookie headers according to RFC, because
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   994
    # if multiple headers are allowed, they must be joinable with ",".
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   995
    # Neither new RFC2965 cookies nor old netscape cookies are.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   996
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   997
    header = ';'.join(headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   998
    if header[0:8].lower() == "$version":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   999
        # RFC2965 cookie
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1000
        h = tokenize([header], foldCase=False)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1001
        r_cookies = split(h, Token(','))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1002
        for r_cookie in r_cookies:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1003
            last_cookie = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1004
            rr_cookies = split(r_cookie, Token(';'))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1005
            for cookie in rr_cookies:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1006
                nameval = tuple(split(cookie, Token('=')))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1007
                if len(nameval) == 2:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1008
                    (name,), (value,) = nameval
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1009
                else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1010
                    (name,), = nameval
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1011
                    value = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1012
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1013
                name = name.lower()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1014
                if name == '$version':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1015
                    continue
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1016
                if name[0] == '$':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1017
                    if last_cookie is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1018
                        if name == '$path':
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1019
                            last_cookie.path = value
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1020
                        elif name == '$domain':
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1021
                            last_cookie.domain = value
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1022
                        elif name == '$port':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1023
                            if value is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1024
                                last_cookie.ports = ()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1025
                            else:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1026
                                last_cookie.ports = tuple([int(s) for s in value.split(',')])
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1027
                else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1028
                    last_cookie = Cookie(name, value, version=1)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1029
                    cookies.append(last_cookie)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1030
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1031
        # Oldstyle cookies don't do quoted strings or anything sensible.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1032
        # All characters are valid for names except ';' and '=', and all
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1033
        # characters are valid for values except ';'. Spaces are stripped,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1034
        # however.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1035
        r_cookies = header.split(';')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1036
        for r_cookie in r_cookies:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1037
            name, value = r_cookie.split('=', 1)
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1038
            name = name.strip(' \t')
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1039
            value = value.strip(' \t')
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1040
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1041
            cookies.append(Cookie(name, value))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1042
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1043
    return cookies
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1044
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1045
cookie_validname = "[^"+re.escape(http_tokens+http_ctls)+"]*$"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1046
cookie_validname_re = re.compile(cookie_validname)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1047
cookie_validvalue = cookie_validname+'|"([^"]|\\\\")*"$'
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1048
cookie_validvalue_re = re.compile(cookie_validvalue)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1049
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1050
def generateCookie(cookies):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1051
    # There's a fundamental problem with the two cookie specifications.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1052
    # They both use the "Cookie" header, and the RFC Cookie header only allows
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1053
    # one version to be specified. Thus, when you have a collection of V0 and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1054
    # V1 cookies, you have to either send them all as V0 or send them all as
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1055
    # V1.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1056
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1057
    # I choose to send them all as V1.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1058
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1059
    # You might think converting a V0 cookie to a V1 cookie would be lossless,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1060
    # but you'd be wrong. If you do the conversion, and a V0 parser tries to
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1061
    # read the cookie, it will see a modified form of the cookie, in cases
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1062
    # where quotes must be added to conform to proper V1 syntax.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1063
    # (as a real example: "Cookie: cartcontents=oid:94680,qty:1,auto:0,esp:y")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1064
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1065
    # However, that is what we will do, anyways. It has a high probability of
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1066
    # breaking applications that only handle oldstyle cookies, where some other
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1067
    # application set a newstyle cookie that is applicable over for site
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1068
    # (or host), AND where the oldstyle cookie uses a value which is invalid
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1069
    # syntax in a newstyle cookie.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1070
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1071
    # Also, the cookie name *cannot* be quoted in V1, so some cookies just
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1072
    # cannot be converted at all. (e.g. "Cookie: phpAds_capAd[32]=2"). These
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1073
    # are just dicarded during conversion.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1074
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1075
    # As this is an unsolvable problem, I will pretend I can just say
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1076
    # OH WELL, don't do that, or else upgrade your old applications to have
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1077
    # newstyle cookie parsers.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1078
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1079
    # I will note offhandedly that there are *many* sites which send V0 cookies
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1080
    # that are not valid V1 cookie syntax. About 20% for my cookies file.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1081
    # However, they do not generally mix them with V1 cookies, so this isn't
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1082
    # an issue, at least right now. I have not tested to see how many of those
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1083
    # webapps support RFC2965 V1 cookies. I suspect not many.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1084
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1085
    max_version = max([cookie.version for cookie in cookies])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1086
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1087
    if max_version == 0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1088
        # no quoting or anything.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1089
        return ';'.join(["%s=%s" % (cookie.name, cookie.value) for cookie in cookies])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1090
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1091
        str_cookies = ['$Version="1"']
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1092
        for cookie in cookies:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1093
            if cookie.version == 0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1094
                # Version 0 cookie: we make sure the name and value are valid
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1095
                # V1 syntax.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1096
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1097
                # If they are, we use them as is. This means in *most* cases,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1098
                # the cookie will look literally the same on output as it did
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1099
                # on input.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1100
                # If it isn't a valid name, ignore the cookie.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1101
                # If it isn't a valid value, quote it and hope for the best on
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1102
                # the other side.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1103
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1104
                if cookie_validname_re.match(cookie.name) is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1105
                    continue
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1106
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1107
                value = cookie.value
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1108
                if cookie_validvalue_re.match(cookie.value) is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1109
                    value = quoteString(value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1110
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1111
                str_cookies.append("%s=%s" % (cookie.name, value))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1112
            else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1113
                # V1 cookie, nice and easy
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1114
                str_cookies.append("%s=%s" % (cookie.name, quoteString(cookie.value)))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1115
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1116
            if cookie.path:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1117
                str_cookies.append("$Path=%s" % quoteString(cookie.path))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1118
            if cookie.domain:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1119
                str_cookies.append("$Domain=%s" % quoteString(cookie.domain))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1120
            if cookie.ports is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1121
                if len(cookie.ports) == 0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1122
                    str_cookies.append("$Port")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1123
                else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1124
                    str_cookies.append("$Port=%s" % quoteString(",".join([str(x) for x in cookie.ports])))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1125
        return ';'.join(str_cookies)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1126
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1127
def parseSetCookie(headers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1128
    setCookies = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1129
    for header in headers:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1130
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1131
            parts = header.split(';')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1132
            l = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1133
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1134
            for part in parts:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1135
                namevalue = part.split('=',1)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1136
                if len(namevalue) == 1:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1137
                    name = namevalue[0]
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1138
                    value = None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1139
                else:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1140
                    name, value = namevalue
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1141
                    value = value.strip(' \t')
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1142
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1143
                name = name.strip(' \t')
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1144
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1145
                l.append((name, value))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1146
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1147
            setCookies.append(makeCookieFromList(l, True))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1148
        except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1149
            # If we can't parse one Set-Cookie, ignore it,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1150
            # but not the rest of Set-Cookies.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1151
            pass
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1152
    return setCookies
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1153
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1154
def parseSetCookie2(toks):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1155
    outCookies = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1156
    for cookie in [[parseKeyValue(x) for x in split(y, Token(';'))]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1157
                   for y in split(toks, Token(','))]:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1158
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1159
            outCookies.append(makeCookieFromList(cookie, False))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1160
        except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1161
            # Again, if we can't handle one cookie -- ignore it.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1162
            pass
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1163
    return outCookies
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1164
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1165
def makeCookieFromList(tup, netscapeFormat):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1166
    name, value = tup[0]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1167
    if name is None or value is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1168
        raise ValueError("Cookie has missing name or value")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1169
    if name.startswith("$"):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1170
        raise ValueError("Invalid cookie name: %r, starts with '$'." % name)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1171
    cookie = Cookie(name, value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1172
    hadMaxAge = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1173
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1174
    for name, value in tup[1:]:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1175
        name = name.lower()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1176
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1177
        if value is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1178
            if name in ("discard", "secure"):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1179
                # Boolean attrs
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1180
                value = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1181
            elif name != "port":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1182
                # Can be either boolean or explicit
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1183
                continue
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1184
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1185
        if name in ("comment", "commenturl", "discard", "domain", "path", "secure"):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1186
            # simple cases
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1187
            setattr(cookie, name, value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1188
        elif name == "expires" and not hadMaxAge:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1189
            if netscapeFormat and value[0] == '"' and value[-1] == '"':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1190
                value = value[1:-1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1191
            cookie.expires = parseDateTime(value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1192
        elif name == "max-age":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1193
            hadMaxAge = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1194
            cookie.expires = int(value) + time.time()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1195
        elif name == "port":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1196
            if value is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1197
                cookie.ports = ()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1198
            else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1199
                if netscapeFormat and value[0] == '"' and value[-1] == '"':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1200
                    value = value[1:-1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1201
                cookie.ports = tuple([int(s) for s in value.split(',')])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1202
        elif name == "version":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1203
            cookie.version = int(value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1204
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1205
    return cookie
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1206
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1207
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1208
def generateSetCookie(cookies):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1209
    setCookies = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1210
    for cookie in cookies:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1211
        out = ["%s=%s" % (cookie.name, cookie.value)]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1212
        if cookie.expires:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1213
            out.append("expires=%s" % generateDateTime(cookie.expires))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1214
        if cookie.path:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1215
            out.append("path=%s" % cookie.path)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1216
        if cookie.domain:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1217
            out.append("domain=%s" % cookie.domain)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1218
        if cookie.secure:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1219
            out.append("secure")
10001
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
  1220
        if cookie.httponly:
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
  1221
            out.append("HttpOnly")
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1222
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1223
        setCookies.append('; '.join(out))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1224
    return setCookies
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1225
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1226
def generateSetCookie2(cookies):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1227
    setCookies = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1228
    for cookie in cookies:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1229
        out = ["%s=%s" % (cookie.name, quoteString(cookie.value))]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1230
        if cookie.comment:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1231
            out.append("Comment=%s" % quoteString(cookie.comment))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1232
        if cookie.commenturl:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1233
            out.append("CommentURL=%s" % quoteString(cookie.commenturl))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1234
        if cookie.discard:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1235
            out.append("Discard")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1236
        if cookie.domain:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1237
            out.append("Domain=%s" % quoteString(cookie.domain))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1238
        if cookie.expires:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1239
            out.append("Max-Age=%s" % (cookie.expires - time.time()))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1240
        if cookie.path:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1241
            out.append("Path=%s" % quoteString(cookie.path))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1242
        if cookie.ports is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1243
            if len(cookie.ports) == 0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1244
                out.append("Port")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1245
            else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1246
                out.append("Port=%s" % quoteString(",".join([str(x) for x in cookie.ports])))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1247
        if cookie.secure:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1248
            out.append("Secure")
10001
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
  1249
        if cookie.httponly:
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 10000
diff changeset
  1250
            out.append("HttpOnly")
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1251
        out.append('Version="1"')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1252
        setCookies.append('; '.join(out))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1253
    return setCookies
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1254
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1255
def parseDepth(depth):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1256
    if depth not in ("0", "1", "infinity"):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1257
        raise ValueError("Invalid depth header value: %s" % (depth,))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1258
    return depth
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1259
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1260
def parseOverWrite(overwrite):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1261
    if overwrite == "F":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1262
        return False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1263
    elif overwrite == "T":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1264
        return True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1265
    raise ValueError("Invalid overwrite header value: %s" % (overwrite,))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1266
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1267
def generateOverWrite(overwrite):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1268
    if overwrite:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1269
        return "T"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1270
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1271
        return "F"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1272
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1273
##### Random stuff that looks useful.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1274
# def sortMimeQuality(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1275
#     def sorter(item1, item2):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1276
#         if item1[0] == '*':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1277
#             if item2[0] == '*':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1278
#                 return 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1279
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1280
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1281
# def sortQuality(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1282
#     def sorter(item1, item2):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1283
#         if item1[1] < item2[1]:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1284
#             return -1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1285
#         if item1[1] < item2[1]:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1286
#             return 1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1287
#         if item1[0] == item2[0]:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1288
#             return 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1289
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1290
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1291
# def getMimeQuality(mimeType, accepts):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1292
#     type, args = parseArgs(mimeType)
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1293
#     type = type.split(Token('/'))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1294
#     if len(type) != 2:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1295
#         raise ValueError, "MIME Type "+s+" invalid."
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1296
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1297
#     for accept in accepts:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1298
#         accept, acceptQual = accept
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1299
#         acceptType = accept[0:1]
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1300
#         acceptArgs = accept[2]
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1301
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1302
#         if ((acceptType == type or acceptType == (type[0],'*') or acceptType==('*','*')) and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1303
#             (args == acceptArgs or len(acceptArgs) == 0)):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1304
#             return acceptQual
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1305
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1306
# def getQuality(type, accepts):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1307
#     qual = accepts.get(type)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1308
#     if qual is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1309
#         return qual
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1310
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1311
#     return accepts.get('*')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1312
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1313
# Headers object
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1314
class __RecalcNeeded(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1315
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1316
        return "<RecalcNeeded>"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1317
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1318
_RecalcNeeded = __RecalcNeeded()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1319
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1320
class Headers(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1321
    """This class stores the HTTP headers as both a parsed representation and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1322
    the raw string representation. It converts between the two on demand."""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1323
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1324
    def __init__(self, headers=None, rawHeaders=None, handler=DefaultHTTPHandler):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1325
        self._raw_headers = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1326
        self._headers = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1327
        self.handler = handler
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1328
        if headers is not None:
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
  1329
            for key, value in headers.items():
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1330
                self.setHeader(key, value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1331
        if rawHeaders is not None:
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
  1332
            for key, value in rawHeaders.items():
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1333
                self.setRawHeaders(key, value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1334
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1335
    def _setRawHeaders(self, headers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1336
        self._raw_headers = headers
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1337
        self._headers = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1338
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1339
    def _toParsed(self, name):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1340
        r = self._raw_headers.get(name, None)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1341
        h = self.handler.parse(name, r)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1342
        if h is not None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1343
            self._headers[name] = h
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1344
        return h
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1345
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1346
    def _toRaw(self, name):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1347
        h = self._headers.get(name, None)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1348
        r = self.handler.generate(name, h)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1349
        if r is not None:
9726
8905267dc7ae [web] Add asserts to the raw header conversion functions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9572
diff changeset
  1350
            assert isinstance(r, list)
8905267dc7ae [web] Add asserts to the raw header conversion functions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9572
diff changeset
  1351
            for v in r:
8905267dc7ae [web] Add asserts to the raw header conversion functions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9572
diff changeset
  1352
                assert isinstance(v, str)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1353
            self._raw_headers[name] = r
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1354
        return r
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1355
8313
386b6313de28 http-header: support __contains__ in Headers
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8081
diff changeset
  1356
    def __contains__(self, name):
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1357
        """Does a header with the given name exist?"""
8697
574bb05e40a4 [toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8696
diff changeset
  1358
        return name.lower() in self._raw_headers
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1359
8313
386b6313de28 http-header: support __contains__ in Headers
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8081
diff changeset
  1360
    hasHeader = __contains__
386b6313de28 http-header: support __contains__ in Headers
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8081
diff changeset
  1361
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1362
    def getRawHeaders(self, name, default=None):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1363
        """Returns a list of headers matching the given name as the raw string given."""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1364
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1365
        name = name.lower()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1366
        raw_header = self._raw_headers.get(name, default)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1367
        if raw_header is not _RecalcNeeded:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1368
            return raw_header
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1369
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1370
        return self._toRaw(name)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1371
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1372
    def getHeader(self, name, default=None):
9435
dde8ea5d7ffd [web] Fix typo in docstring
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8697
diff changeset
  1373
        """Returns the parsed representation of the given header.
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1374
        The exact form of the return value depends on the header in question.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1375
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1376
        If no parser for the header exists, raise ValueError.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1377
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1378
        If the header doesn't exist, return default (or None if not specified)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1379
        """
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1380
        name = name.lower()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1381
        parsed = self._headers.get(name, default)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1382
        if parsed is not _RecalcNeeded:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1383
            return parsed
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1384
        return self._toParsed(name)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1385
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1386
    def setRawHeaders(self, name, value):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1387
        """Sets the raw representation of the given header.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1388
        Value should be a list of strings, each being one header of the
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1389
        given name.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1390
        """
9726
8905267dc7ae [web] Add asserts to the raw header conversion functions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9572
diff changeset
  1391
        assert isinstance(value, list)
8905267dc7ae [web] Add asserts to the raw header conversion functions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9572
diff changeset
  1392
        for v in value:
8905267dc7ae [web] Add asserts to the raw header conversion functions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9572
diff changeset
  1393
            assert isinstance(v, str)
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1394
        name = name.lower()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1395
        self._raw_headers[name] = value
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1396
        self._headers[name] = _RecalcNeeded
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1397
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1398
    def setHeader(self, name, value):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1399
        """Sets the parsed representation of the given header.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1400
        Value should be a list of objects whose exact form depends
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1401
        on the header in question.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1402
        """
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1403
        name = name.lower()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1404
        self._raw_headers[name] = _RecalcNeeded
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1405
        self._headers[name] = value
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1406
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1407
    def addRawHeader(self, name, value):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1408
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1409
        Add a raw value to a header that may or may not already exist.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1410
        If it exists, add it as a separate header to output; do not
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1411
        replace anything.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1412
        """
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1413
        name = name.lower()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1414
        raw_header = self._raw_headers.get(name)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1415
        if raw_header is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1416
            # No header yet
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1417
            raw_header = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1418
            self._raw_headers[name] = raw_header
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1419
        elif raw_header is _RecalcNeeded:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1420
            raw_header = self._toRaw(name)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1421
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1422
        raw_header.append(value)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1423
        self._headers[name] = _RecalcNeeded
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1424
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1425
    def addHeader(self, name, value):
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1426
        """
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1427
        Add a parsed representatoin to a header that may or may not already exist.
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1428
        If it exists, add it as a separate header to output; do not
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1429
        replace anything.
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1430
        """
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1431
        name = name.lower()
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1432
        header = self._headers.get(name)
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1433
        if header is None:
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1434
            # No header yet
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1435
            header = []
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1436
            self._headers[name] = header
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1437
        elif header is _RecalcNeeded:
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1438
            header = self._toParsed(name)
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1439
        header.append(value)
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1440
        self._raw_headers[name] = _RecalcNeeded
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5155
diff changeset
  1441
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1442
    def removeHeader(self, name):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1443
        """Removes the header named."""
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1444
        name = name.lower()
8697
574bb05e40a4 [toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8696
diff changeset
  1445
        if name in self._raw_headers:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1446
            del self._raw_headers[name]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1447
            del self._headers[name]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1448
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1449
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1450
        return '<Headers: Raw: %s Parsed: %s>'% (self._raw_headers, self._headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1451
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1452
    def canonicalNameCaps(self, name):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1453
        """Return the name with the canonical capitalization, if known,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1454
        otherwise, Caps-After-Dashes"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1455
        return header_case_mapping.get(name) or dashCapitalize(name)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1456
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1457
    def getAllRawHeaders(self):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1458
        """Return an iterator of key, value pairs of all headers
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1459
        contained in this object, as strings. The keys are capitalized
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1460
        in canonical capitalization."""
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
  1461
        for k, v in self._raw_headers.items():
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1462
            if v is _RecalcNeeded:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1463
                v = self._toRaw(k)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1464
            yield self.canonicalNameCaps(k), v
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1465
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1466
    def makeImmutable(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1467
        """Make this header set immutable. All mutating operations will
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1468
        raise an exception."""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1469
        self.setHeader = self.setRawHeaders = self.removeHeader = self._mutateRaise
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1470
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1471
    def _mutateRaise(self, *args):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1472
        raise AttributeError("This header object is immutable as the headers have already been sent.")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1473
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1474
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1475
"""The following dicts are all mappings of header to list of operations
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1476
   to perform. The first operation should generally be 'tokenize' if the
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1477
   header can be parsed according to the normal tokenization rules. If
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1478
   it cannot, generally the first thing you want to do is take only the
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1479
   last instance of the header (in case it was sent multiple times, which
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1480
   is strictly an error, but we're nice.).
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1481
   """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1482
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
  1483
iteritems = lambda x: x.items()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1484
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1485
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1486
parser_general_headers = {
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1487
    'Cache-Control': (tokenize, listParser(parseCacheControl), dict),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1488
    'Connection': (tokenize, filterTokens),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1489
    'Date': (last, parseDateTime),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1490
#    'Pragma':tokenize
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1491
#    'Trailer':tokenize
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1492
    'Transfer-Encoding': (tokenize, filterTokens),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1493
#    'Upgrade':tokenize
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1494
#    'Via':tokenize, stripComment
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1495
#    'Warning':tokenize
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1496
}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1497
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1498
generator_general_headers = {
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1499
    'Cache-Control': (iteritems, listGenerator(generateCacheControl), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1500
    'Connection': (generateList, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1501
    'Date': (generateDateTime, singleHeader),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1502
#    'Pragma':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1503
#    'Trailer':
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1504
    'Transfer-Encoding': (generateList, singleHeader),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1505
#    'Upgrade':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1506
#    'Via':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1507
#    'Warning':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1508
}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1509
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1510
parser_request_headers = {
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1511
    'Accept': (tokenize, listParser(parseAccept), dict),
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1512
    'Accept-Charset': (tokenize, listParser(parseAcceptQvalue), dict, addDefaultCharset),
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1513
    'Accept-Encoding': (tokenize, listParser(parseAcceptQvalue), dict, addDefaultEncoding),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1514
    'Accept-Language': (tokenize, listParser(parseAcceptQvalue), dict),
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
  1515
    'Access-Control-Request-Method': (parseHTTPMethod, ),
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
  1516
    'Access-Control-Request-Headers': (filterTokens, ),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1517
    'Authorization': (last, parseAuthorization),
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1518
    'Cookie': (parseCookie,),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1519
    'Expect': (tokenize, listParser(parseExpect), dict),
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
  1520
    'Origin': (last,),
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1521
    'From': (last,),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1522
    'Host': (last,),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1523
    'If-Match': (tokenize, listParser(parseStarOrETag), list),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1524
    'If-Modified-Since': (last, parseIfModifiedSince),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1525
    'If-None-Match': (tokenize, listParser(parseStarOrETag), list),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1526
    'If-Range': (parseIfRange,),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1527
    'If-Unmodified-Since': (last, parseDateTime),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1528
    'Max-Forwards': (last, int),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1529
#    'Proxy-Authorization':str, # what is "credentials"
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1530
    'Range': (tokenize, parseRange),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1531
    'Referer': (last, str), # TODO: URI object?
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1532
    'TE': (tokenize, listParser(parseAcceptQvalue), dict),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1533
    'User-Agent': (last, str),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1534
}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1535
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1536
generator_request_headers = {
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1537
    'Accept': (iteritems, listGenerator(generateAccept), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1538
    'Accept-Charset': (iteritems, listGenerator(generateAcceptQvalue), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1539
    'Accept-Encoding': (iteritems, removeDefaultEncoding,
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1540
                        listGenerator(generateAcceptQvalue), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1541
    'Accept-Language': (iteritems, listGenerator(generateAcceptQvalue), singleHeader),
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
  1542
    'Access-Control-Request-Method': (unique, str, singleHeader, ),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1543
    'Authorization': (generateAuthorization,), # what is "credentials"
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1544
    'Cookie': (generateCookie, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1545
    'Expect': (iteritems, listGenerator(generateExpect), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1546
    'From': (unique, str, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1547
    'Host': (unique, str, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1548
    'If-Match': (listGenerator(generateStarOrETag), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1549
    'If-Modified-Since': (generateDateTime, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1550
    'If-None-Match': (listGenerator(generateStarOrETag), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1551
    'If-Range': (generateIfRange, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1552
    'If-Unmodified-Since': (generateDateTime, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1553
    'Max-Forwards': (unique, str, singleHeader),
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
  1554
    'Origin': (unique, str, singleHeader),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1555
#    'Proxy-Authorization':str, # what is "credentials"
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1556
    'Range': (generateRange, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1557
    'Referer': (unique, str, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1558
    'TE': (iteritems, listGenerator(generateAcceptQvalue), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1559
    'User-Agent': (unique, str, singleHeader),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1560
}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1561
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1562
parser_response_headers = {
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1563
    'Accept-Ranges': (tokenize, filterTokens),
9989
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1564
    'Access-Control-Allow-Origin': (last, parseAllowOrigin,),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1565
    'Access-Control-Allow-Credentials': (last, parseAllowCreds,),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1566
    'Access-Control-Allow-Methods': (tokenize, listParser(parseHTTPMethod), list),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1567
    'Access-Control-Allow-Headers': (listGenerator(str), ),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1568
    'Access-Control-Expose-Headers': (filterTokens, ),
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1569
    'Age': (last, int),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1570
    'ETag': (tokenize, ETag.parse),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1571
    'Location': (last,), # TODO: URI object?
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1572
#    'Proxy-Authenticate'
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1573
    'Retry-After': (last, parseRetryAfter),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1574
    'Server': (last,),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1575
    'Set-Cookie': (parseSetCookie,),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1576
    'Set-Cookie2': (tokenize, parseSetCookie2),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1577
    'Vary': (tokenize, filterTokens),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1578
    'WWW-Authenticate': (lambda h: tokenize(h, foldCase=False),
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1579
                         parseWWWAuthenticate,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1580
}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1581
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1582
generator_response_headers = {
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1583
    'Accept-Ranges': (generateList, singleHeader),
9989
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1584
    'Access-Control-Allow-Origin': (unique, str, singleHeader),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1585
    'Access-Control-Allow-Credentials': (generateTrueFalse, singleHeader),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1586
    'Access-Control-Allow-Headers': (set, generateList, singleHeader),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1587
    'Access-Control-Allow-Methods': (set, generateList, singleHeader),
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
  1588
    'Access-Control-Expose-Headers': (set, generateList, singleHeader),
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1589
    'Age': (unique, str, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1590
    'ETag': (ETag.generate, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1591
    'Location': (unique, str, singleHeader),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1592
#    'Proxy-Authenticate'
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1593
    'Retry-After': (generateRetryAfter, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1594
    'Server': (unique, str, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1595
    'Set-Cookie': (generateSetCookie,),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1596
    'Set-Cookie2': (generateSetCookie2,),
9936
5dbf45204109 [web/headers] don't list a request header twice in "Vary"
Julien Cristau <julien.cristau@logilab.fr>
parents: 9572
diff changeset
  1597
    'Vary': (set, generateList, singleHeader),
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1598
    'WWW-Authenticate': (generateWWWAuthenticate,)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1599
}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1600
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1601
parser_entity_headers = {
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1602
    'Allow': (lambda str:tokenize(str, foldCase=False), filterTokens),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1603
    'Content-Encoding': (tokenize, filterTokens),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1604
    'Content-Language': (tokenize, filterTokens),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1605
    'Content-Length': (last, int),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1606
    'Content-Location': (last,), # TODO: URI object?
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1607
    'Content-MD5': (last, parseContentMD5),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1608
    'Content-Range': (last, parseContentRange),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1609
    'Content-Type': (lambda str:tokenize(str, foldCase=False), parseContentType),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1610
    'Expires': (last, parseExpires),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1611
    'Last-Modified': (last, parseDateTime),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1612
    }
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1613
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1614
generator_entity_headers = {
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1615
    'Allow': (generateList, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1616
    'Content-Encoding': (generateList, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1617
    'Content-Language': (generateList, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1618
    'Content-Length': (unique, str, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1619
    'Content-Location': (unique, str, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1620
    'Content-MD5': (base64.encodestring, lambda x: x.strip("\n"), singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1621
    'Content-Range': (generateContentRange, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1622
    'Content-Type': (generateContentType, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1623
    'Expires': (generateDateTime, singleHeader),
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1624
    'Last-Modified': (generateDateTime, singleHeader),
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1625
    }
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1626
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1627
DefaultHTTPHandler.updateParsers(parser_general_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1628
DefaultHTTPHandler.updateParsers(parser_request_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1629
DefaultHTTPHandler.updateParsers(parser_response_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1630
DefaultHTTPHandler.updateParsers(parser_entity_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1631
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1632
DefaultHTTPHandler.updateGenerators(generator_general_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1633
DefaultHTTPHandler.updateGenerators(generator_request_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1634
DefaultHTTPHandler.updateGenerators(generator_response_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1635
DefaultHTTPHandler.updateGenerators(generator_entity_headers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1636
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1637
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1638
# casemappingify(DefaultHTTPParsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1639
# casemappingify(DefaultHTTPGenerators)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1640
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1641
# lowerify(DefaultHTTPParsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1642
# lowerify(DefaultHTTPGenerators)