section id="webrick" xreflabel="WEBrick"
Přesunout tuto sekci do části Programování webových aplikací, případně z ní udělat samostatnou kapitolu v téže části.
Od verze ruby 1.8.0 je WEBrick jako knihovna součástí ruby.
hash parametrů předaných za otazníkem (...?a=2)
@query_string
řetězec parametrů předaných za otazníkem (...?a=2)
Příklad 51.2. Jednoduchý daytime server 1
#!/usr/bin/env ruby require 'webrick' s = WEBrick::GenericServer.new( :Port => 2000 ) trap ("INT") { s.shutdown } s.start {|sock| sock.print(Time.now.to_s + "\r\n") }
Příklad 51.3. Jednoduchý daytime server 2
#!/usr/bin/env ruby # $Id: daytime_server2.rb,v 1.1 2002/06/07 07:10:10 radek Exp $ require 'webrick' class DaytimeServer < WEBrick::GenericServer def run(sock) sock.print(Time.now.to_s + "\r\n") end end s = DaytimeServer.new( :Port => 2000 ) trap ("INT") { s.shutdown } s.start
Příklad 51.4. Jednoduchý HTTP server 1
#!/usr/bin/env ruby # $Id: http_server1.rb,v 1.1 2002/06/07 07:10:10 radek Exp $ require 'webrick' include WEBrick s = HTTPServer.new( :Port => 2000, :DocumentRoot => Dir::pwd + "/htdocs" ) ## mount subdirectories s.mount("/~gotoyuzo", HTTPServlet::FileHandler, "/home/radek/documents/") s.mount("/www", HTTPServlet::FileHandler, "/var/www/") trap ("INT") { s.shutdown } s.start
Příklad 51.5. Jednoduchý HTTPS server 1
#!/usr/bin/env ruby # $Id: https_server1.rb,v 1.1 2002/06/07 07:10:11 radek Exp $ require 'webrick' require 'webrick/https' s = WEBrick::HTTPServer.new( :Port => 2000, :DocumentRoot => Dir::pwd + "/htdocs", :SSLEnable => true, :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE, :SSLCertName => [ ["C","JP"], ["O", "WEBrick.Org"], ["CN", "WWW"] ] ) ## mount subdirectories s.mount("/~gotoyuzo", HTTPServlet::FileHandler, "/home/radek/documents/") s.mount("/www", HTTPServlet::FileHandler, "/var/www/") trap ("INT") { s.shutdown } s.start
Příklad 51.6. Jednoduchý HTTP server se servletem
#!/usr/bin/env ruby # $Id: http_server2.rb,v 1.1 2002/06/07 07:10:10 radek Exp $ require 'webrick' include WEBrick s = HTTPServer.new( :Port => 2000, :DocumentRoot => Dir::pwd + "/htdocs" ) # HTTPServer#mount(path, servletclass) # When a request referring "/hello" is received, # then HTTPServer get an instance of servletclass # and then call a method named do_"a HTTP method". class HelloServlet < HTTPServlet::AbstractServlet def do_GET(req, res) res.body = "<HTML>hello, world.</HTML>" res['Content-Type'] = "text/html" end end s.mount("/hello", HelloServlet) # HTTPServer#mount_proc(path){|req, res| ...} # You can mount also a block by 'mount_proc'. # This block is called when GET or POST. s.mount_proc("/hello/again") {|req, res| res.body = "<HTML>hello (again)</HTML>" res['Content-Type'] = "text/html" } ## mount subdirectories s.mount("/~gotoyuzo", HTTPServlet::FileHandler, "/home/radek/documents/") s.mount("/www", HTTPServlet::FileHandler, "/var/www/") trap ("INT") { s.shutdown } s.start # Run the server
Příklad 51.7. Jednoduchý HTTP server spouštěný z inetd
#!/usr/bin/env ruby # $Id: httpd.in-1.rb,v 1.1 2002/06/07 07:10:10 radek Exp $ log = open("/var/log/webrick/httpd.log", "a") STDERR.reopen(log) # do not send stderr to client. require 'webrick' require 'getopts' getopts nil, 'r:' sock = TCPSocket.for_fd(0, "w+") # create TCPSocket from fd. port = sock.addr[1] s = WEBrick::HTTPServer.new( :DoNotListen => true, :Port => port, :Logger => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG), :DocumentRoot => $OPT_r || "/var/www" ) s.run(sock)
Příklad 51.8. Jednoduchý HTTPS server spouštěný z inetd
#!/usr/bin/env ruby # $Id: httpsd.in-1.rb,v 1.1 2002/06/07 07:10:11 radek Exp $ # sample of HTTPS server spawned from inetd log = open("/var/log/webrick/httpds.log", "a") STDERR.reopen(log) # do not send stderr to client. require 'webrick/https' require 'getopts' getopts nil, 'r:' pkey = cert = cert_name = nil begin data = open(dir + "/conf/sample.key") {|io| io.read} pkey = OpenSSL::PKey::RSA.new(data) data = open(dir + "/conf/sample.crt") {|io| io.read} cert = OpenSSL::X509::Certificate.new(data) rescue $stderr.puts "Switching to use self-signed certificate" cert_name = [ ["C","JP"], ["O","WEBrick.Org"], ["CN", "WWW"] ] end sock = TCPSocket.for_fd(0, "w+") # create TCPSocket from fd. port = sock.addr[1] s = WEBrick::HTTPServer.new( :DoNotListen => true, :Port => port, :Logger => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG), :DocumentRoot => $OPT_r || "/var/www", :SSLEnable => true, :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE, :SSLCertificate => cert, :SSLPrivateKey => pkey, :SSLCertName => cert_name ) s.run(sock)
Další příklad je persistentní servlet.
Příklad 51.9. Persistant Servlets
#!/usr/local/bin/ruby require 'webrick' include WEBrick s = HTTPServer.new( :Port => 2000 ) class HelloServlet < HTTPServlet::AbstractServlet # Overloads AbstractServlet#get_instance # which creates new servant. class << self def get_instance(*arg) self end end def initialize(server, *options) @i = 0 end def do_GET(req, res) res.body = "<HTML>Count #{@i}</HTML>" res['Content-Type'] = "text/html" @i += 1 end end s.mount("/hello", HelloServlet) s.mount("/hello", HelloServlet.new) trap("INT"){ s.shutdown } s.start