waitress-core 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -4
  3. data/LICENSE +21 -21
  4. data/Rakefile +17 -17
  5. data/bin/waitress +22 -22
  6. data/ext/Thanks.md +1 -1
  7. data/ext/waitress_http11/ext_help.h +15 -15
  8. data/ext/waitress_http11/extconf.rb +6 -6
  9. data/ext/waitress_http11/http11.c +532 -532
  10. data/ext/waitress_http11/http11_parser.c +1216 -1216
  11. data/ext/waitress_http11/http11_parser.h +49 -49
  12. data/ext/waitress_http11/http11_parser.java.rl +171 -171
  13. data/ext/waitress_http11/http11_parser.rl +165 -165
  14. data/ext/waitress_http11/http11_parser_common.rl +55 -55
  15. data/ext/waitress_http11/http11_wrb_parser.h +91 -91
  16. data/lib/waitress.rb +100 -99
  17. data/lib/waitress/chef.rb +113 -113
  18. data/lib/waitress/configure.rb +127 -121
  19. data/lib/waitress/evalbind.rb +9 -9
  20. data/lib/waitress/handlers/dirhandler.rb +39 -39
  21. data/lib/waitress/handlers/handler.rb +57 -57
  22. data/lib/waitress/handlers/handler404.rb +25 -25
  23. data/lib/waitress/handlers/libhandler.rb +135 -58
  24. data/lib/waitress/kernel.rb +189 -189
  25. data/lib/waitress/parse/query.rb +60 -60
  26. data/lib/waitress/request.rb +45 -45
  27. data/lib/waitress/resources/default_config.rb +52 -52
  28. data/lib/waitress/resources/default_less.yml +7 -0
  29. data/lib/waitress/resources/http/404.html +18 -18
  30. data/lib/waitress/resources/http/css/hack.css +37 -37
  31. data/lib/waitress/resources/http/css/waitress.css +57 -57
  32. data/lib/waitress/resources/http/fonts/eot/latin/hack-bold-latin-webfont.eot +0 -0
  33. data/lib/waitress/resources/http/fonts/eot/latin/hack-bolditalic-latin-webfont.eot +0 -0
  34. data/lib/waitress/resources/http/fonts/eot/latin/hack-italic-latin-webfont.eot +0 -0
  35. data/lib/waitress/resources/http/fonts/eot/latin/hack-regular-latin-webfont.eot +0 -0
  36. data/lib/waitress/resources/http/fonts/svg/latin/hack-bold-latin-webfont.svg +240 -240
  37. data/lib/waitress/resources/http/fonts/svg/latin/hack-bolditalic-latin-webfont.svg +240 -240
  38. data/lib/waitress/resources/http/fonts/svg/latin/hack-italic-latin-webfont.svg +240 -240
  39. data/lib/waitress/resources/http/fonts/svg/latin/hack-regular-latin-webfont.svg +240 -240
  40. data/lib/waitress/resources/http/fonts/web-ttf/latin/hack-bold-latin-webfont.ttf +0 -0
  41. data/lib/waitress/resources/http/fonts/web-ttf/latin/hack-bolditalic-latin-webfont.ttf +0 -0
  42. data/lib/waitress/resources/http/fonts/web-ttf/latin/hack-italic-latin-webfont.ttf +0 -0
  43. data/lib/waitress/resources/http/fonts/web-ttf/latin/hack-regular-latin-webfont.ttf +0 -0
  44. data/lib/waitress/resources/http/fonts/woff/latin/hack-bold-latin-webfont.woff +0 -0
  45. data/lib/waitress/resources/http/fonts/woff/latin/hack-bolditalic-latin-webfont.woff +0 -0
  46. data/lib/waitress/resources/http/fonts/woff/latin/hack-italic-latin-webfont.woff +0 -0
  47. data/lib/waitress/resources/http/fonts/woff/latin/hack-regular-latin-webfont.woff +0 -0
  48. data/lib/waitress/resources/http/fonts/woff2/latin/hack-bold-latin-webfont.woff2 +0 -0
  49. data/lib/waitress/resources/http/fonts/woff2/latin/hack-bolditalic-latin-webfont.woff2 +0 -0
  50. data/lib/waitress/resources/http/fonts/woff2/latin/hack-italic-latin-webfont.woff2 +0 -0
  51. data/lib/waitress/resources/http/fonts/woff2/latin/hack-regular-latin-webfont.woff2 +0 -0
  52. data/lib/waitress/resources/http/img/404.png +0 -0
  53. data/lib/waitress/resources/http/index.html +15 -15
  54. data/lib/waitress/response.rb +105 -105
  55. data/lib/waitress/server.rb +160 -160
  56. data/lib/waitress/util/less_watcher.rb +56 -0
  57. data/lib/waitress/{util.rb → util/util.rb} +707 -707
  58. data/lib/waitress/version.rb +3 -3
  59. data/lib/waitress/vhost.rb +229 -227
  60. data/lib/waitress_http11.so +0 -0
  61. data/waitress-core.gemspec +32 -29
  62. metadata +48 -4
  63. data/lib/waitress_http11.bundle +0 -0
@@ -1,3 +1,3 @@
1
- module Waitress
2
- VERSION = "0.2.4"
3
- end
1
+ module Waitress
2
+ VERSION = "0.3.0"
3
+ end
@@ -1,227 +1,229 @@
1
- module Waitress
2
- # The VHost class is responsible for the actions of a Virtual Host, that is
3
- # reacting to a certain subdomain and handling the requests that are sent to it.
4
- # The requests are managed through the usage of +Waitress::Handler+ instances.
5
- class Vhost < Array
6
-
7
- attr_accessor :priority
8
- attr_accessor :domain
9
- attr_accessor :load_path
10
-
11
- attr_accessor :combos
12
- attr_accessor :libraries
13
-
14
- # Create a new Virtual Host to manage traffic on a Subdomain (Host header)
15
- # Params:
16
- # +pattern+:: The regex pattern used to match this VHost to subdomain(s)
17
- # +priority+:: The priority of the VHost. If multiple VHosts match the subdomain,
18
- # the VHost with the highest priority will be chosen. Default: 50
19
- def initialize pattern, priority=50
20
- @domain = pattern
21
- @priority = priority
22
- @load_path = []
23
- @libdir = "~/.waitress/www/libs"
24
- @liburi = "libraries"
25
-
26
- @libraries = {}
27
- @combos = {}
28
-
29
- @on_request = []
30
- @after_request = []
31
-
32
- @config_data = {}
33
-
34
- enable_waitress_resources
35
- end
36
-
37
- # The config data of the VHost. This config data is set by the configuration
38
- # file and can be read by the server when serving requests. This is so users
39
- # can set data in their config.rb file and read it out on their requests. Use
40
- # this to set constants such as Social Media IDs and other details
41
- def config
42
- @config_data
43
- end
44
-
45
- # Register a listener that will be triggered once a request has been received
46
- # by this VHost, but before it has been passed to a listener or served. Use this
47
- # to modify a request before it is handled.
48
- # The Block should take arguments:
49
- # +request+:: The +Waitress::Request+ object
50
- # +vhost+:: The VirtualHost instance
51
- # +client+:: The Client Socket object
52
- def on_request &block
53
- @on_request << block
54
- end
55
-
56
- # Register a listener that will be triggered once a request has been received
57
- # by this VHost, after it has been handled, but before it is served.
58
- # The Block should take arguments:
59
- # +request+:: The +Waitress::Request+ object
60
- # +response+:: The +Waitress::Response+ object
61
- # +vhost+:: The VirtualHost instance
62
- # +client+:: The Client Socket object
63
- def after_request &block
64
- @after_request << block
65
- end
66
-
67
- # Call this to disable the Waitress Handler (Waitress' default CSS, JS, 404 and Index)
68
- def disable_waitress_resources
69
- @resources = false
70
- end
71
-
72
- # Call this to enable the Waitress Handler (Waitress' default CSS, JS, 404 and Index)
73
- def enable_waitress_resources
74
- @resources = true
75
- end
76
-
77
- # Returns true if the VHost has the Waitress Handler enabled (Waitress' default CSS, JS, 404 and Index)
78
- def resources?
79
- @resources
80
- end
81
-
82
- # Change the default 404 page for the VHost. This should be an absolute file path to your 404 page
83
- # file that you wish to use
84
- def set_404 link
85
- @page_404 = link
86
- end
87
-
88
- # Get the absolute file path for the 404 page to use for this VHost. May be nil.
89
- def get_404
90
- @page_404
91
- end
92
-
93
- # Add a Document Root for this VHost. This document root will contain all of your
94
- # public files that can be served by the vhost.
95
- # Params:
96
- # +dir+:: The directory for the Document Root
97
- # +priority+:: The priority for the directory. If multiple Handlers respond to the target
98
- # URI, the one with the highest priority will be chosen. Default: 50
99
- def root dir, priority=50
100
- self << Waitress::DirHandler.new(File.expand_path(dir), priority)
101
- end
102
-
103
- # Internal
104
- def set_configure conf
105
- @configuration = conf
106
- end
107
-
108
- # Set the directory in which Libraries are contained. Libraries are specially handled by
109
- # waitress to be loaded into .wrb files and handlers. Default: "libs/"
110
- # This file will contain your CSS, JS and any other libraries.
111
- def libdir name
112
- @libdir = File.expand_path(name)
113
- end
114
-
115
- # Change the uri that Libraries should be accessed from. Libraries present in the "libs/"
116
- # directory are served from this URI. This URI is relative to the base "/" uri for the VirtualHost,
117
- # for example, a LibUri of "libraries/" (the default) will be accessed by "your.site/libraries/lib_name"
118
- def liburi name=nil
119
- @liburi = name unless name.nil?
120
- @liburi
121
- end
122
-
123
- # Bind a library to a name. By default, when libraries are found in the css/ and js/ folders of your
124
- # lib directory, they are loaded under the lib name of "filename.type", with type being .css or .js.
125
- # By binding a library, you can give this a different name based on the file found by regular expression,
126
- # for example, binding /jquery/ to "jquery" will match any filenames containing 'jquery', meaning the full
127
- # name of 'jquery-ver.sion' is matched, and can be accessed simply by "jquery"
128
- # Params:
129
- # +pattern+:: The regular expression to check files against. This will match the first file it finds with
130
- # the expression to the library.
131
- # +type+:: The type of file. This should be a symbol of either :js or :css, or any other supported lib types
132
- # +name+:: The new name to reference this library by
133
- # +options+:: Any extra options to use when loading the library (to be used in the future)
134
- def bind_lib pattern, type, name, *options
135
- lib = { :pattern => pattern, :bindtype => type, :options => options}
136
- @libraries[name.to_sym] = lib
137
- end
138
-
139
- # Define a combination of libraries. Calling this method will bind all the libaries listed under one common
140
- # name that can be loaded into .wrb files and handlers using combo(). This is used to load a collection of
141
- # libraries simply and easily without repeating lib() methods.
142
- # Params:
143
- # +name+:: The desired name of the combo
144
- # +targets+:: A list of all the libraries to bind to this combo
145
- def combo name, *targets
146
- @combos[name.to_sym] = targets
147
- targets
148
- end
149
-
150
- # Internal
151
- def parse_libraries
152
- self << Waitress::LibraryHandler.new(@libraries, @libdir, @liburi, self)
153
- end
154
-
155
- # Internal (called on Server Start)
156
- def on_server_start srv
157
- parse_libraries
158
- end
159
-
160
- # Used to define the load path for the VHost. The Load Path defines what files can be
161
- # included from outside of the public web directory, such as database backend or
162
- # authentication. These files are not available to the public, but can be included by
163
- # the .wrb files.
164
- # Params:
165
- # +dir+: The directory to use for the loadpath. If a string is provided, it is added,
166
- # however, if an array is provided, it will override with the array.
167
- def includes dir
168
- if dir.is_a? String
169
- load_path << File.expand_path(dir)
170
- elsif dir.is_a? Array
171
- load_path = dir.map { |x| File.expand_path(x) }
172
- end
173
- end
174
-
175
- # Cancel the current request (don't serve the response)
176
- def cancel_request
177
- @cancelled = true
178
- end
179
-
180
- # Define a rewrite rule. A rewrite rule is used to rewrite a URL's path
181
- # before it is handled. Keep in mind that this does not function for a query string,
182
- # which should instead be handled with the on_request event.
183
- # Params;
184
- # +pattern+:: The pattern to match the URL against
185
- # +newpath+:: The new path to replace the matched pattern with. This can include
186
- # capture groups through the use of \\1 (where 1 is the capture group number)
187
- def rewrite pattern, newpath
188
- on_request do |request, vhost|
189
- request.path = request.path.gsub(pattern, newpath)
190
- end
191
- end
192
-
193
- # Internal (matches requests)
194
- def handle_request request, client
195
- @cancelled = false
196
-
197
- response = Waitress::Response.new
198
-
199
- $REQUEST = request
200
- $RESPONSE = response
201
- $VHOST = self
202
-
203
- @on_request.each { |x| x.call(request, self, client) }
204
-
205
- unless @cancelled
206
- match = nil
207
- if @resources && Waitress::DirHandler.resources_handler.respond?(request, self)
208
- match = Waitress::DirHandler.resources_handler
209
- end
210
-
211
- self.each do |handler|
212
- match = handler if handler.respond?(request, self) && (match.nil? || handler.priority > match.priority)
213
- end
214
-
215
- if match.nil?
216
- Waitress::Chef.error 404, request, response, client, self
217
- else
218
- match.serve! request, response, client, self
219
- end
220
- end
221
-
222
- @after_request.each { |x| x.call(request, response, self, client) }
223
- response.serve(client) unless (response.done? || client.closed?)
224
- end
225
-
226
- end
227
- end
1
+ module Waitress
2
+ # The VHost class is responsible for the actions of a Virtual Host, that is
3
+ # reacting to a certain subdomain and handling the requests that are sent to it.
4
+ # The requests are managed through the usage of +Waitress::Handler+ instances.
5
+ class Vhost < Array
6
+
7
+ attr_accessor :priority
8
+ attr_accessor :domain
9
+ attr_accessor :load_path
10
+
11
+ attr_accessor :combos
12
+ attr_accessor :libraries
13
+
14
+ # Create a new Virtual Host to manage traffic on a Subdomain (Host header)
15
+ # Params:
16
+ # +pattern+:: The regex pattern used to match this VHost to subdomain(s)
17
+ # +priority+:: The priority of the VHost. If multiple VHosts match the subdomain,
18
+ # the VHost with the highest priority will be chosen. Default: 50
19
+ def initialize pattern, priority=50
20
+ @domain = pattern
21
+ @priority = priority
22
+ @load_path = []
23
+
24
+ @libdir = "~/.waitress/www/libs"
25
+ @liburi = "libraries"
26
+
27
+ @libraries = {}
28
+ @combos = {}
29
+
30
+ @on_request = []
31
+ @after_request = []
32
+
33
+ @config_data = {}
34
+
35
+ enable_waitress_resources
36
+ end
37
+
38
+ # The config data of the VHost. This config data is set by the configuration
39
+ # file and can be read by the server when serving requests. This is so users
40
+ # can set data in their config.rb file and read it out on their requests. Use
41
+ # this to set constants such as Social Media IDs and other details
42
+ def config
43
+ @config_data
44
+ end
45
+
46
+ # Register a listener that will be triggered once a request has been received
47
+ # by this VHost, but before it has been passed to a listener or served. Use this
48
+ # to modify a request before it is handled.
49
+ # The Block should take arguments:
50
+ # +request+:: The +Waitress::Request+ object
51
+ # +vhost+:: The VirtualHost instance
52
+ # +client+:: The Client Socket object
53
+ def on_request &block
54
+ @on_request << block
55
+ end
56
+
57
+ # Register a listener that will be triggered once a request has been received
58
+ # by this VHost, after it has been handled, but before it is served.
59
+ # The Block should take arguments:
60
+ # +request+:: The +Waitress::Request+ object
61
+ # +response+:: The +Waitress::Response+ object
62
+ # +vhost+:: The VirtualHost instance
63
+ # +client+:: The Client Socket object
64
+ def after_request &block
65
+ @after_request << block
66
+ end
67
+
68
+ # Call this to disable the Waitress Handler (Waitress' default CSS, JS, 404 and Index)
69
+ def disable_waitress_resources
70
+ @resources = false
71
+ end
72
+
73
+ # Call this to enable the Waitress Handler (Waitress' default CSS, JS, 404 and Index)
74
+ def enable_waitress_resources
75
+ @resources = true
76
+ end
77
+
78
+ # Returns true if the VHost has the Waitress Handler enabled (Waitress' default CSS, JS, 404 and Index)
79
+ def resources?
80
+ @resources
81
+ end
82
+
83
+ # Change the default 404 page for the VHost. This should be an absolute file path to your 404 page
84
+ # file that you wish to use
85
+ def set_404 link
86
+ @page_404 = link
87
+ end
88
+
89
+ # Get the absolute file path for the 404 page to use for this VHost. May be nil.
90
+ def get_404
91
+ @page_404
92
+ end
93
+
94
+ # Add a Document Root for this VHost. This document root will contain all of your
95
+ # public files that can be served by the vhost.
96
+ # Params:
97
+ # +dir+:: The directory for the Document Root
98
+ # +priority+:: The priority for the directory. If multiple Handlers respond to the target
99
+ # URI, the one with the highest priority will be chosen. Default: 50
100
+ def root dir, priority=50
101
+ self << Waitress::DirHandler.new(File.expand_path(dir), priority)
102
+ end
103
+
104
+ # Internal
105
+ def set_configure conf
106
+ @configuration = conf
107
+ end
108
+
109
+ # Set the directory in which Libraries are contained. Libraries are specially handled by
110
+ # waitress to be loaded into .wrb files and handlers. Default: "libs/"
111
+ # This file will contain your CSS, JS and any other libraries.
112
+ # Bower packages are also supported under this directory ("bower_components")
113
+ def libdir name
114
+ @libdir = File.expand_path(name)
115
+ end
116
+
117
+ # Change the uri that Libraries should be accessed from. Libraries present in the "libs/"
118
+ # directory are served from this URI. This URI is relative to the base "/" uri for the VirtualHost,
119
+ # for example, a LibUri of "libraries/" (the default) will be accessed by "your.site/libraries/lib_name"
120
+ def liburi name=nil
121
+ @liburi = name unless name.nil?
122
+ @liburi
123
+ end
124
+
125
+ # Bind a library to a name. By default, when libraries are found in the css/ and js/ folders of your
126
+ # lib directory, they are loaded under the lib name of "filename.type", with type being .css or .js.
127
+ # By binding a library, you can give this a different name based on the file found by regular expression,
128
+ # for example, binding /jquery/ to "jquery" will match any filenames containing 'jquery', meaning the full
129
+ # name of 'jquery-ver.sion' is matched, and can be accessed simply by "jquery"
130
+ # Params:
131
+ # +pattern+:: The regular expression to check files against. This will match the first file it finds with
132
+ # the expression to the library.
133
+ # +type+:: The type of file. This should be a symbol of either :js or :css, or any other supported lib types
134
+ # +name+:: The new name to reference this library by
135
+ # +options+:: Any extra options to use when loading the library (to be used in the future)
136
+ def bind_lib pattern, type, name, *options
137
+ lib = { :pattern => pattern, :bindtype => type, :options => options}
138
+ @libraries[name.to_sym] = lib
139
+ end
140
+
141
+ # Define a combination of libraries. Calling this method will bind all the libaries listed under one common
142
+ # name that can be loaded into .wrb files and handlers using combo(). This is used to load a collection of
143
+ # libraries simply and easily without repeating lib() methods.
144
+ # Params:
145
+ # +name+:: The desired name of the combo
146
+ # +targets+:: A list of all the libraries to bind to this combo
147
+ def combo name, *targets
148
+ @combos[name.to_sym] = targets
149
+ targets
150
+ end
151
+
152
+ # Internal
153
+ def parse_libraries
154
+ self << Waitress::LibraryHandler.new(@libraries, @libdir, @liburi, self)
155
+ end
156
+
157
+ # Internal (called on Server Start)
158
+ def on_server_start srv
159
+ parse_libraries
160
+ end
161
+
162
+ # Used to define the load path for the VHost. The Load Path defines what files can be
163
+ # included from outside of the public web directory, such as database backend or
164
+ # authentication. These files are not available to the public, but can be included by
165
+ # the .wrb files.
166
+ # Params:
167
+ # +dir+: The directory to use for the loadpath. If a string is provided, it is added,
168
+ # however, if an array is provided, it will override with the array.
169
+ def includes dir
170
+ if dir.is_a? String
171
+ load_path << File.expand_path(dir)
172
+ elsif dir.is_a? Array
173
+ load_path = dir.map { |x| File.expand_path(x) }
174
+ end
175
+ end
176
+
177
+ # Cancel the current request (don't serve the response)
178
+ def cancel_request
179
+ @cancelled = true
180
+ end
181
+
182
+ # Define a rewrite rule. A rewrite rule is used to rewrite a URL's path
183
+ # before it is handled. Keep in mind that this does not function for a query string,
184
+ # which should instead be handled with the on_request event.
185
+ # Params;
186
+ # +pattern+:: The pattern to match the URL against
187
+ # +newpath+:: The new path to replace the matched pattern with. This can include
188
+ # capture groups through the use of \\1 (where 1 is the capture group number)
189
+ def rewrite pattern, newpath
190
+ on_request do |request, vhost|
191
+ request.path = request.path.gsub(pattern, newpath)
192
+ end
193
+ end
194
+
195
+ # Internal (matches requests)
196
+ def handle_request request, client
197
+ @cancelled = false
198
+
199
+ response = Waitress::Response.new
200
+
201
+ $REQUEST = request
202
+ $RESPONSE = response
203
+ $VHOST = self
204
+
205
+ @on_request.each { |x| x.call(request, self, client) }
206
+
207
+ unless @cancelled
208
+ match = nil
209
+ if @resources && Waitress::DirHandler.resources_handler.respond?(request, self)
210
+ match = Waitress::DirHandler.resources_handler
211
+ end
212
+
213
+ self.each do |handler|
214
+ match = handler if handler.respond?(request, self) && (match.nil? || handler.priority > match.priority)
215
+ end
216
+
217
+ if match.nil?
218
+ Waitress::Chef.error 404, request, response, client, self
219
+ else
220
+ match.serve! request, response, client, self
221
+ end
222
+ end
223
+
224
+ @after_request.each { |x| x.call(request, response, self, client) }
225
+ response.serve(client) unless (response.done? || client.closed?)
226
+ end
227
+
228
+ end
229
+ end