Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

WEBrick servlet with HTTP authentication (See related posts)

// HTTP authentication for a directory listing


require 'webrick'
include WEBrick

dir = Dir::pwd
port = 1234

authenticate = Proc.new do |req, res|
  HTTPAuth.basic_auth(req, res, '') do |user, password|
    user == 'foo' && password == 'bar'
  end
end

s = HTTPServer.new(:Port => port, :ServerType => Daemon)
s.mount('/', HTTPServlet::FileHandler, dir,
  :FancyIndexing => true,
  :HandlerCallback => authenticate # Hook up the authentication proc.
)

trap('INT') { s.shutdown }
s.start

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts