From 91e17a2661310ecc97c103e86aabb22fd8be3ba0 Mon Sep 17 00:00:00 2001 From: Jonathan Flat Date: Wed, 22 Jan 2025 16:53:40 -0800 Subject: [PATCH] (143159003) Don't encode colon if URLComponents path starts with colon --- .../URL/URLComponents.swift | 17 +++++++++++++---- Tests/FoundationEssentialsTests/URLTests.swift | 10 ++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Sources/FoundationEssentials/URL/URLComponents.swift b/Sources/FoundationEssentials/URL/URLComponents.swift index d2d156c3c..43bd493be 100644 --- a/Sources/FoundationEssentials/URL/URLComponents.swift +++ b/Sources/FoundationEssentials/URL/URLComponents.swift @@ -367,14 +367,23 @@ public struct URLComponents: Hashable, Equatable, Sendable { } private var percentEncodedPathNoColon: String { - guard percentEncodedPath.utf8.first(where: { $0 == ._colon || $0 == ._slash }) == ._colon else { - return percentEncodedPath + let p = percentEncodedPath + guard p.utf8.first(where: { $0 == ._colon || $0 == ._slash }) == ._colon else { + return p } - let colonEncodedPath = Array(percentEncodedPath.utf8).replacing( + if p.utf8.first == ._colon { + // In the rare case that an app relies on URL allowing an empty + // scheme and passes its URL string directly to URLComponents + // to modify other components, we need to return the path without + // encoding the colons. + return p + } + let firstSlash = p.utf8.firstIndex(of: ._slash) ?? p.endIndex + let colonEncodedSegment = Array(p[..