Use filesystem encoding as FileHandler's encoding

instead of `@root.encoding`.
And fallback to ASCII-8BIT when filesystem encoding is US-ASCII.

When `@root.encoding` is not compatible filesystem encoding,
`Encoding::CompatibilityError` raised at `webrick/httpservlet/filehandler.rb:341`.
So `DocumentRoot` must be compatible with filesystem encoding.
This commit is contained in:
Kazuhiro NISHIYAMA 2020-06-19 22:47:08 +09:00
parent 78d4eace02
commit 97c1782db6
No known key found for this signature in database
GPG key ID: 262ED8DBB4222F7A
2 changed files with 14 additions and 10 deletions

View file

@ -212,6 +212,14 @@ module WEBrick
# :stopdoc:
def set_filesystem_encoding(str)
if Encoding.find('filesystem') == Encoding::US_ASCII
str.b
else
str.dup.force_encoding('filesystem')
end
end
def service(req, res)
# if this class is mounted on "/" and /~username is requested.
# we're going to override path information before invoking service.
@ -325,16 +333,7 @@ module WEBrick
def set_filename(req, res)
res.filename = @root
path_info = req.path_info.scan(%r|/[^/]*|)
begin
path_info.map! do |path|
path.force_encoding('filesystem').encode(@root.encoding)
end
rescue EncodingError
path_info.map! do |path|
path.force_encoding(@root.encoding)
end
end
path_info = set_filesystem_encoding(req.path_info).scan(%r|/[^/]*|)
path_info.unshift("") # dummy for checking @root dir
while base = path_info.first