class Net::HTTP::Server::Daemon
Constants
- DEFAULT_HOST
Default host to bind to.
- DEFAULT_PORT
Default port to listen on.
- MAX_CONNECTIONS
Maximum number of simultaneous connections.
Public Class Methods
new(options={},&block)
click to toggle source
@param [Hash] options
Options for the daemon.
@option options [String] :host (DEFAULT_HOST
)
The host to run on.
@option options [String] :port (DEFAULT_PORT
)
The port to listen on.
@option options [Integer] :max_connections (MAX_CONNECTIONS
)
The maximum number of simultaneous connections.
@option options [IO] :log ($stderr)
The log to write errors to.
@option options [#call] :handler
The HTTP Request Handler object.
@yield [request, socket]
If a block is given, it will be used to process HTTP Requests.
@yieldparam [Hash{Symbol => String,Array,Hash}] request
The HTTP Request.
@yieldparam [TCPSocket] socket
The TCP socket of the client.
Calls superclass method
# File lib/net/http/server/daemon.rb, line 56 def initialize(options={},&block) host = options.fetch(:host,DEFAULT_HOST) port = options.fetch(:port,DEFAULT_PORT).to_i max_connections = options.fetch(:max_connections,MAX_CONNECTIONS) log = options.fetch(:log,$stderr) super(port,host,max_connections,log,false,true) handler(options[:handler],&block) end
Public Instance Methods
handler(object=nil,&block)
click to toggle source
Sets the HTTP
Request Handler.
@param [#call, nil] object
The HTTP Request Handler object.
@yield [request, stream]
If a block is given, it will be used to process HTTP Requests.
@yieldparam [Hash{Symbol => String,Array,Hash}] request
The HTTP Request.
@yieldparam [Stream, ChunkedStream] stream
The stream of the HTTP Request body.
@raise [ArgumentError]
The HTTP Request Handler must respond to `#call`.
# File lib/net/http/server/daemon.rb, line 85 def handler(object=nil,&block) if object unless object.respond_to?(:call) raise(ArgumentError,"HTTP Request Handler must respond to #call") end elsif block.nil? raise(ArgumentError,"no HTTP Request Handler block given") end @handler = (object || block) end
serve(socket)
click to toggle source
Receives HTTP
Requests
and handles them.
@param [TCPSocket] socket
A new TCP connection.
# File lib/net/http/server/daemon.rb, line 103 def serve(socket) if (raw_request = read_request(socket)) parser = Parser.new begin request = parser.parse(raw_request) rescue Parslet::ParseFailed => error return Responses::BAD_REQUEST end normalize_request(request) stream = if request[:headers]['Transfer-Encoding'] == 'chunked' ChunkedStream.new(socket) else Stream.new(socket) end # rack compliant status, headers, body = @handler.call(request,stream) write_response(socket,status,headers,body) end end