Module: Roda::RodaPlugins::Public::RequestMethods

Defined in:
lib/roda/plugins/public.rb

Instance Method Summary collapse

Instance Method Details

#publicObject

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/roda/plugins/public.rb', line 44

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]

        if mime_type = ::Rack::Mime.mime_type(::File.extname(path), 'text/plain')
          headers['Content-Type'] = mime_type
        end
        headers['Content-Encoding'] = 'gzip'

        halt res
      end
    end

    if public_file_readable?(path)
      halt public_serve(server, path)
    end
  end
end