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
  if is_get?
    path = PARSER.unescape(real_remaining_path)
    return if path.include?("\0")
    roda_opts = roda_class.opts
    server = roda_opts[:public_server]
    path = ::File.join(server.root, *public_path_segments(path))
    if roda_opts[:public_gzip] && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
      gzip_path = path + '.gz'
      if public_file_readable?(gzip_path)
        res = public_serve(server, gzip_path)
        headers = res[1]
        unless res[0] == 304
          if mime_type = ::Rack::Mime.mime_type(::File.extname(path), 'text/plain')
            headers['Content-Type'] = mime_type
          end
          headers['Content-Encoding'] = 'gzip'
        end
        halt res
      end
    end
    if public_file_readable?(path)
      halt public_serve(server, path)
    end
  end
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::File server.
def public_serve(server, path)
  server.serving(self, path)
end

def public_serve(server, path)

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