Skip to content

Instantly share code, notes, and snippets.

@bachue
Last active December 19, 2015 11:38
Show Gist options
  • Save bachue/5948604 to your computer and use it in GitHub Desktop.
Save bachue/5948604 to your computer and use it in GitHub Desktop.
When I visit the file system by ruby httpd, it will crash when some Chinese file names in the folder. I debugged it here, the encoding of regular expression is ascii, but the Chinese file names encoding is UTF-8, so it could be the root cause.
From: /Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httputils.rb @ line 444 WEBrick::HTTPUtils._escape:
440: def _escape(str, regex)
441: begin
442: str.gsub(regex){ "%%%02X" % $1.ord }
443: rescue Exception
=> 444: require 'pry'; binding.pry
445: end
446: end
[1] pry(WEBrick::HTTPUtils)> str
=> "0day安全:软件漏洞分析技术.pdf"
[2] pry(WEBrick::HTTPUtils)> str.encoding
=> #<Encoding:UTF-8>
[3] pry(WEBrick::HTTPUtils)> regex
=> /([\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\v\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\ <>\#%"\{\}\|\\\^\[\]`\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF])/n
[4] pry(WEBrick::HTTPUtils)> regex.encoding
=> #<Encoding:ASCII-8BIT>
[5] pry(WEBrick::HTTPUtils)> str.gsub(regex){ "%%%02X" % $1.ord }
Encoding::CompatibilityError: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
from (pry):5:in `gsub'
[6] pry(WEBrick::HTTPUtils)>
➜ 0 ~/Google Drive/Books ruby -run -e httpd . -p5000
[2013-07-08 21:03:36] INFO WEBrick 1.3.1
[2013-07-08 21:03:36] INFO ruby 2.0.0 (2013-05-14) [x86_64-darwin12.3.0]
[2013-07-08 21:03:36] INFO WEBrick::HTTPServer#start: pid=2138 port=5000
[2013-07-08 21:03:43] ERROR Encoding::CompatibilityError: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httputils.rb:440:in `gsub'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httputils.rb:440:in `_escape'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httputils.rb:455:in `escape'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpservlet/filehandler.rb:465:in `block in set_dir_list'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpservlet/filehandler.rb:457:in `each'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpservlet/filehandler.rb:457:in `set_dir_list'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpservlet/filehandler.rb:218:in `do_GET'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpservlet/abstract.rb:106:in `service'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpservlet/filehandler.rb:213:in `service'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
localhost - - [08/Jul/2013:21:03:43 CST] "GET / HTTP/1.1" 500 350
- -> /
[2013-07-08 21:03:44] ERROR `/favicon.ico' not found.
localhost - - [08/Jul/2013:21:03:44 CST] "GET /favicon.ico HTTP/1.1" 404 279
- -> /favicon.ico
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment