This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
uri.js
256 lines (225 loc) · 7 KB
/
uri.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* uri.js
* A URI parser, compliant with assorted RFCs, providing parsing and resolution utilities.
**/
/*
Blatantly stolen with permission from Narwhal, which did the same from Chiron,
and bits taken from parseUri 1.2.1 (c) 2007 Steven Levithan <stevenlevithan.com> MIT License,
and probably also plagiarizing http://code.google.com/p/js-uri/ in some ways.
Most lines have been changed, please don't blame any of the above persons for
any errors in this file.
*/
exports.parse = uri_parse;
exports.format = uri_format;
exports.resolve = uri_resolve;
exports.resolveObject = uri_resolveObject;
exports.decode = uri_decode;
/****
Decode a URI, replacing + with space
*/
function uri_decode (s) {
return decodeURIComponent(s.replace(/\+/g, ' '));
}
/**** expressionKeys
members of a parsed URI object that you get
from evaluting the strict regular expression.
*/
var expressionKeys = [
"url",
"protocol",
"authorityRoot",
"authority",
"userInfo",
"user",
"password",
"domain",
"port",
"path",
"root",
"directory",
"file",
"query",
"anchor"
],
strictExpression = new RegExp( /* url */
"^" +
"(?:" +
"([^:/?#]+):" + /* protocol */
")?" +
"(?:" +
"(//)" + /* authorityRoot */
"(" + /* authority */
"(?:" +
"(" + /* userInfo */
"([^:@/]*)" + /* user */
":?" +
"([^@/]*)" + /* password */
")?" +
"@" +
")?" +
"([^:/?#]*)" + /* domain */
"(?::(\\d*))?" + /* port */
")" +
")?" +
"(" + /* path */
"(/?)" + /* root */
"((?:[^?#/]*/)*)" +
"([^?#]*)" + /* file */
")" +
"(?:\\?([^#]*))?" + /* query */
"(?:#(.*))?" /*anchor */
);
/**** parse
a URI parser function that uses the `strictExpression`
and `expressionKeys` and returns an `Object`
mapping all `keys` to values.
*/
function uri_parse (url) {
var items = {},
parts = strictExpression.exec(url);
for (var i = 0; i < parts.length; i++) {
items[expressionKeys[i]] = parts[i] ? parts[i] : "";
}
items.root = (items.root || items.authorityRoot) ? '/' : '';
items.directories = items.directory.split("/");
if (items.directories[items.directories.length - 1] == "") {
items.directories.pop();
}
/* normalize */
var directories = [];
for (var i = 0; i < items.directories.length; i++) {
var directory = items.directories[i];
if (directory == '.') {
} else if (directory == '..') {
if (directories.length && directories[directories.length - 1] != '..')
directories.pop();
else
directories.push('..');
} else {
directories.push(directory);
}
}
items.directories = directories;
items.domains = items.domain.split(".");
return items;
};
/**** format
accepts a parsed URI object and returns
the corresponding string.
*/
function uri_format (object) {
if (typeof(object) == 'undefined')
throw new Error("UrlError: URL undefined for urls#format");
if (object instanceof String || typeof(object) === 'string')
return object;
var domain =
object.domains ?
object.domains.join(".") :
object.domain;
var userInfo = (
object.user ||
object.password
) ? (
(object.user || "") +
(object.password ? ":" + object.password : "")
) :
object.userInfo;
var authority = object.authority || ((
userInfo ||
domain ||
object.port
) ? (
(userInfo ? userInfo + "@" : "") +
(domain || "") +
(object.port ? ":" + object.port : "")
) : "");
var directory =
object.directories ?
object.directories.join("/") :
object.directory;
var path =
object.path ? object.path.substr(1) : (
(directory || object.file) ? (
(directory ? directory + "/" : "") +
(object.file || "")
) : "");
var authorityRoot =
object.authorityRoot
|| authority ? "//" : "";
return object.url = ((
(object.protocol ? object.protocol + ":" : "") +
(authorityRoot) +
(authority) +
(object.root || (authority && path) ? "/" : "") +
(path ? path : "") +
(object.query ? "?" + object.query : "") +
(object.anchor ? "#" + object.anchor : "")
) || object.url || "");
};
function uri_resolveObject (source, relative) {
if (!source) return relative;
// parse a string, or get new objects
source = uri_parse(uri_format(source));
relative = uri_parse(uri_format(relative));
if (relative.url === "") return source;
// links to xyz:... from abc:... are always absolute.
if (relative.protocol && source.protocol && relative.protocol !== source.protocol) {
return relative;
}
// if there's an authority root, but no protocol, then keep the current protocol
if (relative.authorityRoot && !relative.protocol) {
relative.protocol = source.protocol;
}
// if we have an authority root, then it's absolute
if (relative.authorityRoot) return relative;
// at this point, we start doing the tricky stuff
// we know that relative doesn't have an authority, but might have root,
// path, file, query, etc.
// also, the directory segments might contain .. or .
// start mutating "source", and then return that.
// relative urls that start with / are absolute to the authority/protocol
if (relative.root) {
[
"path", "root", "directory", "directories", "file", "query", "anchor"
].forEach(function (part) { source[part] = relative[part] });
} else {
// if you have /a/b/c and you're going to x/y/z, then that's /a/b/x/y/z
// if you have /a/b/c/ and you're going ot x/y/z, then that's /a/b/c/x/y/z
// if you have /a/b/c and you're going to ?foo, then that's /a/b/c?foo
if (relative.file || relative.directory) {
source.file = relative.file;
source.query = relative.query;
source.anchor = relative.anchor;
}
if (relative.query) source.query = relative.query;
if (relative.query || relative.anchor) source.anchor = relative.anchor;
// just append the dirs. we'll sort out .. and . later
source.directories = source.directories.concat(relative.directories);
}
// back up "file" to the first non-dot
// one step for ., two for ..
var file = source.file;
while (file === ".." || file === ".") {
if (file === "..") source.directories.pop();
file = source.directories.pop();
}
source.file = file || "";
// walk over the dirs, replacing a/b/c/.. with a/b/ and a/b/c/. with a/b/c
var dirs = [];
source.directories.forEach(function (dir, i) {
if (dir === "..") dirs.pop();
else if (dir !== "." && dir !== "") dirs.push(dir);
});
// now construct path/etc.
source.directories = dirs;
source.directory = dirs.concat("").join("/");
source.path = source.root + source.directory + source.file;
source.url = uri_format(source);
return source;
};
/**** resolve
returns a URL resovled to a relative URL from a source URL.
*/
function uri_resolve (source, relative) {
return uri_format(uri_resolveObject(source, relative));
};