waitress-core 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -4
  3. data/LICENSE +21 -21
  4. data/Rakefile +13 -13
  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/chef.rb +113 -113
  17. data/lib/waitress/configure.rb +121 -121
  18. data/lib/waitress/evalbind.rb +9 -9
  19. data/lib/waitress/handlers/dirhandler.rb +39 -39
  20. data/lib/waitress/handlers/handler.rb +57 -57
  21. data/lib/waitress/handlers/handler404.rb +25 -25
  22. data/lib/waitress/handlers/libhandler.rb +58 -58
  23. data/lib/waitress/kernel.rb +182 -182
  24. data/lib/waitress/parse/query.rb +60 -60
  25. data/lib/waitress/request.rb +45 -45
  26. data/lib/waitress/resources/default_config.rb +52 -52
  27. data/lib/waitress/resources/http/404.html +18 -18
  28. data/lib/waitress/resources/http/css/hack.css +37 -37
  29. data/lib/waitress/resources/http/css/waitress.css +57 -57
  30. data/lib/waitress/resources/http/fonts/svg/latin/hack-bold-latin-webfont.svg +240 -240
  31. data/lib/waitress/resources/http/fonts/svg/latin/hack-bolditalic-latin-webfont.svg +240 -240
  32. data/lib/waitress/resources/http/fonts/svg/latin/hack-italic-latin-webfont.svg +240 -240
  33. data/lib/waitress/resources/http/fonts/svg/latin/hack-regular-latin-webfont.svg +240 -240
  34. data/lib/waitress/resources/http/index.html +15 -15
  35. data/lib/waitress/response.rb +105 -105
  36. data/lib/waitress/server.rb +161 -158
  37. data/lib/waitress/util.rb +707 -707
  38. data/lib/waitress/version.rb +3 -3
  39. data/lib/waitress/vhost.rb +217 -217
  40. data/lib/waitress.rb +106 -106
  41. data/lib/waitress_http11.so +0 -0
  42. data/waitress-core.gemspec +29 -29
  43. metadata +3 -3
  44. data/lib/waitress_http11.bundle +0 -0
data/lib/waitress/chef.rb CHANGED
@@ -1,113 +1,113 @@
1
- module Waitress
2
-
3
- # The chef class handles the cooking of responses, by serving errors and files
4
- # from the Filesystem, as well as providing a system for including files within
5
- # handlers and .wrb files. This class handles most loading from Filesystems
6
- # and serving of the body of a response.
7
- class Chef
8
-
9
- HANDLERS = {
10
- 404 => Handler404.new
11
- }
12
-
13
- # Get the waitress resources directory, containing templates and the default
14
- # http assets of waitress
15
- def self.resources
16
- File.expand_path "../resources", __FILE__
17
- end
18
-
19
- # Get the waitress HTTP assets directory, containing the default index, 404
20
- # and styles resources.
21
- def self.resources_http
22
- File.join(resources, "http")
23
- end
24
-
25
- # Set the response to use an error page with the given error code (usually 404)
26
- def self.error code, request, response, client, vhost
27
- HANDLERS[code].serve!(request, response, client, vhost)
28
- end
29
-
30
- # Serve a file from the Filesystem, automatically handling based on whether the
31
- # file is dynamic or static. This will set the body io and any required
32
- # headers on the reponse object.
33
- def self.serve_file request, response, client, vhost, file
34
- if File.extname(file) == ".wrb" || File.extname(file) == ".rb"
35
- response.mime ".html"
36
- include_absfile File.expand_path(file)
37
- else
38
- response.mime File.extname(file)
39
- response.body_io File.open(file, "r")
40
- end
41
- end
42
-
43
- # Include a file from the VHost loadpath in the current running instance.
44
- # Params:
45
- # +filename+:: The filename of the file, relative to the load path of the VHost.
46
- def self.include_file filename
47
- lp = $VHOST.load_path
48
- target = nil
49
- lp.each do |path|
50
- fl = File.join(path, filename)
51
- ext = ["", ".rb", ".wrb"]
52
- ext.each do |e|
53
- flnew = "#{fl}#{e}"
54
- target = flnew if File.exist?(flnew)
55
- end
56
- end
57
-
58
- raise LoadError.new("Include does not exist in Load Path: #{target}") if target.nil?
59
- include_absfile target
60
- end
61
-
62
- # Include a file from anywhere in the filesystem (absolute) in the current running
63
- # instance. This will load the file's content and, if dynamic, evaluate it to the
64
- # current response.
65
- # Params:
66
- # +target+:: The target file, given in absolute path form.
67
- def self.include_absfile target
68
- ext = File.extname target
69
- bind = Waitress::EvalBindings.new
70
- if ext == ".wrb"
71
- Waitress::WRBParser.parse!(File.read(target), $RESPONSE.body_io) do |txt, lino|
72
- eval(txt, bind.context, target, lino) # For some reason, rb_f_eval is not exposed to ruby.h
73
- end
74
- elsif ext == ".rb"
75
- require target
76
- else
77
- echo File.read(target)
78
- end
79
- end
80
-
81
- # Find a file to serve. This is used by the DirHandler
82
- # to automatically include an index file if it exists under the
83
- # requested directory, automatically add the .wrb or .html file extension
84
- # for paths without the extension, etc. A hash containing the result (ok / notfound)
85
- # and the new filepath will be given.
86
- def self.find_file abspath
87
- ret = {}
88
- if File.exist?(abspath)
89
- if File.directory?(abspath)
90
- wrb = File.join(abspath, "index.wrb")
91
- html = File.join(abspath, "index.html")
92
- if File.exist?(wrb)
93
- ret = { :result => :ok, :file => wrb }
94
- elsif File.exist?(html)
95
- ret = { :result => :ok, :file => html }
96
- else
97
- ret = { :result => :dir, :file => abspath }
98
- end
99
- else
100
- ret = { :result => :ok, :file => abspath }
101
- end
102
- elsif File.exist?("#{abspath}.html")
103
- ret = { :result => :ok, :file => "#{abspath}.html" }
104
- elsif File.exist?("#{abspath}.wrb")
105
- ret = { :result => :ok, :file => "#{abspath}.wrb" }
106
- else
107
- ret = { :result => :notfound }
108
- end
109
- ret
110
- end
111
-
112
- end
113
- end
1
+ module Waitress
2
+
3
+ # The chef class handles the cooking of responses, by serving errors and files
4
+ # from the Filesystem, as well as providing a system for including files within
5
+ # handlers and .wrb files. This class handles most loading from Filesystems
6
+ # and serving of the body of a response.
7
+ class Chef
8
+
9
+ HANDLERS = {
10
+ 404 => Handler404.new
11
+ }
12
+
13
+ # Get the waitress resources directory, containing templates and the default
14
+ # http assets of waitress
15
+ def self.resources
16
+ File.expand_path "../resources", __FILE__
17
+ end
18
+
19
+ # Get the waitress HTTP assets directory, containing the default index, 404
20
+ # and styles resources.
21
+ def self.resources_http
22
+ File.join(resources, "http")
23
+ end
24
+
25
+ # Set the response to use an error page with the given error code (usually 404)
26
+ def self.error code, request, response, client, vhost
27
+ HANDLERS[code].serve!(request, response, client, vhost)
28
+ end
29
+
30
+ # Serve a file from the Filesystem, automatically handling based on whether the
31
+ # file is dynamic or static. This will set the body io and any required
32
+ # headers on the reponse object.
33
+ def self.serve_file request, response, client, vhost, file
34
+ if File.extname(file) == ".wrb" || File.extname(file) == ".rb"
35
+ response.mime ".html"
36
+ include_absfile File.expand_path(file)
37
+ else
38
+ response.mime File.extname(file)
39
+ response.body_io File.open(file, "r")
40
+ end
41
+ end
42
+
43
+ # Include a file from the VHost loadpath in the current running instance.
44
+ # Params:
45
+ # +filename+:: The filename of the file, relative to the load path of the VHost.
46
+ def self.include_file filename
47
+ lp = $VHOST.load_path
48
+ target = nil
49
+ lp.each do |path|
50
+ fl = File.join(path, filename)
51
+ ext = ["", ".rb", ".wrb"]
52
+ ext.each do |e|
53
+ flnew = "#{fl}#{e}"
54
+ target = flnew if File.exist?(flnew)
55
+ end
56
+ end
57
+
58
+ raise LoadError.new("Include does not exist in Load Path: #{target}") if target.nil?
59
+ include_absfile target
60
+ end
61
+
62
+ # Include a file from anywhere in the filesystem (absolute) in the current running
63
+ # instance. This will load the file's content and, if dynamic, evaluate it to the
64
+ # current response.
65
+ # Params:
66
+ # +target+:: The target file, given in absolute path form.
67
+ def self.include_absfile target
68
+ ext = File.extname target
69
+ bind = Waitress::EvalBindings.new
70
+ if ext == ".wrb"
71
+ Waitress::WRBParser.parse!(File.read(target), $RESPONSE.body_io) do |txt, lino|
72
+ eval(txt, bind.context, target, lino) # For some reason, rb_f_eval is not exposed to ruby.h
73
+ end
74
+ elsif ext == ".rb"
75
+ require target
76
+ else
77
+ echo File.read(target)
78
+ end
79
+ end
80
+
81
+ # Find a file to serve. This is used by the DirHandler
82
+ # to automatically include an index file if it exists under the
83
+ # requested directory, automatically add the .wrb or .html file extension
84
+ # for paths without the extension, etc. A hash containing the result (ok / notfound)
85
+ # and the new filepath will be given.
86
+ def self.find_file abspath
87
+ ret = {}
88
+ if File.exist?(abspath)
89
+ if File.directory?(abspath)
90
+ wrb = File.join(abspath, "index.wrb")
91
+ html = File.join(abspath, "index.html")
92
+ if File.exist?(wrb)
93
+ ret = { :result => :ok, :file => wrb }
94
+ elsif File.exist?(html)
95
+ ret = { :result => :ok, :file => html }
96
+ else
97
+ ret = { :result => :dir, :file => abspath }
98
+ end
99
+ else
100
+ ret = { :result => :ok, :file => abspath }
101
+ end
102
+ elsif File.exist?("#{abspath}.html")
103
+ ret = { :result => :ok, :file => "#{abspath}.html" }
104
+ elsif File.exist?("#{abspath}.wrb")
105
+ ret = { :result => :ok, :file => "#{abspath}.wrb" }
106
+ else
107
+ ret = { :result => :notfound }
108
+ end
109
+ ret
110
+ end
111
+
112
+ end
113
+ end
@@ -1,121 +1,121 @@
1
- module Waitress
2
-
3
- # The Configure Class is used to load Waitress configurations from the filesystem,
4
- # usually used in the config.rb file.
5
- class Configure
6
-
7
- # Endpoint for Waitress.configure!
8
- def self.configure! *args, &block
9
- raise "Using 'configure!' outside of file target" if @@configure_target.nil?
10
- @@configure_target.read_configure *args, &block
11
- end
12
-
13
- def self.config_target o
14
- @@configure_target = o
15
- end
16
-
17
- attr_accessor :servers
18
- attr_accessor :root
19
-
20
- def initialize root
21
- @root = root
22
- @configurations = []
23
- generate_configs
24
-
25
- load_cfg
26
-
27
- @servers = @configurations.map do |conf|
28
- s = Waitress::HttpServer.new
29
- conf.hosts.each { |h| s << h }
30
- s.set_processes conf.processes
31
- s.ports *conf.ports
32
- s
33
- end
34
- puts "Waitress Configuration Complete"
35
- puts "Servers Started on Ports: "
36
- @servers.each do |x|
37
- puts "\t#{x.ports.join ", "}"
38
- end
39
- end
40
-
41
- # Run all servers in this configuration
42
- def run
43
- @servers.each { |s| s.run }
44
- end
45
-
46
- # Join all the server threads in this configuration
47
- def join
48
- @servers.each { |s| s.join }
49
- end
50
-
51
- # Read the configuration object
52
- def read_configure *args, &block
53
- config = Configuration.new self, *args
54
- block.call(config)
55
- @configurations << config
56
- end
57
-
58
- # Generate the configuration file if it doesn't already exist.
59
- def generate_configs
60
- FileUtils.mkdir_p(@root) unless File.exist?(@root)
61
- raise "File #{@root} is not a directory!" unless File.directory?(@root)
62
- @config = File.join(@root, "config.rb")
63
- unless File.exist?(@config)
64
- c = File.read(File.join(Waitress::Chef.resources, "default_config.rb"))
65
- File.write(@config, c)
66
- end
67
- end
68
-
69
- # Load the config.rb file
70
- def load_cfg
71
- Waitress::Configure.config_target self
72
- path = File.absolute_path(@config)
73
- require path
74
- Waitress::Configure.config_target nil
75
- end
76
-
77
- end
78
-
79
- # The Configuration Class is the object that contains a configuration
80
- # of a single server instance, hosting all the methods provided by
81
- # Waitress.configure!
82
- class Configuration
83
-
84
- attr_accessor :hosts
85
- attr_accessor :ports
86
- attr_accessor :processes
87
-
88
- def initialize configure, *ports
89
- @ports = *ports
90
- @hosts = []
91
- @configure = configure
92
-
93
- @processes = 10
94
- @processes = ENV["WAITRESS_PROCESSES"].to_i if ENV.include? "WAITRESS_PROCESSES"
95
- end
96
-
97
- # Create a new VirtualHost with the given regex, priority, and configure it
98
- # inside of its own block.
99
- def host regex, priority=50, &block
100
- host = Waitress::Vhost.new regex, priority
101
- host.set_configure @configure
102
- block.call(host)
103
- @hosts << host
104
- host
105
- end
106
-
107
- # Over all the registered VHosts, call this method. Use this to configure
108
- # a similar setting for all vhosts at once instead of duplicating code
109
- # across all of them.
110
- def all_hosts &block
111
- @hosts.each { |h| block.call(h) }
112
- end
113
-
114
- def to_s
115
- m = lambda { |a,x| x.nil? ? "" : "\r\n#{a}=#{x.inspect}" }
116
- "#<#{self.class} #{m.call('ports', @ports)} #{m.call('hosts', @hosts)}>"
117
- end
118
-
119
- end
120
-
121
- end
1
+ module Waitress
2
+
3
+ # The Configure Class is used to load Waitress configurations from the filesystem,
4
+ # usually used in the config.rb file.
5
+ class Configure
6
+
7
+ # Endpoint for Waitress.configure!
8
+ def self.configure! *args, &block
9
+ raise "Using 'configure!' outside of file target" if @@configure_target.nil?
10
+ @@configure_target.read_configure *args, &block
11
+ end
12
+
13
+ def self.config_target o
14
+ @@configure_target = o
15
+ end
16
+
17
+ attr_accessor :servers
18
+ attr_accessor :root
19
+
20
+ def initialize root
21
+ @root = root
22
+ @configurations = []
23
+ generate_configs
24
+
25
+ load_cfg
26
+
27
+ @servers = @configurations.map do |conf|
28
+ s = Waitress::HttpServer.new
29
+ conf.hosts.each { |h| s << h }
30
+ s.set_processes conf.processes
31
+ s.ports *conf.ports
32
+ s
33
+ end
34
+ puts "Waitress Configuration Complete"
35
+ puts "Servers Started on Ports: "
36
+ @servers.each do |x|
37
+ puts "\t#{x.ports.join ", "}"
38
+ end
39
+ end
40
+
41
+ # Run all servers in this configuration
42
+ def run
43
+ @servers.each { |s| s.run }
44
+ end
45
+
46
+ # Join all the server threads in this configuration
47
+ def join
48
+ @servers.each { |s| s.join }
49
+ end
50
+
51
+ # Read the configuration object
52
+ def read_configure *args, &block
53
+ config = Configuration.new self, *args
54
+ block.call(config)
55
+ @configurations << config
56
+ end
57
+
58
+ # Generate the configuration file if it doesn't already exist.
59
+ def generate_configs
60
+ FileUtils.mkdir_p(@root) unless File.exist?(@root)
61
+ raise "File #{@root} is not a directory!" unless File.directory?(@root)
62
+ @config = File.join(@root, "config.rb")
63
+ unless File.exist?(@config)
64
+ c = File.read(File.join(Waitress::Chef.resources, "default_config.rb"))
65
+ File.write(@config, c)
66
+ end
67
+ end
68
+
69
+ # Load the config.rb file
70
+ def load_cfg
71
+ Waitress::Configure.config_target self
72
+ path = File.absolute_path(@config)
73
+ require path
74
+ Waitress::Configure.config_target nil
75
+ end
76
+
77
+ end
78
+
79
+ # The Configuration Class is the object that contains a configuration
80
+ # of a single server instance, hosting all the methods provided by
81
+ # Waitress.configure!
82
+ class Configuration
83
+
84
+ attr_accessor :hosts
85
+ attr_accessor :ports
86
+ attr_accessor :processes
87
+
88
+ def initialize configure, *ports
89
+ @ports = *ports
90
+ @hosts = []
91
+ @configure = configure
92
+
93
+ @processes = 5
94
+ @processes = ENV["WAITRESS_PROCESSES"].to_i if ENV.include? "WAITRESS_PROCESSES"
95
+ end
96
+
97
+ # Create a new VirtualHost with the given regex, priority, and configure it
98
+ # inside of its own block.
99
+ def host regex, priority=50, &block
100
+ host = Waitress::Vhost.new regex, priority
101
+ host.set_configure @configure
102
+ block.call(host)
103
+ @hosts << host
104
+ host
105
+ end
106
+
107
+ # Over all the registered VHosts, call this method. Use this to configure
108
+ # a similar setting for all vhosts at once instead of duplicating code
109
+ # across all of them.
110
+ def all_hosts &block
111
+ @hosts.each { |h| block.call(h) }
112
+ end
113
+
114
+ def to_s
115
+ m = lambda { |a,x| x.nil? ? "" : "\r\n#{a}=#{x.inspect}" }
116
+ "#<#{self.class} #{m.call('ports', @ports)} #{m.call('hosts', @hosts)}>"
117
+ end
118
+
119
+ end
120
+
121
+ end
@@ -1,9 +1,9 @@
1
- module Waitress
2
- class EvalBindings
3
-
4
- def context
5
- binding()
6
- end
7
-
8
- end
9
- end
1
+ module Waitress
2
+ class EvalBindings
3
+
4
+ def context
5
+ binding()
6
+ end
7
+
8
+ end
9
+ end
@@ -1,39 +1,39 @@
1
- module Waitress
2
-
3
- # The DirHandler class is an instance of +Waitress::Handler+ that is responsible
4
- # for loading files from the filesystem and serving them if they exist in the VHost's
5
- # root. It automatically handles mimetypes, evaluation and almost everything about
6
- # the serving process for files in the FileSystem.
7
- class DirHandler < Handler
8
-
9
- attr_accessor :priority
10
- attr_accessor :directory
11
-
12
- # Get the instance of DirHandler that will load Waitress' resources
13
- # such as the default 404 and index pages, as well as CSS and JS
14
- def self.resources_handler
15
- @@resources_handler ||= Waitress::DirHandler.new(Waitress::Chef.resources_http, -1000)
16
- @@resources_handler
17
- end
18
-
19
- # Create a new DirHandler, with the given FileSystem directory as a root
20
- # and priority.
21
- def initialize directory, priority=50
22
- @directory = File.absolute_path(directory)
23
- @priority = priority
24
- end
25
-
26
- def respond? request, vhost
27
- path = File.expand_path File.join("#{directory}", request.path)
28
- res = Waitress::Chef.find_file(path)[:result]
29
- path.include?(directory) && (res == :ok)
30
- end
31
-
32
- def serve request, response, client, vhost
33
- path = File.expand_path File.join("#{directory}", request.path)
34
- file = Waitress::Chef.find_file(path)[:file]
35
- Waitress::Chef.serve_file request, response, client, vhost, file
36
- end
37
-
38
- end
39
- end
1
+ module Waitress
2
+
3
+ # The DirHandler class is an instance of +Waitress::Handler+ that is responsible
4
+ # for loading files from the filesystem and serving them if they exist in the VHost's
5
+ # root. It automatically handles mimetypes, evaluation and almost everything about
6
+ # the serving process for files in the FileSystem.
7
+ class DirHandler < Handler
8
+
9
+ attr_accessor :priority
10
+ attr_accessor :directory
11
+
12
+ # Get the instance of DirHandler that will load Waitress' resources
13
+ # such as the default 404 and index pages, as well as CSS and JS
14
+ def self.resources_handler
15
+ @@resources_handler ||= Waitress::DirHandler.new(Waitress::Chef.resources_http, -1000)
16
+ @@resources_handler
17
+ end
18
+
19
+ # Create a new DirHandler, with the given FileSystem directory as a root
20
+ # and priority.
21
+ def initialize directory, priority=50
22
+ @directory = File.absolute_path(directory)
23
+ @priority = priority
24
+ end
25
+
26
+ def respond? request, vhost
27
+ path = File.expand_path File.join("#{directory}", request.path)
28
+ res = Waitress::Chef.find_file(path)[:result]
29
+ path.include?(directory) && (res == :ok)
30
+ end
31
+
32
+ def serve request, response, client, vhost
33
+ path = File.expand_path File.join("#{directory}", request.path)
34
+ file = Waitress::Chef.find_file(path)[:file]
35
+ Waitress::Chef.serve_file request, response, client, vhost, file
36
+ end
37
+
38
+ end
39
+ end