[WEBrick] howto rewrite url

S

Simon Strandgaard

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 ?
 
G

GOTOU Yuuzou

In message said:
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
 
S

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)
 
G

GOTOU Yuuzou

In message said:
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.
 
S

Simon Strandgaard

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 :eek:ld_initialize :initialize
alias :eek:ld_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top