Class: Net::HTTP::Server::Daemon

Inherits:
GServer
  • Object
show all
Includes:
Requests, Responses
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

Requests::DEFAULT_PORTS

Instance Method Summary collapse

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.

Parameters:

  • host (String) (defaults to: DEFAULT_HOST)

    The host to run on.

  • port (String) (defaults to: DEFAULT_PORT)

    The port to listen on.

  • max_connections (Integer) (defaults to: MAX_CONNECTIONS)

    The maximum number of simultaneous connections.

  • log (IO) (defaults to: $stderr)

    The log to write errors to.

  • handler (#call) (defaults to: nil)

    The HTTP Request Handler object.

Yields:

  • (request, socket)

    If a block is given, it will be used to process HTTP Requests.

Yield Parameters:

  • request (Hash{Symbol => String,Array,Hash})

    The HTTP Request.

  • socket (TCPSocket)

    The TCP socket of the client.

Raises:

  • (ArgumentError)

    No handler: value was given.



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.

Parameters:

  • object (#call, nil) (defaults to: nil)

    The HTTP Request Handler object.

Yields:

  • (request, stream)

    If a block is given, it will be used to process HTTP Requests.

Yield Parameters:

  • request (Hash{Symbol => String,Array,Hash})

    The HTTP Request.

  • stream (Stream, ChunkedStream)

    The stream of the HTTP Request body.

Raises:

  • (ArgumentError)

    The HTTP Request Handler must respond to #call.



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.

Parameters:

  • socket (TCPSocket)

    A new TCP connection.



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