Sha256: 65679aaf9dcfe69ef4d799f46bda0cedb28b6ff0772815e15916bac85d53adb8

Contents?: true

Size: 1.74 KB

Versions: 47

Compression:

Stored size: 1.74 KB

Contents

require 'webrick/httputils'

module YARD
  module Server
    module Commands
      # Serves static content when no other router matches a request
      class StaticFileCommand < Base
        include WEBrick::HTTPUtils

        DefaultMimeTypes['js'] = 'text/javascript'

        # Defines the paths used to search for static assets. To define an
        # extra path, use {YARD::Server.register_static_path} rather than
        # modifying this constant directly. Also note that files in the
        # document root will always take precedence over these paths.
        STATIC_PATHS = []

        def run
          assets_template = Templates::Engine.template(:default, :fulldoc, :html)
          path = File.cleanpath(request.path).gsub(%r{^(../)+}, '')

          file = nil
          ([adapter.document_root] + STATIC_PATHS.reverse).compact.each do |path_prefix|
            file = File.join(path_prefix, path)
            break if File.exist?(file)
            file = nil
          end

          # Search in default/fulldoc/html template if nothing in static asset paths
          file ||= assets_template.find_file(path)

          if file
            ext = "." + (request.path[/\.(\w+)$/, 1] || "html")
            headers['Content-Type'] = mime_type(ext, DefaultMimeTypes)
            self.body = File.read(file)
            return
          end

          favicon?
          self.status = 404
        end

        private

        # Return an empty favicon.ico if it does not exist so that
        # browsers don't complain.
        def favicon?
          return unless request.path == '/favicon.ico'
          self.headers['Content-Type'] = 'image/png'
          self.status = 200
          self.body = ''
          raise FinishRequest
        end
      end
    end
  end
end

Version data entries

47 entries across 39 versions & 7 rubygems

Version Path
yard-0.9.0 lib/yard/server/commands/static_file_command.rb
yard-0.8.7.6 lib/yard/server/commands/static_file_command.rb
yard-0.8.7.5 lib/yard/server/commands/static_file_command.rb
climine-0.0.7 vendor/bundle/ruby/2.1.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
climine-0.0.7 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
climine-0.0.6 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
climine-0.0.5 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
yard-0.8.7.4 lib/yard/server/commands/static_file_command.rb
climine-0.0.4 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
climine-0.0.3 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
climine-0.0.2 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
climine-0.0.1 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/server/commands/static_file_command.rb
yard-0.8.7.3 lib/yard/server/commands/static_file_command.rb
yard-0.8.7.2 lib/yard/server/commands/static_file_command.rb
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/yard-0.8.7/lib/yard/server/commands/static_file_command.rb
candlepin-api-0.4.0 bundle/ruby/1.8/gems/yard-0.8.7/lib/yard/server/commands/static_file_command.rb
candlepin-api-0.4.0 bundle/ruby/gems/yard-0.8.7/lib/yard/server/commands/static_file_command.rb