Module: Kernel
- Defined in:
- lib/waitress/kernel.rb
Overview
The Kernel Module provides global methods that will provide the ‘builtins’ for .wrb files and handlers. This is used to prevent verbose access of a namespace like Waitress::Global, and instead provide them here. Because requests are handled in new Processes, these values will change in each request and will not interfere.
Instance Method Summary collapse
-
#combo(name) ⇒ Object
Automatically load a Library Combo into this file.
-
#config ⇒ Object
The configuration for the VHost.
-
#echo(obj) ⇒ Object
Write a string to the output buffer.
-
#file_ext(extension) ⇒ Object
Set the content-type of the response.
-
#get ⇒ Object
Returns a Hash of the GET query of the request.
-
#get_body ⇒ Object
Get the request body.
-
#get_header(name) ⇒ Object
Get a header from the HTTP Request.
-
#get_headers ⇒ Object
Return the full list of headers available in the request object.
-
#get_method ⇒ Object
Returns the HTTP method that was used to retrieve this page (GET, POST, UPDATE, DELETE, PUT, etc).
-
#get_path ⇒ Object
Get the path of this request.
-
#get_querystring ⇒ Object
Get the querystring object as a string before it is parsed.
-
#get_uri ⇒ Object
Get the URI of this request.
-
#includes(filename) ⇒ Object
Include another .wrb, .rb or any other file in the load path of the VHost into this file.
-
#includes_file(filename) ⇒ Object
Include another .wrb, .rb or any other file in this file.
-
#kernel_prepare ⇒ Object
Prepare the Kernel, by linking global variables.
-
#lib(name) ⇒ Object
Automatically load a library header into this file in the form of HTML.
-
#post ⇒ Object
Returns a Hash of the POST query of the request.
-
#println(obj) ⇒ Object
Write a string to the output buffer, followed by a newline.
-
#request_object ⇒ Object
The
Waitress::Request
object being used. -
#response_object ⇒ Object
The
Waitress::Response
object being used. -
#set_content_type(raw_type) ⇒ Object
Set the content-type of the response.
-
#set_header(name, value) ⇒ Object
Set a response header.
-
#write(bytes) ⇒ Object
Write a set of bytes directly to the output stream.
Instance Method Details
#combo(name) ⇒ Object
Automatically load a Library Combo into this file. This will consecutively load all the libraries bound to the combination with the given name as defined in the VHost’s config.rb file. This will call lib() for each of these libraries. Params:
name
-
The name of the combo to load
50 51 52 53 54 |
# File 'lib/waitress/kernel.rb', line 50 def combo name name = name.to_sym combo_arr = $VHOST.combos[name] combo_arr.each { |n| lib(n) } end |
#config ⇒ Object
The configuration for the VHost. As described in the Waitress::VHost
class, this data is set in the config.rb file and is then read out later by the server. Use this to configure constants such as Social Media links, and other details
77 78 79 |
# File 'lib/waitress/kernel.rb', line 77 def config $VHOST.config end |
#echo(obj) ⇒ Object
Write a string to the output buffer. This will write directly to the body of the response, similar to what ‘print()’ does for STDOUT. Use this to write data to the output stream
168 169 170 171 |
# File 'lib/waitress/kernel.rb', line 168 def echo obj str = obj.to_s write str end |
#file_ext(extension) ⇒ Object
Set the content-type of the response. This is a shortcut to the Content-Type header, and will also lookup the fileextension in the Waitress::Util
mime-type lookup. Params:
extension
-
The file extension to map to a mimetype, e.g. “.html”
161 162 163 |
# File 'lib/waitress/kernel.rb', line 161 def file_ext extension response_object.mime extension end |
#get ⇒ Object
Returns a Hash of the GET query of the request. This may be an empty array if querystring was present
83 84 85 |
# File 'lib/waitress/kernel.rb', line 83 def get request_object.get_query end |
#get_body ⇒ Object
Get the request body. In most cases, this will be blank, but for POST requests it may contain a querystring, and for PUT and UPDATE methods it may contain other data
130 131 132 |
# File 'lib/waitress/kernel.rb', line 130 def get_body request_object.body end |
#get_header(name) ⇒ Object
Get a header from the HTTP Request. This will fetch the header by the given name from the request object. Keep in mind that in requests, Headers are fully capitalized and any hyphens replaced with underscores (e.g. Content-Type becomes CONTENT_TYPE)
98 99 100 |
# File 'lib/waitress/kernel.rb', line 98 def get_header name request_object.headers[name] end |
#get_headers ⇒ Object
Return the full list of headers available in the request object.
103 104 105 |
# File 'lib/waitress/kernel.rb', line 103 def get_headers request_object.headers end |
#get_method ⇒ Object
Returns the HTTP method that was used to retrieve this page (GET, POST, UPDATE, DELETE, PUT, etc)
109 110 111 |
# File 'lib/waitress/kernel.rb', line 109 def get_method request_object.method end |
#get_path ⇒ Object
Get the path of this request. This is after a URL rewrite, and does not contain a querystring. Be careful with these, as they start with a “/” and if not joined correctly can cause issues in the root of your filesystem (use File.join) if you plan to use this
117 118 119 |
# File 'lib/waitress/kernel.rb', line 117 def get_path request_object.path end |
#get_querystring ⇒ Object
Get the querystring object as a string before it is parsed
135 136 137 |
# File 'lib/waitress/kernel.rb', line 135 def get_querystring request_object.querystring end |
#get_uri ⇒ Object
Get the URI of this request. Unlike the path, the URI is not modified after a rewrite, and does contain a querystring. Use this if you want the original path and query of the request before it was rewritten
124 125 126 |
# File 'lib/waitress/kernel.rb', line 124 def get_uri request_object.uri end |
#includes(filename) ⇒ Object
Include another .wrb, .rb or any other file in the load path of the VHost into this file. If this file is .wrb or .rb, it will be evaluated. If it is another type of file (e.g. html), it will be directly echoed to the output buffer Params:
filename
-
The name of the file, relative to the loadpath
61 62 63 |
# File 'lib/waitress/kernel.rb', line 61 def includes filename Waitress::Chef.include_file filename end |
#includes_file(filename) ⇒ Object
Include another .wrb, .rb or any other file in this file. If this file is .wrb or .rb, it will be evaluated. If it is another type of file (e.g. html), it will be directly echoed to the output buffer Params:
filename
-
The absolute filename of the file to load, anywhere in the filesystem
70 71 72 |
# File 'lib/waitress/kernel.rb', line 70 def includes_file filename Waitress::Chef.include_absfile filename end |
#kernel_prepare ⇒ Object
Prepare the Kernel, by linking global variables
18 19 20 21 22 23 24 |
# File 'lib/waitress/kernel.rb', line 18 def kernel_prepare $METHOD = get_method $HEADERS = get_headers $PATH = get_path $URI = get_uri $BODY = get_body end |
#lib(name) ⇒ Object
Automatically load a library header into this file in the form of HTML. This will load a library in the VHost’s libs/ folder, or any lib defined in the VHost’s config.rb file with the name given. JS libraries will be linked with the <script> tag, whilst css files will be linked with the <link rel=“stylesheet”> tag. This is the recommended way of handling library loading. Params:
name
-
The name of the lib (the filename), or the name of the library as bound
in the config.rb file.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/waitress/kernel.rb', line 34 def lib name name = name.to_sym type = $VHOST.libraries[name][:type] libhome = $VHOST.liburi if type == :js echo "<script type='text/javascript' src='/#{libhome}/#{name}'></script>" elsif type == :css echo "<link rel='stylesheet' href='/#{libhome}/#{name}'></link>" end end |
#post ⇒ Object
Returns a Hash of the POST query of the request. This may be an empty array if the body is not a valid querystring, or an exception raised if there was an error in parsing.
90 91 92 |
# File 'lib/waitress/kernel.rb', line 90 def post request_object.post_query end |
#println(obj) ⇒ Object
Write a string to the output buffer, followed by a newline. Similar to echo(), this will write to the output buffer, but also adds a “n”. This does to the client output as ‘puts()’ does to STDOUT. Use this to write data to the output stream
177 178 179 |
# File 'lib/waitress/kernel.rb', line 177 def println obj echo(obj.to_s + "\n") end |
#request_object ⇒ Object
The Waitress::Request
object being used
13 14 15 |
# File 'lib/waitress/kernel.rb', line 13 def request_object $REQUEST end |
#response_object ⇒ Object
The Waitress::Response
object being used
8 9 10 |
# File 'lib/waitress/kernel.rb', line 8 def response_object $RESPONSE end |
#set_content_type(raw_type) ⇒ Object
Set the content-type of the response. This is a shortcut to the Content-Type header and takes the full content-type (not fileextension) as an argument Params:
raw_type
-
The mime type of the content, e.g. “text/html”
152 153 154 |
# File 'lib/waitress/kernel.rb', line 152 def set_content_type raw_type response_object.mime_raw raw_type end |
#set_header(name, value) ⇒ Object
Set a response header. This will be joined when writing the response with the delimiter “: ” as in regular HTTP Protocol fashion. Params:
name
-
The name of the header, e.g. “Content-Type”
value
-
The value of the header, e.g. “text/html”
144 145 146 |
# File 'lib/waitress/kernel.rb', line 144 def set_header name, value response_object.header name, value end |
#write(bytes) ⇒ Object
Write a set of bytes directly to the output stream. Use this if you don’t want to cast to a string as echo() and println() do.
183 184 185 186 187 |
# File 'lib/waitress/kernel.rb', line 183 def write bytes r = response_object r.body "" if r.body_io.nil? r.body_io.write bytes end |