[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[WEBrick] howto rewrite url

Simon Strandgaard

11/28/2004 6:44:00 PM

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


5 Answers

GOTOU Yuuzou

11/29/2004 7:08:00 PM

0

Simon Strandgaard

11/29/2004 7:18:00 PM

0

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
> }


Pretty simple.. nice.

Thanks Gotou.

--
Simon Strandgaard


Simon Strandgaard

11/29/2004 9:16:00 PM

0

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
> > }
[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)

--
Simon Strandgaard


GOTOU Yuuzou

11/29/2004 11:12:00 PM

0

Simon Strandgaard

12/2/2004 7:29:00 PM

0

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.
>


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