Mercurial > cpython-lru_cache-weakref
annotate Lib/urlparse.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | a56d8a80b883 |
| children | 69b3a151aec5 |
| 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 |
|
60654
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
3 urlparse module is based upon the following RFC specifications. |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
4 |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
5 RFC 3986 (STD66): "Uniform Resource Identifiers" by T. Berners-Lee, R. Fielding |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
6 and L. Masinter, January 2005. |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
7 |
|
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 |
|
61108
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
11 RFC 2396: "Uniform Resource Identifiers (URI)": Generic Syntax by T. |
|
60654
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
12 Berners-Lee, R. Fielding, and L. Masinter, August 1998. |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
13 |
|
61108
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
14 RFC 2368: "The mailto URL scheme", by P.Hoffman , L Masinter, J. Zwinski, July 1998. |
|
60654
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
15 |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
16 RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, UC Irvine, June |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
17 1995. |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
18 |
|
61108
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
19 RFC 1738: "Uniform Resource Locators (URL)" by T. Berners-Lee, L. Masinter, M. |
|
60654
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
20 McCahill, December 1994 |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
21 |
|
61108
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
22 RFC 3986 is considered the current standard and any future changes to |
|
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
23 urlparse module should conform with it. The urlparse module is |
|
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
24 currently not entirely compliant with this RFC due to defacto |
|
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
25 scenarios for parsing, and for backward compatibility purposes, some |
|
2d2a858fe3e1
Always add space after RFC; reword paragraph
Andrew M. Kuchling <amk@amk.ca>
parents:
60857
diff
changeset
|
26 parsing quirks from older RFCs are retained. The testcases in |
|
60654
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
diff
changeset
|
27 test_urlparse.py provides a good indicator of parsing behavior. |
|
7b0656929cdb
Fix Issue5650 - Update the RFC List in the urlparse module.
Senthil Kumaran <orsenthil@gmail.com>
parents:
60613
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', |
|
76aba3fa4e2f
Bug #1407902: Added support for sftp:// URIs to urlparse.
Georg Brandl <georg@python.org>
parents:
35289
diff
changeset
|
37 'prospero', 'rtsp', 'rtspu', '', 'sftp'] |
| 32004 | 38 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
|
39 '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
|
40 'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '', |
|
61543
06c6d3a67c17
Fix Issue8657 - adding git and git+ssh as know schemes.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61108
diff
changeset
|
41 'svn', 'svn+ssh', 'sftp','nfs','git', 'git+ssh'] |
| 32004 | 42 non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', |
|
37368
05914e8ded73
add support for the sips: scheme (identical to sip: except for scheme name)
Fred Drake <fdrake@acm.org>
parents:
36339
diff
changeset
|
43 'telnet', 'wais', 'imap', 'snews', 'sip', 'sips'] |
| 32004 | 44 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
|
45 '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
|
46 'mms', '', 'sftp'] |
| 32004 | 47 uses_query = ['http', 'wais', 'imap', 'https', 'shttp', 'mms', |
|
37368
05914e8ded73
add support for the sips: scheme (identical to sip: except for scheme name)
Fred Drake <fdrake@acm.org>
parents:
36339
diff
changeset
|
48 'gopher', 'rtsp', 'rtspu', 'sip', 'sips', ''] |
| 32004 | 49 uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news', |
|
36339
76aba3fa4e2f
Bug #1407902: Added support for sftp:// URIs to urlparse.
Georg Brandl <georg@python.org>
parents:
35289
diff
changeset
|
50 'nntp', 'wais', 'https', 'shttp', 'snews', |
|
76aba3fa4e2f
Bug #1407902: Added support for sftp:// URIs to urlparse.
Georg Brandl <georg@python.org>
parents:
35289
diff
changeset
|
51 'file', 'prospero', ''] |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 # 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
|
54 scheme_chars = ('abcdefghijklmnopqrstuvwxyz' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
55 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
56 '0123456789' |
|
db69c092c906
Be explicit about scheme_chars -- string.letters is locale dependent
Guido van Rossum <guido@python.org>
parents:
12967
diff
changeset
|
57 '+-.') |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
5785
b247297be9a0
After some discussion with Jeremy and Fred, decided to limit the
Guido van Rossum <guido@python.org>
parents:
5776
diff
changeset
|
59 MAX_CACHE_SIZE = 20 |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
60 _parse_cache = {} |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
61 |
|
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
62 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
|
63 """Clear the parse cache.""" |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
64 _parse_cache.clear() |
|
3652
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 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
67 class ResultMixin(object): |
|
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
68 """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
|
69 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
70 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
71 def username(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
72 netloc = self.netloc |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
73 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
|
74 userinfo = netloc.rsplit("@", 1)[0] |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
75 if ":" in userinfo: |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
76 userinfo = userinfo.split(":", 1)[0] |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
77 return userinfo |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
78 return None |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
79 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
80 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
81 def password(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
82 netloc = self.netloc |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
83 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
|
84 userinfo = netloc.rsplit("@", 1)[0] |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
85 if ":" in userinfo: |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
86 return userinfo.split(":", 1)[1] |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
87 return None |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
88 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
89 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
90 def hostname(self): |
|
60613
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
91 netloc = self.netloc.split('@')[-1] |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
92 if '[' in netloc and ']' in netloc: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
93 return netloc.split(']')[0][1:].lower() |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
94 elif ':' in netloc: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
95 return netloc.split(':')[0].lower() |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
96 elif netloc == '': |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
97 return None |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
98 else: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
99 return netloc.lower() |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
100 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
101 @property |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
102 def port(self): |
|
60613
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
103 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
|
104 if ':' in netloc: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
105 port = netloc.split(':')[1] |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
106 return int(port, 10) |
|
60613
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
107 else: |
|
7b9e328f7533
Fix issue2987: RFC2732 support for urlparse (IPv6 addresses)
Senthil Kumaran <orsenthil@gmail.com>
parents:
59681
diff
changeset
|
108 return None |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
109 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
110 from collections import namedtuple |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
111 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
112 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
|
113 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
114 __slots__ = () |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
115 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
116 def geturl(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
117 return urlunsplit(self) |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
118 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
119 |
|
44644
79579fa8d752
Speed-up and simplify code urlparse's result objects.
Raymond Hettinger <python@rcn.com>
parents:
44510
diff
changeset
|
120 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
|
121 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
122 __slots__ = () |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
123 |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
124 def geturl(self): |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
125 return urlunparse(self) |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
126 |
|
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 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
|
129 """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
|
130 <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
|
131 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
|
132 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
|
133 (e.g. netloc is a single string) and we don't expand % escapes.""" |
|
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
|
134 tuple = urlsplit(url, scheme, allow_fragments) |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
135 scheme, netloc, url, query, fragment = tuple |
|
174ccb035b3d
Fix parsing of parameters from a URL; urlparse() did not check that it only
Fred Drake <fdrake@acm.org>
parents:
19209
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 params = '' |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
140 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
|
141 |
|
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 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 |
|
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
|
151 def _splitnetloc(url, start=0): |
|
44510
65fe6e56d4f8
Patch #1637: fix urlparse for URLs like 'http://x.com?arg=/foo'.
Guido van Rossum <guido@python.org>
parents:
44483
diff
changeset
|
152 delim = len(url) # position of end of domain part of url, default is end |
|
65fe6e56d4f8
Patch #1637: fix urlparse for URLs like 'http://x.com?arg=/foo'.
Guido van Rossum <guido@python.org>
parents:
44483
diff
changeset
|
153 for c in '/?#': # look for delimiters; the order is NOT important |
|
65fe6e56d4f8
Patch #1637: fix urlparse for URLs like 'http://x.com?arg=/foo'.
Guido van Rossum <guido@python.org>
parents:
44483
diff
changeset
|
154 wdelim = url.find(c, start) # find first of this delim |
|
65fe6e56d4f8
Patch #1637: fix urlparse for URLs like 'http://x.com?arg=/foo'.
Guido van Rossum <guido@python.org>
parents:
44483
diff
changeset
|
155 if wdelim >= 0: # if found |
|
65fe6e56d4f8
Patch #1637: fix urlparse for URLs like 'http://x.com?arg=/foo'.
Guido van Rossum <guido@python.org>
parents:
44483
diff
changeset
|
156 delim = min(delim, wdelim) # use earliest delim position |
|
65fe6e56d4f8
Patch #1637: fix urlparse for URLs like 'http://x.com?arg=/foo'.
Guido van Rossum <guido@python.org>
parents:
44483
diff
changeset
|
157 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
|
158 |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
159 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
|
160 """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
|
161 <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
|
162 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
|
163 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
|
164 (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
|
165 allow_fragments = bool(allow_fragments) |
|
44284
6ae94a2ba5df
Backport r59480.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
37393
diff
changeset
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 (']' 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 url, query = url.split('?', 1) |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
187 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
|
188 _parse_cache[key] = v |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
189 return v |
|
63714
1489f399aba1
Fix Issue754016 - urlparse goes wrong with IP:port without scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
63021
diff
changeset
|
190 if url.endswith(':') or not url[i+1].isdigit(): |
|
1489f399aba1
Fix Issue754016 - urlparse goes wrong with IP:port without scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
63021
diff
changeset
|
191 for c in url[:i]: |
|
1489f399aba1
Fix Issue754016 - urlparse goes wrong with IP:port without scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
63021
diff
changeset
|
192 if c not in scheme_chars: |
|
1489f399aba1
Fix Issue754016 - urlparse goes wrong with IP:port without scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
63021
diff
changeset
|
193 break |
|
1489f399aba1
Fix Issue754016 - urlparse goes wrong with IP:port without scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
63021
diff
changeset
|
194 else: |
|
1489f399aba1
Fix Issue754016 - urlparse goes wrong with IP:port without scheme
Senthil Kumaran <orsenthil@gmail.com>
parents:
63021
diff
changeset
|
195 scheme, url = url[:i].lower(), url[i+1:] |
|
58950
7da7f9bfdaac
Fix for Issue7904. urlparse.urlsplit to handle schemes in the way defined by RFC3986
Senthil Kumaran <orsenthil@gmail.com>
parents:
52476
diff
changeset
|
196 |
|
7da7f9bfdaac
Fix for Issue7904. urlparse.urlsplit to handle schemes in the way defined by RFC3986
Senthil Kumaran <orsenthil@gmail.com>
parents:
52476
diff
changeset
|
197 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
|
198 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
|
199 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
|
200 (']' 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
|
201 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
|
202 if allow_fragments and scheme in uses_fragment 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
|
203 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
|
204 if scheme in uses_query 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
|
205 url, query = url.split('?', 1) |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
206 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
|
207 _parse_cache[key] = v |
|
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
208 return v |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
48140
cc43b998f5b5
Silence some SyntaxWarnings for tuple unpacking in a parameter list for
Brett Cannon <bcannon@gmail.com>
parents:
44847
diff
changeset
|
210 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
|
211 """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
|
212 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
|
213 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
|
214 (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
|
215 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
|
216 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
|
217 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
|
218 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
|
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 urlunsplit(data): |
|
62591
9f0188e4f81a
Fix Issue8653 - Docstring for urlunsplit function.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61901
diff
changeset
|
221 """Combine the elements of a tuple as returned by urlsplit() into a |
|
9f0188e4f81a
Fix Issue8653 - Docstring for urlunsplit function.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61901
diff
changeset
|
222 complete URL as a string. The data argument can be any five-item iterable. |
|
9f0188e4f81a
Fix Issue8653 - Docstring for urlunsplit function.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61901
diff
changeset
|
223 This may result in a slightly different, but equivalent URL, if the URL that |
|
9f0188e4f81a
Fix Issue8653 - Docstring for urlunsplit function.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61901
diff
changeset
|
224 was parsed originally had unnecessary delimiters (for example, a ? with an |
|
9f0188e4f81a
Fix Issue8653 - Docstring for urlunsplit function.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61901
diff
changeset
|
225 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
|
226 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
|
227 if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'): |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
228 if url and url[:1] != '/': url = '/' + url |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 return url |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 |
|
37393
a6bce7aebe05
Patch #624325: urlparse.urlparse() and urlparse.urlsplit() results
Fred Drake <fdrake@acm.org>
parents:
37368
diff
changeset
|
238 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
|
239 """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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 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
|
258 params, query, fragment)) |
|
48233
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
259 if not path: |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
260 path = bpath |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
261 if not params: |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
262 params = bparams |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
263 else: |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
264 path = path[:-1] |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
265 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
|
266 params, query, fragment)) |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
267 if not query: |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
268 query = bquery |
|
af7388ad3164
Issue 1432. Fixes a bug caused because of the evolution
Facundo Batista <facundobatista@gmail.com>
parents:
48140
diff
changeset
|
269 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
|
270 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
|
271 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
|
272 # 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 break |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
285 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
|
286 else: |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
287 break |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
288 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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 params, query, fragment)) |
|
1875
8c9cc054c5ed
New tty/pty modules by Steen; new urlparser.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
295 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
|
296 """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
|
297 |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15741
diff
changeset
|
298 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
|
299 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
|
300 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
|
301 """ |
|
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
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 return url, '' |
|
3652
728a39a3e0d5
optimizations due to Fred Drake; added urldefrag() function
Guido van Rossum <guido@python.org>
parents:
3641
diff
changeset
|
308 |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
309 # 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
|
310 # 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
|
311 # 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
|
312 # update it also in urllib. This code duplication does not existin in Python3. |
|
59681
a1b6a54e2b6a
Fix for Issue8135 - urllib.unquote to support mixed percent escapes
Senthil Kumaran <orsenthil@gmail.com>
parents:
58950
diff
changeset
|
313 |
|
a1b6a54e2b6a
Fix for Issue8135 - urllib.unquote to support mixed percent escapes
Senthil Kumaran <orsenthil@gmail.com>
parents:
58950
diff
changeset
|
314 _hexdig = '0123456789ABCDEFabcdef' |
|
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 _hextochr = dict((a+b, chr(int(a+b,16))) |
|
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 for a in _hexdig for b in _hexdig) |
|
48995
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
317 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
318 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
|
319 """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
|
320 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
|
321 # 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
|
322 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
|
323 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
|
324 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
|
325 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
|
326 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
|
327 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
334 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
|
335 """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
|
336 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
337 Arguments: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
338 |
|
63888
a56d8a80b883
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63714
diff
changeset
|
339 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
|
340 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
341 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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 not included. |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
347 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
348 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
|
349 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
|
350 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
|
351 """ |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
352 dict = {} |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
353 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
|
354 if name in dict: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
355 dict[name].append(value) |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
356 else: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
357 dict[name] = [value] |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
358 return dict |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
359 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
360 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
|
361 """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
|
362 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
363 Arguments: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
364 |
|
63888
a56d8a80b883
Merged revisions 83901 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
63714
diff
changeset
|
365 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
|
366 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
373 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
|
374 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
|
375 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
|
376 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
377 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
|
378 """ |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
379 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
|
380 r = [] |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
381 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
|
382 if not name_value and not strict_parsing: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
383 continue |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
384 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
|
385 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
|
386 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
|
387 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
|
388 # 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
|
389 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
|
390 nv.append('') |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
391 else: |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
392 continue |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
393 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
|
394 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
|
395 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
|
396 r.append((name, value)) |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
397 |
|
2b855a947f0f
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
Facundo Batista <facundobatista@gmail.com>
parents:
48233
diff
changeset
|
398 return r |
