From 6fe1919486111bcdd5ef41fea15b0e82f7f50d82 Mon Sep 17 00:00:00 2001 From: Kazuhiro NISHIYAMA Date: Fri, 19 Jun 2020 20:06:26 +0900 Subject: Fix failure on mswin CI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://rubyci.org/logs/mswinci.japaneast.cloudapp.azure.com/vc12-x64/ruby-master/log/20200619T054159Z.fail.html.gz ``` 1) Failure: WEBrick::TestFileHandler#test_cjk_in_path [D:/tmp/mswin-build20200619-14304-utgij/ruby/test/webrick/utils.rb:72]: exceptions on 2 threads: webrick log start: [2020-06-19 16:28:42] ERROR `/あ.txt' not found. webrick log end Filesystem encoding is Windows-31J. <"200"> expected but was <"404">. --- <[]> expected but was <["[2020-06-19 16:28:42] ERROR `/\xE3\x81\x82.txt' not found.\n"]>. ``` `prevent_directory_traversal` treats `path_info` as filesystem encoding. So path_info should be filesystem encoding in request URL. On some environments, fallback to ASCII-8BIT when EncodingError. --- lib/webrick/httpservlet/filehandler.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lib/webrick') diff --git a/lib/webrick/httpservlet/filehandler.rb b/lib/webrick/httpservlet/filehandler.rb index 06d1a3067d..f41a5b07cf 100644 --- a/lib/webrick/httpservlet/filehandler.rb +++ b/lib/webrick/httpservlet/filehandler.rb @@ -324,8 +324,17 @@ module WEBrick end def set_filename(req, res) - res.filename = @root.b + 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.unshift("") # dummy for checking @root dir while base = path_info.first -- cgit v1.2.3