Hi,

In message <opru5op2dyoglyup / mail1.telia.com>,
 `Robert Feldt <feldt / ce.chalmers.se>' wrote:
> My idea is to simply write a subclass of HTTPServer
> that adds a virtual_mount method that mounts servlets
> on given paths and UriRegexp's. For the servlet to be invoked the uri must 
> match the UriRegexp and then
> search_servlet is used like now.

I found that HTTPServer is a container of enough parameters
for virtual hosting. (ie. port, bind address, hostname and
url namespace)
In my approach, the entire server is added to another server.

In current WEBrick, access logging couldn't be separated for
each server. Please let me think more.

require 'webrick'

module WEBrick
  class VirtualHostServer < HTTPServer
    def initialize(config)
      super(config)
      @virtual_hosts = []
    end

    def virtual_host(server)
      @virtual_hosts << server
    end

    def service(req, res)
      if server = lookup_server(req)
        server.service(req, res)
      else
        super
      end
    end

    def lookup_server(req) 
      # this condition is a quick hacked.
      # it may be a security issue. should considered more...
      @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
  end
end

## build default server
svr = WEBrick::VirtualHostServer.new(
  :Port         => 10080,
  :BindAddress  => "0.0.0.0"
)
svr.mount_proc("/"){|req, res|
  res.body = "This is default server!"
  res['content-type'] = "text/plain"
}

## build a virtual server for "localhost"
localhost = WEBrick::HTTPServer.new(
  :DoNotListen => true,        # don't forget!
  :Port        => nil,
  :BindAddress => nil,
  :ServerName  => "localhost"
)
localhost.mount_proc("/"){|req, res|
  res.body = "This is localhost!"
  res['content-type'] = "text/plain"
}

svr.virtual_host(localhost)
svr.start

-- 
gotoyuzo