Class: Low::ResponseBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/responses/response_builder.rb

Class Method Summary collapse

Class Method Details

.respond(config:, socket:, response:) ⇒ Object

TODO: Use Async wherever we can where it doesn’t have “Task” requirement.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/responses/response_builder.rb', line 7

def respond(config:, socket:, response:)
  socket.puts "#{response.version} #{response.status}\r\n"
  socket.puts config.port.nil? ? "Host: #{config.host}\r\n" : "Host: #{config.host}:#{config.port}\r\n"

  response.headers.fields.each_slice(2) do |key, value|
    socket.puts "#{key}: #{value}\r\n"
  end

  socket.puts "\r\n"

  if response.body.respond_to?(:file)
    IO.copy_stream(response.body.file, socket)
  else
    socket.puts(response.body.read)
  end

  socket.close
end