Created
August 5, 2008 08:13
-
-
Save koduki/4040 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 "rubygems" | |
require "rack" | |
include Rack | |
class VerySmallHttpd | |
def get_path env | |
paths = env["PATH_INFO"].split("/") | |
path = (paths.empty?) ? nil : paths[1] | |
end | |
def call(env) | |
req = Rack::Request.new(env) | |
path = get_path env | |
type = "text/html" | |
body = if path | |
type = 'text/plain' unless path =~ /.*\.htm/ | |
open(path).read | |
else | |
files = `ls -1`.split /\n/ | |
files.map! do |f| "<li><a href='#{f}'>#{f}</a></li>" end | |
'<html>' + | |
'<head><title>Index of</title></head>' + | |
'<body>' + | |
'<h1>Index of</h1>' + | |
'<ul>' + files.to_s + '</ul>' + | |
'</body>' + | |
'</html>' | |
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