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,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A sample Gemfile
4
+ source "https://rubygems.org"
5
+
6
+ gem "sinatra"
7
+ gem "falcon"
8
+
9
+ gem "async-redis", ">= 0.3.1"
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sinatra/base'
4
+ require 'async/redis'
5
+ require 'async/clock'
6
+
7
+ CLIENT = Async::Redis::Client.new(Async::Redis.local_endpoint)
8
+
9
+ class MyApp < Sinatra::Base
10
+ get "/" do
11
+ puts "Starting BLPOP SLEEP..."
12
+ duration = Async::Clock.measure do
13
+ CLIENT.call "BLPOP", "SLEEP", 1
14
+ end
15
+ puts "Finished BLPOP SLEEP after #{duration.round(2)}s"
16
+
17
+ "ok"
18
+ end
19
+ end
20
+
21
+ use MyApp # Then, it will get to Sinatra.
22
+ run lambda {|env| [404, {}, []]} # Bottom of the stack, give 404.
23
+
24
+ # Start server like this:
25
+ # falcon --verbose serve --threaded --count 1 --bind http://localhost:9292
26
+
27
+ # Test server, e.g.:
28
+ # time ab -n 64 -c 64 http://localhost:9292/
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'falcon'
4
+ gem 'sequel'
@@ -0,0 +1,8 @@
1
+ # config.ru
2
+ require 'sequel'
3
+
4
+ # Turn on single threaded mode
5
+ Sequel.single_threaded = true
6
+ DB = Sequel.sqlite('data.sqlite3')
7
+
8
+ run(proc{|env| [200, {}, [DB[:user].get(:name)]]})
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'async'
4
+ require 'async/http/endpoint'
5
+ require 'async/websocket/adapters/rack'
6
+
7
+ require 'falcon'
8
+
9
+ module WebSocketApp
10
+ def self.call(env)
11
+ Async::WebSocket::Adapters::Rack.open(env, protocols: %w[ws]) do |connection|
12
+ while (message = connection.read)
13
+ pp message
14
+ end
15
+ end or [200, [], ["Websocket only."]]
16
+ end
17
+ end
18
+
19
+ Async do
20
+ websocket_endpoint = Async::HTTP::Endpoint.parse('http://127.0.0.1:3000')
21
+
22
+ app = Falcon::Server.middleware(WebSocketApp)
23
+
24
+ server = Falcon::Server.new(app, websocket_endpoint)
25
+
26
+ server.run.each(&:wait)
27
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A sample Gemfile
4
+ source "https://rubygems.org"
5
+
6
+ gem "sinatra"
7
+ gem "falcon"
@@ -0,0 +1,53 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ async (1.9.1)
5
+ nio4r (~> 2.3)
6
+ timers (~> 4.1)
7
+ async-container (0.5.0)
8
+ async (~> 1.0)
9
+ async-io (~> 1.4)
10
+ async-http (0.24.3)
11
+ async (~> 1.6)
12
+ async-io (~> 1.12)
13
+ http-2 (~> 0.9.0)
14
+ async-io (1.12.1)
15
+ async (~> 1.3)
16
+ falcon (0.15.2)
17
+ async-container (~> 0.5.0)
18
+ async-http (~> 0.24.0)
19
+ async-io (~> 1.9)
20
+ rack (>= 1.0)
21
+ samovar (~> 1.3)
22
+ hitimes (1.2.6)
23
+ http-2 (0.9.1)
24
+ mapping (1.1.1)
25
+ mustermann (1.0.2)
26
+ nio4r (2.3.1)
27
+ rack (2.0.8)
28
+ rack-protection (2.0.3)
29
+ rack
30
+ rainbow (2.2.2)
31
+ rake
32
+ rake (13.0.1)
33
+ samovar (1.8.0)
34
+ mapping (~> 1.0)
35
+ rainbow (~> 2.0)
36
+ sinatra (2.0.3)
37
+ mustermann (~> 1.0)
38
+ rack (~> 2.0)
39
+ rack-protection (= 2.0.3)
40
+ tilt (~> 2.0)
41
+ tilt (2.0.8)
42
+ timers (4.1.2)
43
+ hitimes
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ falcon
50
+ sinatra
51
+
52
+ BUNDLED WITH
53
+ 1.16.1
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env falcon --verbose serve -c
2
+ # frozen_string_literal: true
3
+
4
+ # Save this as `config.ru`, make it executable and then run it (or run falcon serve by hand)
5
+
6
+ # Middleware that responds to incoming requests:
7
+ require 'sinatra/base'
8
+ class MyApp < Sinatra::Base
9
+ get "/" do
10
+ response = Faraday.get 'http://sushi.com/nigiri/sake.json'
11
+ end
12
+ end
13
+
14
+ # Build the middleware stack:
15
+ use MyApp # Then, it will get to Sinatra.
16
+ run lambda {|env| [404, {}, []]} # Bottom of the stack, give 404.
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env falcon --verbose serve -c
2
+ # frozen_string_literal: true
3
+
4
+ require 'async'
5
+
6
+ class RequestLogger
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ logger = Async.logger.with(level: :debug, name: "middleware")
13
+
14
+ Async(logger: logger) do
15
+ @app.call(env)
16
+ end.wait
17
+ end
18
+ end
19
+
20
+ use RequestLogger
21
+
22
+ run lambda {|env|
23
+ start_time = Async::Clock.now
24
+
25
+ server_timing = ->{
26
+ "app;dur=#{Async::Clock.now - start_time}"
27
+ }
28
+
29
+ [
30
+ 200,
31
+ [["trailers", "server-timing"], ["server-timing", server_timing]],
32
+ ["Hello World"]
33
+ ]
34
+ }
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env falcon-host
2
+ # frozen_string_literal: true
3
+
4
+ load :rack, :self_signed_tls, :supervisor
5
+
6
+ rack 'trailers.localhost', :self_signed_tls
7
+
8
+ supervisor
@@ -0,0 +1,45 @@
1
+
2
+ require_relative 'lib/falcon/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "falcon"
6
+ spec.version = Falcon::VERSION
7
+ spec.authors = ["Samuel Williams"]
8
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
+
10
+ spec.summary = "A fast, asynchronous, rack-compatible web server."
11
+ spec.homepage = "https://github.com/socketry/falcon"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+
17
+ spec.required_ruby_version = '~> 2.4'
18
+
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "async", "~> 1.13"
23
+ spec.add_dependency "async-io", "~> 1.22"
24
+ spec.add_dependency "async-http", "~> 0.52.0"
25
+ spec.add_dependency "async-http-cache", "~> 0.2.0"
26
+ spec.add_dependency "async-container", "~> 0.16.0"
27
+
28
+ spec.add_dependency "rack", ">= 1.0"
29
+
30
+ spec.add_dependency 'samovar', "~> 2.1"
31
+ spec.add_dependency 'localhost', "~> 1.1"
32
+ spec.add_dependency 'build-environment', '~> 1.13'
33
+
34
+ spec.add_dependency 'process-metrics', '~> 0.2.0'
35
+
36
+ spec.add_development_dependency "async-rspec", "~> 1.7"
37
+ spec.add_development_dependency "async-websocket", "~> 0.14.0"
38
+ spec.add_development_dependency "async-process", "~> 1.1"
39
+
40
+ spec.add_development_dependency "bake"
41
+ spec.add_development_dependency "covered"
42
+ spec.add_development_dependency "bundler"
43
+ spec.add_development_dependency "rspec", "~> 3.6"
44
+ spec.add_development_dependency "bake-bundler"
45
+ end
@@ -0,0 +1,4 @@
1
+
2
+ eval_gemfile("../Gemfile")
3
+
4
+ gem 'rack', '~> 1.0'
@@ -0,0 +1,4 @@
1
+
2
+ eval_gemfile("../Gemfile")
3
+
4
+ gem 'rack', github: 'rack/rack'
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2017, 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 "falcon/server"
@@ -0,0 +1,49 @@
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 'protocol/http/middleware'
24
+
25
+ module Falcon
26
+ module Adapters
27
+ class EarlyHints
28
+ PRELOAD = /<(?<path>.*?)>;.*?rel=preload/
29
+
30
+ def initialize(request)
31
+ @request = request
32
+ end
33
+
34
+ def push(path, preload: true, **options)
35
+ @request.push(path)
36
+ end
37
+
38
+ def call(headers)
39
+ headers.each do |key, value|
40
+ if key.casecmp("link").zero? and match = PRELOAD.match(value)
41
+ @request.push(match[:path])
42
+ else
43
+ Async.logger.warn(@request) {"Unsure how to handle early hints header: #{key}"}
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,131 @@
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/io/buffer'
24
+ require 'protocol/http/body/rewindable'
25
+
26
+ module Falcon
27
+ module Adapters
28
+ # The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.
29
+ class Input
30
+ def initialize(body)
31
+ @body = body
32
+
33
+ # Will hold remaining data in `#read`.
34
+ @buffer = nil
35
+ @finished = @body.nil?
36
+ end
37
+
38
+ attr :body
39
+
40
+ # each must be called without arguments and only yield Strings.
41
+ def each(&block)
42
+ return to_enum unless block_given?
43
+
44
+ while chunk = gets
45
+ yield chunk
46
+ end
47
+ end
48
+
49
+ # rewind must be called without arguments. It rewinds the input stream back to the beginning. It must not raise Errno::ESPIPE: that is, it may not be a pipe or a socket. Therefore, handler developers must buffer the input data into some rewindable object if the underlying input stream is not rewindable.
50
+ # @return [Boolean] whether the body could be rewound.
51
+ def rewind
52
+ if @body and @body.respond_to? :rewind
53
+ # If the body is not rewindable, this will fail.
54
+ @body.rewind
55
+ @buffer = nil
56
+ @finished = false
57
+
58
+ return true
59
+ end
60
+
61
+ return false
62
+ end
63
+
64
+ # read behaves like IO#read. Its signature is read([length, [buffer]]). If given, length must be a non-negative Integer (>= 0) or nil, and buffer must be a String and may not be nil. If length is given and not nil, then this method reads at most length bytes from the input stream. If length is not given or nil, then this method reads all data until EOF. When EOF is reached, this method returns nil if length is given and not nil, or “” if length is not given or is nil. If buffer is given, then the read data will be placed into buffer instead of a newly created String object.
65
+ # @param length [Integer] the amount of data to read
66
+ # @param buffer [String] the buffer which will receive the data
67
+ # @return a buffer containing the data
68
+ def read(length = nil, buffer = nil)
69
+ buffer ||= Async::IO::Buffer.new
70
+ buffer.clear
71
+
72
+ until buffer.bytesize == length
73
+ @buffer = read_next if @buffer.nil?
74
+ break if @buffer.nil?
75
+
76
+ remaining_length = length - buffer.bytesize if length
77
+
78
+ if remaining_length && remaining_length < @buffer.bytesize
79
+ # We know that we are not going to reuse the original buffer.
80
+ # But byteslice will generate a hidden copy. So let's freeze it first:
81
+ @buffer.freeze
82
+
83
+ buffer << @buffer.byteslice(0, remaining_length)
84
+ @buffer = @buffer.byteslice(remaining_length, @buffer.bytesize)
85
+ else
86
+ buffer << @buffer
87
+ @buffer = nil
88
+ end
89
+ end
90
+
91
+ return nil if buffer.empty? && length && length > 0
92
+
93
+ return buffer
94
+ end
95
+
96
+ def eof?
97
+ @finished and @buffer.nil?
98
+ end
99
+
100
+ # gets must be called without arguments and return a string, or nil on EOF.
101
+ # @return [String, nil] The next chunk from the body.
102
+ def gets
103
+ if @buffer.nil?
104
+ return read_next
105
+ else
106
+ buffer = @buffer
107
+ @buffer = nil
108
+ return buffer
109
+ end
110
+ end
111
+
112
+ # close must never be called on the input stream. huh?
113
+ def close
114
+ @body&.close
115
+ end
116
+
117
+ private
118
+
119
+ def read_next
120
+ return nil if @finished
121
+
122
+ if chunk = @body.read
123
+ return chunk
124
+ else
125
+ @finished = true
126
+ return nil
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end