Debian Codegraph @

src/url-toolkit.js

raw symbols SCIP
1 // see https://tools.ietf.org/html/rfc1808
2
3 (function (root) {
4 var URL_REGEX =
5 /^(?=((?:[a-zA-Z0-9+\-.]+:)?))\1(?=((?:\/\/[^\/?#]*)?))\2(?=((?:(?:[^?#\/]*\/)*[^;?#\/]*)?))\3((?:;[^?#]*)?)(\?[^#]*)?(#[^]*)?$/;
6 var FIRST_SEGMENT_REGEX = /^(?=([^\/?#]*))\1([^]*)$/;
7 var SLASH_DOT_REGEX = /(?:\/|^)\.(?=\/)/g;
8 var SLASH_DOT_DOT_REGEX = /(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g;
9
10 var URLToolkit = {
11 // If opts.alwaysNormalize is true then the path will always be normalized even when it starts with / or //
12 // E.g
13 // With opts.alwaysNormalize = false (default, spec compliant)
14 // http://a.com/b/cd + /e/f/../g => http://a.com/e/f/../g
15 // With opts.alwaysNormalize = true (not spec compliant)
16 // http://a.com/b/cd + /e/f/../g => http://a.com/e/g
17 buildAbsoluteURL: function (baseURL, relativeURL, opts) {
18 opts = opts || {};
19 // remove any remaining space and CRLF
20 baseURL = baseURL.trim();
21 relativeURL = relativeURL.trim();
22 if (!relativeURL) {
23 // 2a) If the embedded URL is entirely empty, it inherits the
24 // entire base URL (i.e., is set equal to the base URL)
25 // and we are done.
26 if (!opts.alwaysNormalize) {
27 return baseURL;
28 }
29 var basePartsForNormalise = URLToolkit.parseURL(baseURL);
30 if (!basePartsForNormalise) {
31 throw new Error('Error trying to parse base URL.');
32 }
33 basePartsForNormalise.path = URLToolkit.normalizePath(
34 basePartsForNormalise.path
35 );
36 return URLToolkit.buildURLFromParts(basePartsForNormalise);
37 }
38 var relativeParts = URLToolkit.parseURL(relativeURL);
39 if (!relativeParts) {
40 throw new Error('Error trying to parse relative URL.');
41 }
42 if (relativeParts.scheme) {
43 // 2b) If the embedded URL starts with a scheme name, it is
44 // interpreted as an absolute URL and we are done.
45 if (!opts.alwaysNormalize) {
46 return relativeURL;
47 }
48 relativeParts.path = URLToolkit.normalizePath(relativeParts.path);
49 return URLToolkit.buildURLFromParts(relativeParts);
50 }
51 var baseParts = URLToolkit.parseURL(baseURL);
52 if (!baseParts) {
53 throw new Error('Error trying to parse base URL.');
54 }
55 if (!baseParts.netLoc && baseParts.path && baseParts.path[0] !== '/') {
56 // If netLoc missing and path doesn't start with '/', assume everthing before the first '/' is the netLoc
57 // This causes 'example.com/a' to be handled as '//example.com/a' instead of '/example.com/a'
58 var pathParts = FIRST_SEGMENT_REGEX.exec(baseParts.path);
59 baseParts.netLoc = pathParts[1];
60 baseParts.path = pathParts[2];
61 }
62 if (baseParts.netLoc && !baseParts.path) {
63 baseParts.path = '/';
64 }
65 var builtParts = {
66 // 2c) Otherwise, the embedded URL inherits the scheme of
67 // the base URL.
68 scheme: baseParts.scheme,
69 netLoc: relativeParts.netLoc,
70 path: null,
71 params: relativeParts.params,
72 query: relativeParts.query,
73 fragment: relativeParts.fragment,
74 };
75 if (!relativeParts.netLoc) {
76 // 3) If the embedded URL's <net_loc> is non-empty, we skip to
77 // Step 7. Otherwise, the embedded URL inherits the <net_loc>
78 // (if any) of the base URL.
79 builtParts.netLoc = baseParts.netLoc;
80 // 4) If the embedded URL path is preceded by a slash "/", the
81 // path is not relative and we skip to Step 7.
82 if (relativeParts.path[0] !== '/') {
83 if (!relativeParts.path) {
84 // 5) If the embedded URL path is empty (and not preceded by a
85 // slash), then the embedded URL inherits the base URL path
86 builtParts.path = baseParts.path;
87 // 5a) if the embedded URL's <params> is non-empty, we skip to
88 // step 7; otherwise, it inherits the <params> of the base
89 // URL (if any) and
90 if (!relativeParts.params) {
91 builtParts.params = baseParts.params;
92 // 5b) if the embedded URL's <query> is non-empty, we skip to
93 // step 7; otherwise, it inherits the <query> of the base
94 // URL (if any) and we skip to step 7.
95 if (!relativeParts.query) {
96 builtParts.query = baseParts.query;
97 }
98 }
99 } else {
100 // 6) The last segment of the base URL's path (anything
101 // following the rightmost slash "/", or the entire path if no
102 // slash is present) is removed and the embedded URL's path is
103 // appended in its place.
104 var baseURLPath = baseParts.path;
105 var newPath =
106 baseURLPath.substring(0, baseURLPath.lastIndexOf('/') + 1) +
107 relativeParts.path;
108 builtParts.path = URLToolkit.normalizePath(newPath);
109 }
110 }
111 }
112 if (builtParts.path === null) {
113 builtParts.path = opts.alwaysNormalize
114 ? URLToolkit.normalizePath(relativeParts.path)
115 : relativeParts.path;
116 }
117 return URLToolkit.buildURLFromParts(builtParts);
118 },
119 parseURL: function (url) {
120 var parts = URL_REGEX.exec(url);
121 if (!parts) {
122 return null;
123 }
124 return {
125 scheme: parts[1] || '',
126 netLoc: parts[2] || '',
127 path: parts[3] || '',
128 params: parts[4] || '',
129 query: parts[5] || '',
130 fragment: parts[6] || '',
131 };
132 },
133 normalizePath: function (path) {
134 // The following operations are
135 // then applied, in order, to the new path:
136 // 6a) All occurrences of "./", where "." is a complete path
137 // segment, are removed.
138 // 6b) If the path ends with "." as a complete path segment,
139 // that "." is removed.
140 path = path.split('').reverse().join('').replace(SLASH_DOT_REGEX, '');
141 // 6c) All occurrences of "<segment>/../", where <segment> is a
142 // complete path segment not equal to "..", are removed.
143 // Removal of these path segments is performed iteratively,
144 // removing the leftmost matching pattern on each iteration,
145 // until no matching pattern remains.
146 // 6d) If the path ends with "<segment>/..", where <segment> is a
147 // complete path segment not equal to "..", that
148 // "<segment>/.." is removed.
149 while (
150 path.length !== (path = path.replace(SLASH_DOT_DOT_REGEX, '')).length
151 ) {}
152 return path.split('').reverse().join('');
153 },
154 buildURLFromParts: function (parts) {
155 return (
156 parts.scheme +
157 parts.netLoc +
158 parts.path +
159 parts.params +
160 parts.query +
161 parts.fragment
162 );
163 },
164 };
165
166 if (typeof exports === 'object' && typeof module === 'object')
167 module.exports = URLToolkit;
168 else if (typeof define === 'function' && define.amd)
169 define([], function () {
170 return URLToolkit;
171 });
172 else if (typeof exports === 'object') exports['URLToolkit'] = URLToolkit;
173 else root['URLToolkit'] = URLToolkit;
174 })(this);