Class: Thin::Connection

Class Thin::Connection < EventMachine::Connection

(in files lib/thin/connection.rb )

Connection between the server and client. This class is instanciated by EventMachine on each new connection that is opened.

Includes

Methods

Public Instance can_persist!()

Allows this connection to be persistent.

     # File lib/thin/connection.rb, line 124
124:     def can_persist!
125:       @can_persist = true
126:     end

Public Instance can_persist?()

Return true if this connection is allowed to stay open and be persistent.

     # File lib/thin/connection.rb, line 129
129:     def can_persist?
130:       @can_persist
131:     end

Public Instance handle_error()

Logs catched exception and closes the connection.

     # File lib/thin/connection.rb, line 98
 98:     def handle_error
 99:       log "!! Unexpected error while processing request: #{$!.message}"
100:       log_error
101:       close_connection rescue nil
102:     end

Public Instance persistent?()

Return true if the connection must be left open and ready to be reused for another request.

     # File lib/thin/connection.rb, line 135
135:     def persistent?
136:       @can_persist && @response.persistent?
137:     end

Public Instance post_init()

Get the connection ready to process a request.

    # File lib/thin/connection.rb, line 31
31:     def post_init
32:       @request  = Request.new
33:       @response = Response.new
34:     end

Public Instance post_process(result)

    # File lib/thin/connection.rb, line 70
70:     def post_process(result)
71:       return unless result
72: 
73:       # Set the Content-Length header if possible
74:       set_content_length(result) if need_content_length?(result)
75: 
76:       @response.status, @response.headers, @response.body = result
77: 
78:       log "!! Rack application returned nil body. Probably you wanted it to be an empty string?" if @response.body.nil?
79:       # Make the response persistent if requested by the client
80:       @response.persistent! if @request.persistent?
81: 
82:       # Send the response
83:       @response.each do |chunk|
84:         trace { chunk }
85:         send_data chunk
86:       end
87: 
88:       # If no more request on that same connection, we close it.
89:       close_connection_after_writing unless persistent?
90: 
91:     rescue Exception
92:       handle_error
93:     ensure
94:       terminate_request
95:     end

Public Instance pre_process()

    # File lib/thin/connection.rb, line 58
58:     def pre_process
59:       # Add client info to the request env
60:       @request.remote_address = remote_address
61: 
62:       # Process the request calling the Rack adapter
63:       @app.call(@request.env)
64:     rescue Exception
65:       handle_error
66:       terminate_request
67:       nil # Signal to post_process that the request could not be processed
68:     end

Public Instance process()

Called when all data was received and the request is ready to be processed.

    # File lib/thin/connection.rb, line 48
48:     def process
49:       if threaded?
50:         @request.threaded = true
51:         EventMachine.defer(method(:pre_process), method(:post_process))
52:       else
53:         @request.threaded = false
54:         post_process(pre_process)
55:       end
56:     end

Public Instance receive_data(data)

Called when data is received from the client.

    # File lib/thin/connection.rb, line 37
37:     def receive_data(data)
38:       trace { data }
39:       process if @request.parse(data)
40:     rescue InvalidRequest => e
41:       log "!! Invalid request"
42:       log_error e
43:       close_connection
44:     end

Public Instance remote_address()

IP Address of the remote client.

     # File lib/thin/connection.rb, line 147
147:     def remote_address
148:       @request.forwarded_for || socket_address
149:     rescue Exception
150:       log_error
151:       nil
152:     end

Public Instance terminate_request()

Does request and response cleanup (closes open IO streams and deletes created temporary files). Re-initializes response and request if client supports persistent connection.

     # File lib/thin/connection.rb, line 108
108:     def terminate_request
109:       @request.close  rescue nil
110:       @response.close rescue nil
111: 
112:       # Prepare the connection for another request if the client
113:       # supports HTTP pipelining (persistent connection).
114:       post_init if persistent?
115:     end

Public Instance threaded?()

true if app.call will be called inside a thread. You can set all requests as threaded setting Connection#threaded=true or on a per-request case returning true in app.deferred?.

     # File lib/thin/connection.rb, line 142
142:     def threaded?
143:       @threaded || (@app.respond_to?(:deferred?) && @app.deferred?(@request.env))
144:     end

Public Instance unbind()

Called when the connection is unbinded from the socket and can no longer be used to process requests.

     # File lib/thin/connection.rb, line 119
119:     def unbind
120:       @backend.connection_finished(self)
121:     end

Protected Instance socket_address()

Returns IP address of peer as a string.

     # File lib/thin/connection.rb, line 157
157:       def socket_address
158:         Socket.unpack_sockaddr_in(get_peername)[1]
159:       end