Index: [Article Count Order] [Thread]

Date: Sat, 20 Nov 2004 08:01:28 +0900 (JST)
From: GOTOU Yuuzou <gotoyuzo@notwork.org>
Subject: [webricken:130] Re: Invoking a FileHandler from a mount_proc?
To: webricken@notwork.org
Message-Id: <20041120.080128.295394621.gotoyuzo@sawara.does.notwork.org>
In-Reply-To: <32734.167.203.48.180.1100893533.squirrel@167.203.48.180>
References: <32734.167.203.48.180.1100893533.squirrel@167.203.48.180>
X-Mail-Count: 00130

In message <32734.167.203.48.180.1100893533.squirrel@167.203.48.180>,
 `"Lloyd Zusman" <ljz@asfast.com>' wrote:
> I have a mount_proc that handles a range of URL's.  After doing
> some initial processing within the mount_proc, I want to pass a
> subset of these URL's to a FileHandler; for the rest of the URL's
> I just want to output a message.

[snip]

> What do I put between the 'if' and the 'else' in order to send the
> matching requests to a FileHandler?

Setup an instance of FileHandler by get_instance() and call
service().

In the following code, the mounted proc maps /icons/*.png
into files under IconDir. FileHandler will return the
content of the file which indicated by IconDir + req.path_info.

  require "webrick"

  IconDir = File.expand_path("~/public_html/icons")
  httpd = WEBrick::HTTPServer.new(:Port => 8080)

  httpd.mount_proc("/"){|req, res|
    if %r{^(/icons)(/[^/]+.png)$} =~ req.path
      req.script_name = $1
      req.path_info = $2
      klass = WEBrick::HTTPServlet::FileHandler
      fh = klass.get_instance(httpd, IconDir)
      fh.service(req, res)
    else
      res.body = 'No FileHandler -- just this simple message'
      res['Content-type'] = 'text/plain'
    end
  }
  Signal.trap(:INT){ httpd.shutdown }
  httpd.start

hope this helps,

-- 
gotoyuzo