Class: Waitress::Chef
- Inherits:
-
Object
- Object
- Waitress::Chef
- Defined in:
- lib/waitress/chef.rb
Overview
The chef class handles the cooking of responses, by serving errors and files from the Filesystem, as well as providing a system for including files within handlers and .wrb files. This class handles most loading from Filesystems and serving of the body of a response.
Constant Summary collapse
- HANDLERS =
{ 404 => Handler404.new }
Class Method Summary collapse
-
.error(code, request, response, client, vhost) ⇒ Object
Set the response to use an error page with the given error code (usually 404).
-
.find_file(abspath) ⇒ Object
Find a file to serve.
-
.include_absfile(target) ⇒ Object
Include a file from anywhere in the filesystem (absolute) in the current running instance.
-
.include_file(filename) ⇒ Object
Include a file from the VHost loadpath in the current running instance.
-
.resources ⇒ Object
Get the waitress resources directory, containing templates and the default http assets of waitress.
-
.resources_http ⇒ Object
Get the waitress HTTP assets directory, containing the default index, 404 and styles resources.
-
.serve_file(request, response, client, vhost, file) ⇒ Object
Serve a file from the Filesystem, automatically handling based on whether the file is dynamic or static.
Class Method Details
.error(code, request, response, client, vhost) ⇒ Object
Set the response to use an error page with the given error code (usually 404)
26 27 28 |
# File 'lib/waitress/chef.rb', line 26 def self.error code, request, response, client, vhost HANDLERS[code].serve!(request, response, client, vhost) end |
.find_file(abspath) ⇒ Object
Find a file to serve. This is used by the DirHandler to automatically include an index file if it exists under the requested directory, automatically add the .wrb or .html file extension for paths without the extension, etc. A hash containing the result (ok / notfound) and the new filepath will be given.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/waitress/chef.rb', line 86 def self.find_file abspath ret = {} if File.exist?(abspath) if File.directory?(abspath) wrb = File.join(abspath, "index.wrb") html = File.join(abspath, "index.html") if File.exist?(wrb) ret = { :result => :ok, :file => wrb } elsif File.exist?(html) ret = { :result => :ok, :file => html } else ret = { :result => :dir, :file => abspath } end else ret = { :result => :ok, :file => abspath } end elsif File.exist?("#{abspath}.html") ret = { :result => :ok, :file => "#{abspath}.html" } elsif File.exist?("#{abspath}.wrb") ret = { :result => :ok, :file => "#{abspath}.wrb" } else ret = { :result => :notfound } end ret end |
.include_absfile(target) ⇒ Object
Include a file from anywhere in the filesystem (absolute) in the current running instance. This will load the file’s content and, if dynamic, evaluate it to the current response. Params:
target
-
The target file, given in absolute path form.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/waitress/chef.rb', line 67 def self.include_absfile target ext = File.extname target bind = Waitress::EvalBindings.new if ext == ".wrb" Waitress::WRBParser.parse!(File.read(target), $RESPONSE.body_io) do |txt, lino| eval(txt, bind.context, target, lino) # For some reason, rb_f_eval is not exposed to ruby.h end elsif ext == ".rb" require target else echo File.read(target) end end |
.include_file(filename) ⇒ Object
Include a file from the VHost loadpath in the current running instance. Params:
filename
-
The filename of the file, relative to the load path of the VHost.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/waitress/chef.rb', line 46 def self.include_file filename lp = $VHOST.load_path target = nil lp.each do |path| fl = File.join(path, filename) ext = ["", ".rb", ".wrb"] ext.each do |e| flnew = "#{fl}#{e}" target = flnew if File.exist?(flnew) end end raise LoadError.new("Include does not exist in Load Path: #{target}") if target.nil? include_absfile target end |
.resources ⇒ Object
Get the waitress resources directory, containing templates and the default http assets of waitress
15 16 17 |
# File 'lib/waitress/chef.rb', line 15 def self.resources File. "../resources", __FILE__ end |
.resources_http ⇒ Object
Get the waitress HTTP assets directory, containing the default index, 404 and styles resources.
21 22 23 |
# File 'lib/waitress/chef.rb', line 21 def self.resources_http File.join(resources, "http") end |
.serve_file(request, response, client, vhost, file) ⇒ Object
Serve a file from the Filesystem, automatically handling based on whether the file is dynamic or static. This will set the body io and any required headers on the reponse object.
33 34 35 36 37 38 39 40 41 |
# File 'lib/waitress/chef.rb', line 33 def self.serve_file request, response, client, vhost, file if File.extname(file) == ".wrb" || File.extname(file) == ".rb" response.mime ".html" include_absfile File.(file) else response.mime File.extname(file) response.body_io File.open(file, "r") end end |