-
Notifications
You must be signed in to change notification settings - Fork 223
Make URI parsing more platform aware #3805
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
Open
pkondzior
wants to merge
1
commit into
Shopify:main
Choose a base branch
from
pkondzior:platform-related-uri-optimisations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,22 +12,22 @@ class Generic | |
| # NOTE: We also define this in the shim | ||
| PARSER = const_defined?(:RFC2396_PARSER) ? RFC2396_PARSER : DEFAULT_PARSER | ||
|
|
||
| # This unsafe regex is the same one used in the URI::RFC2396_REGEXP class with the exception of the fact that we | ||
| # do not include colon as a safe character. VS Code URIs always escape colons and we need to ensure we do the | ||
| # same to avoid inconsistencies in our URIs, which are used to identify resources | ||
| UNSAFE_REGEX = %r{[^\-_.!~*'()a-zA-Z\d;/?@&=+$,\[\]]} | ||
|
|
||
| class << self | ||
| #: (path: String, ?fragment: String?, ?scheme: String, ?load_path_entry: String?) -> URI::Generic | ||
| def from_path(path:, fragment: nil, scheme: "file", load_path_entry: nil) | ||
| # This unsafe regex is the same one used in the URI::RFC2396_REGEXP class with the exception of the fact that we | ||
| # do not include colon as a safe character. VS Code URIs always escape colons and we need to ensure we do the | ||
| # same to avoid inconsistencies in our URIs, which are used to identify resources | ||
| unsafe_regex = %r{[^\-_.!~*'()a-zA-Z\d;/?@&=+$,\[\]]} | ||
|
|
||
| def from_win_path(path:, fragment: nil, scheme: "file", load_path_entry: nil) | ||
| # On Windows, if the path begins with the disk name, we need to add a leading slash to make it a valid URI | ||
| escaped_path = if /^[A-Z]:/i.match?(path) | ||
| PARSER.escape("/#{path}", unsafe_regex) | ||
| PARSER.escape("/#{path}", UNSAFE_REGEX) | ||
| elsif path.start_with?("//?/") | ||
| # Some paths on Windows start with "//?/". This is a special prefix that allows for long file paths | ||
| PARSER.escape(path.delete_prefix("//?"), unsafe_regex) | ||
| PARSER.escape(path.delete_prefix("//?"), UNSAFE_REGEX) | ||
| else | ||
| PARSER.escape(path, unsafe_regex) | ||
| PARSER.escape(path, UNSAFE_REGEX) | ||
| end | ||
|
|
||
| uri = build(scheme: scheme, path: escaped_path, fragment: fragment) | ||
|
|
@@ -38,6 +38,21 @@ def from_path(path:, fragment: nil, scheme: "file", load_path_entry: nil) | |
|
|
||
| uri | ||
| end | ||
|
|
||
| #: (path: String, ?fragment: String?, ?scheme: String, ?load_path_entry: String?) -> URI::Generic | ||
| def from_unix_path(path:, fragment: nil, scheme: "file", load_path_entry: nil) | ||
| escaped_path = PARSER.escape(path, UNSAFE_REGEX) | ||
|
|
||
| uri = build(scheme: scheme, path: escaped_path, fragment: fragment) | ||
|
|
||
| if load_path_entry | ||
| uri.require_path = path.delete_prefix("#{load_path_entry}/").delete_suffix(".rb") | ||
| end | ||
|
|
||
| uri | ||
| end | ||
|
|
||
| alias_method :from_path, Gem.win_platform? ? :from_win_path : :from_unix_path | ||
| end | ||
|
|
||
| #: String? | ||
|
|
@@ -52,21 +67,37 @@ def add_require_path_from_load_entry(load_path_entry) | |
| end | ||
|
|
||
| #: -> String? | ||
| def to_standardized_path | ||
| # On Windows, when we're getting the file system path back from the URI, we need to remove the leading forward | ||
| # slash | ||
| def to_standardized_win_path | ||
| parsed_path = path | ||
|
|
||
| return unless parsed_path | ||
|
|
||
| # we can bail out parsing if there is nothing to unescape | ||
| return parsed_path unless parsed_path.match?(/%[0-9A-Fa-f]{2}/) | ||
|
|
||
| unescaped_path = PARSER.unescape(parsed_path) | ||
|
|
||
| # On Windows, when we're getting the file system path back from the URI, we need to remove the leading forward | ||
| # slash | ||
| if %r{^/[A-Z]:}i.match?(unescaped_path) | ||
| unescaped_path.delete_prefix("/") | ||
| else | ||
| unescaped_path | ||
| end | ||
| end | ||
|
|
||
| alias_method :full_path, :to_standardized_path | ||
| #: -> String? | ||
| def to_standardized_unix_path | ||
| unescaped_path = path | ||
| return unless unescaped_path | ||
|
|
||
| # we can bail out parsing if there is nothing to be unescaped | ||
| return unescaped_path unless unescaped_path.match?(/%[0-9A-Fa-f]{2}/) | ||
|
Comment on lines
+94
to
+95
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question, why does this not need to be escaped? |
||
|
|
||
| PARSER.unescape(unescaped_path) | ||
| end | ||
|
|
||
| alias_method :to_standardized_path, Gem.win_platform? ? :to_standardized_win_path : :to_standardized_unix_path | ||
| alias_method :full_path, Gem.win_platform? ? :to_standardized_win_path : :to_standardized_unix_path | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can we avoid unescaping here?