Implements a simple HTTP-server by using John W. Small's (jsmall@laser.net) ruby-generic-server: GServer.

Namespace
Methods
H
N
S
Constants
CRLF = "\r\n"
 
HTTP_PROTO = "HTTP/1.0"
 
SERVER_NAME = "HttpServer (Ruby #{RUBY_VERSION})"
 
DEFAULT_HEADER = { "Server" => SERVER_NAME }
 

Default header for the server name

StatusCodeMapping = { 200 => "OK", 400 => "Bad Request", 403 => "Forbidden", 405 => "Method Not Allowed", 411 => "Length Required", 500 => "Internal Server Error" }
 

Mapping of status codes and error messages

Class Public methods
new(handle_obj, port = 8080, host = DEFAULT_HOST, maxConnections = 4, stdlog = $stdout, audit = true, debug = true)

handle_obj specifies the object, that receives calls from request_handler and ip_auth_handler

# File lib/xmlrpc/httpserver.rb, line 16
def initialize(handle_obj, port = 8080, host = DEFAULT_HOST, maxConnections = 4,
               stdlog = $stdout, audit = true, debug = true)
  @handler = handle_obj
  super(port, host, maxConnections, stdlog, audit, debug)
end
Instance Private methods
http_date( aTime )

Returns a string which represents the time as rfc1123-date of HTTP-date

# File lib/xmlrpc/httpserver.rb, line 115
def http_date( aTime ) # :doc:
  aTime.gmtime.strftime( "%a, %d %b %Y %H:%M:%S GMT" )
end
http_header(header=nil)

Generates a Hash with the HTTP headers

# File lib/xmlrpc/httpserver.rb, line 104
def http_header(header=nil) # :doc:
  new_header = Table.new(DEFAULT_HEADER)
  new_header.update(header) unless header.nil?

  new_header["Connection"] = "close"
  new_header["Date"]       = http_date(Time.now)

  new_header
end
http_resp(status_code, status_message=nil, header=nil, body=nil)

Returns a string which includes the status code message as, http headers, and body for the response.

# File lib/xmlrpc/httpserver.rb, line 121
def http_resp(status_code, status_message=nil, header=nil, body=nil) # :doc:
  status_message ||= StatusCodeMapping[status_code]

  str = ""
  str << "#{HTTP_PROTO} #{status_code} #{status_message}" << CRLF
  http_header(header).writeTo(str)
  str << CRLF
  str << body unless body.nil?
  str
end
serve(io)

Handles the HTTP request and writes the response back to the client, io.

If an Exception is raised while handling the request, the client will receive a 500 “Internal Server Error” message.

# File lib/xmlrpc/httpserver.rb, line 136
def serve(io) # :doc:
  # perform IP authentication
  unless @handler.ip_auth_handler(io)
    io << http_resp(403, "Forbidden")
    return
  end

  # parse first line
  if io.gets =~ /^(\S+)\s+(\S+)\s+(\S+)/
    request = Request.new(io, $1, $2, $3)
  else
    io << http_resp(400, "Bad Request")
    return
  end

  # parse HTTP headers
  while (line=io.gets) !~ /^(\n|\r)/
    if line =~ /^([\w-]+):\s*(.*)$/
      request.header[$1] = $2.strip
    end
  end

  io.binmode
  response = Response.new

  # execute request handler
  @handler.request_handler(request, response)

  # write response back to the client
  io << http_resp(response.status, response.status_message,
                  response.header, response.body)

rescue Exception
  io << http_resp(500, "Internal Server Error")
end