Class: Thin::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/thin/response.rb

Overview

A response sent to the client.

Constant Summary collapse

CONNECTION =
'Connection'.freeze
CLOSE =
'close'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



9
10
11
12
13
# File 'lib/thin/response.rb', line 9

def initialize
  @headers = Headers.new
  @body = StringIO.new
  @status = 200
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/thin/response.rb', line 7

def body
  @body
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/thin/response.rb', line 7

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/thin/response.rb', line 7

def status
  @status
end

Instance Method Details

#closeObject



39
40
41
# File 'lib/thin/response.rb', line 39

def close
  @body.close
end

#content_typeObject



19
20
21
# File 'lib/thin/response.rb', line 19

def content_type
  @headers[CONTENT_TYPE]
end

#content_type=(type) ⇒ Object



15
16
17
# File 'lib/thin/response.rb', line 15

def content_type=(type)
  @headers[CONTENT_TYPE] = type
end

#headObject



29
30
31
# File 'lib/thin/response.rb', line 29

def head
  "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status.to_i]}\r\n#{headers_output}\r\n"
end

#headers_outputObject



23
24
25
26
27
# File 'lib/thin/response.rb', line 23

def headers_output
  @headers[CONTENT_LENGTH] = @body.size
  @headers[CONNECTION] = CLOSE
  @headers.to_s
end

#start(status) {|@headers, @body| ... } ⇒ Object

Yields:



43
44
45
46
# File 'lib/thin/response.rb', line 43

def start(status)
  @status = status
  yield @headers, @body
end

#to_sObject



48
49
50
51
52
# File 'lib/thin/response.rb', line 48

def to_s
  out = ''
  write out
  out
end

#write(socket) ⇒ Object



33
34
35
36
37
# File 'lib/thin/response.rb', line 33

def write(socket)
  socket << head
  @body.rewind
  socket << @body.read
end