Class: YARD::Server::Commands::StaticFileCommand
- Includes:
- WEBrick::HTTPUtils
- Defined in:
- lib/yard/server/commands/static_file_command.rb
Overview
Serves static content when no other router matches a request
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.
[]
Basic Command and Adapter Options collapse
-
#adapter ⇒ Adapter
inherited
from Base
The server adapter.
-
#caching ⇒ Boolean
inherited
from Base
Whether to cache.
-
#command_options ⇒ Hash
inherited
from Base
The options passed to the command's constructor.
Attributes Set Per Request collapse
-
#body ⇒ String
inherited
from Base
The response body.
-
#headers ⇒ Hash{String => String}
inherited
from Base
Response headers.
-
#path ⇒ String
inherited
from Base
The path after the command base URI.
-
#request ⇒ Request
inherited
from Base
Request object.
-
#status ⇒ Numeric
inherited
from Base
Status code.
Instance Method Summary collapse
Constructor Details
This class inherits a constructor from YARD::Server::Commands::Base
Instance Attribute Details
#body ⇒ String Originally defined in class Base
Returns the response body. Defaults to empty string.
#caching ⇒ Boolean Originally defined in class Base
Returns whether to cache
#command_options ⇒ Hash Originally defined in class Base
Returns the options passed to the command's constructor
#request ⇒ Request Originally defined in class Base
Returns request object
#status ⇒ Numeric Originally defined in class Base
Returns status code. Defaults to 200 per request
Instance Method Details
#run ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/yard/server/commands/static_file_command.rb', line 18 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 |