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

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

#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



161
162
163
164
# File 'lib/waitress/kernel.rb', line 161

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”



154
155
156
# File 'lib/waitress/kernel.rb', line 154

def file_ext extension
  response_object.mime extension
end

#getObject

Returns a Hash of the GET query of the request. This may be an empty array if querystring was present



76
77
78
# File 'lib/waitress/kernel.rb', line 76

def get
  request_object.get_query
end

#get_bodyObject

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



123
124
125
# File 'lib/waitress/kernel.rb', line 123

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)



91
92
93
# File 'lib/waitress/kernel.rb', line 91

def get_header name
  request_object.headers[name]
end

#get_headersObject

Return the full list of headers available in the request object.



96
97
98
# File 'lib/waitress/kernel.rb', line 96

def get_headers
  request_object.headers
end

#get_methodObject

Returns the HTTP method that was used to retrieve this page (GET, POST, UPDATE, DELETE, PUT, etc)



102
103
104
# File 'lib/waitress/kernel.rb', line 102

def get_method
  request_object.method
end

#get_pathObject

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



110
111
112
# File 'lib/waitress/kernel.rb', line 110

def get_path
  request_object.path
end

#get_querystringObject

Get the querystring object as a string before it is parsed



128
129
130
# File 'lib/waitress/kernel.rb', line 128

def get_querystring
  request_object.querystring
end

#get_uriObject

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



117
118
119
# File 'lib/waitress/kernel.rb', line 117

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_prepareObject

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

#postObject

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.



83
84
85
# File 'lib/waitress/kernel.rb', line 83

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



170
171
172
# File 'lib/waitress/kernel.rb', line 170

def println obj
  echo(obj.to_s + "\n")
end

#request_objectObject

The Waitress::Request object being used



13
14
15
# File 'lib/waitress/kernel.rb', line 13

def request_object
  $REQUEST
end

#response_objectObject

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”



145
146
147
# File 'lib/waitress/kernel.rb', line 145

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”



137
138
139
# File 'lib/waitress/kernel.rb', line 137

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.



176
177
178
179
180
# File 'lib/waitress/kernel.rb', line 176

def write bytes
  r = response_object
  r.body "" if r.body_io.nil?
  r.body_io.write bytes
end