class TP2::HTTP1Connection

def respond_with_static_file(req, path, opts, cache_headers)

def respond_with_static_file(req, path, opts, cache_headers)
  fd = @machine.open(path, UM::O_RDONLY)
  opts ||= {}
  if opts[:headers]
    opts[:headers].merge!(cache_headers)
  else
    opts[:headers] = cache_headers
  end
  maxlen = opts[:max_len] || 65_536
  buf = String.new(capacity: maxlen)
  headers_sent = nil
  loop do
    res = @machine.read(fd, buf, maxlen, 0)
    if res < maxlen && !headers_sent
      return respond(req, buf, opts[:headers])
    elsif res == 0
      return finish(req)
    end
    if !headers_sent
      send_headers(req, opts[:headers])
      headers_sent = true
    end
    done = res < maxlen
    send_chunk(req, buf, done: done)
    return if done
  end
ensure
  @machine.close(fd) if fd
end