Mercurial > cpython-lru_cache-weakref
annotate Lib/urlparse.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | a43481210964 |
| children | ff0426b5d75e |
| rev | line source |
|---|---|
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
10830
diff
changeset
|
1 """Parse (absolute and relative) URLs. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
10830
diff
changeset
|
2 |
|
60655
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
3 urlparse module is based upon the following RFC specifications. |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
4 |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
5 RFC 3986 (STD66): "Uniform Resource Identifiers" by T. Berners-Lee, R. Fielding |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
6 and L. Masinter, January 2005. |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
7 |
|
60654
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
8 RFC 2732 : "Format for Literal IPv6 Addresses in URL's by R.Hinden, B.Carpenter |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
9 and L.Masinter, December 1999. |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
10 |
|
61544
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
11 RFC 2396: "Uniform Resource Identifiers (URI)": Generic Syntax by T. |
|
60655
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
12 Berners-Lee, R. Fielding, and L. Masinter, August 1998. |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
13 |
|
61544
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
14 RFC 2368: "The mailto URL scheme", by P.Hoffman , L Masinter, J. Zwinski, July 1998. |
|
60655
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
15 |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
16 RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, UC Irvine, June |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
17 1995. |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
18 |
|
61544
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
19 RFC 1738: "Uniform Resource Locators (URL)" by T. Berners-Lee, L. Masinter, M. |
|
60655
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
20 McCahill, December 1994 |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
21 |
|
61544
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
22 RFC 3986 is considered the current standard and any future changes to |
|
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
23 urlparse module should conform with it. The urlparse module is |
|
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
24 currently not entirely compliant with this RFC due to defacto |
|
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
25 scenarios for parsing, and for backward compatibility purposes, some |
|
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
26 parsing quirks from older RFCs are retained. The testcases in |
|
60655
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
27 test_urlparse.py provides a good indicator of parsing behavior. |
|
2938bf899863
Merged revisions 80146 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60072
diff
changeset
|
28 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
10830
diff
changeset
|
29 """ |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
25814
b8cbdb735cf2
Added missing entries to __all__.
Fred Drake <fdrake@acm.org>
parents:
25787
diff
changeset
|
31 __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag", |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
32 "urlsplit", "urlunsplit", "parse_qs", "parse_qsl"] |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
33 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 # A classification of schemes ('' means apply by default) |
| 32004 | 35 uses_relative = ['ftp', 'http', 'gopher', 'nntp', 'imap', |
|
36339
76aba3fa4e2f
Bug #1407902: Added support for sftp:// URIs to urlparse.
Georg Brandl <georg@python.org>
parents:
35289
diff
changeset
|
36 'wais', 'file', 'https', 'shttp', 'mms', |
|
71729
5278aa2d9d9a
Fix closes issue12683 - urljoin to work with relative join of svn scheme.
Senthil Kumaran <senthil@uthcode.com>
parents:
69365
diff
changeset
|
37 'prospero', 'rtsp', 'rtspu', '', 'sftp', |
|
71731
aa9342093199
include svn+ssh as well.
Senthil Kumaran <senthil@uthcode.com>
parents:
71729
diff
changeset
|
38 'svn', 'svn+ssh'] |
| 32004 | 39 uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet', |
|
36339
76aba3fa4e2f
Bug #1407902: Added support for sftp:// URIs to urlparse.
Georg Brandl <georg@python.org>
parents:
35289
diff
changeset
|
40 'imap', 'wais', 'file', 'mms', 'https', 'shttp', |
|
76aba3fa4e2f
Bug #1407902: Added support for sftp:// URIs to urlparse.
Georg Brandl <georg@python.org>
parents:
35289
diff
changeset
|
41 'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '', |
|
61544
2c29cd5668c7
Merged revisions 81130 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
60839
diff
changeset
|
42 'svn', 'svn+ssh', 'sftp','nfs','git', 'git+ssh'] |
| 32004 | 43 uses_params = ['ftp', 'hdl', 'prospero', 'http', 'imap', |
|
37368
05914e8ded73
add support for the sips: scheme (identical to sip: except for scheme name)
Fred Drake <fdrake@acm.org>
parents:
36339
diff
changeset
|
44 'https', 'shttp', 'rtsp', 'rtspu', 'sip', 'sips', |
|
36339
76aba3fa4e2f
Bug #1407902: Added support for sftp:// URIs to urlparse.
Georg Brandl <georg@python.org>
parents:
35289
diff
changeset
|
45 'mms', '', 'sftp'] |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 |
|
78736
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
47 # These are not actually used anymore, but should stay for backwards |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
48 # compatibility. (They are undocumented, but have a public-looking name.) |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
49 non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
50 'telnet', 'wais', 'imap', 'snews', 'sip', 'sips'] |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
51 uses_query = ['http', 'wais', 'imap', 'https', 'shttp', 'mms', |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
52 'gopher', 'rtsp', 'rtspu', 'sip', 'sips', ''] |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
53 uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news', |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
54 'nntp', 'wais', 'https', 'shttp', 'snews', |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
55 'file', 'prospero', ''] |
|
a43481210964
Closes #9374: add back now-unused module attributes; removing them is a backward compatibility issue, since they have a public-seeming name.
Georg Brandl <georg@python.org>
parents:
77858
diff
changeset
|
56 |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 # Characters valid in scheme names |
|
15623
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
58 scheme_chars = ('abcdefghijklmnopqrstuvwxyz' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
59 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
60 '0123456789' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
61 '+-.') |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
5785
b247297be9a0
After some discussion with Jeremy and Fred, decided to limit the
Guido van Rossum <guido@python.org>
parents:
5776
diff
changeset
|
63 MAX_CACHE_SIZE = 20 |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
64 _parse_cache = {} |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
65 |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
66 def clear_cache(): |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
67 """Clear the parse cache.""" |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
68 _parse_cache.clear() |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
69 |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
70 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
71 class ResultMixin(object): |
|
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
72 """Shared methods for the parsed result objects.""" |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
73 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
74 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
75 def username(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
76 netloc = self.netloc |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
77 if "@" in netloc: |
|
44483
2e2fa291914c
Patch #1698 by Senthil: allow '@' in username when parsed by urlparse.py.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
78 userinfo = netloc.rsplit("@", 1)[0] |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
79 if ":" in userinfo: |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
80 userinfo = userinfo.split(":", 1)[0] |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
81 return userinfo |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
82 return None |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
83 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
84 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
85 def password(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
86 netloc = self.netloc |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
87 if "@" in netloc: |
|
44483
2e2fa291914c
Patch #1698 by Senthil: allow '@' in username when parsed by urlparse.py.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
88 userinfo = netloc.rsplit("@", 1)[0] |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
89 if ":" in userinfo: |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
90 return userinfo.split(":", 1)[1] |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
91 return None |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
92 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
93 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
94 def hostname(self): |
|
60613
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
95 netloc = self.netloc.split('@')[-1] |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
96 if '[' in netloc and ']' in netloc: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
97 return netloc.split(']')[0][1:].lower() |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
98 elif ':' in netloc: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
99 return netloc.split(':')[0].lower() |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
100 elif netloc == '': |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
101 return None |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
102 else: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
103 return netloc.lower() |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
104 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
105 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
106 def port(self): |
|
60613
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
107 netloc = self.netloc.split('@')[-1].split(']')[-1] |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
108 if ':' in netloc: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
109 port = netloc.split(':')[1] |
|
77119
988903cf24c5
Issue #14036: return None when port in urlparse cross 65535
Senthil Kumaran <senthil@uthcode.com>
parents:
77051
diff
changeset
|
110 port = int(port, 10) |
|
988903cf24c5
Issue #14036: return None when port in urlparse cross 65535
Senthil Kumaran <senthil@uthcode.com>
parents:
77051
diff
changeset
|
111 # verify legal port |
|
988903cf24c5
Issue #14036: return None when port in urlparse cross 65535
Senthil Kumaran <senthil@uthcode.com>
parents:
77051
diff
changeset
|
112 if (0 <= port <= 65535): |
|
988903cf24c5
Issue #14036: return None when port in urlparse cross 65535
Senthil Kumaran <senthil@uthcode.com>
parents:
77051
diff
changeset
|
113 return port |
|
988903cf24c5
Issue #14036: return None when port in urlparse cross 65535
Senthil Kumaran <senthil@uthcode.com>
parents:
77051
diff
changeset
|
114 return None |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
115 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
116 from collections import namedtuple |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
117 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
118 class SplitResult(namedtuple('SplitResult', 'scheme netloc path query fragment'), ResultMixin): |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
119 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
120 __slots__ = () |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
121 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
122 def geturl(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
123 return urlunsplit(self) |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
124 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
125 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
126 class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin): |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
127 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
128 __slots__ = () |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
129 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
130 def geturl(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
131 return urlunparse(self) |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
132 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
133 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
134 def urlparse(url, scheme='', allow_fragments=True): |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
135 """Parse a URL into 6 components: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
136 <scheme>://<netloc>/<path>;<params>?<query>#<fragment> |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
137 Return a 6-tuple: (scheme, netloc, path, params, query, fragment). |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
138 Note that we don't break the components up in smaller bits |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
139 (e.g. netloc is a single string) and we don't expand % escapes.""" |
|
77858
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
140 tuple = urlsplit(url, scheme, allow_fragments) |
|
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
141 scheme, netloc, url, query, fragment = tuple |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
142 if scheme in uses_params and ';' in url: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
143 url, params = _splitparams(url) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
144 else: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
145 params = '' |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
146 return ParseResult(scheme, netloc, url, params, query, fragment) |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
147 |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
148 def _splitparams(url): |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
149 if '/' in url: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
150 i = url.find(';', url.rfind('/')) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
151 if i < 0: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
152 return url, '' |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
153 else: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
154 i = url.find(';') |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
155 return url[:i], url[i+1:] |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
156 |
|
34378
a0bc4419663c
Patch #712317: In URLs such as http://www.example.com?query=spam, treat '?' as
Johannes Gijsbers <jlg@dds.nl>
parents:
34293
diff
changeset
|
157 def _splitnetloc(url, start=0): |
|
44516
32681ae6c5c1
Forgot to backport the rest of #1637.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
158 delim = len(url) # position of end of domain part of url, default is end |
|
32681ae6c5c1
Forgot to backport the rest of #1637.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
159 for c in '/?#': # look for delimiters; the order is NOT important |
|
32681ae6c5c1
Forgot to backport the rest of #1637.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
160 wdelim = url.find(c, start) # find first of this delim |
|
32681ae6c5c1
Forgot to backport the rest of #1637.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
161 if wdelim >= 0: # if found |
|
32681ae6c5c1
Forgot to backport the rest of #1637.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
162 delim = min(delim, wdelim) # use earliest delim position |
|
32681ae6c5c1
Forgot to backport the rest of #1637.
Guido van Rossum <guido@python.org>
parents:
44284
diff
changeset
|
163 return url[start:delim], url[delim:] # return (domain, rest) |
|
34378
a0bc4419663c
Patch #712317: In URLs such as http://www.example.com?query=spam, treat '?' as
Johannes Gijsbers <jlg@dds.nl>
parents:
34293
diff
changeset
|
164 |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
165 def urlsplit(url, scheme='', allow_fragments=True): |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
166 """Parse a URL into 5 components: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
167 <scheme>://<netloc>/<path>?<query>#<fragment> |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
168 Return a 5-tuple: (scheme, netloc, path, query, fragment). |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
169 Note that we don't break the components up in smaller bits |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
170 (e.g. netloc is a single string) and we don't expand % escapes.""" |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
171 allow_fragments = bool(allow_fragments) |
|
44284
6ae94a2ba5df
Backport r59480.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
37393
diff
changeset
|
172 key = url, scheme, allow_fragments, type(url), type(scheme) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
173 cached = _parse_cache.get(key, None) |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
174 if cached: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
175 return cached |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
176 if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
177 clear_cache() |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
178 netloc = query = fragment = '' |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
179 i = url.find(':') |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
180 if i > 0: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
181 if url[:i] == 'http': # optimize the common case |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
182 scheme = url[:i].lower() |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
183 url = url[i+1:] |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
184 if url[:2] == '//': |
|
34378
a0bc4419663c
Patch #712317: In URLs such as http://www.example.com?query=spam, treat '?' as
Johannes Gijsbers <jlg@dds.nl>
parents:
34293
diff
changeset
|
185 netloc, url = _splitnetloc(url, 2) |
|
60857
9dc864ef6425
Changed tests to only urlparse one, which was enough, addressed Ezio's comment
Senthil Kumaran <orsenthil@gmail.com>
parents:
60783
diff
changeset
|
186 if (('[' in netloc and ']' not in netloc) or |
|
9dc864ef6425
Changed tests to only urlparse one, which was enough, addressed Ezio's comment
Senthil Kumaran <orsenthil@gmail.com>
parents:
60783
diff
changeset
|
187 (']' in netloc and '[' not in netloc)): |
|
9dc864ef6425
Changed tests to only urlparse one, which was enough, addressed Ezio's comment
Senthil Kumaran <orsenthil@gmail.com>
parents:
60783
diff
changeset
|
188 raise ValueError("Invalid IPv6 URL") |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
189 if allow_fragments and '#' in url: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
190 url, fragment = url.split('#', 1) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
191 if '?' in url: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
192 url, query = url.split('?', 1) |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
193 v = SplitResult(scheme, netloc, url, query, fragment) |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
194 _parse_cache[key] = v |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
195 return v |
|
69365
7a693e283c68
Issue #11467: Fix urlparse behavior when handling urls which contains scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
68256
diff
changeset
|
196 for c in url[:i]: |
|
7a693e283c68
Issue #11467: Fix urlparse behavior when handling urls which contains scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
68256
diff
changeset
|
197 if c not in scheme_chars: |
|
7a693e283c68
Issue #11467: Fix urlparse behavior when handling urls which contains scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
68256
diff
changeset
|
198 break |
|
7a693e283c68
Issue #11467: Fix urlparse behavior when handling urls which contains scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
68256
diff
changeset
|
199 else: |
|
77051
ff0fd7b26219
#14072: Fix parsing of tel URIs in urlparse by making the check for ports stricter.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77044
diff
changeset
|
200 # make sure "url" is not actually a port number (in which case |
|
ff0fd7b26219
#14072: Fix parsing of tel URIs in urlparse by making the check for ports stricter.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77044
diff
changeset
|
201 # "scheme" is really part of the path) |
|
ff0fd7b26219
#14072: Fix parsing of tel URIs in urlparse by making the check for ports stricter.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77044
diff
changeset
|
202 rest = url[i+1:] |
|
ff0fd7b26219
#14072: Fix parsing of tel URIs in urlparse by making the check for ports stricter.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77044
diff
changeset
|
203 if not rest or any(c not in '0123456789' for c in rest): |
|
ff0fd7b26219
#14072: Fix parsing of tel URIs in urlparse by making the check for ports stricter.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77044
diff
changeset
|
204 # not a port number |
|
ff0fd7b26219
#14072: Fix parsing of tel URIs in urlparse by making the check for ports stricter.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77044
diff
changeset
|
205 scheme, url = url[:i].lower(), rest |
|
58951
c485e55aaf96
Merged revisions 78234 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
48995
diff
changeset
|
206 |
|
c485e55aaf96
Merged revisions 78234 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
48995
diff
changeset
|
207 if url[:2] == '//': |
|
34378
a0bc4419663c
Patch #712317: In URLs such as http://www.example.com?query=spam, treat '?' as
Johannes Gijsbers <jlg@dds.nl>
parents:
34293
diff
changeset
|
208 netloc, url = _splitnetloc(url, 2) |
|
60857
9dc864ef6425
Changed tests to only urlparse one, which was enough, addressed Ezio's comment
Senthil Kumaran <orsenthil@gmail.com>
parents:
60783
diff
changeset
|
209 if (('[' in netloc and ']' not in netloc) or |
|
9dc864ef6425
Changed tests to only urlparse one, which was enough, addressed Ezio's comment
Senthil Kumaran <orsenthil@gmail.com>
parents:
60783
diff
changeset
|
210 (']' in netloc and '[' not in netloc)): |
|
9dc864ef6425
Changed tests to only urlparse one, which was enough, addressed Ezio's comment
Senthil Kumaran <orsenthil@gmail.com>
parents:
60783
diff
changeset
|
211 raise ValueError("Invalid IPv6 URL") |
|
77044
79e6ff3d9afd
Issue9374 - Generic parsing of query and fragment portion of urls for any scheme
Senthil Kumaran <senthil@uthcode.com>
parents:
71731
diff
changeset
|
212 if allow_fragments and '#' in url: |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
213 url, fragment = url.split('#', 1) |
|
77044
79e6ff3d9afd
Issue9374 - Generic parsing of query and fragment portion of urls for any scheme
Senthil Kumaran <senthil@uthcode.com>
parents:
71731
diff
changeset
|
214 if '?' in url: |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
215 url, query = url.split('?', 1) |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
216 v = SplitResult(scheme, netloc, url, query, fragment) |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
217 _parse_cache[key] = v |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
218 return v |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 |
|
48140
cc43b998f5b5
Silence some SyntaxWarnings for tuple unpacking in a parameter list for
Brett Cannon <bcannon@gmail.com>
parents:
44847
diff
changeset
|
220 def urlunparse(data): |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
221 """Put a parsed URL back together again. This may result in a |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
222 slightly different, but equivalent URL, if the URL that was parsed |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
223 originally had redundant delimiters, e.g. a ? with an empty query |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
224 (the draft states that these are equivalent).""" |
|
48140
cc43b998f5b5
Silence some SyntaxWarnings for tuple unpacking in a parameter list for
Brett Cannon <bcannon@gmail.com>
parents:
44847
diff
changeset
|
225 scheme, netloc, url, params, query, fragment = data |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
226 if params: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
227 url = "%s;%s" % (url, params) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
228 return urlunsplit((scheme, netloc, url, query, fragment)) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
229 |
|
48140
cc43b998f5b5
Silence some SyntaxWarnings for tuple unpacking in a parameter list for
Brett Cannon <bcannon@gmail.com>
parents:
44847
diff
changeset
|
230 def urlunsplit(data): |
|
62592
2003b70a9bad
Merged revisions 82324 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
61544
diff
changeset
|
231 """Combine the elements of a tuple as returned by urlsplit() into a |
|
2003b70a9bad
Merged revisions 82324 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
61544
diff
changeset
|
232 complete URL as a string. The data argument can be any five-item iterable. |
|
2003b70a9bad
Merged revisions 82324 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
61544
diff
changeset
|
233 This may result in a slightly different, but equivalent URL, if the URL that |
|
2003b70a9bad
Merged revisions 82324 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
61544
diff
changeset
|
234 was parsed originally had unnecessary delimiters (for example, a ? with an |
|
2003b70a9bad
Merged revisions 82324 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
61544
diff
changeset
|
235 empty query; the RFC states that these are equivalent).""" |
|
48140
cc43b998f5b5
Silence some SyntaxWarnings for tuple unpacking in a parameter list for
Brett Cannon <bcannon@gmail.com>
parents:
44847
diff
changeset
|
236 scheme, netloc, url, query, fragment = data |
|
25787
bf384111b3b3
Backporting urlparse.py:1.34, test_urlparse.py:1.8:
Guido van Rossum <guido@python.org>
parents:
25425
diff
changeset
|
237 if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'): |
|
77858
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
238 if url and url[:1] != '/': url = '/' + url |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
239 url = '//' + (netloc or '') + url |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
240 if scheme: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
241 url = scheme + ':' + url |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
242 if query: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
243 url = url + '?' + query |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
244 if fragment: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
245 url = url + '#' + fragment |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
246 return url |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
248 def urljoin(base, url, allow_fragments=True): |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
249 """Join a base URL and a possibly relative URL to form an absolute |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
250 interpretation of the latter.""" |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
251 if not base: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
252 return url |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
253 if not url: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
254 return base |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
255 bscheme, bnetloc, bpath, bparams, bquery, bfragment = \ |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
256 urlparse(base, '', allow_fragments) |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
257 scheme, netloc, path, params, query, fragment = \ |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
258 urlparse(url, bscheme, allow_fragments) |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
259 if scheme != bscheme or scheme not in uses_relative: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
260 return url |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
261 if scheme in uses_netloc: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
262 if netloc: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
263 return urlunparse((scheme, netloc, path, |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
264 params, query, fragment)) |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
265 netloc = bnetloc |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
266 if path[:1] == '/': |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
267 return urlunparse((scheme, netloc, path, |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
268 params, query, fragment)) |
|
66984
69b3a151aec5
Merged revisions 87329 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63888
diff
changeset
|
269 if not path and not params: |
|
48233
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
270 path = bpath |
|
66984
69b3a151aec5
Merged revisions 87329 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63888
diff
changeset
|
271 params = bparams |
|
48233
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
272 if not query: |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
273 query = bquery |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
274 return urlunparse((scheme, netloc, path, |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
275 params, query, fragment)) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
276 segments = bpath.split('/')[:-1] + path.split('/') |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
277 # XXX The stuff below is bogus in various ways... |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
278 if segments[-1] == '.': |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
279 segments[-1] = '' |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
280 while '.' in segments: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
281 segments.remove('.') |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
282 while 1: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
283 i = 1 |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
284 n = len(segments) - 1 |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
285 while i < n: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
286 if (segments[i] == '..' |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
287 and segments[i-1] not in ('', '..')): |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
288 del segments[i-1:i+1] |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
289 break |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
290 i = i+1 |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
291 else: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
292 break |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
293 if segments == ['', '..']: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
294 segments[-1] = '' |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
295 elif len(segments) >= 2 and segments[-1] == '..': |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
296 segments[-2:] = [''] |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
297 return urlunparse((scheme, netloc, '/'.join(segments), |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
298 params, query, fragment)) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
300 def urldefrag(url): |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
301 """Removes any existing fragment from URL. |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
302 |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
303 Returns a tuple of the defragmented URL and the fragment. If |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
304 the URL contained no fragments, the second element is the |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
305 empty string. |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
306 """ |
|
20995
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
307 if '#' in url: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
308 s, n, p, a, q, frag = urlparse(url) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
309 defrag = urlunparse((s, n, p, a, q, '')) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
310 return defrag, frag |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
311 else: |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
312 return url, '' |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
313 |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
314 # unquote method for parse_qs and parse_qsl |
|
61901
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
315 # Cannot use directly from urllib as it would create a circular reference |
|
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
316 # because urllib uses urlparse methods (urljoin). If you update this function, |
|
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
317 # update it also in urllib. This code duplication does not existin in Python3. |
|
60072
c3c82d8040d6
Merged revisions 79047 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
58951
diff
changeset
|
318 |
|
c3c82d8040d6
Merged revisions 79047 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
58951
diff
changeset
|
319 _hexdig = '0123456789ABCDEFabcdef' |
|
77858
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
320 _hextochr = dict((a+b, chr(int(a+b,16))) |
|
61901
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
321 for a in _hexdig for b in _hexdig) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
323 def unquote(s): |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
324 """unquote('abc%20def') -> 'abc def'.""" |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
325 res = s.split('%') |
|
61901
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
326 # fastpath |
|
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
327 if len(res) == 1: |
|
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
328 return s |
|
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
329 s = res[0] |
|
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
330 for item in res[1:]: |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
331 try: |
|
61901
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
332 s += _hextochr[item[:2]] + item[2:] |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
333 except KeyError: |
|
61901
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
334 s += '%' + item |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
335 except UnicodeDecodeError: |
|
61901
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
336 s += unichr(int(item[:2], 16)) + item[2:] |
|
2297cd57d945
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
R. David Murray <rdmurray@bitdance.com>
parents:
61543
diff
changeset
|
337 return s |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
338 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
339 def parse_qs(qs, keep_blank_values=0, strict_parsing=0): |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
340 """Parse a query given as a string argument. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
341 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
342 Arguments: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
343 |
|
63888
a56d8a80b883
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63714
diff
changeset
|
344 qs: percent-encoded query string to be parsed |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
345 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
346 keep_blank_values: flag indicating whether blank values in |
|
63888
a56d8a80b883
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63714
diff
changeset
|
347 percent-encoded queries should be treated as blank strings. |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
348 A true value indicates that blanks should be retained as |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
349 blank strings. The default false value indicates that |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
350 blank values are to be ignored and treated as if they were |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
351 not included. |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
353 strict_parsing: flag indicating what to do with parsing errors. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
354 If false (the default), errors are silently ignored. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
355 If true, errors raise a ValueError exception. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
356 """ |
|
77858
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
357 dict = {} |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
358 for name, value in parse_qsl(qs, keep_blank_values, strict_parsing): |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
359 if name in dict: |
|
77858
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
360 dict[name].append(value) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
361 else: |
|
77858
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
362 dict[name] = [value] |
|
264892c47681
Backed out changeset ee51e3aef302 - it broke the test suite
Antoine Pitrou <solipsis@pitrou.net>
parents:
77855
diff
changeset
|
363 return dict |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
364 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
365 def parse_qsl(qs, keep_blank_values=0, strict_parsing=0): |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
366 """Parse a query given as a string argument. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
367 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
368 Arguments: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
369 |
|
63888
a56d8a80b883
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63714
diff
changeset
|
370 qs: percent-encoded query string to be parsed |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
371 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
372 keep_blank_values: flag indicating whether blank values in |
|
63888
a56d8a80b883
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63714
diff
changeset
|
373 percent-encoded queries should be treated as blank strings. A |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
374 true value indicates that blanks should be retained as blank |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
375 strings. The default false value indicates that blank values |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
376 are to be ignored and treated as if they were not included. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
377 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
378 strict_parsing: flag indicating what to do with parsing errors. If |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
379 false (the default), errors are silently ignored. If true, |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
380 errors raise a ValueError exception. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
381 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
382 Returns a list, as G-d intended. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
383 """ |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
384 pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')] |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
385 r = [] |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
386 for name_value in pairs: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
387 if not name_value and not strict_parsing: |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
388 continue |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
389 nv = name_value.split('=', 1) |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
390 if len(nv) != 2: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
391 if strict_parsing: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
392 raise ValueError, "bad query field: %r" % (name_value,) |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
393 # Handle case of a control-name with no equal sign |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
394 if keep_blank_values: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
395 nv.append('') |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
396 else: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
397 continue |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
398 if len(nv[1]) or keep_blank_values: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
399 name = unquote(nv[0].replace('+', ' ')) |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
400 value = unquote(nv[1].replace('+', ' ')) |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
401 r.append((name, value)) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
403 return r |
