module Roda::RodaPlugins::Public::RequestMethods

def public

Serve files from the public directory if the file exists and this is a GET request.

def public
  public_serve_with(roda_class.opts[:public_server])
end

def public_file_readable?(path)

Return whether the given path is a readable regular file.

def public_file_readable?(path)
  ::File.file?(path) && ::File.readable?(path)
rescue SystemCallError
  # :nocov:
  false
  # :nocov:
end

def public_path_segments(path)

and . components
Return an array of segments for the given path, handling ..

def public_path_segments(path)
  segments = []
    
  path.split(SPLIT).each do |seg|
    next if seg.empty? || seg == '.'
    seg == '..' ? segments.pop : segments << seg
  end
    
  segments
end

def public_serve(server, path)

Serve the given path using the given Rack::Files server.

def public_serve(server, path)
  server.serving(self, path)
end

def public_serve(server, path)

def public_serve(server, path)
  server = server.dup
  server.path = path
  server.serving(env)
end

def public_serve_compressed(server, path, suffix, encoding)

def public_serve_compressed(server, path, suffix, encoding)
  if env['HTTP_ACCEPT_ENCODING'] =~ /\b#{encoding}\b/
    compressed_path = path + suffix
    if public_file_readable?(compressed_path)
      s, h, b = public_serve(server, compressed_path)
      headers = response.headers
      headers.replace(h)
      unless s == 304
        headers[RodaResponseHeaders::CONTENT_TYPE] = ::Rack::Mime.mime_type(::File.extname(path), 'text/plain')
        headers[RodaResponseHeaders::CONTENT_ENCODING] = encoding
      end
      halt [s, headers, b]
    end
  end
end

def public_serve_with(server)

def public_serve_with(server)
  return unless is_get?
  path = PARSER.unescape(real_remaining_path)
  return if path.include?("\0")
  roda_opts = roda_class.opts
  path = ::File.join(server.root, *public_path_segments(path))
  public_serve_compressed(server, path, '.br', 'br') if roda_opts[:public_brotli]
  public_serve_compressed(server, path, '.gz', 'gzip') if roda_opts[:public_gzip]
  if public_file_readable?(path)
    s, h, b = public_serve(server, path)
    headers = response.headers
    headers.replace(h)
    halt [s, headers, b]
  end
end