Class: Net::HTTP::Server::Daemon
- Inherits:
-
GServer
- Object
- GServer
- Net::HTTP::Server::Daemon
- Defined in:
- lib/net/http/server/daemon.rb
Constant Summary collapse
- DEFAULT_HOST =
Default host to bind to.
'0.0.0.0'
- DEFAULT_PORT =
Default port to listen on.
8080
- MAX_CONNECTIONS =
Maximum number of simultaneous connections.
256
Constants included from Responses
Responses::BAD_REQUEST, Responses::HTTP_STATUSES, Responses::HTTP_VERSION
Constants included from Requests
Instance Method Summary collapse
-
#handler(object = nil) {|request, stream| ... } ⇒ Object
Sets the HTTP Request Handler.
-
#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, max_connections: MAX_CONNECTIONS, log: $stderr, handler: nil) {|request, socket| ... } ⇒ Daemon
constructor
Creates a new HTTP Daemon.
-
#serve(socket) ⇒ Object
Receives HTTP Requests and handles them.
Methods included from Responses
#write_body, #write_body_streamed, #write_headers, #write_response, #write_status
Methods included from Requests
#normalize_headers, #normalize_request, #normalize_uri, #read_request
Constructor Details
#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, max_connections: MAX_CONNECTIONS, log: $stderr, handler: nil) {|request, socket| ... } ⇒ Daemon
Creates a new HTTP Daemon.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/net/http/server/daemon.rb', line 56 def initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, max_connections: MAX_CONNECTIONS, log: $stderr, handler: nil, &block) super(port.to_i,host,max_connections,log,false,true) handler(handler,&block) end |
Instance Method Details
#handler(object = nil) {|request, stream| ... } ⇒ Object
Sets the HTTP Request Handler.
85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
Receives HTTP Requests and handles them.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# 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 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 |