たむらです。
自分でも挑戦したのですが、うまく動かないのでアドバイスお願いします。
WEBrickでの Basic認証です。
require 'webrick'
include WEBrick
s = HTTPServer.new( :Port => 2000, :BindAddress => 'localhost')
class HelloServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
fmt = "%Y-%m-%D %H:%M:%S"
res.body = "<HTML>hello, world.(#{Time.now.strftime(fmt)})</HTML>"
res['Content-Type'] = "text/html"
end
def basic_auth(req, res)
STDERR.puts "basic auth ..."
if proc = Proc.new{|user,pass|
user = 'ruby' && pass = encode64('matz')
} then
realm = "Basic Auth"
WEBrick::HTTPAuth::basic_auth(req,res,realm,&proc)
end
req.header.delete("basic-authorization")
end
def service(req, res)
basic_auth(req, res)
super
end
end
s.mount("/", HelloServlet)
trap("INT"){ s.shutdown }
s.start
% w3m -dump_both http://localhost:2000/
HTTP/1.1 200 OK
Connection: Keep-Alive
Date: Tue, 17 Dec 2002 15:35:23 GMT
Content-Type: text/html
Server: WEBrick/1.2.3 (2002-09-23) (Ruby/1.6.8/2002-11-09)
Content-Length: 53
ということで、どうして 401 のステータスが送られてくれないのか判らず
ギブアップ。proc の指定もいまいち自信なしなのです。
--
たむら (http://tamura.tdiary.net)