Class: Waitress::Vhost
- Inherits:
-
Array
- Object
- Array
- Waitress::Vhost
- Defined in:
- lib/waitress/vhost.rb
Overview
The VHost class is responsible for the actions of a Virtual Host, that is reacting to a certain subdomain and handling the requests that are sent to it. The requests are managed through the usage of Waitress::Handler
instances.
Instance Attribute Summary collapse
-
#combos ⇒ Object
Returns the value of attribute combos.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#libraries ⇒ Object
Returns the value of attribute libraries.
-
#load_path ⇒ Object
Returns the value of attribute load_path.
-
#priority ⇒ Object
Returns the value of attribute priority.
Instance Method Summary collapse
-
#after_request(&block) ⇒ Object
Register a listener that will be triggered once a request has been received by this VHost, after it has been handled, but before it is served.
-
#bind_lib(pattern, type, name, *options) ⇒ Object
Bind a library to a name.
-
#cancel_request ⇒ Object
Cancel the current request (don’t serve the response).
-
#combo(name, *targets) ⇒ Object
Define a combination of libraries.
-
#config ⇒ Object
The config data of the VHost.
-
#disable_waitress_resources ⇒ Object
Call this to disable the Waitress Handler (Waitress’ default CSS, JS, 404 and Index).
-
#enable_waitress_resources ⇒ Object
Call this to enable the Waitress Handler (Waitress’ default CSS, JS, 404 and Index).
-
#get_404 ⇒ Object
Get the absolute file path for the 404 page to use for this VHost.
-
#handle_request(request, client) ⇒ Object
Internal (matches requests).
-
#includes(dir) ⇒ Object
Used to define the load path for the VHost.
-
#initialize(pattern, priority = 50) ⇒ Vhost
constructor
- Create a new Virtual Host to manage traffic on a Subdomain (Host header) Params:
pattern
- The regex pattern used to match this VHost to subdomain(s)
priority
-
The priority of the VHost.
- The regex pattern used to match this VHost to subdomain(s)
- Create a new Virtual Host to manage traffic on a Subdomain (Host header) Params:
-
#libdir(name) ⇒ Object
Set the directory in which Libraries are contained.
-
#liburi(name = nil) ⇒ Object
Change the uri that Libraries should be accessed from.
-
#on_request(&block) ⇒ Object
Register a listener that will be triggered once a request has been received by this VHost, but before it has been passed to a listener or served.
-
#on_server_start(srv) ⇒ Object
Internal (called on Server Start).
-
#parse_libraries ⇒ Object
Internal.
-
#resources? ⇒ Boolean
Returns true if the VHost has the Waitress Handler enabled (Waitress’ default CSS, JS, 404 and Index).
-
#rewrite(pattern, newpath) ⇒ Object
Define a rewrite rule.
-
#root(dir, priority = 50) ⇒ Object
Add a Document Root for this VHost.
-
#set_404(link) ⇒ Object
Change the default 404 page for the VHost.
-
#set_configure(conf) ⇒ Object
Internal.
Constructor Details
#initialize(pattern, priority = 50) ⇒ Vhost
Create a new Virtual Host to manage traffic on a Subdomain (Host header) Params:
pattern
-
The regex pattern used to match this VHost to subdomain(s)
priority
-
The priority of the VHost. If multiple VHosts match the subdomain,
the VHost with the highest priority will be chosen. Default: 50
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/waitress/vhost.rb', line 19 def initialize pattern, priority=50 @domain = pattern @priority = priority @load_path = [] @libdir = "~/.waitress/www/libs" @liburi = "libraries" @libraries = {} @combos = {} @on_request = [] @after_request = [] @config_data = {} enable_waitress_resources end |
Instance Attribute Details
#combos ⇒ Object
Returns the value of attribute combos.
11 12 13 |
# File 'lib/waitress/vhost.rb', line 11 def combos @combos end |
#domain ⇒ Object
Returns the value of attribute domain.
8 9 10 |
# File 'lib/waitress/vhost.rb', line 8 def domain @domain end |
#libraries ⇒ Object
Returns the value of attribute libraries.
12 13 14 |
# File 'lib/waitress/vhost.rb', line 12 def libraries @libraries end |
#load_path ⇒ Object
Returns the value of attribute load_path.
9 10 11 |
# File 'lib/waitress/vhost.rb', line 9 def load_path @load_path end |
#priority ⇒ Object
Returns the value of attribute priority.
7 8 9 |
# File 'lib/waitress/vhost.rb', line 7 def priority @priority end |
Instance Method Details
#after_request(&block) ⇒ Object
Register a listener that will be triggered once a request has been received by this VHost, after it has been handled, but before it is served. The Block should take arguments:
request
-
The
Waitress::Request
object response
-
The
Waitress::Response
object vhost
-
The VirtualHost instance
client
-
The Client Socket object
64 65 66 |
# File 'lib/waitress/vhost.rb', line 64 def after_request &block @after_request << block end |
#bind_lib(pattern, type, name, *options) ⇒ Object
Bind a library to a name. By default, when libraries are found in the css/ and js/ folders of your lib directory, they are loaded under the lib name of “filename.type”, with type being .css or .js. By binding a library, you can give this a different name based on the file found by regular expression, for example, binding /jquery/ to “jquery” will match any filenames containing ‘jquery’, meaning the full name of ‘jquery-ver.sion’ is matched, and can be accessed simply by “jquery” Params:
pattern
-
The regular expression to check files against. This will match the first file it finds with
the expression to the library.
type
-
The type of file. This should be a symbol of either :js or :css, or any other supported lib types
name
-
The new name to reference this library by
options
-
Any extra options to use when loading the library (to be used in the future)
136 137 138 139 |
# File 'lib/waitress/vhost.rb', line 136 def bind_lib pattern, type, name, * lib = { :pattern => pattern, :bindtype => type, :options => } @libraries[name.to_sym] = lib end |
#cancel_request ⇒ Object
Cancel the current request (don’t serve the response)
178 179 180 |
# File 'lib/waitress/vhost.rb', line 178 def cancel_request @cancelled = true end |
#combo(name, *targets) ⇒ Object
Define a combination of libraries. Calling this method will bind all the libaries listed under one common name that can be loaded into .wrb files and handlers using combo(). This is used to load a collection of libraries simply and easily without repeating lib() methods. Params:
name
-
The desired name of the combo
targets
-
A list of all the libraries to bind to this combo
147 148 149 150 |
# File 'lib/waitress/vhost.rb', line 147 def combo name, *targets @combos[name.to_sym] = targets targets end |
#config ⇒ Object
The config data of the VHost. This config data is set by the configuration file and can be read by the server when serving requests. This is so users can set data in their config.rb file and read it out on their requests. Use this to set constants such as Social Media IDs and other details
42 43 44 |
# File 'lib/waitress/vhost.rb', line 42 def config @config_data end |
#disable_waitress_resources ⇒ Object
Call this to disable the Waitress Handler (Waitress’ default CSS, JS, 404 and Index)
69 70 71 |
# File 'lib/waitress/vhost.rb', line 69 def disable_waitress_resources @resources = false end |
#enable_waitress_resources ⇒ Object
Call this to enable the Waitress Handler (Waitress’ default CSS, JS, 404 and Index)
74 75 76 |
# File 'lib/waitress/vhost.rb', line 74 def enable_waitress_resources @resources = true end |
#get_404 ⇒ Object
Get the absolute file path for the 404 page to use for this VHost. May be nil.
90 91 92 |
# File 'lib/waitress/vhost.rb', line 90 def get_404 @page_404 end |
#handle_request(request, client) ⇒ Object
Internal (matches requests)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/waitress/vhost.rb', line 196 def handle_request request, client @cancelled = false response = Waitress::Response.new $REQUEST = request $RESPONSE = response $VHOST = self @on_request.each { |x| x.call(request, self, client) } unless @cancelled match = nil if @resources && Waitress::DirHandler.resources_handler.respond?(request, self) match = Waitress::DirHandler.resources_handler end self.each do |handler| match = handler if handler.respond?(request, self) && (match.nil? || handler.priority > match.priority) end if match.nil? Waitress::Chef.error 404, request, response, client, self else match.serve! request, response, client, self end end @after_request.each { |x| x.call(request, response, self, client) } response.serve(client) unless (response.done? || client.closed?) end |
#includes(dir) ⇒ Object
Used to define the load path for the VHost. The Load Path defines what files can be included from outside of the public web directory, such as database backend or authentication. These files are not available to the public, but can be included by the .wrb files. Params: dir
: The directory to use for the loadpath. If a string is provided, it is added, however, if an array is provided, it will override with the array.
169 170 171 172 173 174 175 |
# File 'lib/waitress/vhost.rb', line 169 def includes dir if dir.is_a? String load_path << File.(dir) elsif dir.is_a? Array load_path = dir.map { |x| File.(x) } end end |
#libdir(name) ⇒ Object
Set the directory in which Libraries are contained. Libraries are specially handled by waitress to be loaded into .wrb files and handlers. Default: “libs/” This file will contain your CSS, JS and any other libraries. Bower packages are also supported under this directory (“bower_components”)
113 114 115 |
# File 'lib/waitress/vhost.rb', line 113 def libdir name @libdir = File.(name) end |
#liburi(name = nil) ⇒ Object
Change the uri that Libraries should be accessed from. Libraries present in the “libs/” directory are served from this URI. This URI is relative to the base “/” uri for the VirtualHost, for example, a LibUri of “libraries/” (the default) will be accessed by “your.site/libraries/lib_name”
120 121 122 123 |
# File 'lib/waitress/vhost.rb', line 120 def liburi name=nil @liburi = name unless name.nil? @liburi end |
#on_request(&block) ⇒ Object
Register a listener that will be triggered once a request has been received by this VHost, but before it has been passed to a listener or served. Use this to modify a request before it is handled. The Block should take arguments:
request
-
The
Waitress::Request
object vhost
-
The VirtualHost instance
client
-
The Client Socket object
53 54 55 |
# File 'lib/waitress/vhost.rb', line 53 def on_request &block @on_request << block end |
#on_server_start(srv) ⇒ Object
Internal (called on Server Start)
158 159 160 |
# File 'lib/waitress/vhost.rb', line 158 def on_server_start srv parse_libraries end |
#parse_libraries ⇒ Object
Internal
153 154 155 |
# File 'lib/waitress/vhost.rb', line 153 def parse_libraries self << Waitress::LibraryHandler.new(@libraries, @libdir, @liburi, self) end |
#resources? ⇒ Boolean
Returns true if the VHost has the Waitress Handler enabled (Waitress’ default CSS, JS, 404 and Index)
79 80 81 |
# File 'lib/waitress/vhost.rb', line 79 def resources? @resources end |
#rewrite(pattern, newpath) ⇒ Object
Define a rewrite rule. A rewrite rule is used to rewrite a URL’s path before it is handled. Keep in mind that this does not function for a query string, which should instead be handled with the on_request event. Params;
pattern
-
The pattern to match the URL against
newpath
-
The new path to replace the matched pattern with. This can include
capture groups through the use of \1 (where 1 is the capture group number)
189 190 191 192 193 |
# File 'lib/waitress/vhost.rb', line 189 def rewrite pattern, newpath on_request do |request, vhost| request.path = request.path.gsub(pattern, newpath) end end |
#root(dir, priority = 50) ⇒ Object
Add a Document Root for this VHost. This document root will contain all of your public files that can be served by the vhost. Params:
dir
-
The directory for the Document Root
priority
-
The priority for the directory. If multiple Handlers respond to the target
URI, the one with the highest priority will be chosen. Default: 50
100 101 102 |
# File 'lib/waitress/vhost.rb', line 100 def root dir, priority=50 self << Waitress::DirHandler.new(File.(dir), priority) end |
#set_404(link) ⇒ Object
Change the default 404 page for the VHost. This should be an absolute file path to your 404 page file that you wish to use
85 86 87 |
# File 'lib/waitress/vhost.rb', line 85 def set_404 link @page_404 = link end |
#set_configure(conf) ⇒ Object
Internal
105 106 107 |
# File 'lib/waitress/vhost.rb', line 105 def set_configure conf @configuration = conf end |