Hi,
I wrote custom webrick servlet which parses *.html files with embedded
ruby code. It works fine on Linux, but on Windows it doesn't work properly
because pictures that are supposed to be on a web page do not show.
What can I do?
#!/usr/bin/ruby
require 'webrick'
include WEBrick
class CustomServlet < HTTPServlet::AbstractServlet
def do_GET(req, resp)
unless defined?(ERB)
@logger.warn "#{self.class}: ERB not defined."
raise HTTPStatus::Forbidden, "ERBHandler cannot work."
end
#puts req.inspect
#puts "++++ #{req.request_uri} +++++++++++++++"
str=req.inspect
path=Dir.pwd+req.path
uri=req.request_uri.to_s
if File.stat(path).directory?
resp.body << "\n<html><head><title>Directory</title></head><body>\n"
resp.body << "<h2>Directory: #{req.path}</h2>"
entries=Dir.entries(path)
resp.body << '<a href="/">Root directory</a>'+"<br>\n"
entries[1..-1].each {|x|
resp.body << '<a href="' << uri
resp.body << '/' if req.path[-1,1] != '/'
resp.body << x << '">'
x='Parent Directory' if x=='..'
resp.body << x << "</a><br>\n"
}
resp.body << "\n</body></html>"
else
#puts 'going to process file '+path
file=File.open(path).read #load normal file
if (path[-5..-1]=='.html' or path[-4..-1]=='.htm')
rhtml=ERB.new(file) #convert served string into code
#rhtml.run #run the code
file=rhtml.result #copy result to another string
resp.body << file
else
resp.body << file
end #if
end #if
end#def
end#class
def start_webrick(config = {})
config.update(:Port => 8080)
server = HTTPServer.new(config)
yield server if block_given?
['INT', 'TERM'].each {|signal|
trap(signal) {server.shutdown}
}
server.start
end
start_webrick {|server|
server.mount('/',CustomServlet,"#{Dir.pwd}/proba")
}