Sha256: a7020ea826166c961cd88d8a0aefc3c1e2fff16b8f4ccf127764e068018792dd

Contents?: true

Size: 1.64 KB

Versions: 52

Compression:

Stored size: 1.64 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 = [
          File.join(YARD::TEMPLATE_ROOT, 'default', 'fulldoc', 'html'),
          File.join(File.dirname(__FILE__), '..', 'templates', 'default', 'fulldoc', 'html')
        ]

        def run
          path = File.cleanpath(request.path).gsub(%r{^(../)+}, '')
          ([adapter.document_root] + STATIC_PATHS.reverse).compact.each do |path_prefix|
            file = File.join(path_prefix, path)
            if File.exist?(file)
              ext = "." + (request.path[/\.(\w+)$/, 1] || "html")
              headers['Content-Type'] = mime_type(ext, DefaultMimeTypes)
              self.body = File.read(file)
              return
            end
          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

52 entries across 43 versions & 5 rubygems

Version Path
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-rolls-0.2.0 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.8.3 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.8.1 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-rolls-0.1.0 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-rolls-0.1.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.8.0.pre vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.7.1 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.7.0 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.7.0.pre2 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.7.0.pre vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.6.2 vendor/bundle/gems/yard-0.8.1/lib/yard/server/commands/static_file_command.rb
challah-0.6.2 vendor/bundle/gems/yard-0.8.2.1/lib/yard/server/commands/static_file_command.rb
challah-0.6.2 vendor/bundle/gems/yard-0.8.2/lib/yard/server/commands/static_file_command.rb
challah-0.6.2 vendor/bundle/gems/yard-0.7.5/lib/yard/server/commands/static_file_command.rb
yard-0.8.2.1 lib/yard/server/commands/static_file_command.rb
challah-0.6.1 vendor/bundle/gems/yard-0.7.5/lib/yard/server/commands/static_file_command.rb
challah-0.6.1 vendor/bundle/gems/yard-0.8.1/lib/yard/server/commands/static_file_command.rb
challah-0.6.1 vendor/bundle/gems/yard-0.8.2/lib/yard/server/commands/static_file_command.rb