Skip to content

Instantly share code, notes, and snippets.

@koduki
Created August 5, 2008 08:13
Show Gist options
  • Save koduki/4040 to your computer and use it in GitHub Desktop.
Save koduki/4040 to your computer and use it in GitHub Desktop.
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