summaryrefslogtreecommitdiff
path: root/lib/xmlrpc/httpserver.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/xmlrpc/httpserver.rb')
-rw-r--r--lib/xmlrpc/httpserver.rb47
1 files changed, 26 insertions, 21 deletions
diff --git a/lib/xmlrpc/httpserver.rb b/lib/xmlrpc/httpserver.rb
index dd0d7417c1..66d52139db 100644
--- a/lib/xmlrpc/httpserver.rb
+++ b/lib/xmlrpc/httpserver.rb
@@ -1,3 +1,7 @@
+#
+# Implements a simple HTTP-server by using John W. Small's (jsmall@laser.net)
+# ruby-generic-server.
+#
# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
#
# $Id$
@@ -6,13 +10,11 @@
require "gserver"
-# Implements a simple HTTP-server by using John W. Small's (jsmall@laser.net)
-# ruby-generic-server: GServer.
class HttpServer < GServer
##
- # +handle_obj+ specifies the object, that receives calls from +request_handler+
- # and +ip_auth_handler+
+ # handle_obj specifies the object, that receives calls to request_handler
+ # and ip_auth_handler
def initialize(handle_obj, port = 8080, host = DEFAULT_HOST, maxConnections = 4,
stdlog = $stdout, audit = true, debug = true)
@handler = handle_obj
@@ -21,16 +23,19 @@ class HttpServer < GServer
private
+ # Constants -----------------------------------------------
+
CRLF = "\r\n"
HTTP_PROTO = "HTTP/1.0"
SERVER_NAME = "HttpServer (Ruby #{RUBY_VERSION})"
- # Default header for the server name
DEFAULT_HEADER = {
"Server" => SERVER_NAME
}
- # Mapping of status codes and error messages
+ ##
+ # Mapping of status code and error message
+ #
StatusCodeMapping = {
200 => "OK",
400 => "Bad Request",
@@ -40,6 +45,8 @@ private
500 => "Internal Server Error"
}
+ # Classes -------------------------------------------------
+
class Request
attr_reader :data, :header, :method, :path, :proto
@@ -67,7 +74,10 @@ private
end
end
- # A case-insensitive Hash class for HTTP header
+
+ ##
+ # a case-insensitive Hash class for HTTP header
+ #
class Table
include Enumerable
@@ -93,15 +103,15 @@ private
@hash.each {|k,v| yield k.capitalize, v }
end
- # Output the Hash table for the HTTP header
def writeTo(port)
each { |k,v| port << "#{k}: #{v}" << CRLF }
end
end # class Table
- # Generates a Hash with the HTTP headers
- def http_header(header=nil) # :doc:
+ # Helper Methods ------------------------------------------
+
+ def http_header(header=nil)
new_header = Table.new(DEFAULT_HEADER)
new_header.update(header) unless header.nil?
@@ -111,14 +121,11 @@ private
new_header
end
- # Returns a string which represents the time as rfc1123-date of HTTP-date
- def http_date( aTime ) # :doc:
+ def http_date( aTime )
aTime.gmtime.strftime( "%a, %d %b %Y %H:%M:%S GMT" )
end
- # Returns a string which includes the status code message as,
- # http headers, and body for the response.
- def http_resp(status_code, status_message=nil, header=nil, body=nil) # :doc:
+ def http_resp(status_code, status_message=nil, header=nil, body=nil)
status_message ||= StatusCodeMapping[status_code]
str = ""
@@ -129,11 +136,9 @@ private
str
end
- # 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.
- def serve(io) # :doc:
+ # Main Serve Loop -----------------------------------------
+
+ def serve(io)
# perform IP authentification
unless @handler.ip_auth_handler(io)
io << http_resp(403, "Forbidden")
@@ -165,7 +170,7 @@ private
io << http_resp(response.status, response.status_message,
response.header, response.body)
- rescue Exception
+ rescue Exception => e
io << http_resp(500, "Internal Server Error")
end