Skip to content

Instantly share code, notes, and snippets.

@koduki
Created July 11, 2010 15:02
Show Gist options
  • Save koduki/471614 to your computer and use it in GitHub Desktop.
Save koduki/471614 to your computer and use it in GitHub Desktop.
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