Class: Rack::Handler::HTTP
- Inherits:
-
Object
- Object
- Rack::Handler::HTTP
- 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
-
.run(app, **options) ⇒ Object
Creates a new handler and begins handling HTTP Requests.
Instance Method Summary collapse
-
#call(request, stream) ⇒ Array<Integer, Hash, Array>
Handles an HTTP Request.
-
#initialize(app, **options) ⇒ HTTP
constructor
Initializes the handler.
-
#run ⇒ Object
Starts Net::HTTP::Server and begins handling HTTP Requests.
-
#running? ⇒ Boolean
Determines if the handler is running.
-
#stop ⇒ Object
Stops the handler.
-
#stopped? ⇒ Boolean
Determines whether the handler was stopped.
Constructor Details
#initialize(app, **options) ⇒ HTTP
Initializes the handler.
49 50 51 52 53 |
# File 'lib/rack/handler/http.rb', line 49 def initialize(app,**) @app = app @options = @server = nil end |
Class Method Details
.run(app, **options) ⇒ Object
Creates a new handler and begins handling HTTP Requests.
60 61 62 |
# File 'lib/rack/handler/http.rb', line 60 def self.run(app,**) new(app,**).run end |
Instance Method Details
#call(request, stream) ⇒ Array<Integer, Hash, Array>
Handles an HTTP Request.
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 |
#run ⇒ Object
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.
147 148 149 |
# File 'lib/rack/handler/http.rb', line 147 def running? @server && !(@server.stopped?) end |
#stop ⇒ Object
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.
157 158 159 |
# File 'lib/rack/handler/http.rb', line 157 def stopped? @server.nil? || @server.stopped? end |