Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3017: Refactor remote fetcher r=bronzdoc a=deivid-rodriguez # Description: In rubygems/bundler#7460 I'm vendoring the `uri` library inside `bundler` to add support for using the `uri` version inside Gemfile's just like any other gem. However, that was not the only thing needed since `bundler/setup` also touches some code from this class in rubygems. So the initial approach I took was to duplicate all the code inside bundler, replacing all reference to `URI` with `Bundler::URI` (the vendored namespace). Like in rubygems/bundler@3bcc579. However, this is ugly and hard to maintain so in this PR I'm refactoring this class so that it is enough to override only one method in the `bundler` subclass, like this: https://github.com/bundler/bundler/blob/aa3a25d30383a16e002dd94c209a6078746204a7/lib/bundler/gem_remote_fetcher.rb # Tasks: - [x] Describe the problem / feature - [ ] Write tests - [x] Write code to solve the problem - [ ] Get code review from coworkers / friends I will abide by the [code of conduct](https://github.com/rubygems/rubygems/blob/master/CODE_OF_CONDUCT.md). Co-authored-by: David Rodríguez <deivid.rodriguez@riseup.net>
- Loading branch information
Showing
8 changed files
with
94 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# frozen_string_literal: true | ||
autoload :FileUtils, 'fileutils' | ||
autoload :URI, 'uri' | ||
|
||
require "rubygems/text" | ||
## | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# The UriParser handles parsing URIs. | ||
# | ||
|
||
class Gem::UriParser | ||
|
||
def initialize(uri_namespace) | ||
@uri_namespace = uri_namespace | ||
end | ||
|
||
## | ||
# Parses the #uri, raising if it's invalid | ||
|
||
def parse!(uri) | ||
raise invalid_uri_error unless uri | ||
|
||
# Always escape URI's to deal with potential spaces and such | ||
# It should also be considered that source_uri may already be | ||
# a valid URI with escaped characters. e.g. "{DESede}" is encoded | ||
# as "%7BDESede%7D". If this is escaped again the percentage | ||
# symbols will be escaped. | ||
begin | ||
@uri_namespace.public_send(:parse, uri) | ||
rescue invalid_uri_error | ||
@uri_namespace.public_send(:parse, @uri_namespace.const_get(:DEFAULT_PARSER).escape(uri)) | ||
end | ||
end | ||
|
||
## | ||
# Parses the #uri, returning the original uri if it's invalid | ||
|
||
def parse(uri) | ||
parse!(uri) | ||
rescue invalid_uri_error | ||
uri | ||
end | ||
|
||
private | ||
|
||
def invalid_uri_error | ||
@invalid_uri_error ||= @uri_namespace.const_get(:InvalidURIError) | ||
end | ||
|
||
end |
This file contains 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubygems/uri_parser" | ||
|
||
module Gem::UriParsing | ||
|
||
def parse_uri(source_uri) | ||
return source_uri unless source_uri.is_a?(String) | ||
|
||
uri_parser.parse(source_uri) | ||
end | ||
|
||
private :parse_uri | ||
|
||
def uri_parser | ||
require "uri" | ||
|
||
Gem::UriParser.new(URI) | ||
end | ||
|
||
private :uri_parser | ||
|
||
end |
This file contains 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