parse.mjs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // Changes from joyent/node:
  23. //
  24. // 1. No leading slash in paths,
  25. // e.g. in `url.parse('http://foo?bar')` pathname is ``, not `/`
  26. //
  27. // 2. Backslashes are not replaced with slashes,
  28. // so `http:\\example.org\` is treated like a relative path
  29. //
  30. // 3. Trailing colon is treated like a part of the path,
  31. // i.e. in `http://example.org:foo` pathname is `:foo`
  32. //
  33. // 4. Nothing is URL-encoded in the resulting object,
  34. // (in joyent/node some chars in auth and paths are encoded)
  35. //
  36. // 5. `url.parse()` does not have `parseQueryString` argument
  37. //
  38. // 6. Removed extraneous result properties: `host`, `path`, `query`, etc.,
  39. // which can be constructed using other parts of the url.
  40. //
  41. function Url () {
  42. this.protocol = null
  43. this.slashes = null
  44. this.auth = null
  45. this.port = null
  46. this.hostname = null
  47. this.hash = null
  48. this.search = null
  49. this.pathname = null
  50. }
  51. // Reference: RFC 3986, RFC 1808, RFC 2396
  52. // define these here so at least they only have to be
  53. // compiled once on the first module load.
  54. const protocolPattern = /^([a-z0-9.+-]+:)/i
  55. const portPattern = /:[0-9]*$/
  56. // Special case for a simple path URL
  57. /* eslint-disable-next-line no-useless-escape */
  58. const simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/
  59. // RFC 2396: characters reserved for delimiting URLs.
  60. // We actually just auto-escape these.
  61. const delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t']
  62. // RFC 2396: characters not allowed for various reasons.
  63. const unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims)
  64. // Allowed by RFCs, but cause of XSS attacks. Always escape these.
  65. const autoEscape = ['\''].concat(unwise)
  66. // Characters that are never ever allowed in a hostname.
  67. // Note that any invalid chars are also handled, but these
  68. // are the ones that are *expected* to be seen, so we fast-path
  69. // them.
  70. const nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape)
  71. const hostEndingChars = ['/', '?', '#']
  72. const hostnameMaxLen = 255
  73. const hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/
  74. const hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/
  75. // protocols that can allow "unsafe" and "unwise" chars.
  76. // protocols that never have a hostname.
  77. const hostlessProtocol = {
  78. javascript: true,
  79. 'javascript:': true
  80. }
  81. // protocols that always contain a // bit.
  82. const slashedProtocol = {
  83. http: true,
  84. https: true,
  85. ftp: true,
  86. gopher: true,
  87. file: true,
  88. 'http:': true,
  89. 'https:': true,
  90. 'ftp:': true,
  91. 'gopher:': true,
  92. 'file:': true
  93. }
  94. function urlParse (url, slashesDenoteHost) {
  95. if (url && url instanceof Url) return url
  96. const u = new Url()
  97. u.parse(url, slashesDenoteHost)
  98. return u
  99. }
  100. Url.prototype.parse = function (url, slashesDenoteHost) {
  101. let lowerProto, hec, slashes
  102. let rest = url
  103. // trim before proceeding.
  104. // This is to support parse stuff like " http://foo.com \n"
  105. rest = rest.trim()
  106. if (!slashesDenoteHost && url.split('#').length === 1) {
  107. // Try fast path regexp
  108. const simplePath = simplePathPattern.exec(rest)
  109. if (simplePath) {
  110. this.pathname = simplePath[1]
  111. if (simplePath[2]) {
  112. this.search = simplePath[2]
  113. }
  114. return this
  115. }
  116. }
  117. let proto = protocolPattern.exec(rest)
  118. if (proto) {
  119. proto = proto[0]
  120. lowerProto = proto.toLowerCase()
  121. this.protocol = proto
  122. rest = rest.substr(proto.length)
  123. }
  124. // figure out if it's got a host
  125. // user@server is *always* interpreted as a hostname, and url
  126. // resolution will treat //foo/bar as host=foo,path=bar because that's
  127. // how the browser resolves relative URLs.
  128. /* eslint-disable-next-line no-useless-escape */
  129. if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
  130. slashes = rest.substr(0, 2) === '//'
  131. if (slashes && !(proto && hostlessProtocol[proto])) {
  132. rest = rest.substr(2)
  133. this.slashes = true
  134. }
  135. }
  136. if (!hostlessProtocol[proto] &&
  137. (slashes || (proto && !slashedProtocol[proto]))) {
  138. // there's a hostname.
  139. // the first instance of /, ?, ;, or # ends the host.
  140. //
  141. // If there is an @ in the hostname, then non-host chars *are* allowed
  142. // to the left of the last @ sign, unless some host-ending character
  143. // comes *before* the @-sign.
  144. // URLs are obnoxious.
  145. //
  146. // ex:
  147. // http://a@b@c/ => user:a@b host:c
  148. // http://a@b?@c => user:a host:c path:/?@c
  149. // v0.12 TODO(isaacs): This is not quite how Chrome does things.
  150. // Review our test case against browsers more comprehensively.
  151. // find the first instance of any hostEndingChars
  152. let hostEnd = -1
  153. for (let i = 0; i < hostEndingChars.length; i++) {
  154. hec = rest.indexOf(hostEndingChars[i])
  155. if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) {
  156. hostEnd = hec
  157. }
  158. }
  159. // at this point, either we have an explicit point where the
  160. // auth portion cannot go past, or the last @ char is the decider.
  161. let auth, atSign
  162. if (hostEnd === -1) {
  163. // atSign can be anywhere.
  164. atSign = rest.lastIndexOf('@')
  165. } else {
  166. // atSign must be in auth portion.
  167. // http://a@b/c@d => host:b auth:a path:/c@d
  168. atSign = rest.lastIndexOf('@', hostEnd)
  169. }
  170. // Now we have a portion which is definitely the auth.
  171. // Pull that off.
  172. if (atSign !== -1) {
  173. auth = rest.slice(0, atSign)
  174. rest = rest.slice(atSign + 1)
  175. this.auth = auth
  176. }
  177. // the host is the remaining to the left of the first non-host char
  178. hostEnd = -1
  179. for (let i = 0; i < nonHostChars.length; i++) {
  180. hec = rest.indexOf(nonHostChars[i])
  181. if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) {
  182. hostEnd = hec
  183. }
  184. }
  185. // if we still have not hit it, then the entire thing is a host.
  186. if (hostEnd === -1) {
  187. hostEnd = rest.length
  188. }
  189. if (rest[hostEnd - 1] === ':') { hostEnd-- }
  190. const host = rest.slice(0, hostEnd)
  191. rest = rest.slice(hostEnd)
  192. // pull out port.
  193. this.parseHost(host)
  194. // we've indicated that there is a hostname,
  195. // so even if it's empty, it has to be present.
  196. this.hostname = this.hostname || ''
  197. // if hostname begins with [ and ends with ]
  198. // assume that it's an IPv6 address.
  199. const ipv6Hostname = this.hostname[0] === '[' &&
  200. this.hostname[this.hostname.length - 1] === ']'
  201. // validate a little.
  202. if (!ipv6Hostname) {
  203. const hostparts = this.hostname.split(/\./)
  204. for (let i = 0, l = hostparts.length; i < l; i++) {
  205. const part = hostparts[i]
  206. if (!part) { continue }
  207. if (!part.match(hostnamePartPattern)) {
  208. let newpart = ''
  209. for (let j = 0, k = part.length; j < k; j++) {
  210. if (part.charCodeAt(j) > 127) {
  211. // we replace non-ASCII char with a temporary placeholder
  212. // we need this to make sure size of hostname is not
  213. // broken by replacing non-ASCII by nothing
  214. newpart += 'x'
  215. } else {
  216. newpart += part[j]
  217. }
  218. }
  219. // we test again with ASCII char only
  220. if (!newpart.match(hostnamePartPattern)) {
  221. const validParts = hostparts.slice(0, i)
  222. const notHost = hostparts.slice(i + 1)
  223. const bit = part.match(hostnamePartStart)
  224. if (bit) {
  225. validParts.push(bit[1])
  226. notHost.unshift(bit[2])
  227. }
  228. if (notHost.length) {
  229. rest = notHost.join('.') + rest
  230. }
  231. this.hostname = validParts.join('.')
  232. break
  233. }
  234. }
  235. }
  236. }
  237. if (this.hostname.length > hostnameMaxLen) {
  238. this.hostname = ''
  239. }
  240. // strip [ and ] from the hostname
  241. // the host field still retains them, though
  242. if (ipv6Hostname) {
  243. this.hostname = this.hostname.substr(1, this.hostname.length - 2)
  244. }
  245. }
  246. // chop off from the tail first.
  247. const hash = rest.indexOf('#')
  248. if (hash !== -1) {
  249. // got a fragment string.
  250. this.hash = rest.substr(hash)
  251. rest = rest.slice(0, hash)
  252. }
  253. const qm = rest.indexOf('?')
  254. if (qm !== -1) {
  255. this.search = rest.substr(qm)
  256. rest = rest.slice(0, qm)
  257. }
  258. if (rest) { this.pathname = rest }
  259. if (slashedProtocol[lowerProto] &&
  260. this.hostname && !this.pathname) {
  261. this.pathname = ''
  262. }
  263. return this
  264. }
  265. Url.prototype.parseHost = function (host) {
  266. let port = portPattern.exec(host)
  267. if (port) {
  268. port = port[0]
  269. if (port !== ':') {
  270. this.port = port.substr(1)
  271. }
  272. host = host.substr(0, host.length - port.length)
  273. }
  274. if (host) { this.hostname = host }
  275. }
  276. export default urlParse