WEBrick素人(2)

"Rack+WEBrick素人"の例は、動的生成の例だから、書き直してみた。

hello_webrick.rb

#!/usr/bin/env ruby

require 'webrick'

include WEBrick

class HelloServlet < HTTPServlet::AbstractServlet
  def do_GET(req, res)
    res.body = "Hello, World!"
    res['Content-Type'] = 'text/plain'
  end
end

s = HTTPServer.new(
  :Port => 8000
)

s.mount('/', HelloServlet)

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

結果
http://localhost:8000

http://localhost:8000/

GET / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; ja-JP-mac; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ja,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 200 OK 
Connection: Keep-Alive
Date: Thu, 22 Jan 2009 12:30:59 GMT
Content-Type: text/plain
Server: WEBrick/1.3.1 (Ruby/1.8.6/2007-09-23)
Content-Length: 13

あ、Content-Lengthが一緒になった。いや、別にいいんだけど。