Class: YARD::Server::Commands::StaticFileCommand

Inherits:
Base
  • Object
show all
Includes:
WEBrick::HTTPUtils
Defined in:
lib/yard/server/commands/static_file_command.rb

Overview

Serves static content when no other router matches a request

Since:

Constant Summary

STATIC_PATHS =

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.

Since:

  • 0.6.0

[
  File.join(YARD::TEMPLATE_ROOT, 'default', 'fulldoc', 'html'),
  File.join(File.dirname(__FILE__), '..', 'templates', 'default', 'fulldoc', 'html')
]

Basic Command and Adapter Options (collapse)

Attributes Set Per Request (collapse)

Instance Method Summary (collapse)

Constructor Details

This class inherits a constructor from YARD::Server::Commands::Base

Instance Attribute Details

- (Adapter) adapter Originally defined in class Base

The server adapter

Returns:

Since:

  • 0.6.0

- (String) body Originally defined in class Base

The response body. Defaults to empty string.

Returns:

  • (String)

    the response body. Defaults to empty string.

Since:

  • 0.6.0

- (Boolean) caching Originally defined in class Base

Whether to cache

Returns:

  • (Boolean)

    whether to cache

Since:

  • 0.6.0

- (Hash) command_options Originally defined in class Base

The options passed to the command's constructor

Returns:

  • (Hash)

    the options passed to the command's constructor

Since:

  • 0.6.0

- (Hash{String => String}) headers Originally defined in class Base

Response headers

Returns:

Since:

  • 0.6.0

- (String) path Originally defined in class Base

The path after the command base URI

Returns:

  • (String)

    the path after the command base URI

Since:

  • 0.6.0

- (Request) request Originally defined in class Base

Request object

Returns:

  • (Request)

    request object

Since:

  • 0.6.0

- (Numeric) status Originally defined in class Base

Status code. Defaults to 200 per request

Returns:

  • (Numeric)

    status code. Defaults to 200 per request

Since:

  • 0.6.0

Instance Method Details

- (Object) run

Since:

  • 0.6.0



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yard/server/commands/static_file_command.rb', line 21

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