Skip to content

Commit

Permalink
Use Addressable instead of Webrick for encode/unencode (#2622)
Browse files Browse the repository at this point in the history
This is a backport of 433a424 from master, but
removing the unrelated changes in the Gemfile.lock.

Co-authored-by: Juan Manuel Cuello <jcuello@fu.do>
  • Loading branch information
Juanmcuello and Juan Manuel Cuello committed Feb 19, 2023
1 parent 7b6a268 commit 59ad2b1
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion middleman-core/lib/middleman-core/builder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'addressable/uri'
require 'pathname'
require 'fileutils'
require 'tempfile'
Expand Down Expand Up @@ -229,7 +230,7 @@ def output_resource(resource)
if resource.binary?
export_file!(output_file, resource.file_descriptor[:full_path])
else
response = @rack.get(::WEBrick::HTTPUtils.escape(resource.request_path))
response = @rack.get(Addressable::URI.encode(resource.request_path))

# If we get a response, save it to a tempfile.
if response.status == 200
Expand Down
3 changes: 2 additions & 1 deletion middleman-core/lib/middleman-core/extensions/asset_hash.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'addressable/uri'
require 'middleman-core/util'
require 'middleman-core/rack'

Expand Down Expand Up @@ -87,7 +88,7 @@ def manipulate_single_resource(resource)
else
# Render through the Rack interface so middleware and mounted apps get a shot
response = @rack_client.get(
::WEBrick::HTTPUtils.escape(resource.destination_path),
Addressable::URI.encode(resource.destination_path),
'bypass_inline_url_rewriter_asset_hash' => 'true'
)

Expand Down
4 changes: 2 additions & 2 deletions middleman-core/lib/middleman-core/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rack/lint'
require 'rack/head'
require 'rack/utils'
require 'webrick'
require 'addressable/uri'

require 'middleman-core/util'
require 'middleman-core/logger'
Expand Down Expand Up @@ -87,7 +87,7 @@ def halt(response)
def process_request(env, req, res)
start_time = Time.now

request_path = WEBrick::HTTPUtils.unescape(env['PATH_INFO'].dup)
request_path = Addressable::URI.unencode(env['PATH_INFO'].dup)
if request_path.respond_to? :force_encoding
request_path.force_encoding('UTF-8')
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'middleman-core/rack'
require 'rspec/expectations'
require 'capybara/cucumber'
require 'webrick'
require 'addressable/uri'

Given /^a clean server$/ do
@initialize_commands = []
Expand Down Expand Up @@ -73,11 +73,11 @@
end

When /^I go to "([^\"]*)"$/ do |url|
visit(WEBrick::HTTPUtils.escape(url))
visit(Addressable::URI.encode(url))
end

Then /^going to "([^\"]*)" should not raise an exception$/ do |url|
expect{ visit(WEBrick::HTTPUtils.escape(url)) }.to_not raise_exception
expect { visit(Addressable::URI.encode(url)) }.to_not raise_exception
end

Then /^the content type should be "([^\"]*)"$/ do |expected|
Expand Down
3 changes: 1 addition & 2 deletions middleman-core/lib/middleman-core/util/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require 'addressable/uri'
require 'memoist'
require 'tilt'
require 'webrick'

require 'middleman-core/contracts'

Expand Down Expand Up @@ -34,7 +33,7 @@ def tilt_class(path)
Contract String => String
def normalize_path(path)
# The tr call works around a bug in Ruby's Unicode handling
WEBrick::HTTPUtils.unescape(path).sub(%r{^/}, '').tr('', '')
Addressable::URI.unencode(path).sub(%r{^/}, '').tr('', '')
end
memoize :normalize_path

Expand Down

0 comments on commit 59ad2b1

Please sign in to comment.