[WEBrick] howto rewrite url

I have searched on google, but couldn't find any useful info on this.
For instance I would like to rename urls from "show-all.html" to "show.cgi?all".

Any ideas ?

···

--
Simon Strandgaard

In message <df1390cc04112810447ed53aa@mail.gmail.com>,

I have searched on google, but couldn't find any useful info on this.
For instance I would like to rename urls from "show-all.html" to "show.cgi?all".

If the modified URL is allowed to be accessed directly,
redirection is easy to do it.

require "webrick"

request_callback = Proc.new{|req, res|
  if %r{/show-all.html$} =~ req.path_info
    res.set_redirect(WEBrick::HTTPStatus::Found, "show.cgi?all")
  end
}

httpd = WEBrick::HTTPServer.new(
  :Port=>10080,
  :RequestCallback => request_callback,
  :DocumentRoot => Dir.pwd
)
trap(:INT){ httpd.shutdown }
httpd.start

···

`Simon Strandgaard <neoneye@gmail.com>' wrote:

--
gotoyuzo

[snip]

request_callback = Proc.new{|req, res|
  if %r{/show-all.html$} =~ req.path_info
    res.set_redirect(WEBrick::HTTPStatus::Found, "show.cgi?all")
  end
}

Pretty simple.. nice.

Thanks Gotou.

···

On Tue, 30 Nov 2004 04:07:30 +0900, GOTOU Yuuzou <gotoyuzo@notwork.org> wrote:

--
Simon Strandgaard

[snip]

This transmits a redirect request to the user-agent.

I am curious to if its possible to rename the url of the
request "on the server".. so that user-agent never knows about that
redirecting occured?

(sorry for all these recent webrick questions)

···

On Mon, 29 Nov 2004 20:18:07 +0100, Simon Strandgaard <neoneye@gmail.com> wrote:

On Tue, 30 Nov 2004 04:07:30 +0900, GOTOU Yuuzou <gotoyuzo@notwork.org> wrote:
[snip]
> request_callback = Proc.new{|req, res|
> if %r{/show-all.html$} =~ req.path_info
> res.set_redirect(WEBrick::HTTPStatus::Found, "show.cgi?all")
> end
> }

--
Simon Strandgaard

In message <df1390cc041129131574b9146d@mail.gmail.com>,

···

`Simon Strandgaard <neoneye@gmail.com>' wrote:

I am curious to if its possible to rename the url of the
request "on the server".. so that user-agent never knows about that
redirecting occured?

It isn't possible yet. First, I'd like to make it possible
in 1.9.

--
gotoyuzo

I have investigated how to rewrite urls.. and have made this code.
It rewrites urls from "show-all.html" to "show.cgi?all".
And from "show-abcd.html" to "show.cgi?first=a;last=d".

I hope its usable.

require "webrick"

class WEBrick::HTTPServer
  alias :old_initialize :initialize
  alias :old_service :service
  def initialize(config={}, default=WEBrick::Config::HTTP)
    old_initialize(config, default)
    @rewrite_rules =
  end
  def rewrite(pattern, subst)
    @logger.debug("rewrite rule %s -> %s." %
      [pattern.inspect, subst])
    @rewrite_rules << [pattern, subst]
  end
  def service(req, res)
    path = req.path
    @rewrite_rules.each do |pattern, subst|
      if pattern =~ path
        new_path = path.gsub(pattern, subst)
        @logger.debug("rewrote url from %s to %s" % [path, new_path])
        req.instance_variable_set("@path", new_path)
        # TODO: req.query = reload
        break
      end
    end
    old_service(req, res)
  end
end

class PageServlet < WEBrick::HTTPServlet::AbstractServlet
  def do_GET(req, res)
    ary = [
      ["path", req.path],
      ["path_info", req.path_info],
      ["request_uri", req.request_uri.inspect],
      ["unparsed_uri", req.unparsed_uri.inspect],
      ["query", req.query.inspect],
      ["query_string", req.query_string.inspect]
    ]
    rows = ary.map{|line|
      cells = line.map{|cell| "<td>#{cell}</td>"}.join
      "<tr>#{cells}</tr>"
    }.join
    str = "<table>#{rows}</table>"
    res.body = "<html><body>#{str}</body></html>"
    res['Content-Type'] = "text/html; charset=iso-8859-1"
  end
end

if $0 == __FILE__
  s = WEBrick::HTTPServer.new(
    :Port => 10080,
    :Logger => WEBrick::Log.new($stderr, WEBrick::Log::DEBUG),
    :AccessLog => [
      [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
      [ $stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT ],
      [ $stderr, WEBrick::AccessLog::AGENT_LOG_FORMAT ],
    ]
  )
  s.mount("/", PageServlet)
  s.mount("/show.cgi", PageServlet)
  s.rewrite(/show-all.html(?=$|\?)/, "show.cgi?all")
  s.rewrite(/show-abcd.html$/, "show.cgi?first=a;last=d")
  trap(:INT) do
    #fork{ s.shutdown }
    exit!
  end
  s.start
end

···

On Tue, 30 Nov 2004 08:12:29 +0900, GOTOU Yuuzou <gotoyuzo@notwork.org> wrote:

In message <df1390cc041129131574b9146d@mail.gmail.com>,
`Simon Strandgaard <neoneye@gmail.com>' wrote:
> I am curious to if its possible to rename the url of the
> request "on the server".. so that user-agent never knows about that
> redirecting occured?

It isn't possible yet. First, I'd like to make it possible
in 1.9.