Created
July 11, 2010 15:02
-
-
Save koduki/471614 to your computer and use it in GitHub Desktop.
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 "rack" | |
include Rack | |
class VerySmallHttpd | |
def get_path env | |
paths = env["PATH_INFO"].split("/") | |
paths.shift | |
paths.join("/") | |
end | |
def index path | |
files = (Dir::entries path) | |
files.map!{ |f| "<li><a href='#{f}'>#{f}</a></li>" } | |
'<html>' + | |
'<head><title>Index of</title></head>' + | |
'<body>' + | |
'<h1>Index of</h1>' + | |
'<ul>' + files.join("") + '</ul>' + | |
'</body>' + | |
'</html>' | |
end | |
def call(env) | |
req = Rack::Request.new(env) | |
path = get_path env | |
type = "text/html" | |
body = if path == "" | |
index Dir::pwd | |
else | |
if File::ftype(path) == "directory" | |
index (Dir::pwd + "/" + path) | |
else | |
type = 'text/plain' unless path =~ /.*\.htm/ | |
open(path).read | |
end | |
end | |
head = {"Content-Type" => type, "Pragma" => "no-cache", "Cache-Control"=> "no-cache"} | |
[200, head, [body]] | |
end | |
end | |
Handler::WEBrick.run VerySmallHttpd.new, :Port => 3000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment