Skip to content

Don't encode colon if URLComponents path starts with colon #1139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

jrflat
Copy link
Contributor

@jrflat jrflat commented Jan 24, 2025

Apps can take a URL string like

://host/path

which URL now allows (after the compatibility change to go back to allowing an empty scheme), and pass it to URLComponents(string:). URLComponents follows RFC 3986, which says a scheme cannot be empty, so it interprets the whole string as a path. The app could then use components.queryItems = ... to assign query items and call components.url, which needs to generate the new .string.

During string generation, URLComponents percent encodes the colon at the beginning of the path, so the URL returned does not have the scheme, host, and path it started out with. We can fix this by not percent-encoding any colons in the first path segment if the colon is the first character in the string.

This still preserves the behaviors of both URL and URLComponents: URL can interpret the colon as an empty scheme like before, and URLComponents, knowing that a scheme cannot be empty according to RFC 3986, can still interpret the string as a relative path.

@jrflat
Copy link
Contributor Author

jrflat commented Jan 24, 2025

@swift-ci please test

@jrflat jrflat merged commit 0e6c827 into swiftlang:main Jan 24, 2025
2 of 3 checks passed
jrflat added a commit to jrflat/swift-foundation that referenced this pull request Mar 5, 2025
jrflat added a commit to jrflat/swift-foundation that referenced this pull request Mar 5, 2025
parkera pushed a commit that referenced this pull request Mar 5, 2025
* (141549683) Restore behavior of URL(string: "") returning nil (#1103)

* (142076445) Allow URL.standardized to return an empty string URL (#1110)

* (142076445) Allow URL.standardized to return an empty string URL

* Add ?? self to prevent force-unwrap

* (142446243) Compatibility behaviors for Swift URL (#1113)

* (142589056) URLComponents.string should percent-encode colons in first path segment if needed (#1117)

* (142667792) URL.absoluteString crashes if baseURL starts with colon (#1119)

* (143159003) Don't encode colon if URLComponents path starts with colon (#1139)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants