Class: Zapp::HTTPContext::Response

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

Overview

Represents an HTTP response being sent back to a client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket:) ⇒ Response

Returns a new instance of Response.



9
10
11
# File 'lib/zapp/http_context/response.rb', line 9

def initialize(socket:)
  @socket = socket
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#write(data:, status:, headers:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/zapp/http_context/response.rb', line 13

def write(data:, status:, headers:)
  @status = status
  @data = data
  @headers = headers

  response = +"HTTP/1.1 #{status}\n"

  response << "Content-Length: #{data.size}\n" unless headers["Content-Length"]

  headers.each do |k, v|
    response << "#{k}: #{v}\n"
  end

  response << "\n#{data}\n"

  @socket.write(response)
end