Class: EmmyHttp::Server::Response

Inherits:
Object
  • Object
show all
Includes:
ModelPack::Document
Defined in:
lib/emmy_http/server/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



13
14
15
# File 'lib/emmy_http/server/response.rb', line 13

def connection
  @connection
end

#finished=(value) ⇒ Object (writeonly)

Sets the attribute finished

Parameters:

  • value

    the value to set the attribute finished to.



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

def finished=(value)
  @finished = value
end

#headers_sent=(value) ⇒ Object (writeonly)

Sets the attribute headers_sent

Parameters:

  • value

    the value to set the attribute headers_sent to.



14
15
16
# File 'lib/emmy_http/server/response.rb', line 14

def headers_sent=(value)
  @headers_sent = value
end

#keep_aliveObject

Returns the value of attribute keep_alive.



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

def keep_alive
  @keep_alive
end

Instance Method Details

#attach(conn) ⇒ Object



53
54
55
56
# File 'lib/emmy_http/server/response.rb', line 53

def attach(conn)
  @connection = conn
  listen conn, :close, :close
end

#close(reason = nil) ⇒ Object



47
48
49
50
51
# File 'lib/emmy_http/server/response.rb', line 47

def close(reason=nil)
  body.close if body.respond_to?(:close)
  terminate!(reason, self, connection) if reason
  dettach
end

#dettachObject



58
59
60
61
62
63
# File 'lib/emmy_http/server/response.rb', line 58

def dettach
  if connection
    stop_listen connection, :close
    @connection = nil
  end
end

#finished?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/emmy_http/server/response.rb', line 69

def finished?
  @finished
end

#headers_sent?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/emmy_http/server/response.rb', line 65

def headers_sent?
  @headers_sent
end

#write_allObject



17
18
19
20
# File 'lib/emmy_http/server/response.rb', line 17

def write_all
  write_head unless headers_sent?
  write_body unless finished?
end

#write_bodyObject



38
39
40
41
42
43
44
45
# File 'lib/emmy_http/server/response.rb', line 38

def write_body
  if body.respond_to?(:each)
    body.each { |chunk| connection.send_data(chunk) }
  else
    connection.send_data(body) if body
  end
  @finished = true
end

#write_headObject

Raises:

  • (ResponseError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/emmy_http/server/response.rb', line 22

def write_head
  raise ResponseError, "Invalid HTTP status" unless Server::HTTP_STATUS_CODES.key?(status)

  prepare_headers
  head = "HTTP/1.1 #{status} #{Server::HTTP_STATUS_CODES[status.to_i]}\r\n"

  headers.each do |n, v|
    head += "#{n}: #{v}\r\n" if v
  end

  head += "\r\n"

  connection.send_data(head)
  @headers_sent = true
end