Created
October 13, 2014 16:01
-
-
Save nafu/aaed90d7dbe37e1e4e3e to your computer and use it in GitHub Desktop.
WEBrick for Wget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
class TestContentServlet < WEBrick::HTTPServlet::AbstractServlet | |
def do_GET(req, res) | |
res.body = get_body(req.path) | |
res.content_type = WEBrick::HTTPUtils.mime_type( | |
req.path_info, WEBrick::HTTPUtils::DefaultMimeTypes) | |
end | |
def get_body(path) | |
return html_content(path) if path =~ /\.html$/ | |
return txt_content(path) if path =~ /\.txt$/ | |
'dummy' | |
end | |
def html_content(path) | |
node = path[0..-6] | |
<<HTML | |
<html><head><title>#{path}</title></head> | |
<body><p> | |
<a href="#{node}/1.html">#{node}/1.html</a><br> | |
<a href="#{node}/2.html">#{node}/2.html</a><br> | |
<a href="#{node}.txt">#{node}.txt</a><br> | |
<a href="http://localhost:7777#{node}.htm">#{node}.htm</a><br> | |
<a href='/1.html'>/1.html</a><br> | |
</p></body></html> | |
HTML | |
end | |
def txt_content(path) | |
"This is #{path}" | |
end | |
end | |
srv = WEBrick::HTTPServer.new(BindAddress: '127.0.0.1', Port: 7777) | |
srv.mount('/', TestContentServlet) | |
trap('INT') { srv.shutdown } | |
srv.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment