Wednesday, April 05, 2006

Develop A Simple Webserver using Webrick

Last week during my weekend, I got bored with security stuffs. So I tried something new. I read Programming Ruby 2nd edition. In it I found out about Webrick.

I thought this was a cool stuff. With Webrick you can create your own webserver in Ruby. Isn't that cool?

So I hacked the code for a simple webserver :
#!/usr/bin/env ruby
require 'webrick'
include WEBrick

s = HTTPServer.new(
:Port => 2000,

:DocumentRoot => File.join(Dir.pwd,"/home/tedi/public_html")

)

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


After that, I started the webserver :
$ ruby webserver.rb
[2006-04-01 22:24:32] INFO WEBrick 1.3.1
[2006-04-01 22:24:32] INFO ruby 1.8.2 (2004-12-25) [i686-linux]

[2006-04-01 22:24:32] WARN TCPServer Error: Address already in use - bind(2)

[2006-04-01 22:24:32] INFO WEBrick::HTTPServer#start: pid=7117 port=2000

To test it, I launched my browser :


To shutdown the webserver, just press Ctrl-C :
[2006-04-01 22:29:10] INFO going to shutdown ...
[2006-04-01 22:29:10] INFO WEBrick::HTTPServer#start done.

No comments: