httpd.rb - Simple WEBRICK based Ruby web server

require 'webrick'
 
# The :AccessLog configuration takes an array.
# Each element of the array should be
# a two-element array where the first element
# is the stream (or anything responding to <<)  and
# the second element is the access log format.
# Please see webrick/accesslog.rb for available formats.
 
access_log_stream = File.open('access.log', 'w')
access_log = [ [ access_log_stream, WEBrick::AccessLog::COMBINED_LOG_FORMAT ] ]
 
DocumentRoot = '/home/madan/public_html'
HTTPPort = 8008
 
begin
   d = Dir.open(DocumentRoot)
rescue
   puts "Creating Documentroot"
   mkdir DocumentRoot
end
 
class DownloadHandler < WEBrick::HTTPServlet::AbstractServlet
    def do_get(req,resp)
        p "req : ",req
        p "resp : ",resp
    end
 
    def go_post(req,resp)
        resp
    end
end
 
f = File.open(DocumentRoot+"/index.html", "w")
f.puts "<html><head><title>"
f.puts "It works on port #{HTTPPort}"
f.puts "</title></head><body>"
f.puts "It works on #{HTTPPort}"
f.puts "</body></html>"
f.close
 
s = WEBrick::HTTPServer.new(
  :Port             => HTTPPort,
  :DocumentRoot     => DocumentRoot,
  :FancyIndexing    => true
  #:Logger          => WEBrick::Log.new('download.log'),
  #:AccessLog       => access_log
)
 
# for cgi-bin handling
s.mount("/cgi-bin",
        WEBrick::HTTPServlet::FileHandler,
        "/home/madan/public_html/cgi-bin",
        {:FancyIndexing=>true})
 
trap("INT"){ s.shutdown }
s.start

Using PHP scripts from webrick server

  1. Download and compile php
  2. using above httpd.rb place your php script in the cgi-bin folder, the ruby scripts have to be named .cgi

httpd_rb.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0