Class: Waitress::DirHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/waitress/handlers/dirhandler.rb

Overview

The DirHandler class is an instance of Waitress::Handler that is responsible for loading files from the filesystem and serving them if they exist in the VHost’s root. It automatically handles mimetypes, evaluation and almost everything about the serving process for files in the FileSystem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Handler

#serve!

Constructor Details

#initialize(directory, priority = 50) ⇒ DirHandler

Create a new DirHandler, with the given FileSystem directory as a root and priority.



21
22
23
24
# File 'lib/waitress/handlers/dirhandler.rb', line 21

def initialize directory, priority=50
  @directory = File.absolute_path(directory)
  @priority = priority
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



10
11
12
# File 'lib/waitress/handlers/dirhandler.rb', line 10

def directory
  @directory
end

#priorityObject

Returns the value of attribute priority.



9
10
11
# File 'lib/waitress/handlers/dirhandler.rb', line 9

def priority
  @priority
end

Class Method Details

.resources_handlerObject

Get the instance of DirHandler that will load Waitress’ resources such as the default 404 and index pages, as well as CSS and JS



14
15
16
17
# File 'lib/waitress/handlers/dirhandler.rb', line 14

def self.resources_handler
  @@resources_handler ||= Waitress::DirHandler.new(Waitress::Chef.resources_http, -1000)
  @@resources_handler
end

Instance Method Details

#respond?(request, vhost) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/waitress/handlers/dirhandler.rb', line 26

def respond? request, vhost
  path = File.expand_path File.join("#{directory}", request.path)
  res = Waitress::Chef.find_file(path)[:result]
  path.include?(directory) && (res == :ok)
end

#serve(request, response, client, vhost) ⇒ Object



32
33
34
35
36
# File 'lib/waitress/handlers/dirhandler.rb', line 32

def serve request, response, client, vhost
  path = File.expand_path File.join("#{directory}", request.path)
  file = Waitress::Chef.find_file(path)[:file]
  Waitress::Chef.serve_file request, response, client, vhost, file
end