class Rack::Handler::HTTP

A Rack handler for {Net::HTTP::Server}.

Constants

DEFAULT_ENV

The default environment settings.

SPECIAL_HEADERS

Special HTTP Headers used by Rack::Request

Public Class Methods

new(app,options={}) click to toggle source

Initializes the handler.

@param [#call] app

The application the handler will be passing requests to.

@param [Hash] options

Additional options.

@option options [String] :Host

The host to bind to.

@option options [Integer] :Port

The port to listen on.
# File lib/rack/handler/http.rb, line 49
def initialize(app,options={})
  @app     = app
  @options = options
  @server  = nil
end
run(app,options={}) click to toggle source

Creates a new handler and begins handling HTTP Requests.

@see initialize

# File lib/rack/handler/http.rb, line 60
def self.run(app,options={})
  new(app,options).run
end

Public Instance Methods

call(request,stream) click to toggle source

Handles an HTTP Request.

@param [Hash] request

An HTTP Request received from {Net::HTTP::Server}.

@param [Net::HTTP::Server::Stream, Net::HTTP::Server::ChunkedStream] stream

The stream that represents the body of the request.

@return [Array<Integer, Hash, Array>]

The response status, headers and body.
# File lib/rack/handler/http.rb, line 90
def call(request,stream)
  request_uri    = request[:uri]
  remote_address = stream.socket.remote_address
  local_address  = stream.socket.local_address

  env = {}

  # add the default values
  env.merge!(DEFAULT_ENV)

  # populate
  env['rack.input'] = Rack::RewindableInput.new(stream)

  if request_uri[:scheme]
    env['rack.url_scheme'] = request_uri[:scheme].to_s
  end

  env['SERVER_NAME']     = local_address.getnameinfo[0]
  env['SERVER_PORT']     = local_address.ip_port.to_s
  env['SERVER_PROTOCOL'] = "HTTP/#{request[:http_version]}"

  env['REMOTE_ADDR'] = remote_address.ip_address
  env['REMOTE_PORT'] = remote_address.ip_port.to_s

  env['REQUEST_METHOD'] = request[:method].to_s
  env['PATH_INFO']      = request_uri.fetch(:path,'*').to_s
  env['QUERY_STRING']   = request_uri[:query_string].to_s

  # add the headers
  request[:headers].each do |name,value|
    key = name.dup
    
    key.upcase!
    key.tr!('-','_')

    # if the header is not special, prepend 'HTTP_'
    unless SPECIAL_HEADERS.include?(name)
      key.insert(0,'HTTP_')
    end

    env[key] = case value
               when Array
                 value.join("\n")
               else
                 value.to_s
               end
  end

  @app.call(env)
end
run() click to toggle source

Starts {Net::HTTP::Server} and begins handling HTTP Requests.

# File lib/rack/handler/http.rb, line 67
def run
  @server = Net::HTTP::Server::Daemon.new(
    :host    => @options[:Host],
    :port    => @options[:Port],
    :handler => self
  )

  @server.start
  @server.join
end
running?() click to toggle source

Determines if the handler is running.

@return [Boolean]

Specifies whether the handler is still running.
# File lib/rack/handler/http.rb, line 147
def running?
  @server && !(@server.stopped?)
end
stop() click to toggle source

Stops the handler.

# File lib/rack/handler/http.rb, line 164
def stop
  @server.stop if @server
end
stopped?() click to toggle source

Determines whether the handler was stopped.

@return [Boolean]

Specifies whether the handler was previously stopped.
# File lib/rack/handler/http.rb, line 157
def stopped?
  @server.nil? || @server.stopped?
end