author | Julien Cristau <julien.cristau@logilab.fr> |
Wed, 09 Apr 2014 10:49:18 +0200 | |
changeset 9670 | 5b661f58978c |
parent 9572 | 73b2410bdadc |
child 9726 | 8905267dc7ae |
child 9936 | 5dbf45204109 |
permissions | -rw-r--r-- |
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 |
from __future__ import generators |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
6 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
7 |
import types, time |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
8 |
from calendar import timegm |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
9 |
import base64 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
10 |
import re |
9571
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
11 |
import urlparse |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
12 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
13 |
def dashCapitalize(s): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
14 |
''' 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
|
15 |
return '-'.join([ x.capitalize() for x in s.split('-')]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
16 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
17 |
# datetime parsing and formatting |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
18 |
weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
19 |
weekdayname_lower = [name.lower() for name in weekdayname] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
20 |
monthname = [None, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
21 |
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
22 |
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
23 |
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
|
24 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
25 |
# HTTP Header parsing API |
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 |
header_case_mapping = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
28 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
29 |
def casemappingify(d): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
30 |
global header_case_mapping |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
31 |
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
|
32 |
header_case_mapping.update(newd) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
33 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
34 |
def lowerify(d): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
35 |
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
|
36 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
37 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
38 |
class HeaderHandler(object): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
39 |
"""HeaderHandler manages header generating and parsing functions. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
40 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
41 |
HTTPParsers = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
42 |
HTTPGenerators = {} |
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 |
def __init__(self, parsers=None, generators=None): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
45 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
46 |
@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
|
47 |
@type parsers: L{dict} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
48 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
49 |
@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
|
50 |
@type generators: L{dict} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
51 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
52 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
53 |
if parsers: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
54 |
self.HTTPParsers.update(parsers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
55 |
if generators: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
56 |
self.HTTPGenerators.update(generators) |
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 |
def parse(self, name, header): |
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 |
Parse the given header based on its given name. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
61 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
62 |
@param name: The header name to parse. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
63 |
@type name: C{str} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
64 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
65 |
@param header: A list of unparsed headers. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
66 |
@type header: C{list} of C{str} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
67 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
68 |
@return: The return value is the parsed header representation, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
69 |
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
|
70 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
71 |
parser = self.HTTPParsers.get(name, None) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
72 |
if parser is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
73 |
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
|
74 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
75 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
76 |
for p in parser: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
77 |
#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
|
78 |
header = p(header) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
79 |
# if isinstance(h, types.GeneratorType): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
80 |
# 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
|
81 |
except ValueError as v: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
82 |
# print v |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
83 |
header = None |
5155
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 |
return 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 |
def generate(self, name, header): |
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 |
Generate the given header based on its given name. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
90 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
91 |
@param name: The header name to generate. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
92 |
@type name: C{str} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
93 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
94 |
@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
|
95 |
L{HeaderHandler}.parse. |
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 |
@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
|
98 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
99 |
generator = self.HTTPGenerators.get(name, None) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
100 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
101 |
if generator is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
102 |
# print self.generators |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
103 |
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
|
104 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
105 |
for g in generator: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
106 |
header = g(header) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
107 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
108 |
#self._raw_headers[name] = h |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
109 |
return header |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
110 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
111 |
def updateParsers(self, parsers): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
112 |
"""Update en masse the parser maps. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
113 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
114 |
@param parsers: Map of header names to parser chains. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
115 |
@type parsers: C{dict} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
116 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
117 |
casemappingify(parsers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
118 |
self.HTTPParsers.update(lowerify(parsers)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
119 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
120 |
def addParser(self, name, value): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
121 |
"""Add an individual parser chain for the given header. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
122 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
123 |
@param name: Name of the header to add |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
124 |
@type name: C{str} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
125 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
126 |
@param value: The parser chain |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
127 |
@type value: C{str} |
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 |
self.updateParsers({name: value}) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
130 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
131 |
def updateGenerators(self, generators): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
132 |
"""Update en masse the generator maps. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
133 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
134 |
@param parsers: Map of header names to generator chains. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
135 |
@type parsers: C{dict} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
136 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
137 |
casemappingify(generators) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
138 |
self.HTTPGenerators.update(lowerify(generators)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
139 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
140 |
def addGenerators(self, name, value): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
141 |
"""Add an individual generator chain for the given header. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
142 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
143 |
@param name: Name of the header to add |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
144 |
@type name: C{str} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
145 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
146 |
@param value: The generator chain |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
147 |
@type value: C{str} |
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 |
self.updateGenerators({name: value}) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
150 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
151 |
def update(self, parsers, generators): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
152 |
"""Conveniently update parsers and generators all at once. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
153 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
154 |
self.updateParsers(parsers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
155 |
self.updateGenerators(generators) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
156 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
157 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
158 |
DefaultHTTPHandler = HeaderHandler() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
159 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
160 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
161 |
## HTTP DateTime parser |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
162 |
def parseDateTime(dateString): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
163 |
"""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
|
164 |
parts = dateString.split() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
165 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
166 |
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
|
167 |
# Weekday is stupid. Might have been omitted. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
168 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
169 |
return parseDateTime("Sun, "+dateString) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
170 |
except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
171 |
# Guess not. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
172 |
pass |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
173 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
174 |
partlen = len(parts) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
175 |
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
|
176 |
# 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
|
177 |
# (Note: "GMT" is literal, not a variable timezone) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
178 |
# (also handles without "GMT") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
179 |
# This is the normal format |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
180 |
day = parts[1] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
181 |
month = parts[2] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
182 |
year = parts[3] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
183 |
time = parts[4] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
184 |
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
|
185 |
# 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
|
186 |
# (Note: "GMT" is literal, not a variable timezone) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
187 |
# (also handles without without "GMT") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
188 |
# Two digit year, yucko. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
189 |
day, month, year = parts[1].split('-') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
190 |
time = parts[2] |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
191 |
year = int(year) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
192 |
if year < 69: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
193 |
year = year + 2000 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
194 |
elif year < 100: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
195 |
year = year + 1900 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
196 |
elif len(parts) == 5: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
197 |
# 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
|
198 |
# ANSI C asctime() format. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
199 |
day = parts[2] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
200 |
month = parts[1] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
201 |
year = parts[4] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
202 |
time = parts[3] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
203 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
204 |
raise ValueError("Unknown datetime format %r" % dateString) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
205 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
206 |
day = int(day) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
207 |
month = int(monthname_lower.index(month.lower())) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
208 |
year = int(year) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
209 |
hour, min, sec = map(int, time.split(':')) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
210 |
return int(timegm((year, month, day, hour, min, sec))) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
211 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
212 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
213 |
##### HTTP tokenizer |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
214 |
class Token(str): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
215 |
__slots__=[] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
216 |
tokens = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
217 |
def __new__(self, char): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
218 |
token = Token.tokens.get(char) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
219 |
if token is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
220 |
Token.tokens[char] = token = str.__new__(self, char) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
221 |
return token |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
222 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
223 |
def __repr__(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
224 |
return "Token(%s)" % str.__repr__(self) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
225 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
226 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
227 |
http_tokens = " \t\"()<>@,;:\\/[]?={}" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
228 |
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
|
229 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
230 |
def tokenize(header, foldCase=True): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
231 |
"""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
|
232 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
233 |
In particular: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
234 |
- 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
|
235 |
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
|
236 |
- Quoted string support including embedded backslashes. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
237 |
- 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
|
238 |
(unless foldCase=False) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
239 |
- Multiple headers are concatenated with ',' |
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 |
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
|
242 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
243 |
Takes a raw header value (list of strings), and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
244 |
Returns a generator of strings and Token class instances. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
245 |
""" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
246 |
tokens = http_tokens |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
247 |
ctls = http_ctls |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
248 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
249 |
string = ",".join(header) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
250 |
list = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
251 |
start = 0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
252 |
cur = 0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
253 |
quoted = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
254 |
qpair = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
255 |
inSpaces = -1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
256 |
qstring = None |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
257 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
258 |
for x in string: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
259 |
if quoted: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
260 |
if qpair: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
261 |
qpair = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
262 |
qstring = qstring+string[start:cur-1]+x |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
263 |
start = cur+1 |
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 |
qpair = True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
266 |
elif x == '"': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
267 |
quoted = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
268 |
yield qstring+string[start:cur] |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
269 |
qstring = None |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
270 |
start = cur+1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
271 |
elif x in tokens: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
272 |
if start != cur: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
273 |
if foldCase: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
274 |
yield string[start:cur].lower() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
275 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
276 |
yield string[start:cur] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
277 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
278 |
start = cur+1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
279 |
if x == '"': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
280 |
quoted = True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
281 |
qstring = "" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
282 |
inSpaces = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
283 |
elif x in " \t": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
284 |
if inSpaces is False: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
285 |
inSpaces = True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
286 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
287 |
inSpaces = -1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
288 |
yield Token(x) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
289 |
elif x in ctls: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
290 |
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
|
291 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
292 |
if inSpaces is True: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
293 |
yield Token(' ') |
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 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
296 |
inSpaces = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
297 |
cur = cur+1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
298 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
299 |
if qpair: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
300 |
raise ValueError, "Missing character after '\\'" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
301 |
if quoted: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
302 |
raise ValueError, "Missing end quote" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
303 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
304 |
if start != cur: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
305 |
if foldCase: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
306 |
yield string[start:cur].lower() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
307 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
308 |
yield string[start:cur] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
309 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
310 |
def split(seq, delim): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
311 |
"""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
|
312 |
Too bad it's not builtin to python!""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
313 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
314 |
cur = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
315 |
for item in seq: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
316 |
if item == delim: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
317 |
yield cur |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
318 |
cur = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
319 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
320 |
cur.append(item) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
321 |
yield cur |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
322 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
323 |
# def find(seq, *args): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
324 |
# """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
|
325 |
# Too bad it's not builtin to python!""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
326 |
# try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
327 |
# return seq.index(value, *args) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
328 |
# except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
329 |
# return -1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
330 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
331 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
332 |
def filterTokens(seq): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
333 |
"""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
|
334 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
335 |
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
|
336 |
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
|
337 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
338 |
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
|
339 |
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
|
340 |
hurt anything, in any case. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
341 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
342 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
343 |
l = [] |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
344 |
for x in seq: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
345 |
if not isinstance(x, Token): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
346 |
l.append(x) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
347 |
return l |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
348 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
349 |
##### parser utilities: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
350 |
def checkSingleToken(tokens): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
351 |
if len(tokens) != 1: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
352 |
raise ValueError, "Expected single token, not %s." % (tokens,) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
353 |
return tokens[0] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
354 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
355 |
def parseKeyValue(val): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
356 |
if len(val) == 1: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
357 |
return val[0], None |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
358 |
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
|
359 |
return val[0], val[2] |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
360 |
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
|
361 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
362 |
def parseArgs(field): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
363 |
args = split(field, Token(';')) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
364 |
val = args.next() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
365 |
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
|
366 |
return val, args |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
367 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
368 |
def listParser(fun): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
369 |
"""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
|
370 |
comma-separated list""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
371 |
def listParserHelper(tokens): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
372 |
fields = split(tokens, Token(',')) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
373 |
for field in fields: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
374 |
if len(field) != 0: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
375 |
yield fun(field) |
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 |
return listParserHelper |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
378 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
379 |
def last(seq): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
380 |
"""Return seq[-1]""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
381 |
return seq[-1] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
382 |
|
9569
ac0bc417b55f
[web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents:
9435
diff
changeset
|
383 |
def unique(seq): |
ac0bc417b55f
[web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents:
9435
diff
changeset
|
384 |
'''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
|
385 |
if isinstance(seq, basestring): |
ac0bc417b55f
[web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents:
9435
diff
changeset
|
386 |
return seq |
ac0bc417b55f
[web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents:
9435
diff
changeset
|
387 |
if len(seq) != 1: |
ac0bc417b55f
[web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents:
9435
diff
changeset
|
388 |
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
|
389 |
return seq[0] |
ac0bc417b55f
[web] Fix handling of some http headers
Julien Cristau <julien.cristau@logilab.fr>
parents:
9435
diff
changeset
|
390 |
|
9571
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
391 |
def parseHTTPMethod(method): |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
392 |
"""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
|
393 |
method = method.strip() |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
394 |
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
|
395 |
"TRACE", "CONNECT"): |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
396 |
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
|
397 |
return method |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
398 |
|
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
399 |
def parseAllowOrigin(origin): |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
400 |
"""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
|
401 |
if origin == 'null': |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
402 |
return origin |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
403 |
p = urlparse.urlparse(origin) |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
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
|
408 |
if not p.netloc: |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
409 |
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
|
410 |
return origin |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
411 |
|
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
412 |
def parseAllowCreds(cred): |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
413 |
"""Can be "true" """ |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
414 |
if cred: |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
415 |
cred = cred.lower() |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
416 |
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
|
417 |
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
|
418 |
return cred |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
419 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
420 |
##### Generation utilities |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
421 |
def quoteString(s): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
422 |
return '"%s"' % s.replace('\\', '\\\\').replace('"', '\\"') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
423 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
424 |
def listGenerator(fun): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
425 |
"""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
|
426 |
the given list, then joins the result with generateList""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
427 |
def listGeneratorHelper(l): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
428 |
return generateList([fun(e) for e in l]) |
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 |
return listGeneratorHelper |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
431 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
432 |
def generateList(seq): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
433 |
return ", ".join(seq) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
434 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
435 |
def singleHeader(item): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
436 |
return [item] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
437 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
438 |
def generateKeyValues(kvs): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
439 |
l = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
440 |
# print kvs |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
441 |
for k, v in kvs: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
442 |
if v is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
443 |
l.append('%s' % k) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
444 |
else: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
445 |
l.append('%s=%s' % (k, v)) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
446 |
return ";".join(l) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
447 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
448 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
449 |
class MimeType(object): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
450 |
def fromString(klass, mimeTypeString): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
451 |
"""Generate a MimeType object from the given string. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
452 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
453 |
@param mimeTypeString: The mimetype to parse |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
454 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
455 |
@return: L{MimeType} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
456 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
457 |
return DefaultHTTPHandler.parse('content-type', [mimeTypeString]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
458 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
459 |
fromString = classmethod(fromString) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
460 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
461 |
def __init__(self, mediaType, mediaSubtype, params={}, **kwargs): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
462 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
463 |
@type mediaType: C{str} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
464 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
465 |
@type mediaSubtype: C{str} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
466 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
467 |
@type params: C{dict} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
468 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
469 |
self.mediaType = mediaType |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
470 |
self.mediaSubtype = mediaSubtype |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
471 |
self.params = dict(params) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
472 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
473 |
if kwargs: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
474 |
self.params.update(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 |
def __eq__(self, other): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
477 |
if not isinstance(other, MimeType): return NotImplemented |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
478 |
return (self.mediaType == other.mediaType and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
479 |
self.mediaSubtype == other.mediaSubtype and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
480 |
self.params == other.params) |
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 |
def __ne__(self, other): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
483 |
return not self.__eq__(other) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
484 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
485 |
def __repr__(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
486 |
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
|
487 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
488 |
def __hash__(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
489 |
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
|
490 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
491 |
##### Specific header parsers. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
492 |
def parseAccept(field): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
493 |
type, args = parseArgs(field) |
5155
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 |
if len(type) != 3 or type[1] != Token('/'): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
496 |
raise ValueError, "MIME Type "+str(type)+" invalid." |
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 |
# 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
|
499 |
# between MIME parameters and (as yet undefined) additional HTTP |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
500 |
# parameters. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
501 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
502 |
num = 0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
503 |
for arg in args: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
504 |
if arg[0] == 'q': |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
505 |
mimeparams = tuple(args[0:num]) |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
506 |
params = args[num:] |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
507 |
break |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
508 |
num = num + 1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
509 |
else: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
510 |
mimeparams = tuple(args) |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
511 |
params = [] |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
512 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
513 |
# Default values for parameters: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
514 |
qval = 1.0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
515 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
516 |
# Parse accept parameters: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
517 |
for param in params: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
518 |
if param[0] == 'q': |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
519 |
qval = float(param[1]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
520 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
521 |
# Warn? ignored parameter. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
522 |
pass |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
523 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
524 |
ret = MimeType(type[0], type[2], mimeparams), qval |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
525 |
return ret |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
526 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
527 |
def parseAcceptQvalue(field): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
528 |
type, args = parseArgs(field) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
529 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
530 |
type = checkSingleToken(type) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
531 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
532 |
qvalue = 1.0 # Default qvalue is 1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
533 |
for arg in args: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
534 |
if arg[0] == 'q': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
535 |
qvalue = float(arg[1]) |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
536 |
return type, qvalue |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
537 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
538 |
def addDefaultCharset(charsets): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
539 |
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
|
540 |
charsets['iso-8859-1'] = 1.0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
541 |
return charsets |
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 |
def addDefaultEncoding(encodings): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
544 |
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
|
545 |
# 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
|
546 |
# "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
|
547 |
encodings['identity'] = .0001 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
548 |
return encodings |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
549 |
|
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 parseContentType(header): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
552 |
# 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
|
553 |
# Content-Type: multipart/form-data; boundary=CaSeFuLsTuFf |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
554 |
# 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
|
555 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
556 |
type, args = parseArgs(header) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
557 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
558 |
if len(type) != 3 or type[1] != Token('/'): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
559 |
raise ValueError, "MIME Type "+str(type)+" invalid." |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
560 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
561 |
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
|
562 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
563 |
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
|
564 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
565 |
def parseContentMD5(header): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
566 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
567 |
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
|
568 |
except Exception as e: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
569 |
raise ValueError(e) |
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 |
def parseContentRange(header): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
572 |
"""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
|
573 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
574 |
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
|
575 |
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
|
576 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
577 |
kind, other = header.strip().split() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
578 |
if kind.lower() != "bytes": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
579 |
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
|
580 |
startend, realLength = other.split("/") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
581 |
if startend.strip() == '*': |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
582 |
start, end = None, None |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
583 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
584 |
start, end = map(int, startend.split("-")) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
585 |
if realLength == "*": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
586 |
realLength = None |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
587 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
588 |
realLength = int(realLength) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
589 |
return (kind, start, end, realLength) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
590 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
591 |
def parseExpect(field): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
592 |
type, args = parseArgs(field) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
593 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
594 |
type = parseKeyValue(type) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
595 |
return (type[0], (lambda *args:args)(type[1], *args)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
596 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
597 |
def parseExpires(header): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
598 |
# """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
|
599 |
# 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
|
600 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
601 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
602 |
return parseDateTime(header) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
603 |
except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
604 |
return 0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
605 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
606 |
def parseIfModifiedSince(header): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
607 |
# 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
|
608 |
# 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
|
609 |
# which is blantantly RFC-violating and not documented anywhere |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
610 |
# except bug-trackers for web frameworks. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
611 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
612 |
# So, we'll just strip off everything after a ';'. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
613 |
return parseDateTime(header.split(';', 1)[0]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
614 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
615 |
def parseIfRange(headers): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
616 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
617 |
return ETag.parse(tokenize(headers)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
618 |
except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
619 |
return parseDateTime(last(headers)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
620 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
621 |
def parseRange(range): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
622 |
range = list(range) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
623 |
if len(range) < 3 or range[1] != Token('='): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
624 |
raise ValueError("Invalid range header format: %s" %(range,)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
625 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
626 |
type = range[0] |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
627 |
if type != 'bytes': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
628 |
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
|
629 |
rangeset = split(range[2:], Token(',')) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
630 |
ranges = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
631 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
632 |
for byterangespec in rangeset: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
633 |
if len(byterangespec) != 1: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
634 |
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
|
635 |
start, end = byterangespec[0].split('-') |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
636 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
637 |
if not start and not end: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
638 |
raise ValueError("Invalid range header format: %s" % (range,)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
639 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
640 |
if start: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
641 |
start = int(start) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
642 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
643 |
start = None |
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 |
if end: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
646 |
end = int(end) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
647 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
648 |
end = None |
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 start and end and start > end: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
651 |
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
|
652 |
ranges.append((start, end)) |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
653 |
return type, ranges |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
654 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
655 |
def parseRetryAfter(header): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
656 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
657 |
# delta seconds |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
658 |
return time.time() + int(header) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
659 |
except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
660 |
# or datetime |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
661 |
return parseDateTime(header) |
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 |
# WWW-Authenticate and Authorization |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
664 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
665 |
def parseWWWAuthenticate(tokenized): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
666 |
headers = [] |
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 |
tokenList = list(tokenized) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
669 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
670 |
while tokenList: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
671 |
scheme = tokenList.pop(0) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
672 |
challenge = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
673 |
last = None |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
674 |
kvChallenge = False |
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 |
while tokenList: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
677 |
token = tokenList.pop(0) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
678 |
if token == Token('='): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
679 |
kvChallenge = True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
680 |
challenge[last] = tokenList.pop(0) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
681 |
last = None |
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 |
elif token == Token(','): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
684 |
if kvChallenge: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
685 |
if len(tokenList) > 1 and tokenList[1] != Token('='): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
686 |
break |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
687 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
688 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
689 |
break |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
690 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
691 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
692 |
last = token |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
693 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
694 |
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
|
695 |
challenge = last |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
696 |
last = None |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
697 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
698 |
headers.append((scheme, challenge)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
699 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
700 |
if last and last not in (Token('='), Token(',')): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
701 |
if headers[-1] == (scheme, challenge): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
702 |
scheme = last |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
703 |
challenge = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
704 |
headers.append((scheme, challenge)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
705 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
706 |
return headers |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
707 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
708 |
def parseAuthorization(header): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
709 |
scheme, rest = header.split(' ', 1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
710 |
# 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
|
711 |
# in the unquoted base64 encoded credentials |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
712 |
return scheme.lower(), rest |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
713 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
714 |
#### Header generators |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
715 |
def generateAccept(accept): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
716 |
mimeType, q = accept |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
717 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
718 |
out ="%s/%s"%(mimeType.mediaType, mimeType.mediaSubtype) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
719 |
if mimeType.params: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
720 |
out+=';'+generateKeyValues(mimeType.params.iteritems()) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
721 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
722 |
if q != 1.0: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
723 |
out+=(';q=%.3f' % (q,)).rstrip('0').rstrip('.') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
724 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
725 |
return out |
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 |
def removeDefaultEncoding(seq): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
728 |
for item in seq: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
729 |
if item[0] != 'identity' or item[1] != .0001: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
730 |
yield item |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
731 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
732 |
def generateAcceptQvalue(keyvalue): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
733 |
if keyvalue[1] == 1.0: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
734 |
return "%s" % keyvalue[0:1] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
735 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
736 |
return ("%s;q=%.3f" % keyvalue).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 |
def parseCacheControl(kv): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
739 |
k, v = parseKeyValue(kv) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
740 |
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
|
741 |
# Required integer argument |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
742 |
if v is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
743 |
v = 0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
744 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
745 |
v = int(v) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
746 |
elif k == 'max-stale': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
747 |
# Optional integer argument |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
748 |
if v is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
749 |
v = int(v) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
750 |
elif k == 'private' or k == 'no-cache': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
751 |
# Optional list argument |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
752 |
if v is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
753 |
v = [field.strip().lower() for field in v.split(',')] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
754 |
return k, v |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
755 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
756 |
def generateCacheControl((k, v)): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
757 |
if v is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
758 |
return str(k) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
759 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
760 |
if k == 'no-cache' or k == 'private': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
761 |
# quoted list of values |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
762 |
v = quoteString(generateList( |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
763 |
[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
|
764 |
return '%s=%s' % (k, v) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
765 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
766 |
def generateContentRange(tup): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
767 |
"""tup is (type, start, end, len) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
768 |
len can be None. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
769 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
770 |
type, start, end, len = tup |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
771 |
if len == None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
772 |
len = '*' |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
773 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
774 |
len = int(len) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
775 |
if start == None and end == None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
776 |
startend = '*' |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
777 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
778 |
startend = '%d-%d' % (start, end) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
779 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
780 |
return '%s %s/%s' % (type, startend, len) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
781 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
782 |
def generateDateTime(secSinceEpoch): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
783 |
"""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
|
784 |
# 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
|
785 |
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
|
786 |
s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
787 |
weekdayname[wd], |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
788 |
day, monthname[month], year, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
789 |
hh, mm, ss) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
790 |
return s |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
791 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
792 |
def generateExpect(item): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
793 |
if item[1][0] is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
794 |
out = '%s' % (item[0],) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
795 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
796 |
out = '%s=%s' % (item[0], item[1][0]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
797 |
if len(item[1]) > 1: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
798 |
out += ';'+generateKeyValues(item[1][1:]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
799 |
return out |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
800 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
801 |
def generateRange(range): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
802 |
def noneOr(s): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
803 |
if s is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
804 |
return '' |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
805 |
return s |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
806 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
807 |
type, ranges = range |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
808 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
809 |
if type != 'bytes': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
810 |
raise ValueError("Unknown range unit: "+type+".") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
811 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
812 |
return (type+'='+ |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
813 |
','.join(['%s-%s' % (noneOr(startend[0]), noneOr(startend[1])) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
814 |
for startend in ranges])) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
815 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
816 |
def generateRetryAfter(when): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
817 |
# always generate delta seconds format |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
818 |
return str(int(when - time.time())) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
819 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
820 |
def generateContentType(mimeType): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
821 |
out = "%s/%s" % (mimeType.mediaType, mimeType.mediaSubtype) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
822 |
if mimeType.params: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
823 |
out += ';' + generateKeyValues(mimeType.params.iteritems()) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
824 |
return out |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
825 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
826 |
def generateIfRange(dateOrETag): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
827 |
if isinstance(dateOrETag, ETag): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
828 |
return dateOrETag.generate() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
829 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
830 |
return generateDateTime(dateOrETag) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
831 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
832 |
# WWW-Authenticate and Authorization |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
833 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
834 |
def generateWWWAuthenticate(headers): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
835 |
_generated = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
836 |
for seq in headers: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
837 |
scheme, challenge = seq[0], seq[1] |
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 |
# 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
|
840 |
# 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
|
841 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
842 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
843 |
l = [] |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
844 |
for k, v in dict(challenge).iteritems(): |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
845 |
l.append("%s=%s" % (k, quoteString(v))) |
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 |
_generated.append("%s %s" % (scheme, ", ".join(l))) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
848 |
except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
849 |
_generated.append("%s %s" % (scheme, challenge)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
850 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
851 |
return _generated |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
852 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
853 |
def generateAuthorization(seq): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
854 |
return [' '.join(seq)] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
855 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
856 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
857 |
#### |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
858 |
class ETag(object): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
859 |
def __init__(self, tag, weak=False): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
860 |
self.tag = str(tag) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
861 |
self.weak = weak |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
862 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
863 |
def match(self, other, strongCompare): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
864 |
# Sec 13.3. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
865 |
# 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
|
866 |
# 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
|
867 |
# |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
868 |
# 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
|
869 |
# 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
|
870 |
# 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
|
871 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
872 |
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
|
873 |
return False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
874 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
875 |
if strongCompare and (other.weak or self.weak): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
876 |
return False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
877 |
return True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
878 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
879 |
def __eq__(self, other): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
880 |
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
|
881 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
882 |
def __ne__(self, other): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
883 |
return not self.__eq__(other) |
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 |
def __repr__(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
886 |
return "Etag(%r, weak=%r)" % (self.tag, self.weak) |
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 |
def parse(tokens): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
889 |
tokens = tuple(tokens) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
890 |
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
|
891 |
return ETag(tokens[0]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
892 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
893 |
if(len(tokens) == 3 and tokens[0] == "w" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
894 |
and tokens[1] == Token('/')): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
895 |
return ETag(tokens[2], weak=True) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
896 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
897 |
raise ValueError("Invalid ETag.") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
898 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
899 |
parse = staticmethod(parse) |
5155
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 generate(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
902 |
if self.weak: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
903 |
return 'W/'+quoteString(self.tag) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
904 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
905 |
return quoteString(self.tag) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
906 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
907 |
def parseStarOrETag(tokens): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
908 |
tokens = tuple(tokens) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
909 |
if tokens == ('*',): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
910 |
return '*' |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
911 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
912 |
return ETag.parse(tokens) |
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 generateStarOrETag(etag): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
915 |
if etag == '*': |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
916 |
return etag |
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 etag.generate() |
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 |
#### Cookies. Blech! |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
921 |
class Cookie(object): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
922 |
# __slots__ = ['name', 'value', 'path', 'domain', 'ports', 'expires', 'discard', 'secure', 'comment', 'commenturl', 'version'] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
923 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
924 |
def __init__(self, name, value, path=None, domain=None, ports=None, expires=None, discard=False, secure=False, 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
|
925 |
self.name = name |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
926 |
self.value = value |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
927 |
self.path = path |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
928 |
self.domain = domain |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
929 |
self.ports = ports |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
930 |
self.expires = expires |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
931 |
self.discard = discard |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
932 |
self.secure = secure |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
933 |
self.comment = comment |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
934 |
self.commenturl = commenturl |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
935 |
self.version = version |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
936 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
937 |
def __repr__(self): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
938 |
s = "Cookie(%r=%r" % (self.name, self.value) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
939 |
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
|
940 |
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
|
941 |
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
|
942 |
if self.expires is not None: s+=", expires=%r" % (self.expires,) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
943 |
if self.secure is not False: s+=", secure=%r" % (self.secure,) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
944 |
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
|
945 |
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
|
946 |
if self.version != 0: s+=", version=%r" % (self.version,) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
947 |
s+=")" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
948 |
return s |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
949 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
950 |
def __eq__(self, other): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
951 |
return (isinstance(other, Cookie) and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
952 |
other.path == self.path and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
953 |
other.domain == self.domain and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
954 |
other.ports == self.ports and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
955 |
other.expires == self.expires and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
956 |
other.secure == self.secure and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
957 |
other.comment == self.comment and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
958 |
other.commenturl == self.commenturl and |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
959 |
other.version == self.version) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
960 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
961 |
def __ne__(self, other): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
962 |
return not self.__eq__(other) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
963 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
964 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
965 |
def parseCookie(headers): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
966 |
"""Bleargh, the cookie spec sucks. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
967 |
This surely needs interoperability testing. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
968 |
There are two specs that are supported: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
969 |
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
|
970 |
Version 1) http://www.faqs.org/rfcs/rfc2965.html |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
971 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
972 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
973 |
cookies = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
974 |
# 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
|
975 |
# 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
|
976 |
# Neither new RFC2965 cookies nor old netscape cookies are. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
977 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
978 |
header = ';'.join(headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
979 |
if header[0:8].lower() == "$version": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
980 |
# RFC2965 cookie |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
981 |
h = tokenize([header], foldCase=False) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
982 |
r_cookies = split(h, Token(',')) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
983 |
for r_cookie in r_cookies: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
984 |
last_cookie = None |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
985 |
rr_cookies = split(r_cookie, Token(';')) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
986 |
for cookie in rr_cookies: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
987 |
nameval = tuple(split(cookie, Token('='))) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
988 |
if len(nameval) == 2: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
989 |
(name,), (value,) = nameval |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
990 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
991 |
(name,), = nameval |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
992 |
value = None |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
993 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
994 |
name = name.lower() |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
995 |
if name == '$version': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
996 |
continue |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
997 |
if name[0] == '$': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
998 |
if last_cookie is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
999 |
if name == '$path': |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1000 |
last_cookie.path = value |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1001 |
elif name == '$domain': |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1002 |
last_cookie.domain = value |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1003 |
elif name == '$port': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1004 |
if value is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1005 |
last_cookie.ports = () |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1006 |
else: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1007 |
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
|
1008 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1009 |
last_cookie = Cookie(name, value, version=1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1010 |
cookies.append(last_cookie) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1011 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1012 |
# 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
|
1013 |
# 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
|
1014 |
# characters are valid for values except ';'. Spaces are stripped, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1015 |
# however. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1016 |
r_cookies = header.split(';') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1017 |
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
|
1018 |
name, value = r_cookie.split('=', 1) |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1019 |
name = name.strip(' \t') |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1020 |
value = value.strip(' \t') |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1021 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1022 |
cookies.append(Cookie(name, value)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1023 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1024 |
return cookies |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1025 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1026 |
cookie_validname = "[^"+re.escape(http_tokens+http_ctls)+"]*$" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1027 |
cookie_validname_re = re.compile(cookie_validname) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1028 |
cookie_validvalue = cookie_validname+'|"([^"]|\\\\")*"$' |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1029 |
cookie_validvalue_re = re.compile(cookie_validvalue) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1030 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1031 |
def generateCookie(cookies): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1032 |
# 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
|
1033 |
# 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
|
1034 |
# 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
|
1035 |
# 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
|
1036 |
# V1. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1037 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1038 |
# I choose to send them all as V1. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1039 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1040 |
# 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
|
1041 |
# 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
|
1042 |
# 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
|
1043 |
# 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
|
1044 |
# (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
|
1045 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1046 |
# 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
|
1047 |
# 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
|
1048 |
# 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
|
1049 |
# (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
|
1050 |
# syntax in a newstyle cookie. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1051 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1052 |
# 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
|
1053 |
# 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
|
1054 |
# are just dicarded during conversion. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1055 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1056 |
# 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
|
1057 |
# 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
|
1058 |
# newstyle cookie parsers. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1059 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1060 |
# 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
|
1061 |
# 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
|
1062 |
# 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
|
1063 |
# 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
|
1064 |
# webapps support RFC2965 V1 cookies. I suspect not many. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1065 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1066 |
max_version = max([cookie.version for cookie in cookies]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1067 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1068 |
if max_version == 0: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1069 |
# no quoting or anything. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1070 |
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
|
1071 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1072 |
str_cookies = ['$Version="1"'] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1073 |
for cookie in cookies: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1074 |
if cookie.version == 0: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1075 |
# 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
|
1076 |
# V1 syntax. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1077 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1078 |
# 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
|
1079 |
# 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
|
1080 |
# on input. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1081 |
# 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
|
1082 |
# 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
|
1083 |
# the other side. |
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 |
if cookie_validname_re.match(cookie.name) is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1086 |
continue |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1087 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1088 |
value = cookie.value |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1089 |
if cookie_validvalue_re.match(cookie.value) is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1090 |
value = quoteString(value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1091 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1092 |
str_cookies.append("%s=%s" % (cookie.name, value)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1093 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1094 |
# V1 cookie, nice and easy |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1095 |
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
|
1096 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1097 |
if cookie.path: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1098 |
str_cookies.append("$Path=%s" % quoteString(cookie.path)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1099 |
if cookie.domain: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1100 |
str_cookies.append("$Domain=%s" % quoteString(cookie.domain)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1101 |
if cookie.ports is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1102 |
if len(cookie.ports) == 0: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1103 |
str_cookies.append("$Port") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1104 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1105 |
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
|
1106 |
return ';'.join(str_cookies) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1107 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1108 |
def parseSetCookie(headers): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1109 |
setCookies = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1110 |
for header in headers: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1111 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1112 |
parts = header.split(';') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1113 |
l = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1114 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1115 |
for part in parts: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1116 |
namevalue = part.split('=',1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1117 |
if len(namevalue) == 1: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1118 |
name = namevalue[0] |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1119 |
value = None |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1120 |
else: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1121 |
name, value = namevalue |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1122 |
value = value.strip(' \t') |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1123 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1124 |
name = name.strip(' \t') |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1125 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1126 |
l.append((name, value)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1127 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1128 |
setCookies.append(makeCookieFromList(l, True)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1129 |
except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1130 |
# 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
|
1131 |
# but not the rest of Set-Cookies. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1132 |
pass |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1133 |
return setCookies |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1134 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1135 |
def parseSetCookie2(toks): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1136 |
outCookies = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1137 |
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
|
1138 |
for y in split(toks, Token(','))]: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1139 |
try: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1140 |
outCookies.append(makeCookieFromList(cookie, False)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1141 |
except ValueError: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1142 |
# 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
|
1143 |
pass |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1144 |
return outCookies |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1145 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1146 |
def makeCookieFromList(tup, netscapeFormat): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1147 |
name, value = tup[0] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1148 |
if name is None or value is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1149 |
raise ValueError("Cookie has missing name or value") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1150 |
if name.startswith("$"): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1151 |
raise ValueError("Invalid cookie name: %r, starts with '$'." % name) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1152 |
cookie = Cookie(name, value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1153 |
hadMaxAge = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1154 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1155 |
for name, value in tup[1:]: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1156 |
name = name.lower() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1157 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1158 |
if value is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1159 |
if name in ("discard", "secure"): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1160 |
# Boolean attrs |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1161 |
value = True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1162 |
elif name != "port": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1163 |
# Can be either boolean or explicit |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1164 |
continue |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1165 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1166 |
if name in ("comment", "commenturl", "discard", "domain", "path", "secure"): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1167 |
# simple cases |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1168 |
setattr(cookie, name, value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1169 |
elif name == "expires" and not hadMaxAge: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1170 |
if netscapeFormat and value[0] == '"' and value[-1] == '"': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1171 |
value = value[1:-1] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1172 |
cookie.expires = parseDateTime(value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1173 |
elif name == "max-age": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1174 |
hadMaxAge = True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1175 |
cookie.expires = int(value) + time.time() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1176 |
elif name == "port": |
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 |
cookie.ports = () |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1179 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1180 |
if netscapeFormat and value[0] == '"' and value[-1] == '"': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1181 |
value = value[1:-1] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1182 |
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
|
1183 |
elif name == "version": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1184 |
cookie.version = int(value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1185 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1186 |
return cookie |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1187 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1188 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1189 |
def generateSetCookie(cookies): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1190 |
setCookies = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1191 |
for cookie in cookies: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1192 |
out = ["%s=%s" % (cookie.name, cookie.value)] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1193 |
if cookie.expires: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1194 |
out.append("expires=%s" % generateDateTime(cookie.expires)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1195 |
if cookie.path: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1196 |
out.append("path=%s" % cookie.path) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1197 |
if cookie.domain: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1198 |
out.append("domain=%s" % cookie.domain) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1199 |
if cookie.secure: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1200 |
out.append("secure") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1201 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1202 |
setCookies.append('; '.join(out)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1203 |
return setCookies |
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 |
def generateSetCookie2(cookies): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1206 |
setCookies = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1207 |
for cookie in cookies: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1208 |
out = ["%s=%s" % (cookie.name, quoteString(cookie.value))] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1209 |
if cookie.comment: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1210 |
out.append("Comment=%s" % quoteString(cookie.comment)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1211 |
if cookie.commenturl: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1212 |
out.append("CommentURL=%s" % quoteString(cookie.commenturl)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1213 |
if cookie.discard: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1214 |
out.append("Discard") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1215 |
if cookie.domain: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1216 |
out.append("Domain=%s" % quoteString(cookie.domain)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1217 |
if cookie.expires: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1218 |
out.append("Max-Age=%s" % (cookie.expires - time.time())) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1219 |
if cookie.path: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1220 |
out.append("Path=%s" % quoteString(cookie.path)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1221 |
if cookie.ports is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1222 |
if len(cookie.ports) == 0: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1223 |
out.append("Port") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1224 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1225 |
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
|
1226 |
if cookie.secure: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1227 |
out.append("Secure") |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1228 |
out.append('Version="1"') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1229 |
setCookies.append('; '.join(out)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1230 |
return setCookies |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1231 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1232 |
def parseDepth(depth): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1233 |
if depth not in ("0", "1", "infinity"): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1234 |
raise ValueError("Invalid depth header value: %s" % (depth,)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1235 |
return depth |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1236 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1237 |
def parseOverWrite(overwrite): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1238 |
if overwrite == "F": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1239 |
return False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1240 |
elif overwrite == "T": |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1241 |
return True |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1242 |
raise ValueError("Invalid overwrite header value: %s" % (overwrite,)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1243 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1244 |
def generateOverWrite(overwrite): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1245 |
if overwrite: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1246 |
return "T" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1247 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1248 |
return "F" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1249 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1250 |
##### Random stuff that looks useful. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1251 |
# def sortMimeQuality(s): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1252 |
# def sorter(item1, item2): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1253 |
# if item1[0] == '*': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1254 |
# if item2[0] == '*': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1255 |
# return 0 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1256 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1257 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1258 |
# def sortQuality(s): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1259 |
# def sorter(item1, item2): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1260 |
# if item1[1] < item2[1]: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1261 |
# return -1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1262 |
# if item1[1] < item2[1]: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1263 |
# return 1 |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1264 |
# if item1[0] == item2[0]: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1265 |
# return 0 |
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 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1268 |
# def getMimeQuality(mimeType, accepts): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1269 |
# type, args = parseArgs(mimeType) |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1270 |
# type = type.split(Token('/')) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1271 |
# if len(type) != 2: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1272 |
# raise ValueError, "MIME Type "+s+" invalid." |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1273 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1274 |
# for accept in accepts: |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1275 |
# accept, acceptQual = accept |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1276 |
# acceptType = accept[0:1] |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1277 |
# acceptArgs = accept[2] |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1278 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1279 |
# 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
|
1280 |
# (args == acceptArgs or len(acceptArgs) == 0)): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1281 |
# return acceptQual |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1282 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1283 |
# def getQuality(type, accepts): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1284 |
# qual = accepts.get(type) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1285 |
# if qual is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1286 |
# return qual |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1287 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1288 |
# return accepts.get('*') |
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 |
# Headers object |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1291 |
class __RecalcNeeded(object): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1292 |
def __repr__(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1293 |
return "<RecalcNeeded>" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1294 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1295 |
_RecalcNeeded = __RecalcNeeded() |
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 |
class Headers(object): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1298 |
"""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
|
1299 |
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
|
1300 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1301 |
def __init__(self, headers=None, rawHeaders=None, handler=DefaultHTTPHandler): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1302 |
self._raw_headers = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1303 |
self._headers = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1304 |
self.handler = handler |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1305 |
if headers is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1306 |
for key, value in headers.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1307 |
self.setHeader(key, value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1308 |
if rawHeaders is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1309 |
for key, value in rawHeaders.iteritems(): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1310 |
self.setRawHeaders(key, value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1311 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1312 |
def _setRawHeaders(self, headers): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1313 |
self._raw_headers = headers |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1314 |
self._headers = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1315 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1316 |
def _toParsed(self, name): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1317 |
r = self._raw_headers.get(name, None) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1318 |
h = self.handler.parse(name, r) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1319 |
if h is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1320 |
self._headers[name] = h |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1321 |
return h |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1322 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1323 |
def _toRaw(self, name): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1324 |
h = self._headers.get(name, None) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1325 |
r = self.handler.generate(name, h) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1326 |
if r is not None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1327 |
self._raw_headers[name] = r |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1328 |
return r |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1329 |
|
8313
386b6313de28
http-header: support __contains__ in Headers
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8081
diff
changeset
|
1330 |
def __contains__(self, name): |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1331 |
"""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
|
1332 |
return name.lower() in self._raw_headers |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1333 |
|
8313
386b6313de28
http-header: support __contains__ in Headers
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8081
diff
changeset
|
1334 |
hasHeader = __contains__ |
386b6313de28
http-header: support __contains__ in Headers
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8081
diff
changeset
|
1335 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1336 |
def getRawHeaders(self, name, default=None): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1337 |
"""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
|
1338 |
|
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1339 |
name = name.lower() |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1340 |
raw_header = self._raw_headers.get(name, default) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1341 |
if raw_header is not _RecalcNeeded: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1342 |
return raw_header |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1343 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1344 |
return self._toRaw(name) |
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 getHeader(self, name, default=None): |
9435
dde8ea5d7ffd
[web] Fix typo in docstring
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8697
diff
changeset
|
1347 |
"""Returns the parsed representation of the given header. |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1348 |
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
|
1349 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1350 |
If no parser for the header exists, raise ValueError. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1351 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1352 |
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
|
1353 |
""" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1354 |
name = name.lower() |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1355 |
parsed = self._headers.get(name, default) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1356 |
if parsed is not _RecalcNeeded: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1357 |
return parsed |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1358 |
return self._toParsed(name) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1359 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1360 |
def setRawHeaders(self, name, value): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1361 |
"""Sets the raw representation of the given header. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1362 |
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
|
1363 |
given name. |
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 |
self._raw_headers[name] = value |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1367 |
self._headers[name] = _RecalcNeeded |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1368 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1369 |
def setHeader(self, name, value): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1370 |
"""Sets the parsed representation of the given header. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1371 |
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
|
1372 |
on the header in question. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1373 |
""" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1374 |
name = name.lower() |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1375 |
self._raw_headers[name] = _RecalcNeeded |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1376 |
self._headers[name] = value |
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 |
def addRawHeader(self, name, value): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1379 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1380 |
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
|
1381 |
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
|
1382 |
replace anything. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1383 |
""" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1384 |
name = name.lower() |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1385 |
raw_header = self._raw_headers.get(name) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1386 |
if raw_header is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1387 |
# No header yet |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1388 |
raw_header = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1389 |
self._raw_headers[name] = raw_header |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1390 |
elif raw_header is _RecalcNeeded: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1391 |
raw_header = self._toRaw(name) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1392 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1393 |
raw_header.append(value) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1394 |
self._headers[name] = _RecalcNeeded |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1395 |
|
7855
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1396 |
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
|
1397 |
""" |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1398 |
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
|
1399 |
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
|
1400 |
replace anything. |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1401 |
""" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1402 |
name = name.lower() |
7855
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1403 |
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
|
1404 |
if header is None: |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1405 |
# No header yet |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1406 |
header = [] |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1407 |
self._headers[name] = header |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1408 |
elif header is _RecalcNeeded: |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1409 |
header = self._toParsed(name) |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1410 |
header.append(value) |
54283a5b7afc
[web request]Â fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
1411 |
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
|
1412 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1413 |
def removeHeader(self, name): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1414 |
"""Removes the header named.""" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1415 |
name = name.lower() |
8697
574bb05e40a4
[toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8696
diff
changeset
|
1416 |
if name in self._raw_headers: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1417 |
del self._raw_headers[name] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1418 |
del self._headers[name] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1419 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1420 |
def __repr__(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1421 |
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
|
1422 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1423 |
def canonicalNameCaps(self, name): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1424 |
"""Return the name with the canonical capitalization, if known, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1425 |
otherwise, Caps-After-Dashes""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1426 |
return header_case_mapping.get(name) or dashCapitalize(name) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1427 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1428 |
def getAllRawHeaders(self): |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1429 |
"""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
|
1430 |
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
|
1431 |
in canonical capitalization.""" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1432 |
for k, v in self._raw_headers.iteritems(): |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1433 |
if v is _RecalcNeeded: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1434 |
v = self._toRaw(k) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1435 |
yield self.canonicalNameCaps(k), v |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1436 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1437 |
def makeImmutable(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1438 |
"""Make this header set immutable. All mutating operations will |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1439 |
raise an exception.""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1440 |
self.setHeader = self.setRawHeaders = self.removeHeader = self._mutateRaise |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1441 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1442 |
def _mutateRaise(self, *args): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1443 |
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
|
1444 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1445 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1446 |
"""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
|
1447 |
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
|
1448 |
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
|
1449 |
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
|
1450 |
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
|
1451 |
is strictly an error, but we're nice.). |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1452 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1453 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1454 |
iteritems = lambda x: x.iteritems() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1455 |
|
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 |
parser_general_headers = { |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1458 |
'Cache-Control': (tokenize, listParser(parseCacheControl), dict), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1459 |
'Connection': (tokenize, filterTokens), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1460 |
'Date': (last, parseDateTime), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1461 |
# 'Pragma':tokenize |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1462 |
# 'Trailer':tokenize |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1463 |
'Transfer-Encoding': (tokenize, filterTokens), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1464 |
# 'Upgrade':tokenize |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1465 |
# 'Via':tokenize, stripComment |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1466 |
# 'Warning':tokenize |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1467 |
} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1468 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1469 |
generator_general_headers = { |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1470 |
'Cache-Control': (iteritems, listGenerator(generateCacheControl), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1471 |
'Connection': (generateList, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1472 |
'Date': (generateDateTime, singleHeader), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1473 |
# 'Pragma': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1474 |
# 'Trailer': |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1475 |
'Transfer-Encoding': (generateList, singleHeader), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1476 |
# 'Upgrade': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1477 |
# 'Via': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1478 |
# 'Warning': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1479 |
} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1480 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1481 |
parser_request_headers = { |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1482 |
'Accept': (tokenize, listParser(parseAccept), dict), |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1483 |
'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
|
1484 |
'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
|
1485 |
'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
|
1486 |
'Access-Control-Allow-Origin': (last, parseAllowOrigin,), |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
1487 |
'Access-Control-Allow-Credentials': (last, parseAllowCreds,), |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
1488 |
'Access-Control-Allow-Methods': (tokenize, listParser(parseHTTPMethod), list), |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
1489 |
'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
|
1490 |
'Access-Control-Request-Headers': (filterTokens, ), |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
1491 |
'Access-Control-Expose-Headers': (filterTokens, ), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1492 |
'Authorization': (last, parseAuthorization), |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1493 |
'Cookie': (parseCookie,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1494 |
'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
|
1495 |
'Origin': (last,), |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1496 |
'From': (last,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1497 |
'Host': (last,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1498 |
'If-Match': (tokenize, listParser(parseStarOrETag), list), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1499 |
'If-Modified-Since': (last, parseIfModifiedSince), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1500 |
'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
|
1501 |
'If-Range': (parseIfRange,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1502 |
'If-Unmodified-Since': (last, parseDateTime), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1503 |
'Max-Forwards': (last, int), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1504 |
# '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
|
1505 |
'Range': (tokenize, parseRange), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1506 |
'Referer': (last, str), # TODO: URI object? |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1507 |
'TE': (tokenize, listParser(parseAcceptQvalue), dict), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1508 |
'User-Agent': (last, str), |
5155
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 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1511 |
generator_request_headers = { |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1512 |
'Accept': (iteritems, listGenerator(generateAccept), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1513 |
'Accept-Charset': (iteritems, listGenerator(generateAcceptQvalue), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1514 |
'Accept-Encoding': (iteritems, removeDefaultEncoding, |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1515 |
listGenerator(generateAcceptQvalue), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1516 |
'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
|
1517 |
'Access-Control-Request-Method': (unique, str, singleHeader, ), |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
1518 |
'Access-Control-Expose-Headers': (listGenerator(str), ), |
aaf83cc07eed
[web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
9569
diff
changeset
|
1519 |
'Access-Control-Allow-Headers': (listGenerator(str), ), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1520 |
'Authorization': (generateAuthorization,), # what is "credentials" |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1521 |
'Cookie': (generateCookie, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1522 |
'Expect': (iteritems, listGenerator(generateExpect), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1523 |
'From': (unique, str, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1524 |
'Host': (unique, str, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1525 |
'If-Match': (listGenerator(generateStarOrETag), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1526 |
'If-Modified-Since': (generateDateTime, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1527 |
'If-None-Match': (listGenerator(generateStarOrETag), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1528 |
'If-Range': (generateIfRange, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1529 |
'If-Unmodified-Since': (generateDateTime, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1530 |
'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
|
1531 |
'Origin': (unique, str, singleHeader), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1532 |
# '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
|
1533 |
'Range': (generateRange, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1534 |
'Referer': (unique, str, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1535 |
'TE': (iteritems, listGenerator(generateAcceptQvalue), singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1536 |
'User-Agent': (unique, str, singleHeader), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1537 |
} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1538 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1539 |
parser_response_headers = { |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1540 |
'Accept-Ranges': (tokenize, filterTokens), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1541 |
'Age': (last, int), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1542 |
'ETag': (tokenize, ETag.parse), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1543 |
'Location': (last,), # TODO: URI object? |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1544 |
# 'Proxy-Authenticate' |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1545 |
'Retry-After': (last, parseRetryAfter), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1546 |
'Server': (last,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1547 |
'Set-Cookie': (parseSetCookie,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1548 |
'Set-Cookie2': (tokenize, parseSetCookie2), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1549 |
'Vary': (tokenize, filterTokens), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1550 |
'WWW-Authenticate': (lambda h: tokenize(h, foldCase=False), |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1551 |
parseWWWAuthenticate,) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1552 |
} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1553 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1554 |
generator_response_headers = { |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1555 |
'Accept-Ranges': (generateList, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1556 |
'Age': (unique, str, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1557 |
'ETag': (ETag.generate, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1558 |
'Location': (unique, str, singleHeader), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1559 |
# 'Proxy-Authenticate' |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1560 |
'Retry-After': (generateRetryAfter, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1561 |
'Server': (unique, str, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1562 |
'Set-Cookie': (generateSetCookie,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1563 |
'Set-Cookie2': (generateSetCookie2,), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1564 |
'Vary': (generateList, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1565 |
'WWW-Authenticate': (generateWWWAuthenticate,) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1566 |
} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1567 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1568 |
parser_entity_headers = { |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1569 |
'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
|
1570 |
'Content-Encoding': (tokenize, filterTokens), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1571 |
'Content-Language': (tokenize, filterTokens), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1572 |
'Content-Length': (last, int), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1573 |
'Content-Location': (last,), # TODO: URI object? |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1574 |
'Content-MD5': (last, parseContentMD5), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1575 |
'Content-Range': (last, parseContentRange), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1576 |
'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
|
1577 |
'Expires': (last, parseExpires), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1578 |
'Last-Modified': (last, parseDateTime), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1579 |
} |
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 |
generator_entity_headers = { |
9572
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1582 |
'Allow': (generateList, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1583 |
'Content-Encoding': (generateList, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1584 |
'Content-Language': (generateList, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1585 |
'Content-Length': (unique, str, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1586 |
'Content-Location': (unique, str, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1587 |
'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
|
1588 |
'Content-Range': (generateContentRange, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1589 |
'Content-Type': (generateContentType, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1590 |
'Expires': (generateDateTime, singleHeader), |
73b2410bdadc
[web] whitespace cleanup in http_headers.py
David Douard <david.douard@logilab.fr>
parents:
9571
diff
changeset
|
1591 |
'Last-Modified': (generateDateTime, singleHeader), |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1592 |
} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1593 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1594 |
DefaultHTTPHandler.updateParsers(parser_general_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1595 |
DefaultHTTPHandler.updateParsers(parser_request_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1596 |
DefaultHTTPHandler.updateParsers(parser_response_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1597 |
DefaultHTTPHandler.updateParsers(parser_entity_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1598 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1599 |
DefaultHTTPHandler.updateGenerators(generator_general_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1600 |
DefaultHTTPHandler.updateGenerators(generator_request_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1601 |
DefaultHTTPHandler.updateGenerators(generator_response_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1602 |
DefaultHTTPHandler.updateGenerators(generator_entity_headers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1603 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1604 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1605 |
# casemappingify(DefaultHTTPParsers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1606 |
# casemappingify(DefaultHTTPGenerators) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1607 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1608 |
# lowerify(DefaultHTTPParsers) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
diff
changeset
|
1609 |
# lowerify(DefaultHTTPGenerators) |