diff options
| author | bptato <nincsnevem662@gmail.com> | 2025-05-03 16:26:27 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2025-05-03 17:29:15 +0200 |
| commit | 89d7057b8a6e9136aafad65328c11634a6e280f3 (patch) | |
| tree | 89dc86ea3e76e27ef6aea0ea69540c7b86ebd77f /src | |
| parent | 6e2795857546a76e7e0d0641761ac7c6da8f8808 (diff) | |
| download | chawan-89d7057b8a6e9136aafad65328c11634a6e280f3.tar.gz | |
url: add schemeType, fix port bug on protocol assignment
Allows branching on the scheme without string comparisons.
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/cookie.nim | 3 | ||||
| -rw-r--r-- | src/html/dom.nim | 8 | ||||
| -rw-r--r-- | src/html/env.nim | 2 | ||||
| -rw-r--r-- | src/local/pager.nim | 20 | ||||
| -rw-r--r-- | src/server/buffer.nim | 2 | ||||
| -rw-r--r-- | src/server/loader.nim | 14 | ||||
| -rw-r--r-- | src/types/referrer.nim | 11 | ||||
| -rw-r--r-- | src/types/url.nim | 212 |
8 files changed, 152 insertions, 120 deletions
diff --git a/src/config/cookie.nim b/src/config/cookie.nim index 964c5f1b..65731089 100644 --- a/src/config/cookie.nim +++ b/src/config/cookie.nim @@ -183,7 +183,8 @@ proc serialize*(cookieJar: CookieJar; url: URL): string = if cookie.expires != -1 and cookie.expires <= t: expired.add(i) continue - if cookie.secure and url.scheme != "https" and url.hostname != "localhost": + if cookie.secure and url.schemeType != stHttps and + url.hostname != "localhost": continue if not cookiePathMatches(cookie.path, url.pathname): continue diff --git a/src/html/dom.nim b/src/html/dom.nim index 1cac5ac7..383c7d8d 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -1927,7 +1927,7 @@ proc protocol(location: Location; s: string): Err[DOMException] {.jsfset.} = return let copyURL = newURL(location.url) copyURL.setProtocol(s) - if copyURL.scheme != "http" and copyURL.scheme != "https": + if copyURL.schemeType notin {stHttp, stHttps}: return errDOMException("Invalid URL", "SyntaxError") document.window.navigate(copyURL) return ok() @@ -2875,7 +2875,7 @@ proc parseColor(element: Element; s: string): ARGBColor = proc reinitURL*(element: Element): Option[URL] = if element.attrb(satHref): let url = parseURL(element.attr(satHref), some(element.document.baseURL)) - if url.isSome and url.get.scheme != "blob": + if url.isSome and url.get.schemeType != stBlob: return url return none(URL) @@ -4268,10 +4268,10 @@ proc loadResource*(window: Window; image: HTMLImageElement) = let url = parseURL(src, window.document.url.some) if url.isSome: let url = url.get - if window.document.url.scheme == "https" and url.scheme == "http": + if window.document.url.schemeType == stHttps and url.schemeType == stHttp: # mixed content :/ #TODO maybe do this in loader? - url.scheme = "https" + url.setProtocol("https") let surl = $url window.imageURLCache.withValue(surl, p): if p[].expiry > getTime().toUnix(): diff --git a/src/html/env.nim b/src/html/env.nim index d5475775..a8134ffa 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -204,7 +204,7 @@ method isSameOrigin*(window: Window; origin: Origin): bool {.base.} = proc fetch0(window: Window; input: JSRequest): JSResult[FetchPromise] = #TODO cors requests? - if input.request.url.scheme != "data" and + if input.request.url.schemeType != stData and not window.isSameOrigin(input.request.url.origin): let err = newFetchTypeError() return ok(newResolvedPromise(JSResult[Response].err(err))) diff --git a/src/local/pager.nim b/src/local/pager.nim index 3098e88f..83997370 100644 --- a/src/local/pager.nim +++ b/src/local/pager.nim @@ -411,7 +411,7 @@ proc loadJSModule(ctx: JSContext; moduleName: cstringConst; opaque: pointer): parseURL(moduleName, parseURL("file://" & cur & "/")) else: parseURL(moduleName) - if x.isNone or x.get.scheme != "file": + if x.isNone or x.get.schemeType != stFile: JS_ThrowTypeError(ctx, "Invalid URL: %s", cstring(moduleName)) return nil try: @@ -1365,7 +1365,7 @@ proc newContainer(pager: Pager; bufferConfig: BufferConfig; Container = let stream = pager.loader.startRequest(request, loaderConfig) pager.loader.registerFun(stream.fd) - let cacheId = if request.url.scheme == "cache": + let cacheId = if request.url.schemeType == stCache: parseInt32(request.url.pathname).get(-1) else: -1 @@ -2709,12 +2709,14 @@ proc redirect(pager: Pager; container: Container; response: Response; container.applyResponse(response, pager.config.external.mimeTypes) if container.redirectDepth < pager.config.network.maxRedirect: if container.url.scheme == request.url.scheme or - container.url.scheme == "cgi-bin" or - container.url.scheme == "http" and request.url.scheme == "https" or - container.url.scheme == "https" and request.url.scheme == "http": + container.url.schemeType == stCgiBin or + container.url.schemeType == stHttp and + request.url.schemeType == stHttps or + container.url.schemeType == stHttps and + request.url.schemeType == stHttp: pager.redirectTo(container, request) #TODO perhaps make following behavior configurable? - elif request.url.scheme == "cgi-bin": + elif request.url.schemeType == stCgiBin: pager.alert("Blocked redirection attempt to " & $request.url) else: let url = request.url @@ -2804,7 +2806,7 @@ proc connected3(pager: Pager; container: Container; stream: SocketStream; if istreamOutputId != -1: # new buffer if container.cacheId == -1: container.cacheId = loader.addCacheFile(istreamOutputId) - if container.request.url.scheme == "cache": + if container.request.url.schemeType == stCache: # loading from cache; now both the buffer and us hold a new reference # to the cached item, but it's only shared with the buffer. add a # pager ref too. @@ -3120,8 +3122,8 @@ proc handleEvent0(pager: Pager; container: Container; event: ContainerEvent) = let url = event.request.url let sameScheme = container.url.scheme == url.scheme if event.request.httpMethod != hmGet and not sameScheme and - not (container.url.scheme in ["http", "https"] and - url.scheme in ["http", "https"]): + not (container.url.schemeType in {stHttp, stHttps} and + url.schemeType in {stHttp, stHttps}): pager.alert("Blocked cross-scheme POST: " & $url) return #TODO this is horrible UX, async actions shouldn't block input diff --git a/src/server/buffer.nim b/src/server/buffer.nim index e898ee86..f8ced08d 100644 --- a/src/server/buffer.nim +++ b/src/server/buffer.nim @@ -1377,7 +1377,7 @@ proc click(buffer: Buffer; anchor: HTMLAnchorElement): ClickResult = let url = anchor.reinitURL() if url.isSome: var url = url.get - if url.scheme == "javascript": + if url.schemeType == stJavascript: if buffer.config.scripting == smFalse: return ClickResult() let s = buffer.evalJSURL(url) diff --git a/src/server/loader.nim b/src/server/loader.nim index 54e75629..2c12213d 100644 --- a/src/server/loader.nim +++ b/src/server/loader.nim @@ -1341,7 +1341,7 @@ proc loadResource(ctx: var LoaderContext; client: ClientHandle; var prevurl: URL = nil while redo and tries < MaxRewrites: redo = false - if ctx.config.w3mCGICompat and request.url.scheme == "file": + if ctx.config.w3mCGICompat and request.url.schemeType == stFile: let path = request.url.pathname.percentDecode() if ctx.canRewriteForCGICompat(path): let newURL = newURL("cgi-bin:" & path & request.url.search) @@ -1350,25 +1350,25 @@ proc loadResource(ctx: var LoaderContext; client: ClientHandle; inc tries redo = true continue - case request.url.scheme - of "cgi-bin": + case request.url.schemeType + of stCgiBin: ctx.loadCGI(client, handle, request, prevurl, config) if handle.stream != nil: ctx.addFd(handle) else: ctx.close(handle) - of "stream": + of stStream: ctx.loadStream(client, handle, request) if handle.stream != nil: ctx.addFd(handle) else: ctx.close(handle) - of "cache": + of stCache: ctx.loadFromCache(client, handle, request) assert handle.stream == nil - of "data": + of stData: ctx.loadData(handle, request) - of "about": + of stAbout: ctx.loadAbout(handle, request) else: prevurl = request.url diff --git a/src/types/referrer.nim b/src/types/referrer.nim index 7c7b6ffe..d8340d07 100644 --- a/src/types/referrer.nim +++ b/src/types/referrer.nim @@ -16,15 +16,14 @@ proc getReferrer*(prev, target: URL; policy: ReferrerPolicy): string = let origin = prev.origin if origin.t == otOpaque: return "" - if prev.scheme != "http" and prev.scheme != "https": - return "" - if target.scheme != "http" and target.scheme != "https": + if prev.schemeType notin {stHttp, stHttps} or + target.schemeType notin {stHttp, stHttps}: return "" case policy of rpNoReferrer: return "" of rpNoReferrerWhenDowngrade: - if prev.scheme == "https" and target.scheme == "http": + if prev.schemeType == stHttps and target.schemeType == stHttp: return "" return $origin & prev.pathname & prev.search of rpSameOrigin: @@ -34,7 +33,7 @@ proc getReferrer*(prev, target: URL; policy: ReferrerPolicy): string = of rpOrigin: return $origin of rpStrictOrigin: - if prev.scheme == "https" and target.scheme == "http": + if prev.schemeType == stHttps and target.schemeType == stHttp: return "" return $origin of rpOriginWhenCrossOrigin: @@ -42,7 +41,7 @@ proc getReferrer*(prev, target: URL; policy: ReferrerPolicy): string = return $origin return $origin & prev.pathname & prev.search of rpStrictOriginWhenCrossOrigin: - if prev.scheme == "https" and target.scheme == "http": + if prev.schemeType == stHttps and target.schemeType == stHttp: return $origin if not origin.isSameOrigin(target.origin): return $origin diff --git a/src/types/url.nim b/src/types/url.nim index cead6717..78c9aed0 100644 --- a/src/types/url.nim +++ b/src/types/url.nim @@ -28,16 +28,34 @@ type HostType* = enum htNone, htDomain, htIpv4, htIpv6, htOpaque + # List of known schemes. + SchemeType* = enum + stUnknown = "" + stAbout = "about" + stBlob = "blob" + stCache = "cache" + stCgiBin = "cgi-bin" + stData = "data" + stFile = "file" + stFtp = "ftp" + stHttp = "http" + stHttps = "https" + stJavascript = "javascript" + stStream = "stream" + stWs = "ws" + stWss = "wss" + URLSearchParams* = ref object list*: seq[tuple[name, value: string]] url: URL URL* = ref object - scheme*: string + scheme: string username* {.jsget.}: string password* {.jsget.}: string opaquePath: bool hostType: HostType + schemeType*: SchemeType port: Option[uint16] hostname* {.jsget.}: string pathname* {.jsget.}: string @@ -86,17 +104,28 @@ proc sread*(r: var PacketReader; url: var URL) = # -1 if not special # 0 if file # > 0 if special -func findSpecialPort(scheme: string): int32 = - case scheme - of "https": return 443 - of "http": return 80 - of "wss": return 443 - of "ws": return 80 - of "file": return 0 - of "ftp": 21 - else: - {.linearScanEnd.} - return -1 +const SpecialPort = [ + stUnknown: -1i32, + stAbout: -1, + stBlob: -1, + stCache: -1, + stCgiBin: -1, + stData: -1, + stFile: 0, + stFtp: 21, + stHttp: 80, + stHttps: 443, + stJavascript: -1, + stStream: -1, + stWs: 80, + stWss: 443, +] + +template isSpecial(url: URL): bool = + SpecialPort[url.schemeType] >= 0 + +func parseSchemeType(buffer: string): SchemeType = + return parseEnumNoCase[SchemeType](buffer).get(stUnknown) func parseIpv6(input: openArray[char]): string = var pieceIndex = 0 @@ -375,7 +404,7 @@ proc parseHost*(input: string; special: bool; hostType: var HostType): string = return asciiDomain proc shortenPath(url: URL) = - if url.scheme == "file" and (url.pathname.len == 3 or + if url.schemeType == stFile and (url.pathname.len == 3 or url.pathname.len == 4 and url.pathname[2] == '/') and url.pathname[0] == '/' and url.pathname[1] in AsciiAlpha and url.pathname[2] == ':': @@ -413,8 +442,8 @@ proc parseSpecialAuthorityIgnoreSlashes(input: openArray[char]; return usAuthority proc parseRelativeSlash(input: openArray[char]; pointer: var int; - isSpecial: bool; base, url: URL): URLState = - if isSpecial and pointer < input.len and input[pointer] in {'/', '\\'}: + base, url: URL): URLState = + if url.isSpecial and pointer < input.len and input[pointer] in {'/', '\\'}: inc pointer return input.parseSpecialAuthorityIgnoreSlashes(pointer) if pointer < input.len and input[pointer] == '/': @@ -428,14 +457,14 @@ proc parseRelativeSlash(input: openArray[char]; pointer: var int; return usPath proc parseRelative(input: openArray[char]; pointer: var int; - specialPort: var int32; base, url: URL): URLState = - assert base.scheme != "file" + base, url: URL): URLState = url.scheme = base.scheme - specialPort = findSpecialPort(url.scheme) + url.schemeType = base.schemeType + assert url.schemeType != stFile if pointer < input.len and input[pointer] == '/' or - specialPort >= 0 and pointer < input.len and input[pointer] == '\\': + url.isSpecial and pointer < input.len and input[pointer] == '\\': inc pointer - return input.parseRelativeSlash(pointer, specialPort >= 0, base, url) + return input.parseRelativeSlash(pointer, base, url) url.username = base.username url.password = base.password url.hostname = base.hostname @@ -457,16 +486,15 @@ proc parseRelative(input: openArray[char]; pointer: var int; return usPath proc parseSpecialRelativeOrAuthority(input: openArray[char]; pointer: var int; - specialPort: var int32; base, url: URL): URLState = + base, url: URL): URLState = if pointer + 1 < input.len and input[pointer] == '/' and input[pointer + 1] == '/': pointer += 2 return input.parseSpecialAuthorityIgnoreSlashes(pointer) - return input.parseRelative(pointer, specialPort, base, url) + return input.parseRelative(pointer, base, url) -proc parseScheme(input: openArray[char]; pointer: var int; - specialPort: var int32; base: Option[URL]; url: URL; override: bool): - URLState = +proc parseScheme(input: openArray[char]; pointer: var int; base: Option[URL]; + url: URL; override: bool): URLState = var buffer = "" var i = pointer while i < input.len: @@ -474,27 +502,28 @@ proc parseScheme(input: openArray[char]; pointer: var int; if c in AsciiAlphaNumeric + {'+', '-', '.'}: buffer &= c.toLowerAscii() elif c == ':': - let port = findSpecialPort(buffer) + let schemeType = parseSchemeType(buffer) + let port = SpecialPort[schemeType] if override: - if (specialPort >= 0) != (port >= 0): + if url.isSpecial != (port >= 0): return usNoScheme - if (url.includesCredentials or url.port.isSome) and buffer == "file": + if (url.includesCredentials or url.port.isSome) and + schemeType == stFile: return usNoScheme - if url.hostType == htNone and url.scheme == "file": + if url.hostType == htNone and url.schemeType == stFile: return usNoScheme - url.scheme = buffer - specialPort = port + url.scheme = move(buffer) + url.schemeType = schemeType if override: - if url.port.isSome and port != int32(url.port.get): + if url.port.isSome and port == int32(url.port.get): url.port = none(uint16) return usDone pointer = i + 1 - if url.scheme == "file": + if url.schemeType == stFile: return usFile - if specialPort >= 0: + if url.isSpecial: if base.isSome and base.get.scheme == url.scheme: - return input.parseSpecialRelativeOrAuthority(pointer, specialPort, - base.get, url) + return input.parseSpecialRelativeOrAuthority(pointer, base.get, url) # special authority slashes state if pointer + 1 < input.len and input[pointer] == '/' and input[pointer + 1] == '/': @@ -516,12 +545,11 @@ proc parseScheme(input: openArray[char]; pointer: var int; return usNoScheme proc parseSchemeStart(input: openArray[char]; pointer: var int; - specialPort: var int32; base: Option[URL]; url: URL; override: bool): - URLState = + base: Option[URL]; url: URL; override: bool): URLState = var state = usNoScheme if pointer < input.len and input[pointer] in AsciiAlpha: # continue to scheme state - state = input.parseScheme(pointer, specialPort, base, url, override) + state = input.parseScheme(pointer, base, url, override) if state == usNoScheme: pointer = 0 # start over if override: @@ -534,27 +562,27 @@ proc parseSchemeStart(input: openArray[char]; pointer: var int; return usFail if base.opaquePath and pointer < input.len and input[pointer] == '#': url.scheme = base.scheme - specialPort = findSpecialPort(url.scheme) + url.schemeType = base.schemeType url.pathname = base.pathname url.opaquePath = base.opaquePath url.search = base.search url.hash = "#" inc pointer return usFragment - if base.scheme == "file": + if base.schemeType == stFile: return usFile - return input.parseRelative(pointer, specialPort, base, url) + return input.parseRelative(pointer, base, url) return state -proc parseAuthority(input: openArray[char]; pointer: var int; isSpecial: bool; - url: URL): URLState = +proc parseAuthority(input: openArray[char]; pointer: var int; url: URL): + URLState = var atSignSeen = false var passwordSeen = false var buffer = "" var beforeBuffer = pointer while pointer < input.len: let c = input[pointer] - if c in {'/', '?', '#'} or isSpecial and c == '\\': + if c in {'/', '?', '#'} or url.isSpecial and c == '\\': break if c == '@': if atSignSeen: @@ -578,8 +606,8 @@ proc parseAuthority(input: openArray[char]; pointer: var int; isSpecial: bool; pointer = beforeBuffer return usHost -proc parseFileHost(input: openArray[char]; pointer: var int; isSpecial: bool; - url: URL; override: bool): URLState = +proc parseFileHost(input: openArray[char]; pointer: var int; url: URL; + override: bool): URLState = let buffer = input.until({'/', '\\', '?', '#'}, pointer) pointer += buffer.len if not override and buffer.isWinDriveLetter(): @@ -589,7 +617,7 @@ proc parseFileHost(input: openArray[char]; pointer: var int; isSpecial: bool; url.hostname = "" else: var t = htNone - let hostname = parseHost(buffer, isSpecial, t) + let hostname = parseHost(buffer, url.isSpecial, t) if hostname == "": return usFail url.hostType = t @@ -600,10 +628,10 @@ proc parseFileHost(input: openArray[char]; pointer: var int; isSpecial: bool; return usFail return usPathStart -proc parseHostState(input: openArray[char]; pointer: var int; isSpecial: bool; - url: URL; override: bool; state: URLState): URLState = - if override and url.scheme == "file": - return input.parseFileHost(pointer, isSpecial, url, override) +proc parseHostState(input: openArray[char]; pointer: var int; url: URL; + override: bool; state: URLState): URLState = + if override and url.schemeType == stFile: + return input.parseFileHost(pointer, url, override) var insideBrackets = false var buffer = "" while pointer < input.len: @@ -612,14 +640,14 @@ proc parseHostState(input: openArray[char]; pointer: var int; isSpecial: bool; if override and state == usHostname: return usFail var t = htNone - let hostname = parseHost(buffer, isSpecial, t) + let hostname = parseHost(buffer, url.isSpecial, t) if hostname == "": return usFail url.hostname = hostname url.hostType = t inc pointer return usPort - elif c in {'/', '?', '#'} or isSpecial and c == '\\': + elif c in {'/', '?', '#'} or url.isSpecial and c == '\\': break else: if c == '[': @@ -628,12 +656,12 @@ proc parseHostState(input: openArray[char]; pointer: var int; isSpecial: bool; insideBrackets = false buffer &= c inc pointer - if isSpecial and buffer == "": + if url.isSpecial and buffer == "": return usFail if override and buffer == "" and (url.includesCredentials or url.port.isSome): return usFail var t = htNone - let hostname = parseHost(buffer, isSpecial, t) + let hostname = parseHost(buffer, url.isSpecial, t) if hostname == "": return usFail url.hostname = hostname @@ -642,15 +670,15 @@ proc parseHostState(input: openArray[char]; pointer: var int; isSpecial: bool; return usFail return usPathStart -proc parsePort(input: openArray[char]; pointer: var int; specialPort: int32; - url: URL; override: bool): URLState = +proc parsePort(input: openArray[char]; pointer: var int; url: URL; + override: bool): URLState = var buffer = "" var i = pointer while i < input.len: let c = input[i] if c in AsciiDigit: buffer &= c - elif c in {'/', '?', '#'} or specialPort >= 0 and c == '\\' or override: + elif c in {'/', '?', '#'} or url.isSpecial and c == '\\' or override: break else: return usFail @@ -661,7 +689,7 @@ proc parsePort(input: openArray[char]; pointer: var int; specialPort: int32; # can't be negative, buffer only includes AsciiDigit if i > 65535: return usFail - if specialPort == i: + if SpecialPort[url.schemeType] == i: url.port = none(uint16) else: url.port = some(uint16(i)) @@ -678,8 +706,8 @@ proc parseFileSlash(input: openArray[char]; pointer: var int; base: Option[URL]; url: URL; override: bool): URLState = if pointer < input.len and input[pointer] in {'/', '\\'}: inc pointer - return input.parseFileHost(pointer, isSpecial = true, url, override) - if base.isSome and base.get.scheme == "file": + return input.parseFileHost(pointer, url, override) + if base.isSome and base.get.schemeType == stFile: let base = base.get url.hostname = base.hostname url.hostType = base.hostType @@ -692,12 +720,13 @@ proc parseFileSlash(input: openArray[char]; pointer: var int; base: Option[URL]; proc parseFile(input: openArray[char]; pointer: var int; base: Option[URL]; url: URL; override: bool): URLState = url.scheme = "file" + url.schemeType = stFile url.hostname = "" url.hostType = htOpaque if pointer < input.len and input[pointer] in {'/', '\\'}: inc pointer return input.parseFileSlash(pointer, base, url, override) - if base.isSome and base.get.scheme == "file": + if base.isSome and base.get.schemeType == stFile: let base = base.get url.hostname = base.hostname url.hostType = base.hostType @@ -722,9 +751,9 @@ proc parseFile(input: openArray[char]; pointer: var int; base: Option[URL]; url.pathname = "" return usPath -proc parsePathStart(input: openArray[char]; pointer: var int; isSpecial: bool; - url: URL; override: bool): URLState = - if isSpecial: +proc parsePathStart(input: openArray[char]; pointer: var int; url: URL; + override: bool): URLState = + if url.isSpecial: if pointer < input.len and input[pointer] in {'/', '\\'}: inc pointer return usPath @@ -754,13 +783,14 @@ func isDoubleDotPathSegment(s: string): bool = s == ".." or s.equalsIgnoreCase(".%2e") or s.equalsIgnoreCase("%2e.") or s.equalsIgnoreCase("%2e%2e") -proc parsePath(input: openArray[char]; pointer: var int; isSpecial: bool; - url: URL; override: bool): URLState = +proc parsePath(input: openArray[char]; pointer: var int; url: URL; + override: bool): URLState = var state = usPath var buffer = "" while pointer < input.len: let c = input[pointer] - if c == '/' or isSpecial and c == '\\' or not override and c in {'?', '#'}: + if c == '/' or url.isSpecial and c == '\\' or + not override and c in {'?', '#'}: if c == '?': url.search = "?" state = usQuery @@ -771,7 +801,7 @@ proc parsePath(input: openArray[char]; pointer: var int; isSpecial: bool; state = usFragment inc pointer break - let slashCond = c != '/' and (not isSpecial or c != '\\') + let slashCond = c != '/' and (not url.isSpecial or c != '\\') if buffer.isDoubleDotPathSegment(): url.shortenPath() if slashCond: @@ -779,7 +809,7 @@ proc parsePath(input: openArray[char]; pointer: var int; isSpecial: bool; elif buffer.isSingleDotPathSegment() and slashCond: url.pathname &= '/' elif not buffer.isSingleDotPathSegment(): - if url.scheme == "file" and url.pathname == "" and + if url.schemeType == stFile and url.pathname == "" and buffer.isWinDriveLetter(): buffer[1] = ':' url.pathname &= '/' @@ -789,7 +819,7 @@ proc parsePath(input: openArray[char]; pointer: var int; isSpecial: bool; buffer.percentEncode(c, PathPercentEncodeSet) inc pointer let slashCond = pointer >= input.len or input[pointer] != '/' and - (not isSpecial or input[pointer] != '\\') + (not url.isSpecial or input[pointer] != '\\') if buffer.isDoubleDotPathSegment(): url.shortenPath() if slashCond: @@ -797,15 +827,15 @@ proc parsePath(input: openArray[char]; pointer: var int; isSpecial: bool; elif buffer.isSingleDotPathSegment() and slashCond: url.pathname &= '/' elif not buffer.isSingleDotPathSegment(): - if url.scheme == "file" and url.pathname == "" and + if url.schemeType == stFile and url.pathname == "" and buffer.isWinDriveLetter(): buffer[1] = ':' url.pathname &= '/' url.pathname &= buffer return state -proc parseQuery(input: openArray[char]; pointer: var int; isSpecial: bool; - url: URL; override: bool): URLState = +proc parseQuery(input: openArray[char]; pointer: var int; url: URL; + override: bool): URLState = #TODO encoding var buffer = "" var i = pointer @@ -816,7 +846,7 @@ proc parseQuery(input: openArray[char]; pointer: var int; isSpecial: bool; buffer &= c inc i pointer = i - let set = if isSpecial: + let set = if url.isSpecial: SpecialQueryPercentEncodeSet else: QueryPercentEncodeSet @@ -832,28 +862,25 @@ proc basicParseURL0(input: openArray[char]; base: Option[URL]; url: URL; var pointer = 0 # The URL is special if this is >= 0. # A special port of "0" means "no port" (i.e. file scheme). - var specialPort = findSpecialPort(url.scheme) let input = input.deleteChars({'\n', '\t'}) let override = stateOverride.isSome var state = stateOverride.get(usSchemeStart) if state == usSchemeStart: - state = input.parseSchemeStart(pointer, specialPort, base, url, override) + state = input.parseSchemeStart(pointer, base, url, override) if state == usAuthority: - state = input.parseAuthority(pointer, specialPort >= 0, url) + state = input.parseAuthority(pointer, url) if state in {usHost, usHostname}: - state = input.parseHostState(pointer, specialPort >= 0, url, override, - state) + state = input.parseHostState(pointer, url, override, state) if state == usPort: - state = input.parsePort(pointer, specialPort, url, override) + state = input.parsePort(pointer, url, override) if state == usFile: - specialPort = 0 #TODO not sure why this is needed... state = input.parseFile(pointer, base, url, override) if state == usPathStart: - state = input.parsePathStart(pointer, specialPort >= 0, url, override) + state = input.parsePathStart(pointer, url, override) if state == usPath: - state = input.parsePath(pointer, specialPort >= 0, url, override) + state = input.parsePath(pointer, url, override) if state == usQuery: - state = input.parseQuery(pointer, specialPort >= 0, url, override) + state = input.parseQuery(pointer, url, override) if state == usFragment: while pointer < input.len: url.hash.percentEncode(input[pointer], FragmentPercentEncodeSet) @@ -881,7 +908,7 @@ proc parseURL*(input: string; base = none(URL); override = none(URLState)): let url = basicParseURL(input, base, stateOverride = override) if url.isNone: return url - if url.get.scheme == "blob": + if url.get.schemeType == stBlob: #TODO blob urls discard return url @@ -1111,14 +1138,14 @@ proc newURL*(s: string; base: Option[string] = none(string)): return parseAPIURL(s, base) proc origin*(url: URL): Origin = - case url.scheme - of "blob": + case url.schemeType + of stBlob: #TODO let pathURL = parseURL(url.pathname) if pathURL.isNone: return Origin(t: otOpaque, s: $url) return pathURL.get.origin - of "ftp", "http", "https", "ws", "wss": + of stFtp, stHttp, stHttps, stWs, stWss: return Origin(t: otTuple, s: url.scheme & "://" & url.host) else: return Origin(t: otOpaque, s: $url) @@ -1129,7 +1156,7 @@ proc origin*(url: URL): Origin = # file URLs which are special-cased for legacy reasons (they become # "file:///blah", but are treated as absoluteURI). func isNetPath(url: URL): bool = - return url.hostType != htNone and url.scheme != "file" + return url.hostType != htNone and url.schemeType != stFile # This follows somewhat different rules from the standard: # * for URLs with a net_path, the origin is opaque. @@ -1160,6 +1187,9 @@ proc setProtocol*(url: URL; s: string) {.jsfset: "protocol".} = discard basicParseURL(s & ':', url = url, stateOverride = some(usSchemeStart)) +func scheme*(url: URL): lent string = + return url.scheme + proc setUsername*(url: URL; username: string) {.jsfset: "username".} = if url.isNetPath(): url.username = username.percentEncode(UserInfoPercentEncodeSet) |