class EmmyHttp::Server::Response

Attributes

connection[RW]
finished[W]
headers_sent[W]
keep_alive[RW]

Public Instance Methods

attach(conn) click to toggle source
# File lib/emmy_http/server/response.rb, line 53
def attach(conn)
  @connection = conn
  listen conn, :close, :close
end
close(reason=nil) click to toggle source
# 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
dettach() click to toggle source
# File lib/emmy_http/server/response.rb, line 58
def dettach
  if connection
    stop_listen connection, :close
    @connection = nil
  end
end
finished?() click to toggle source
# File lib/emmy_http/server/response.rb, line 69
def finished?
  @finished
end
headers_sent?() click to toggle source
# File lib/emmy_http/server/response.rb, line 65
def headers_sent?
  @headers_sent
end
write_all() click to toggle source
# File lib/emmy_http/server/response.rb, line 17
def write_all
  write_head unless headers_sent?
  write_body unless finished?
end
write_body() click to toggle source
# 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_head() click to toggle source
# 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

Protected Instance Methods

prepare_headers() click to toggle source
# File lib/emmy_http/server/response.rb, line 75
def prepare_headers
  headers['Server'] ||= Server::SERVER_NAME
  headers['Date'] ||= Time.now.httpdate
  headers['Connection'] = keep_alive ? 'keep-alive' : 'close'
  #headers['Content-Length'] ||= "#{body.bytesize}" if body
end