Class: Rack::Handler::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/handler/http.rb

Overview

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

Constant Summary collapse

DEFAULT_ENV =

The default environment settings.

{
  'rack.version' => Rack::VERSION,
  'rack.errors' => $stderr,
  'rack.multithread' => true,
  'rack.multiprocess' => false,
  'rack.run_once' => false,
  'rack.url_scheme' => 'http',

  'SERVER_SOFTWARE' => "Net::HTTP::Server/#{Net::HTTP::Server::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})",
  'SCRIPT_NAME' => ''
}
SPECIAL_HEADERS =

Special HTTP Headers used by Rack::Request

Set[
  'Content-Type',
  'Content-Length'
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, **options) ⇒ HTTP

Initializes the handler.

Parameters:

  • app (#call)

    The application the handler will be passing requests to.

  • options (Hash{Symbol => Object})

    Additional options.

Options Hash (**options):

  • :Host (String)

    The host to bind to.

  • :Port (Integer)

    The port to listen on.



49
50
51
52
53
# File 'lib/rack/handler/http.rb', line 49

def initialize(app,**options)
  @app     = app
  @options = options
  @server  = nil
end

Class Method Details

.run(app, **options) ⇒ Object

Creates a new handler and begins handling HTTP Requests.

See Also:



60
61
62
# File 'lib/rack/handler/http.rb', line 60

def self.run(app,**options)
  new(app,**options).run
end

Instance Method Details

#call(request, stream) ⇒ Array<Integer, Hash, Array>

Handles an HTTP Request.

Parameters:

Returns:

  • (Array<Integer, Hash, Array>)

    The response status, headers and body.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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].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

#runObject

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



67
68
69
70
71
72
73
74
75
76
# 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?Boolean

Determines if the handler is running.

Returns:

  • (Boolean)

    Specifies whether the handler is still running.



147
148
149
# File 'lib/rack/handler/http.rb', line 147

def running?
  @server && !(@server.stopped?)
end

#stopObject

Stops the handler.



164
165
166
# File 'lib/rack/handler/http.rb', line 164

def stop
  @server.stop if @server
end

#stopped?Boolean

Determines whether the handler was stopped.

Returns:

  • (Boolean)

    Specifies whether the handler was previously stopped.



157
158
159
# File 'lib/rack/handler/http.rb', line 157

def stopped?
  @server.nil? || @server.stopped?
end