Class: HTTPkit::Server

Inherits:
Object
  • Object
show all
Includes:
Connection::Status, HTTPkit::Support::HandlerManager::Setup
Defined in:
lib/httpkit/server.rb

Defined Under Namespace

Classes: BlockHandler, BodyHandler, KeepAliveHandler, MandatoryHandler, TimeoutsHandler

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Connection::Status

#close, #closed?, #error?, #network_fault?, #timeout?

Methods included from HTTPkit::Support::HandlerManager::Setup

#setup_handlers

Constructor Details

- (Server) initialize(config, connection)



14
15
16
17
18
19
20
21
# File 'lib/httpkit/server.rb', line 14

def initialize(config, connection)
  @sequence = 0
  @config = config
  (@config[:handlers] ||= []).push(MandatoryHandler.new, BodyHandler.new)

  setup_connection(connection)
  setup_handlers
end

Instance Attribute Details

- (Object) config (readonly)

Returns the value of attribute config



12
13
14
# File 'lib/httpkit/server.rb', line 12

def config
  @config
end

Class Method Details

+ (Object) start(config)



5
6
7
# File 'lib/httpkit/server.rb', line 5

def self.start(config)
  Connection::EventMachine.start_server(config, self)
end

Instance Method Details

- (Object) finish(request)



39
40
41
# File 'lib/httpkit/server.rb', line 39

def finish(request)
  @handlers.invoke(:finish, request)
end

- (Object) respond(request, response)



30
31
32
33
34
35
36
37
# File 'lib/httpkit/server.rb', line 30

def respond(request, response)
  fiber = Fiber.new do
    request, response = @handlers.invoke(:respond, request, response)
    @connection.serialize(response)
    finish(request)
  end
  fiber.resume
end

- (Object) serve(request)



23
24
25
26
27
28
# File 'lib/httpkit/server.rb', line 23

def serve(request)
  served = Promise.new
  served.then { |response| respond(request, response) }

  serve!(request, served).resume
end