lib/roda/plugins/public.rb in roda-3.53.0 vs lib/roda/plugins/public.rb in roda-3.54.0

- old
+ new

@@ -35,10 +35,13 @@ # end # end module Public SPLIT = Regexp.union(*[File::SEPARATOR, File::ALT_SEPARATOR].compact) PARSER = URI::DEFAULT_PARSER + # :nocov: + RACK_FILES = defined?(Rack::Files) ? Rack::Files : Rack::File + # :nocov: # Use options given to setup a Rack::File instance for serving files. Options: # :default_mime :: The default mime type to use if the mime type is not recognized. # :gzip :: Whether to serve already gzipped files with a .gz extension for clients # supporting gzipped transfer encoding. @@ -50,11 +53,11 @@ if opts[:root] app.opts[:public_root] = app.expand_path(opts[:root]) elsif !app.opts[:public_root] app.opts[:public_root] = app.expand_path("public") end - app.opts[:public_server] = ::Rack::File.new(app.opts[:public_root], opts[:headers]||{}, opts[:default_mime] || 'text/plain') + app.opts[:public_server] = RACK_FILES.new(app.opts[:public_root], opts[:headers]||{}, opts[:default_mime] || 'text/plain') app.opts[:public_gzip] = opts[:gzip] app.opts[:public_brotli] = opts[:brotli] end module RequestMethods @@ -97,33 +100,37 @@ 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) - halt public_serve(server, path) + s, h, b = public_serve(server, path) + headers = response.headers + headers.replace(h) + halt [s, headers, b] end end 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) - res = public_serve(server, compressed_path) - headers = res[1] + s, h, b = public_serve(server, compressed_path) + headers = response.headers + headers.replace(h) - unless res[0] == 304 + unless s == 304 headers['Content-Type'] = ::Rack::Mime.mime_type(::File.extname(path), 'text/plain') headers['Content-Encoding'] = encoding end - halt res + halt [s, headers, b] end end end if ::Rack.release > '2' - # Serve the given path using the given Rack::File server. + # Serve the given path using the given Rack::Files server. def public_serve(server, path) server.serving(self, path) end else # :nocov: