falcon 0.36.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +5 -0
  3. data/.github/FUNDING.yml +3 -0
  4. data/.github/workflows/development.yml +60 -0
  5. data/.gitignore +14 -0
  6. data/.rspec +3 -0
  7. data/Gemfile +17 -0
  8. data/README.md +316 -0
  9. data/bake/falcon/supervisor.rb +8 -0
  10. data/bin/falcon +32 -0
  11. data/bin/falcon-host +28 -0
  12. data/examples/beer/config.ru +57 -0
  13. data/examples/beer/falcon.rb +8 -0
  14. data/examples/benchmark/config.ru +39 -0
  15. data/examples/benchmark/falcon.rb +6 -0
  16. data/examples/csv/config.ru +31 -0
  17. data/examples/google/falcon.rb +14 -0
  18. data/examples/hello/config.ru +22 -0
  19. data/examples/hello/falcon.rb +24 -0
  20. data/examples/hello/preload.rb +7 -0
  21. data/examples/internet/config.ru +54 -0
  22. data/examples/memory/allocations.rb +39 -0
  23. data/examples/memory/config.ru +14 -0
  24. data/examples/push/client.rb +29 -0
  25. data/examples/push/config.ru +28 -0
  26. data/examples/push/index.html +14 -0
  27. data/examples/push/script.js +2 -0
  28. data/examples/push/style.css +4 -0
  29. data/examples/redis/Gemfile +9 -0
  30. data/examples/redis/config.ru +28 -0
  31. data/examples/sequel/Gemfile +4 -0
  32. data/examples/sequel/config.ru +8 -0
  33. data/examples/sequel/data.sqlite3 +0 -0
  34. data/examples/server/standalone.rb +27 -0
  35. data/examples/sinatra/Gemfile +7 -0
  36. data/examples/sinatra/Gemfile.lock +53 -0
  37. data/examples/sinatra/config.ru +16 -0
  38. data/examples/trailers/config.ru +34 -0
  39. data/examples/trailers/falcon.rb +8 -0
  40. data/falcon.gemspec +45 -0
  41. data/gems/rack1.gemfile +4 -0
  42. data/gems/rack3.gemfile +4 -0
  43. data/lib/falcon.rb +23 -0
  44. data/lib/falcon/adapters/early_hints.rb +49 -0
  45. data/lib/falcon/adapters/input.rb +131 -0
  46. data/lib/falcon/adapters/output.rb +101 -0
  47. data/lib/falcon/adapters/rack.rb +202 -0
  48. data/lib/falcon/adapters/response.rb +91 -0
  49. data/lib/falcon/adapters/rewindable.rb +67 -0
  50. data/lib/falcon/command.rb +31 -0
  51. data/lib/falcon/command/host.rb +69 -0
  52. data/lib/falcon/command/paths.rb +47 -0
  53. data/lib/falcon/command/proxy.rb +71 -0
  54. data/lib/falcon/command/redirect.rb +76 -0
  55. data/lib/falcon/command/serve.rb +151 -0
  56. data/lib/falcon/command/supervisor.rb +78 -0
  57. data/lib/falcon/command/top.rb +94 -0
  58. data/lib/falcon/command/virtual.rb +89 -0
  59. data/lib/falcon/configuration.rb +147 -0
  60. data/lib/falcon/configuration/application.rb +46 -0
  61. data/lib/falcon/configuration/lets_encrypt_tls.rb +30 -0
  62. data/lib/falcon/configuration/proxy.rb +27 -0
  63. data/lib/falcon/configuration/rack.rb +38 -0
  64. data/lib/falcon/configuration/self_signed_tls.rb +47 -0
  65. data/lib/falcon/configuration/supervisor.rb +37 -0
  66. data/lib/falcon/configuration/tls.rb +70 -0
  67. data/lib/falcon/controller/host.rb +60 -0
  68. data/lib/falcon/controller/proxy.rb +109 -0
  69. data/lib/falcon/controller/redirect.rb +69 -0
  70. data/lib/falcon/controller/serve.rb +111 -0
  71. data/lib/falcon/controller/virtual.rb +100 -0
  72. data/lib/falcon/endpoint.rb +52 -0
  73. data/lib/falcon/extensions/openssl.rb +33 -0
  74. data/lib/falcon/middleware/proxy.rb +145 -0
  75. data/lib/falcon/middleware/redirect.rb +66 -0
  76. data/lib/falcon/proxy_endpoint.rb +71 -0
  77. data/lib/falcon/server.rb +54 -0
  78. data/lib/falcon/service/application.rb +93 -0
  79. data/lib/falcon/service/generic.rb +60 -0
  80. data/lib/falcon/service/proxy.rb +60 -0
  81. data/lib/falcon/service/supervisor.rb +104 -0
  82. data/lib/falcon/services.rb +78 -0
  83. data/lib/falcon/tls.rb +46 -0
  84. data/lib/falcon/verbose.rb +59 -0
  85. data/lib/falcon/version.rb +25 -0
  86. data/lib/rack/handler/falcon.rb +40 -0
  87. data/logo-square.afdesign +0 -0
  88. data/logo.afdesign +0 -0
  89. data/logo.svg +107 -0
  90. data/server.rb +21 -0
  91. data/tasks/benchmark.rake +103 -0
  92. metadata +386 -0
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require_relative '../server'
24
+
25
+ require 'async/container/controller'
26
+ require 'async/io/trap'
27
+
28
+ require 'async/io/shared_endpoint'
29
+
30
+ module Falcon
31
+ module Controller
32
+ class Serve < Async::Container::Controller
33
+ def initialize(command, **options)
34
+ @command = command
35
+
36
+ @endpoint = nil
37
+ @bound_endpoint = nil
38
+ @debug_trap = Async::IO::Trap.new(:USR1)
39
+
40
+ super(**options)
41
+ end
42
+
43
+ def create_container
44
+ @command.container_class.new
45
+ end
46
+
47
+ def endpoint
48
+ @command.endpoint
49
+ end
50
+
51
+ def load_app
52
+ @command.load_app
53
+ end
54
+
55
+ def start
56
+ @endpoint ||= self.endpoint
57
+
58
+ @bound_endpoint = Async::Reactor.run do
59
+ Async::IO::SharedEndpoint.bound(@endpoint)
60
+ end.wait
61
+
62
+ @debug_trap.ignore!
63
+
64
+ super
65
+ end
66
+
67
+ def name
68
+ "Falcon Server"
69
+ end
70
+
71
+ def setup(container)
72
+ container.run(name: self.name, restart: true, **@command.container_options) do |instance|
73
+ Async do |task|
74
+ # Load one app instance per container:
75
+ app = self.load_app
76
+
77
+ task.async do
78
+ if @debug_trap.install!
79
+ Async.logger.info(instance) do
80
+ "- Per-process status: kill -USR1 #{Process.pid}"
81
+ end
82
+ end
83
+
84
+ @debug_trap.trap do
85
+ Async.logger.info(self) do |buffer|
86
+ task.reactor.print_hierarchy(buffer)
87
+ end
88
+ end
89
+ end
90
+
91
+ server = Falcon::Server.new(app, @bound_endpoint, @endpoint.protocol, @endpoint.scheme)
92
+
93
+ server.run
94
+
95
+ instance.ready!
96
+
97
+ task.children.each(&:wait)
98
+ end
99
+ end
100
+ end
101
+
102
+ def stop(*)
103
+ @bound_endpoint&.close
104
+
105
+ @debug_trap.default!
106
+
107
+ super
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/container/controller'
24
+
25
+ module Falcon
26
+ module Controller
27
+ class Virtual < Async::Container::Controller
28
+ def initialize(command, **options)
29
+ @command = command
30
+
31
+ super(**options)
32
+
33
+ trap(SIGHUP, &self.method(:reload))
34
+ end
35
+
36
+ def assume_privileges(path)
37
+ stat = File.stat(path)
38
+
39
+ Process::GID.change_privilege(stat.gid)
40
+ Process::UID.change_privilege(stat.uid)
41
+
42
+ home = Etc.getpwuid(stat.uid).dir
43
+
44
+ return {
45
+ 'HOME' => home,
46
+ }
47
+ end
48
+
49
+ def spawn(path, container, **options)
50
+ container.spawn(name: "Falcon Application", restart: true, key: path) do |instance|
51
+ env = assume_privileges(path)
52
+
53
+ instance.exec(env,
54
+ "bundle", "exec", "--keep-file-descriptors",
55
+ path, ready: false, **options)
56
+ end
57
+ end
58
+
59
+ def falcon_path
60
+ File.expand_path("../../../bin/falcon", __dir__)
61
+ end
62
+
63
+ def setup(container)
64
+ if proxy = container[:proxy]
65
+ proxy.kill(:HUP)
66
+ end
67
+
68
+ if redirect = container[:redirect]
69
+ redirect.kill(:HUP)
70
+ end
71
+
72
+ container.reload do
73
+ @command.resolved_paths do |path|
74
+ path = File.expand_path(path)
75
+ root = File.dirname(path)
76
+
77
+ spawn(path, container, chdir: root)
78
+ end
79
+
80
+ container.spawn(name: "Falcon Redirector", restart: true, key: :redirect) do |instance|
81
+ instance.exec(falcon_path, "redirect",
82
+ "--bind", @command.bind_insecure,
83
+ "--timeout", @command.timeout.to_s,
84
+ "--redirect", @command.bind_secure,
85
+ *@command.paths, ready: false
86
+ )
87
+ end
88
+
89
+ container.spawn(name: "Falcon Proxy", restart: true, key: :proxy) do |instance|
90
+ instance.exec(falcon_path, "proxy",
91
+ "--bind", @command.bind_secure,
92
+ "--timeout", @command.timeout.to_s,
93
+ *@command.paths, ready: false
94
+ )
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/http/endpoint'
24
+ require 'localhost/authority'
25
+
26
+ module Falcon
27
+ class Endpoint < Async::HTTP::Endpoint
28
+ def ssl_context
29
+ @options[:ssl_context] || build_ssl_context
30
+ end
31
+
32
+ def build_ssl_context(hostname = self.hostname)
33
+ authority = Localhost::Authority.fetch(hostname)
34
+
35
+ authority.server_context.tap do |context|
36
+ context.alpn_select_cb = lambda do |protocols|
37
+ if protocols.include? "h2"
38
+ return "h2"
39
+ elsif protocols.include? "http/1.1"
40
+ return "http/1.1"
41
+ elsif protocols.include? "http/1.0"
42
+ return "http/1.0"
43
+ else
44
+ return nil
45
+ end
46
+ end
47
+
48
+ context.session_id_context = "falcon"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'openssl/x509'
24
+
25
+ module OpenSSL::X509
26
+ CERTIFICATE_PATTERN = /-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----/m
27
+
28
+ def self.load_certificates(path)
29
+ File.read(path).scan(CERTIFICATE_PATTERN).collect do |text|
30
+ Certificate.new(text)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/http/client'
24
+ require 'protocol/http/headers'
25
+ require 'protocol/http/middleware'
26
+
27
+ module Falcon
28
+ module Middleware
29
+ module BadRequest
30
+ def self.call(request)
31
+ return Protocol::HTTP::Response[400, {}, []]
32
+ end
33
+
34
+ def self.close
35
+ end
36
+ end
37
+
38
+ class Proxy < Protocol::HTTP::Middleware
39
+ FORWARDED = 'forwarded'.freeze
40
+ X_FORWARDED_FOR = 'x-forwarded-for'.freeze
41
+ X_FORWARDED_PROTO = 'x-forwarded-proto'.freeze
42
+
43
+ VIA = 'via'.freeze
44
+ CONNECTION = 'connection'.freeze
45
+
46
+ HOP_HEADERS = [
47
+ 'connection',
48
+ 'keep-alive',
49
+ 'public',
50
+ 'proxy-authenticate',
51
+ 'transfer-encoding',
52
+ 'upgrade',
53
+ ]
54
+
55
+ def initialize(app, hosts)
56
+ super(app)
57
+
58
+ @server_context = nil
59
+
60
+ @hosts = hosts
61
+ @clients = {}
62
+
63
+ @count = 0
64
+ end
65
+
66
+ attr :count
67
+
68
+ def close
69
+ @clients.each_value(&:close)
70
+
71
+ super
72
+ end
73
+
74
+ def connect(endpoint)
75
+ @clients[endpoint] ||= Async::HTTP::Client.new(endpoint)
76
+ end
77
+
78
+ def lookup(request)
79
+ # Trailing dot and port is ignored/normalized.
80
+ if authority = request.authority&.sub(/(\.)?(:\d+)?$/, '')
81
+ return @hosts[authority]
82
+ end
83
+ end
84
+
85
+ def prepare_headers(headers)
86
+ if connection = headers[CONNECTION]
87
+ headers.extract(connection)
88
+ end
89
+
90
+ headers.extract(HOP_HEADERS)
91
+ end
92
+
93
+ def prepare_request(request, host)
94
+ forwarded = []
95
+
96
+ Async.logger.debug(self) do |buffer|
97
+ buffer.puts "Request authority: #{request.authority}"
98
+ buffer.puts "Host authority: #{host.authority}"
99
+ buffer.puts "Request: #{request.method} #{request.path} #{request.version}"
100
+ buffer.puts "Request headers: #{request.headers.inspect}"
101
+ end
102
+
103
+ # The authority of the request must match the authority of the endpoint we are proxying to, otherwise SNI and other things won't work correctly.
104
+ request.authority = host.authority
105
+
106
+ if address = request.remote_address
107
+ request.headers.add(X_FORWARDED_FOR, address.ip_address)
108
+ forwarded << "for=#{address.ip_address}"
109
+ end
110
+
111
+ if scheme = request.scheme
112
+ request.headers.add(X_FORWARDED_PROTO, scheme)
113
+ forwarded << "proto=#{scheme}"
114
+ end
115
+
116
+ unless forwarded.empty?
117
+ request.headers.add(FORWARDED, forwarded.join(';'))
118
+ end
119
+
120
+ request.headers.add(VIA, "#{request.version} #{self.class}")
121
+
122
+ self.prepare_headers(request.headers)
123
+
124
+ return request
125
+ end
126
+
127
+ def call(request)
128
+ if host = lookup(request)
129
+ @count += 1
130
+
131
+ request = self.prepare_request(request, host)
132
+
133
+ client = connect(host.endpoint)
134
+
135
+ client.call(request)
136
+ else
137
+ super
138
+ end
139
+ rescue
140
+ Async.logger.error(self) {$!}
141
+ return Protocol::HTTP::Response[502, {'content-type' => 'text/plain'}, ["#{$!.inspect}: #{$!.backtrace.join("\n")}"]]
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/http/client'
24
+
25
+ module Falcon
26
+ module Middleware
27
+ module NotFound
28
+ def self.call(request)
29
+ return Protocol::HTTP::Response[404, {}, []]
30
+ end
31
+
32
+ def self.close
33
+ end
34
+ end
35
+
36
+ class Redirect < Protocol::HTTP::Middleware
37
+ def initialize(app, hosts, endpoint)
38
+ super(app)
39
+
40
+ @hosts = hosts
41
+ @endpoint = endpoint
42
+ end
43
+
44
+ def lookup(request)
45
+ # Trailing dot and port is ignored/normalized.
46
+ if authority = request.authority&.sub(/(\.)?(:\d+)?$/, '')
47
+ return @hosts[authority]
48
+ end
49
+ end
50
+
51
+ def call(request)
52
+ if host = lookup(request)
53
+ if @endpoint.default_port?
54
+ location = "#{@endpoint.scheme}://#{host.authority}#{request.path}"
55
+ else
56
+ location = "#{@endpoint.scheme}://#{host.authority}:#{@endpoint.port}#{request.path}"
57
+ end
58
+
59
+ return Protocol::HTTP::Response[301, [['location', location]], []]
60
+ else
61
+ super
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end