From 327182cada3e0873b0c05118ac533f39eb089121 Mon Sep 17 00:00:00 2001 From: gotoyuzo Date: Sat, 24 Jan 2004 16:48:52 +0000 Subject: [PATCH] * lib/webrick/httpserver.rb (WEBrick::HTTPServer#run): support virtual host. * lib/webrick/httpserver.rb (WEBrick::HTTPServer#virtual_host): add new method to register virtual hosting server. * lib/webrick/httpserver.rb (WEBrick::HTTPServer#lookup_server): add new method to lookup virtual hosting server. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5546 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ lib/webrick/httpserver.rb | 22 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6883662e4c..57f4f22ba4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Sun Jan 25 01:45:38 2004 GOTOU Yuuzou + + * lib/webrick/httpserver.rb (WEBrick::HTTPServer#run): support + virtual host. + + * lib/webrick/httpserver.rb (WEBrick::HTTPServer#virtual_host): add + new method to register virtual hosting server. + + * lib/webrick/httpserver.rb (WEBrick::HTTPServer#lookup_server): add + new method to lookup virtual hosting server. + Sat Jan 24 13:06:26 2004 GOTOU Yuuzou * ext/openssl/ossl_x509hame.c (ossl_x509name_initialize): change diff --git a/lib/webrick/httpserver.rb b/lib/webrick/httpserver.rb index 11227cf517..7851ebe201 100644 --- a/lib/webrick/httpserver.rb +++ b/lib/webrick/httpserver.rb @@ -36,22 +36,26 @@ module WEBrick [ $stderr, AccessLog::REFERER_LOG_FORMAT ] ] end + + @virtual_hosts = Array.new end def run(sock) while true res = HTTPResponse.new(@config) req = HTTPRequest.new(@config) + server = self begin req.parse(sock) res.request_method = req.request_method res.request_uri = req.request_uri res.request_http_version = req.http_version res.keep_alive = req.keep_alive? - if handler = @config[:RequestHandler] + server = lookup_server(req) || self + if handler = server[:RequestHandler] handler.call(req, res) end - service(req, res) + server.service(req, res) rescue HTTPStatus::EOFError, HTTPStatus::RequestTimeout => ex res.set_error(ex) rescue HTTPStatus::Error => ex @@ -65,7 +69,7 @@ module WEBrick if req.request_line req.fixup() res.send_response(sock) - access_log(@config, req, res) + server.access_log(@config, req, res) end end break if @http_version < "1.1" @@ -121,6 +125,18 @@ module WEBrick end end + def virtual_host(server) + @virtual_hosts << server + end + + def lookup_server(req) + @virtual_hosts.find{|server| + (server[:Port].nil? || req.port == server[:Port]) && + (server[:BindAddress].nil? || req.addr[3] == server[:BindAddress]) && + (server[:ServerName].nil? || req.host == server[:ServerName]) + } + end + def access_log(config, req, res) param = AccessLog::setup_params(config, req, res) @config[:AccessLog].each{|logger, fmt|