E5Network/E5RFC6266.py

Fri, 13 Nov 2015 22:52:26 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Fri, 13 Nov 2015 22:52:26 +0100
changeset 4555
861e1741985c
parent 4543
2e6a880670e9
child 4631
5c1a96925da4
permissions
-rw-r--r--

Adjustments to future imports for Python 2 compatibility.

4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de>
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a Content-Disposition parser iaw. RFC 6266.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 #
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # This code is adapted from the rfc6266.py module of qutebrowser.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # Original copyright 2014-2015 Florian Bruhin (The Compiler)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 # <mail@qutebrowser.org>
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 #
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from __future__ import unicode_literals
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 try: # Py3
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 import urllib.parse as parse
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 except (ImportError):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 import urlparse as parse # __IGNORE_WARNING__
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 import collections
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 import string
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 import re
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 try:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 import pypeg2 as peg
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 class UniqueNamespace(peg.Namespace):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 A pyPEG2 namespace which prevents setting a value twice.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 def __setitem__(self, key, value):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 Special method to set an item.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @param key key for the item
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 @param value value of the item
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 if key in self:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 raise DuplicateParamError(key)
4555
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4543
diff changeset
42 super(UniqueNamespace, self).__setitem__(key, value)
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 # RFC 2616
4543
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
45 separator_chars = "()<>@,;:\\\"/[]?={} \t" # __IGNORE_WARNING__
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 ctl_chars = ''.join(chr(i) for i in range(32)) + chr(127)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 nontoken_chars = separator_chars + ctl_chars
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 # RFC 5987
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 attr_chars_nonalnum = '!#$&+-.^_`|~'
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 attr_chars = string.ascii_letters + string.digits + attr_chars_nonalnum
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 # RFC 5987 gives this alternative construction of the token character class
4543
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
54 token_chars = attr_chars + "*'%" # __IGNORE_WARNING__
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 # Definitions from https://tools.ietf.org/html/rfc2616#section-2.2
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 # token was redefined from attr_chars to avoid using AnyBut,
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 # which might include non-ascii octets.
4543
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
59 token_re = '[{0}]+'.format(re.escape(token_chars))
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 class Token(str):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 A token (RFC 2616, Section 2.2).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 grammar = re.compile(token_re)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 # RFC 2616 says some linear whitespace (LWS) is in fact allowed in text
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 # and qdtext; however it also mentions folding that whitespace into
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 # a single SP (which isn't in CTL) before interpretation.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 # Assume the caller already that folding when parsing headers.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 # Note: qdtext also allows non-ascii, which we choose to parse
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 # as ISO-8859-1; rejecting it entirely would also be permitted.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 # Some broken browsers attempt encoding-sniffing, which is broken
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 # because the spec only allows iso, and because encoding-sniffing
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 # can mangle valid values.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 # Everything else in this grammar (including RFC 5987 ext values)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 # is in an ascii-safe encoding.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
4543
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
80 qdtext_re = r'[^"{0}]'.format(re.escape(ctl_chars))
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
81 quoted_pair_re = r'\\[{0}]'.format(re.escape(
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 ''.join(chr(i) for i in range(128))))
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 class QuotedString(str):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 A quoted string (RFC 2616, Section 2.2).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 """
4543
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
88 grammar = re.compile(r'"({0}|{1})+"'.format(quoted_pair_re, qdtext_re))
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 def __str__(self):
4555
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4543
diff changeset
91 s = super(QuotedString, self).__str__()
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 s = s[1:-1] # remove quotes
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 s = re.sub(r'\\(.)', r'\1', s) # drop backslashes
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 return s
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 class Value(str):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 A value. (RFC 2616, Section 3.6).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 grammar = [re.compile(token_re), QuotedString]
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 class Charset(str):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 A charset (RFC5987, Section 3.2.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 # Other charsets are forbidden, the spec reserves them
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 # for future evolutions.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 grammar = re.compile('UTF-8|ISO-8859-1', re.I)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 class Language(str):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 A language-tag (RFC 5646, Section 2.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 Fixme: This grammar is not 100% correct yet.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 https://github.com/The-Compiler/qutebrowser/issues/105
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 grammar = re.compile('[A-Za-z0-9-]+')
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
4543
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
119 attr_char_re = '[{0}]'.format(re.escape(attr_chars))
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 hex_digit_re = '%[' + string.hexdigits + ']{2}'
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 class ValueChars(str):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 A value of an attribute.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 Fixme: Can we merge this with Value?
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 https://github.com/The-Compiler/qutebrowser/issues/105
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 """
4543
2e6a880670e9 Fixed a few code style issues (forgotten future imports, copyrights,...).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4318
diff changeset
129 grammar = re.compile('({0}|{1})*'.format(attr_char_re, hex_digit_re))
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 class ExtValue(peg.List):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 An ext-value of an attribute (RFC 5987, Section 3.2).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 grammar = peg.contiguous(Charset, "'", peg.optional(Language), "'",
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 ValueChars)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 class ExtToken(peg.Symbol):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 A token introducing an extended value (RFC 6266, Section 4.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 regex = re.compile(token_re + r'\*')
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 def __str__(self):
4555
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4543
diff changeset
145 return super(ExtToken, self).__str__().lower()
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 class NoExtToken(peg.Symbol):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 A token introducing a normal value (RFC 6266, Section 4.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 regex = re.compile(token_re + r'(?<!\*)')
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 def __str__(self):
4555
861e1741985c Adjustments to future imports for Python 2 compatibility.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4543
diff changeset
154 return super(NoExtToken, self).__str__().lower()
4318
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 class DispositionParm(str):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 A parameter for the Disposition-Type header (RFC6266, Section 4.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 grammar = peg.attr('name', NoExtToken), '=', Value
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 class ExtDispositionParm:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 An extended parameter (RFC6266, Section 4.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 grammar = peg.attr('name', ExtToken), '=', ExtValue
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 def __init__(self, value, name=None):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 self.name = name
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 self.value = value
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 class DispositionType(peg.List):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 The disposition type (RFC6266, Section 4.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 grammar = [re.compile('(inline|attachment)', re.I), Token]
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 class DispositionParmList(UniqueNamespace):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 A list of disposition parameters (RFC6266, Section 4.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 grammar = peg.maybe_some(';', [ExtDispositionParm, DispositionParm])
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 class ContentDispositionValue:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 A complete Content-Disposition value (RFC 6266, Section 4.1).
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 # Allows nonconformant final semicolon
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 # I've seen it in the wild, and browsers accept it
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 # http://greenbytes.de/tech/tc2231/#attwithasciifilenamenqs
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 grammar = (peg.attr('dtype', DispositionType),
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 peg.attr('params', DispositionParmList),
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 peg.optional(';'))
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 LangTagged = collections.namedtuple('LangTagged', ['string', 'langtag'])
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 class DuplicateParamError(Exception):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 Exception raised when a parameter has been given twice.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 class InvalidISO8859Error(Exception):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 Exception raised when a byte is invalid in ISO-8859-1.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 class ContentDisposition:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 Records various indications and hints about content disposition.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 These can be used to know if a file should be downloaded or
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 displayed directly, and to hint what filename it should have
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 in the download case.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 def __init__(self, disposition='inline', assocs=None):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 Used internally after parsing the header.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 Instances should generally be created from a factory
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 function, such as parse_headers and its variants.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 if len(disposition) != 1:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 self.disposition = 'inline'
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 else:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 self.disposition = disposition[0]
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 if assocs is None:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 self.assocs = {}
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 else:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 self.assocs = dict(assocs) # So we can change values
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 if 'filename*' in self.assocs:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 param = self.assocs['filename*']
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 assert isinstance(param, ExtDispositionParm)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 self.assocs['filename*'] = \
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 parse_ext_value(param.value).string
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 def filename(self):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 The filename from the Content-Disposition header or None.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 On safety:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 This property records the intent of the sender.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 You shouldn't use this sender-controlled value as a filesystem
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 path, it can be insecure. Serving files with this filename can be
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 dangerous as well, due to a certain browser using the part after
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 the dot for mime-sniffing. Saving it to a database is fine by
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 itself though.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 if 'filename*' in self.assocs:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 return self.assocs['filename*']
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 elif 'filename' in self.assocs:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 # XXX Reject non-ascii (parsed via qdtext) here?
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 return self.assocs['filename']
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 def is_inline(self):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 Return if the file should be handled inline.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 If not, and unless your application supports other dispositions
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 than the standard inline and attachment, it should be handled
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 as an attachment.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 return self.disposition.lower() == 'inline'
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 def normalize_ws(text):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 Do LWS (linear whitespace) folding.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 return ' '.join(text.split())
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 def parse_headers(content_disposition):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 Build a ContentDisposition from header values.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 @param content_disposition contents of the disposition header
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 @type bytes
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 # We allow non-ascii here (it will only be parsed inside of qdtext, and
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 # rejected by the grammar if it appears in other places), although
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 # parsing it can be ambiguous. Parsing it ensures that a non-ambiguous
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 # filename* value won't get dismissed because of an unrelated ambiguity
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 # in the filename parameter. But it does mean we occasionally give
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 # less-than-certain values for some legacy senders.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 content_disposition = content_disposition.decode('iso-8859-1')
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 # Our parsing is relaxed in these regards:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 # - The grammar allows a final ';' in the header;
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 # - We do LWS-folding, and possibly normalise other broken
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 # whitespace, instead of rejecting non-lws-safe text.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 # XXX Would prefer to accept only the quoted whitespace
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 # case, rather than normalising everything.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 content_disposition = normalize_ws(content_disposition)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 try:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 parsed = peg.parse(content_disposition, ContentDispositionValue)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 except (SyntaxError, DuplicateParamError, InvalidISO8859Error):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 return ContentDisposition()
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 else:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 return ContentDisposition(disposition=parsed.dtype,
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 assocs=parsed.params)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 def parse_ext_value(val):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 Parse the value of an extended attribute.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 if len(val) == 3:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 charset, langtag, coded = val
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 else:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 charset, coded = val
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 langtag = None
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 decoded = parse.unquote(coded, charset, errors='strict')
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 if charset == 'iso-8859-1':
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 # Fail if the filename contains an invalid ISO-8859-1 char
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 for c in decoded:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 if 0x7F <= ord(c) <= 0x9F:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 raise InvalidISO8859Error(c)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 return LangTagged(decoded, langtag)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 except ImportError:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 class ContentDisposition:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 Records various indications and hints about content disposition.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 These can be used to know if a file should be downloaded or
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 displayed directly, and to hint what filename it should have
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 in the download case.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 def __init__(self, filename):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 Constructor
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 @param filename file name to be stored in this surrogate class
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 @type str
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 self.__filename = filename
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 def filename(self):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 Public method to get the stored file name
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 @return file name
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 @rtype str
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 return self.__filename
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 def parse_headers(content_disposition):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 Build a ContentDisposition from header values.
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 @param content_disposition contents of the disposition header
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 @type bytes
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 """
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 header = content_disposition.decode()
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 if header:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 pos = header.find("filename=")
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 if pos != -1:
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 path = header[pos + 9:]
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 if path.startswith('"') and path.endswith('"'):
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 path = path[1:-1]
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 return ContentDisposition(path)
c2f374ca452b Improved parsing of the content disposition header analog to the way it is done in qutebrowser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 return ContentDisposition("")

eric ide

mercurial