web/http_headers.py
author David Douard <david.douard@logilab.fr>
Wed, 22 Apr 2015 11:00:50 +0200
changeset 10308 3f94034cc972
parent 10067 16c554c76eb4
child 10590 7629902e7554
permissions -rw-r--r--
[hooks] add a looping task that dumps the stats regularly in a file
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
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
     5
import types, time
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
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
     9
import urlparse
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    10
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    11
def dashCapitalize(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    12
    ''' 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
    13
    return '-'.join([ x.capitalize() for x in s.split('-')])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    14
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    15
# datetime parsing and formatting
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    16
weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    17
weekdayname_lower = [name.lower() for name in weekdayname]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    18
monthname = [None,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    19
             'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    20
             'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    21
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
    22
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    23
# HTTP Header parsing API
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    24
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    25
header_case_mapping = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    26
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    27
def casemappingify(d):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    28
    global header_case_mapping
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    29
    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
    30
    header_case_mapping.update(newd)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    31
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    32
def lowerify(d):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    33
    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
    34
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
class HeaderHandler(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    37
    """HeaderHandler manages header generating and parsing functions.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    38
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    39
    HTTPParsers = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    40
    HTTPGenerators = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    41
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    42
    def __init__(self, parsers=None, generators=None):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    43
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    44
        @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
    45
        @type parsers: L{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    46
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    47
        @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
    48
        @type generators: L{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    49
        """
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
        if parsers:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    52
            self.HTTPParsers.update(parsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    53
        if generators:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    54
            self.HTTPGenerators.update(generators)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    55
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    56
    def parse(self, name, header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    57
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    58
        Parse the given header based on its given name.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    59
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    60
        @param name: The header name to parse.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    61
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    62
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    63
        @param header: A list of unparsed headers.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    64
        @type header: C{list} of C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    65
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    66
        @return: The return value is the parsed header representation,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    67
            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
    68
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    69
        parser = self.HTTPParsers.get(name, None)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    70
        if parser is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    71
            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
    72
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    73
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    74
            for p in parser:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    75
                #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
    76
                header = p(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    77
                # if isinstance(h, types.GeneratorType):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    78
                #     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
    79
        except ValueError as v:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    80
            # print v
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
    81
            header = None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    82
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    83
        return header
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    84
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    85
    def generate(self, name, header):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    86
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    87
        Generate the given header based on its given name.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    88
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    89
        @param name: The header name to generate.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    90
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    91
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    92
        @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
    93
            L{HeaderHandler}.parse.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    94
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    95
        @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
    96
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    97
        generator = self.HTTPGenerators.get(name, None)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    98
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
    99
        if generator is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   100
            # print self.generators
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   101
            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
   102
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   103
        for g in generator:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   104
            header = g(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   105
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   106
        #self._raw_headers[name] = h
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   107
        return header
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   108
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   109
    def updateParsers(self, parsers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   110
        """Update en masse the parser maps.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   111
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   112
        @param parsers: Map of header names to parser chains.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   113
        @type parsers: C{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   114
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   115
        casemappingify(parsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   116
        self.HTTPParsers.update(lowerify(parsers))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   117
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   118
    def addParser(self, name, value):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   119
        """Add an individual parser chain for the given header.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   120
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   121
        @param name: Name of the header to add
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   122
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   123
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   124
        @param value: The parser chain
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   125
        @type value: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   126
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   127
        self.updateParsers({name: value})
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   128
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   129
    def updateGenerators(self, generators):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   130
        """Update en masse the generator maps.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   131
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   132
        @param parsers: Map of header names to generator chains.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   133
        @type parsers: C{dict}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   134
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   135
        casemappingify(generators)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   136
        self.HTTPGenerators.update(lowerify(generators))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   137
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   138
    def addGenerators(self, name, value):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   139
        """Add an individual generator chain for the given header.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   140
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   141
        @param name: Name of the header to add
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   142
        @type name: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   143
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   144
        @param value: The generator chain
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   145
        @type value: C{str}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   146
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   147
        self.updateGenerators({name: value})
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   148
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   149
    def update(self, parsers, generators):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   150
        """Conveniently update parsers and generators all at once.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   151
        """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   152
        self.updateParsers(parsers)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   153
        self.updateGenerators(generators)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   154
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
DefaultHTTPHandler = HeaderHandler()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   157
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
## HTTP DateTime parser
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   160
def parseDateTime(dateString):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   161
    """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
   162
    parts = dateString.split()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   163
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   164
    if not parts[0][0:3].lower() in weekdayname_lower:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   165
        # Weekday is stupid. Might have been omitted.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   166
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   167
            return parseDateTime("Sun, "+dateString)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   168
        except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   169
            # Guess not.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   170
            pass
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   171
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   172
    partlen = len(parts)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   173
    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
   174
        # 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
   175
        # (Note: "GMT" is literal, not a variable timezone)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   176
        # (also handles without "GMT")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   177
        # This is the normal format
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   178
        day = parts[1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   179
        month = parts[2]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   180
        year = parts[3]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   181
        time = parts[4]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   182
    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
   183
        # 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
   184
        # (Note: "GMT" is literal, not a variable timezone)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   185
        # (also handles without without "GMT")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   186
        # Two digit year, yucko.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   187
        day, month, year = parts[1].split('-')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   188
        time = parts[2]
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   189
        year = int(year)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   190
        if year < 69:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   191
            year = year + 2000
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   192
        elif year < 100:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   193
            year = year + 1900
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   194
    elif len(parts) == 5:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   195
        # 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
   196
        # ANSI C asctime() format.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   197
        day = parts[2]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   198
        month = parts[1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   199
        year = parts[4]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   200
        time = parts[3]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   201
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   202
        raise ValueError("Unknown datetime format %r" % dateString)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   203
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   204
    day = int(day)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   205
    month = int(monthname_lower.index(month.lower()))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   206
    year = int(year)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   207
    hour, min, sec = map(int, time.split(':'))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   208
    return int(timegm((year, month, day, hour, min, sec)))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   209
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
##### HTTP tokenizer
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   212
class Token(str):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   213
    __slots__=[]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   214
    tokens = {}
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   215
    def __new__(self, char):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   216
        token = Token.tokens.get(char)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   217
        if token is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   218
            Token.tokens[char] = token = str.__new__(self, char)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   219
        return token
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   220
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   221
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   222
        return "Token(%s)" % str.__repr__(self)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   223
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
http_tokens = " \t\"()<>@,;:\\/[]?={}"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   226
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
   227
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   228
def tokenize(header, foldCase=True):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   229
    """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
   230
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   231
    In particular:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   232
     - 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
   233
       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
   234
     - Quoted string support including embedded backslashes.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   235
     - 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
   236
        (unless foldCase=False)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   237
     - Multiple headers are concatenated with ','
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   238
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   239
    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
   240
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   241
    Takes a raw header value (list of strings), and
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   242
    Returns a generator of strings and Token class instances.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   243
    """
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   244
    tokens = http_tokens
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   245
    ctls = http_ctls
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   246
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   247
    string = ",".join(header)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   248
    list = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   249
    start = 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   250
    cur = 0
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   251
    quoted = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   252
    qpair = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   253
    inSpaces = -1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   254
    qstring = None
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   255
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   256
    for x in string:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   257
        if quoted:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   258
            if qpair:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   259
                qpair = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   260
                qstring = qstring+string[start:cur-1]+x
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   261
                start = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   262
            elif x == '\\':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   263
                qpair = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   264
            elif x == '"':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   265
                quoted = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   266
                yield qstring+string[start:cur]
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   267
                qstring = None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   268
                start = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   269
        elif x in tokens:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   270
            if start != cur:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   271
                if foldCase:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   272
                    yield string[start:cur].lower()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   273
                else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   274
                    yield string[start:cur]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   275
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   276
            start = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   277
            if x == '"':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   278
                quoted = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   279
                qstring = ""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   280
                inSpaces = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   281
            elif x in " \t":
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   282
                if inSpaces is False:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   283
                    inSpaces = True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   284
            else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   285
                inSpaces = -1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   286
                yield Token(x)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   287
        elif x in ctls:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   288
            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
   289
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   290
            if inSpaces is True:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   291
                yield Token(' ')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   292
                inSpaces = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   293
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   294
            inSpaces = False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   295
        cur = cur+1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   296
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   297
    if qpair:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   298
        raise ValueError, "Missing character after '\\'"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   299
    if quoted:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   300
        raise ValueError, "Missing end quote"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   301
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   302
    if start != cur:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   303
        if foldCase:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   304
            yield string[start:cur].lower()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   305
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   306
            yield string[start:cur]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   307
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   308
def split(seq, delim):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   309
    """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
   310
    Too bad it's not builtin to python!"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   311
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   312
    cur = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   313
    for item in seq:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   314
        if item == delim:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   315
            yield cur
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   316
            cur = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   317
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   318
            cur.append(item)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   319
    yield cur
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   320
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   321
# def find(seq, *args):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   322
#     """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
   323
#     Too bad it's not builtin to python!"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   324
#     try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   325
#         return seq.index(value, *args)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   326
#     except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   327
#         return -1
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   328
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
def filterTokens(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   331
    """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
   332
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   333
    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
   334
    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
   335
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   336
    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
   337
    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
   338
    hurt anything, in any case.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   339
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   340
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   341
    l = []
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   342
    for x in seq:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   343
        if not isinstance(x, Token):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   344
            l.append(x)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   345
    return l
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   346
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   347
##### parser utilities:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   348
def checkSingleToken(tokens):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   349
    if len(tokens) != 1:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   350
        raise ValueError, "Expected single token, not %s." % (tokens,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   351
    return tokens[0]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   352
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   353
def parseKeyValue(val):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   354
    if len(val) == 1:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   355
        return val[0], None
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   356
    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
   357
        return val[0], val[2]
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   358
    raise ValueError, "Expected key or key=value, but got %s." % (val,)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   359
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   360
def parseArgs(field):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   361
    args = split(field, Token(';'))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   362
    val = args.next()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   363
    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
   364
    return val, args
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   365
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   366
def listParser(fun):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   367
    """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
   368
    comma-separated list"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   369
    def listParserHelper(tokens):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   370
        fields = split(tokens, Token(','))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   371
        for field in fields:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   372
            if len(field) != 0:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   373
                yield fun(field)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   374
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   375
    return listParserHelper
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   376
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   377
def last(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   378
    """Return seq[-1]"""
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
9569
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   381
def unique(seq):
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   382
    '''if seq is not a string, check it's a sequence of one element and return it'''
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   383
    if isinstance(seq, basestring):
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   384
        return seq
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   385
    if len(seq) != 1:
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   386
        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
   387
    return seq[0]
ac0bc417b55f [web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents: 9435
diff changeset
   388
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   389
def parseHTTPMethod(method):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   390
    """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
   391
    method = method.strip()
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   392
    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
   393
                      "TRACE", "CONNECT"):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   394
        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
   395
    return method
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   396
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   397
def parseAllowOrigin(origin):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   398
    """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
   399
    if origin == 'null':
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   400
        return origin
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   401
    p = urlparse.urlparse(origin)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   402
    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
   403
        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
   404
    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
   405
        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
   406
    if not p.netloc:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   407
        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
   408
    return origin
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   409
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   410
def parseAllowCreds(cred):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   411
    """Can be "true" """
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   412
    if cred:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   413
        cred = cred.lower()
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   414
    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
   415
        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
   416
    return cred
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9569
diff changeset
   417
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   418
##### Generation utilities
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   419
def quoteString(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   420
    return '"%s"' % s.replace('\\', '\\\\').replace('"', '\\"')
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   421
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   422
def listGenerator(fun):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   423
    """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
   424
    the given list, then joins the result with generateList"""
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   425
    def listGeneratorHelper(l):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   426
        return generateList([fun(e) for e in l])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   427
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   428
    return listGeneratorHelper
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   429
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   430
def generateList(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   431
    return ", ".join(seq)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   432
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   433
def singleHeader(item):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   434
    return [item]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   435
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   436
def generateKeyValues(kvs):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   437
    l = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   438
    # print kvs
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   439
    for k, v in kvs:
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   440
        if v is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   441
            l.append('%s' % k)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   442
        else:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   443
            l.append('%s=%s' % (k, v))
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   444
    return ";".join(l)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   445
9989
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   446
def generateTrueFalse(value):
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   447
    """
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   448
    Return 'true' or 'false' depending on the value.
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   449
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   450
    *   'true' values are `True`, `1`, `"true"`
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   451
    *   'false' values are `False`, `0`, `"false"`
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   452
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
    if (value in (True, 1) or
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   455
            isinstance(value, basestring) and value.lower() == 'true'):
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   456
        return 'true'
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   457
    if (value in (False, 0) or
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   458
            isinstance(value, basestring) and value.lower() == 'false'):
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   459
        return 'false'
cfb6e9dab902 [cors] Fix CORS headers generators
Christophe de Vienne <christophe@unlish.com>
parents: 9936
diff changeset
   460
    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
   461
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   462
class MimeType(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   463
    def fromString(klass, mimeTypeString):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   464
        """Generate a MimeType object from the given string.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   465
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   466
        @param mimeTypeString: The mimetype to parse
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
        @return: L{MimeType}
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 DefaultHTTPHandler.parse('content-type', [mimeTypeString])
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
    fromString = classmethod(fromString)
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):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   502
        return hash(self.mediaType)^hash(self.mediaSubtype)^hash(tuple(self.params.iteritems()))
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('/'):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   509
        raise ValueError, "MIME Type "+str(type)+" invalid."
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('/'):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   572
        raise ValueError, "MIME Type "+str(type)+" invalid."
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:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   733
        out+=';'+generateKeyValues(mimeType.params.iteritems())
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
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   769
def generateCacheControl((k, v)):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   770
    if v is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   771
        return str(k)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   772
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   773
        if k == 'no-cache' or k == 'private':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   774
            # quoted list of values
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   775
            v = quoteString(generateList(
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   776
                [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
   777
        return '%s=%s' % (k, v)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   778
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   779
def generateContentRange(tup):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   780
    """tup is (type, start, end, len)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   781
    len can be None.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   782
    """
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   783
    type, start, end, len = tup
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   784
    if len == None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   785
        len = '*'
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   786
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   787
        len = int(len)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   788
    if start == None and end == None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   789
        startend = '*'
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   790
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   791
        startend = '%d-%d' % (start, end)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   792
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   793
    return '%s %s/%s' % (type, startend, len)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   794
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   795
def generateDateTime(secSinceEpoch):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   796
    """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
   797
    # 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
   798
    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
   799
    s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   800
        weekdayname[wd],
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   801
        day, monthname[month], year,
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   802
        hh, mm, ss)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   803
    return s
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   804
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   805
def generateExpect(item):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   806
    if item[1][0] is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   807
        out = '%s' % (item[0],)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   808
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   809
        out = '%s=%s' % (item[0], item[1][0])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   810
    if len(item[1]) > 1:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   811
        out += ';'+generateKeyValues(item[1][1:])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   812
    return out
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   813
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   814
def generateRange(range):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   815
    def noneOr(s):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   816
        if s is None:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   817
            return ''
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   818
        return s
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   819
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   820
    type, ranges = range
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   821
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   822
    if type != 'bytes':
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   823
        raise ValueError("Unknown range unit: "+type+".")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   824
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   825
    return (type+'='+
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   826
            ','.join(['%s-%s' % (noneOr(startend[0]), noneOr(startend[1]))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   827
                      for startend in ranges]))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   828
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   829
def generateRetryAfter(when):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   830
    # always generate delta seconds format
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   831
    return str(int(when - time.time()))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   832
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   833
def generateContentType(mimeType):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   834
    out = "%s/%s" % (mimeType.mediaType, mimeType.mediaSubtype)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   835
    if mimeType.params:
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   836
        out += ';' + generateKeyValues(mimeType.params.iteritems())
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   837
    return out
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   838
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   839
def generateIfRange(dateOrETag):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   840
    if isinstance(dateOrETag, ETag):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   841
        return dateOrETag.generate()
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   842
    else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   843
        return generateDateTime(dateOrETag)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   844
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   845
# WWW-Authenticate and Authorization
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   846
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   847
def generateWWWAuthenticate(headers):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   848
    _generated = []
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   849
    for seq in headers:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   850
        scheme, challenge = seq[0], seq[1]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   851
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   852
        # 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
   853
        # 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
   854
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   855
        try:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   856
            l = []
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   857
            for k, v in dict(challenge).iteritems():
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   858
                l.append("%s=%s" % (k, quoteString(v)))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   859
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   860
            _generated.append("%s %s" % (scheme, ", ".join(l)))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   861
        except ValueError:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   862
            _generated.append("%s %s" % (scheme, challenge))
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   863
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   864
    return _generated
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   865
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   866
def generateAuthorization(seq):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   867
    return [' '.join(seq)]
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   868
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
class ETag(object):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   872
    def __init__(self, tag, weak=False):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   873
        self.tag = str(tag)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   874
        self.weak = weak
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   875
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   876
    def match(self, other, strongCompare):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   877
        # Sec 13.3.
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   878
        # 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
   879
        #   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
   880
        #
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   881
        # 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
   882
        #   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
   883
        #   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
   884
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   885
        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
   886
            return False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   887
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   888
        if strongCompare and (other.weak or self.weak):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   889
            return False
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   890
        return True
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   891
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   892
    def __eq__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   893
        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
   894
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   895
    def __ne__(self, other):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   896
        return not self.__eq__(other)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   897
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   898
    def __repr__(self):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   899
        return "Etag(%r, weak=%r)" % (self.tag, self.weak)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   900
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   901
    def parse(tokens):
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   902
        tokens = tuple(tokens)
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   903
        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
   904
            return ETag(tokens[0])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   905
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   906
        if(len(tokens) == 3 and tokens[0] == "w"
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   907
           and tokens[1] == Token('/')):
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   908
            return ETag(tokens[2], weak=True)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   909
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   910
        raise ValueError("Invalid ETag.")
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
   911
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
   912
    parse = staticmethod(parse)
5155
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:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1329
            for key, value in headers.iteritems():
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:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1332
            for key, value in rawHeaders.iteritems():
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."""
9572
73b2410bdadc [web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents: 9571
diff changeset
  1461
        for k, v in self._raw_headers.iteritems():
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
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff changeset
  1483
iteritems = lambda x: x.iteritems()
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)