Skip to content

Commit

Permalink
* lib/webrick/cgi.rb: new methods WEBrick::CGI#[], WEBrick::CGI#logger
Browse files Browse the repository at this point in the history
  and WEBrick::CGI#config. (backported from HEAD)

* lib/webrick/httputils.rb (WEBrick::HTTPUtils.escape_path): should
  not use String#split("/"). (backported from HEAD)


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@8424 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
gotoyuzo committed May 11, 2005
1 parent e08694b commit 6b957b3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
@@ -1,3 +1,11 @@
Wed May 11 16:20:01 2005 GOTOU Yuuzou <gotoyuzo@notwork.org>

* lib/webrick/cgi.rb: new methods WEBrick::CGI#[], WEBrick::CGI#logger
and WEBrick::CGI#config. (backported from HEAD)

* lib/webrick/httputils.rb (WEBrick::HTTPUtils.escape_path): should
not use String#split("/"). (backported from HEAD)

Wed May 11 10:39:37 2005 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>

* lib/tempfile.rb (Tempfile#unlink): fixed typo.
Expand Down
26 changes: 13 additions & 13 deletions lib/webrick/cgi.rb
Expand Up @@ -16,6 +16,8 @@ module WEBrick
class CGI
CGIError = Class.new(StandardError)

attr_reader :config, :logger

def initialize(*args)
if defined?(MOD_RUBY)
unless ENV.has_key?("GATEWAY_INTERFACE")
Expand All @@ -26,7 +28,7 @@ def initialize(*args)
httpv = $1
end
@config = WEBrick::Config::HTTP.dup.update(
:ServerSoftware => ENV["SERVER_SOFTWARE"],
:ServerSoftware => ENV["SERVER_SOFTWARE"] || "null",
:HTTPVersion => HTTPVersion.new(httpv || "1.0"),
:RunOnCGI => true, # to detect if it runs on CGI.
:NPH => false # set true to run as NPH script.
Expand All @@ -39,14 +41,18 @@ def initialize(*args)
@options = args
end

def [](key)
@config[key]
end

def start(env=ENV, stdin=$stdin, stdout=$stdout)
sock = WEBrick::CGI::Socket.new(@config, env, stdin, stdout)
req = HTTPRequest.new(@config)
res = HTTPResponse.new(@config)
unless @config[:NPH] or defined?(MOD_RUBY)
def res.setup_header
unless @header["status"]
phrase = HTTPStatus::reason_phrase(@status)
phrase = HTTPStatus::reason_phrase(@status)
@header["status"] = "#{@status} #{phrase}"
end
super
Expand All @@ -58,12 +64,8 @@ def res.status_line

begin
req.parse(sock)
req.script_name = (env["SCRIPT_NAME"] || "").dup
if env["PATH_INFO"].nil? || env["PATH_INFO"].empty?
req.path_info = nil
else
req.path_info = env["PATH_INFO"].dup
end
req.script_name = (env["SCRIPT_NAME"] || File.expand_path($0)).dup
req.path_info = (env["PATH_INFO"] || "").dup
req.user = env["REMOTE_USER"]
res.request_method = req.request_method
res.request_uri = req.request_uri
Expand Down Expand Up @@ -145,11 +147,9 @@ def initialize(config, env, stdin, stdout)
end

def request_line
meth = @env["REQUEST_METHOD"]
url = @env["SCRIPT_NAME"].dup
if path_info = @env["PATH_INFO"]
url << path_info
end
meth = @env["REQUEST_METHOD"] || "GET"
url = (@env["SCRIPT_NAME"] || File.expand_path($0)).dup
url << @env["PATH_INFO"].to_s
url = WEBrick::HTTPUtils.escape_path(url)
if query_string = @env["QUERY_STRING"]
unless query_string.empty?
Expand Down
8 changes: 5 additions & 3 deletions lib/webrick/httputils.rb
Expand Up @@ -384,9 +384,11 @@ def unescape_form(str)
end

def escape_path(str)
str.split("/").collect{|i|
_escape(i, UNESCAPED_PCHAR)
}.join("/")
result = ""
str.scan(%r{/([^/]*)}).each{|i|
result << "/" << _escape(i[0], UNESCAPED_PCHAR)
}
return result
end

def escape8bit(str)
Expand Down
6 changes: 6 additions & 0 deletions test/webrick/test_httputils.rb
Expand Up @@ -87,4 +87,10 @@ def test_unescape_form
assert_equal("//foo/bar baz", unescape_form("/%2Ffoo/bar+baz"))
assert_equal("/~foo/bar baz", unescape_form("/%7Efoo/bar+baz"))
end

def test_escape_path
assert_equal("/foo/bar", escape_path("/foo/bar"))
assert_equal("/foo/bar/", escape_path("/foo/bar/"))
assert_equal("/%25foo/bar/", escape_path("/%foo/bar/"))
end
end
2 changes: 1 addition & 1 deletion test/webrick/webrick.cgi
Expand Up @@ -4,7 +4,7 @@ require "webrick/cgi"
class TestApp < WEBrick::CGI
def do_GET(req, res)
res["content-type"] = "text/plain"
if p = req.path_info
if (p = req.path_info) && p.length > 0
res.body = p
elsif (q = req.query).size > 0
res.body = q.keys.sort.collect{|key|
Expand Down

0 comments on commit 6b957b3

Please sign in to comment.