-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathurlparse.r2py
192 lines (138 loc) · 4.98 KB
/
urlparse.r2py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
<Program Name>
urlparse.r2py
<Started>
May 15, 2009
<Author>
Michael Phan-Ba
<Purpose>
Provides utilities for parsing URLs, based on the Python 2.6.1 module urlparse.
"""
def urlparse_urlsplit(urlstring, default_scheme="", allow_fragments=True):
"""
<Purpose>
Parse a URL into five components, returning a dictionary. This corresponds
to the general structure of a URL:
scheme://netloc/path;parameters?query#fragment. The parameters are not
split from the URL and individual componenets are not separated.
Only absolute server-based URIs are currently supported (all URLs will be
parsed into the components listed, regardless of the scheme).
<Arguments>
default_scheme:
Optional: defaults to the empty string. If specified, gives the default
addressing scheme, to be used only if the URL does not specify one.
allow_fragments:
Optional: defaults to True. If False, fragment identifiers are not
allowed, even if the URL's addressing scheme normally does support them.
<Exceptions>
ValueError on parsing a non-numeric port value.
<Side Effects>
None.
<Returns>
A dictionary containing:
Key Value Value if not present
============================================================================
scheme URL scheme specifier empty string
netloc Network location part empty string
path Hierarchical path empty string
query Query component empty string
fragment Fragment identifier empty string
username User name None
password Password None
hostname Host name (lower case) None
port Port number as integer, if present None
"""
components = {"scheme": default_scheme, "netloc": "", "path": "", "query": "",
"fragment": "", "username": None, "password": None, "hostname": None,
"port": None }
# Extract the scheme, if present.
(lpart, rpart) = _urlparse_splitscheme(urlstring)
if lpart:
components["scheme"] = lpart
# Extract the server information, if present.
if rpart.startswith("//"):
(lpart, rpart) = _urlparse_splitnetloc(rpart, 2)
components["netloc"] = lpart
(components["username"], components["password"], components["hostname"],
components["port"]) = _urlparse_splitauthority(lpart)
# Extract the fragment.
if allow_fragments:
(rpart, components["fragment"]) = _urlparse_splitfragment(rpart)
# Extract the query.
(components["path"], components["query"]) = _urlparse_splitquery(rpart)
return components
def _urlparse_splitscheme(url):
"""Parse the scheme portion of the URL"""
# The scheme is valid only if it contains these characters.
scheme_chars = \
"abcdefghijklmnopqrstuvwxyz0123456789+-."
scheme = ""
rest = url
spart = url.split(":", 1)
if len(spart) == 2:
# Normalize the scheme.
spart[0] = spart[0].lower()
# A scheme is valid only if it starts with an alpha character.
if spart[0] and spart[0][0].isalpha():
for char in spart[0]:
if char not in scheme_chars:
break
(scheme, rest) = spart
return scheme, rest
def _urlparse_splitnetloc(url, start=0):
"""Parse the netloc portion of the URL"""
# By default, the netloc is delimited by the end of the URL.
delim = len(url)
# Find the left-most delimiter.
for char in "/?#":
xdelim = url.find(char, start)
if xdelim >= 0:
delim = min(delim, xdelim)
# Return the netloc and the rest of the URL.
return url[start:delim], url[delim:]
def _urlparse_splitauthority(netloc):
"""Parse the authority portion of the netloc"""
# The authority can have a userinfo portion delimited by "@".
authority = netloc.split("@", 1)
# Default values.
username = None
password = None
hostname = None
port = None
# Is there a userinfo portion?
if len(authority) == 2:
# userinfo can be split into username:password
userinfo = authority[0].split(":", 1)
# hostport can be split into hostname:port
hostport = authority[1].split(":", 1)
if userinfo[0]:
username = userinfo[0]
if len(userinfo) == 2:
password = userinfo[1]
# No userinfo portion found.
else:
# hostport can be split into hostname:port
hostport = netloc.split(":", 1)
# Is there a port value?
if hostport[0]:
hostname = hostport[0]
if len(hostport) == 2:
port = int(hostport[1], 10)
# Return the values.
return username, password, hostname, port
def _urlparse_splitquery(url):
"""Parse the query portion of the url"""
qpart = url.split("?", 1)
if len(qpart) == 2:
query = qpart[1]
else:
query = ""
return qpart[0], query
def _urlparse_splitfragment(url):
"""Parse the query portion of the url"""
fpart = url.split("#", 1)
if len(fpart) == 2:
fragment = fpart[1]
else:
fragment = ""
return fpart[0], fragment