Class: ServerSide::HTTP::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/serverside/connection.rb

Overview

The Connection class represents HTTP connections. Each connection instance creates a separate thread for execution and processes incoming requests in a loop until the connection is closed by either server or client, thus implementing HTTP 1.1 persistent connections.

Instance Method Summary collapse

Constructor Details

#initialize(socket, request_class) ⇒ Connection

Initializes the request instance. A new thread is created for processing requests.



13
14
15
16
# File 'lib/serverside/connection.rb', line 13

def initialize(socket, request_class)
  @socket, @request_class = socket, request_class
  @thread = Thread.new {process}
end

Instance Method Details

#processObject

Processes incoming requests by parsing them and then responding. If any error occurs, or the connection is not persistent, the connection is closed.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/serverside/connection.rb', line 21

def process
  while true
    # the process function is expected to return true or a non-nil value
    # if the connection is to persist.
    break unless @request_class.new(@socket).process
  end
rescue => e
  # We don't care. Just close the connection.
ensure
  @socket.close
end