jun-puma 1.0.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/History.md +2897 -0
  3. data/LICENSE +29 -0
  4. data/README.md +475 -0
  5. data/bin/puma +10 -0
  6. data/bin/puma-wild +25 -0
  7. data/bin/pumactl +12 -0
  8. data/docs/architecture.md +74 -0
  9. data/docs/compile_options.md +55 -0
  10. data/docs/deployment.md +102 -0
  11. data/docs/fork_worker.md +35 -0
  12. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  13. data/docs/images/puma-connection-flow.png +0 -0
  14. data/docs/images/puma-general-arch.png +0 -0
  15. data/docs/jungle/README.md +9 -0
  16. data/docs/jungle/rc.d/README.md +74 -0
  17. data/docs/jungle/rc.d/puma +61 -0
  18. data/docs/jungle/rc.d/puma.conf +10 -0
  19. data/docs/kubernetes.md +78 -0
  20. data/docs/nginx.md +80 -0
  21. data/docs/plugins.md +38 -0
  22. data/docs/rails_dev_mode.md +28 -0
  23. data/docs/restart.md +65 -0
  24. data/docs/signals.md +98 -0
  25. data/docs/stats.md +142 -0
  26. data/docs/systemd.md +253 -0
  27. data/docs/testing_benchmarks_local_files.md +150 -0
  28. data/docs/testing_test_rackup_ci_files.md +36 -0
  29. data/ext/puma_http11/PumaHttp11Service.java +17 -0
  30. data/ext/puma_http11/ext_help.h +15 -0
  31. data/ext/puma_http11/extconf.rb +80 -0
  32. data/ext/puma_http11/http11_parser.c +1057 -0
  33. data/ext/puma_http11/http11_parser.h +65 -0
  34. data/ext/puma_http11/http11_parser.java.rl +145 -0
  35. data/ext/puma_http11/http11_parser.rl +149 -0
  36. data/ext/puma_http11/http11_parser_common.rl +54 -0
  37. data/ext/puma_http11/mini_ssl.c +842 -0
  38. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  39. data/ext/puma_http11/org/jruby/puma/Http11.java +228 -0
  40. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +455 -0
  41. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +509 -0
  42. data/ext/puma_http11/puma_http11.c +495 -0
  43. data/lib/puma/app/status.rb +96 -0
  44. data/lib/puma/binder.rb +502 -0
  45. data/lib/puma/cli.rb +247 -0
  46. data/lib/puma/client.rb +682 -0
  47. data/lib/puma/cluster/worker.rb +180 -0
  48. data/lib/puma/cluster/worker_handle.rb +96 -0
  49. data/lib/puma/cluster.rb +616 -0
  50. data/lib/puma/commonlogger.rb +115 -0
  51. data/lib/puma/configuration.rb +390 -0
  52. data/lib/puma/const.rb +307 -0
  53. data/lib/puma/control_cli.rb +316 -0
  54. data/lib/puma/detect.rb +45 -0
  55. data/lib/puma/dsl.rb +1425 -0
  56. data/lib/puma/error_logger.rb +113 -0
  57. data/lib/puma/events.rb +57 -0
  58. data/lib/puma/io_buffer.rb +46 -0
  59. data/lib/puma/jruby_restart.rb +11 -0
  60. data/lib/puma/json_serialization.rb +96 -0
  61. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  62. data/lib/puma/launcher.rb +488 -0
  63. data/lib/puma/log_writer.rb +147 -0
  64. data/lib/puma/minissl/context_builder.rb +96 -0
  65. data/lib/puma/minissl.rb +459 -0
  66. data/lib/puma/null_io.rb +84 -0
  67. data/lib/puma/plugin/systemd.rb +90 -0
  68. data/lib/puma/plugin/tmp_restart.rb +36 -0
  69. data/lib/puma/plugin.rb +111 -0
  70. data/lib/puma/puma_http11.jar +0 -0
  71. data/lib/puma/rack/builder.rb +297 -0
  72. data/lib/puma/rack/urlmap.rb +93 -0
  73. data/lib/puma/rack_default.rb +24 -0
  74. data/lib/puma/reactor.rb +125 -0
  75. data/lib/puma/request.rb +688 -0
  76. data/lib/puma/runner.rb +213 -0
  77. data/lib/puma/sd_notify.rb +149 -0
  78. data/lib/puma/server.rb +680 -0
  79. data/lib/puma/single.rb +69 -0
  80. data/lib/puma/state_file.rb +68 -0
  81. data/lib/puma/thread_pool.rb +434 -0
  82. data/lib/puma/util.rb +141 -0
  83. data/lib/puma.rb +78 -0
  84. data/lib/rack/handler/puma.rb +144 -0
  85. data/tools/Dockerfile +16 -0
  86. data/tools/trickletest.rb +44 -0
  87. metadata +153 -0
data/lib/puma/const.rb ADDED
@@ -0,0 +1,307 @@
1
+ #encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Puma
5
+ class UnsupportedOption < RuntimeError
6
+ end
7
+
8
+ # Every standard HTTP code mapped to the appropriate message. These are
9
+ # used so frequently that they are placed directly in Puma for easy
10
+ # access rather than Puma::Const itself.
11
+
12
+ # Every standard HTTP code mapped to the appropriate message.
13
+ # Generated with:
14
+ # curl -s https://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv | \
15
+ # ruby -ne 'm = /^(\d{3}),(?!Unassigned|\(Unused\))([^,]+)/.match($_) and \
16
+ # puts "#{m[1]} => \x27#{m[2].strip}\x27,"'
17
+ HTTP_STATUS_CODES = {
18
+ 100 => 'Continue',
19
+ 101 => 'Switching Protocols',
20
+ 102 => 'Processing',
21
+ 103 => 'Early Hints',
22
+ 200 => 'OK',
23
+ 201 => 'Created',
24
+ 202 => 'Accepted',
25
+ 203 => 'Non-Authoritative Information',
26
+ 204 => 'No Content',
27
+ 205 => 'Reset Content',
28
+ 206 => 'Partial Content',
29
+ 207 => 'Multi-Status',
30
+ 208 => 'Already Reported',
31
+ 226 => 'IM Used',
32
+ 300 => 'Multiple Choices',
33
+ 301 => 'Moved Permanently',
34
+ 302 => 'Found',
35
+ 303 => 'See Other',
36
+ 304 => 'Not Modified',
37
+ 305 => 'Use Proxy',
38
+ 307 => 'Temporary Redirect',
39
+ 308 => 'Permanent Redirect',
40
+ 400 => 'Bad Request',
41
+ 401 => 'Unauthorized',
42
+ 402 => 'Payment Required',
43
+ 403 => 'Forbidden',
44
+ 404 => 'Not Found',
45
+ 405 => 'Method Not Allowed',
46
+ 406 => 'Not Acceptable',
47
+ 407 => 'Proxy Authentication Required',
48
+ 408 => 'Request Timeout',
49
+ 409 => 'Conflict',
50
+ 410 => 'Gone',
51
+ 411 => 'Length Required',
52
+ 412 => 'Precondition Failed',
53
+ 413 => 'Content Too Large',
54
+ 414 => 'URI Too Long',
55
+ 415 => 'Unsupported Media Type',
56
+ 416 => 'Range Not Satisfiable',
57
+ 417 => 'Expectation Failed',
58
+ 421 => 'Misdirected Request',
59
+ 422 => 'Unprocessable Content',
60
+ 423 => 'Locked',
61
+ 424 => 'Failed Dependency',
62
+ 425 => 'Too Early',
63
+ 426 => 'Upgrade Required',
64
+ 428 => 'Precondition Required',
65
+ 429 => 'Too Many Requests',
66
+ 431 => 'Request Header Fields Too Large',
67
+ 451 => 'Unavailable For Legal Reasons',
68
+ 500 => 'Internal Server Error',
69
+ 501 => 'Not Implemented',
70
+ 502 => 'Bad Gateway',
71
+ 503 => 'Service Unavailable',
72
+ 504 => 'Gateway Timeout',
73
+ 505 => 'HTTP Version Not Supported',
74
+ 506 => 'Variant Also Negotiates',
75
+ 507 => 'Insufficient Storage',
76
+ 508 => 'Loop Detected',
77
+ 510 => 'Not Extended (OBSOLETED)',
78
+ 511 => 'Network Authentication Required'
79
+ }.freeze
80
+
81
+ # For some HTTP status codes the client only expects headers.
82
+ #
83
+
84
+ STATUS_WITH_NO_ENTITY_BODY = {
85
+ 204 => true,
86
+ 205 => true,
87
+ 304 => true
88
+ }.freeze
89
+
90
+ # Frequently used constants when constructing requests or responses. Many times
91
+ # the constant just refers to a string with the same contents. Using these constants
92
+ # gave about a 3% to 10% performance improvement over using the strings directly.
93
+ #
94
+ # The constants are frozen because Hash#[]= when called with a String key dups
95
+ # the String UNLESS the String is frozen. This saves us therefore 2 object
96
+ # allocations when creating the env hash later.
97
+ #
98
+ # While Puma does try to emulate the CGI/1.2 protocol, it does not use the REMOTE_IDENT,
99
+ # REMOTE_USER, or REMOTE_HOST parameters since those are either a security problem or
100
+ # too taxing on performance.
101
+ module Const
102
+
103
+ PUMA_VERSION = VERSION = "1.0.0"
104
+ CODE_NAME = "The Eagle of Durango - Jun"
105
+
106
+ PUMA_SERVER_STRING = ["puma", PUMA_VERSION, CODE_NAME].join(" ").freeze
107
+
108
+ FAST_TRACK_KA_TIMEOUT = 0.2
109
+
110
+ # How long to wait when getting some write blocking on the socket when
111
+ # sending data back
112
+ WRITE_TIMEOUT = 10
113
+
114
+ # The original URI requested by the client.
115
+ REQUEST_URI= "REQUEST_URI"
116
+ REQUEST_PATH = "REQUEST_PATH"
117
+ QUERY_STRING = "QUERY_STRING"
118
+ CONTENT_LENGTH = "CONTENT_LENGTH"
119
+
120
+ PATH_INFO = "PATH_INFO"
121
+
122
+ PUMA_TMP_BASE = "puma"
123
+
124
+ ERROR_RESPONSE = {
125
+ # Indicate that we couldn't parse the request
126
+ 400 => "HTTP/1.1 400 Bad Request\r\n\r\n",
127
+ # The standard empty 404 response for bad requests. Use Error4040Handler for custom stuff.
128
+ 404 => "HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n",
129
+ # The standard empty 408 response for requests that timed out.
130
+ 408 => "HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n",
131
+ # Indicate that there was an internal error, obviously.
132
+ 500 => "HTTP/1.1 500 Internal Server Error\r\n\r\n",
133
+ # Incorrect or invalid header value
134
+ 501 => "HTTP/1.1 501 Not Implemented\r\n\r\n",
135
+ # A common header for indicating the server is too busy. Not used yet.
136
+ 503 => "HTTP/1.1 503 Service Unavailable\r\n\r\n"
137
+ }.freeze
138
+
139
+ # The basic max request size we'll try to read.
140
+ CHUNK_SIZE = 16 * 1024
141
+
142
+ # This is the maximum header that is allowed before a client is booted. The parser detects
143
+ # this, but we'd also like to do this as well.
144
+ MAX_HEADER = 1024 * (80 + 32)
145
+
146
+ # Maximum request body size before it is moved out of memory and into a tempfile for reading.
147
+ MAX_BODY = MAX_HEADER
148
+
149
+ REQUEST_METHOD = "REQUEST_METHOD"
150
+ HEAD = "HEAD"
151
+
152
+ # based on https://www.rfc-editor.org/rfc/rfc9110.html#name-overview,
153
+ # with CONNECT removed, and PATCH added
154
+ SUPPORTED_HTTP_METHODS = %w[HEAD GET POST PUT DELETE OPTIONS TRACE PATCH].freeze
155
+
156
+ # list from https://www.iana.org/assignments/http-methods/http-methods.xhtml
157
+ # as of 04-May-23
158
+ IANA_HTTP_METHODS = %w[
159
+ ACL
160
+ BASELINE-CONTROL
161
+ BIND
162
+ CHECKIN
163
+ CHECKOUT
164
+ CONNECT
165
+ COPY
166
+ DELETE
167
+ GET
168
+ HEAD
169
+ LABEL
170
+ LINK
171
+ LOCK
172
+ MERGE
173
+ MKACTIVITY
174
+ MKCALENDAR
175
+ MKCOL
176
+ MKREDIRECTREF
177
+ MKWORKSPACE
178
+ MOVE
179
+ OPTIONS
180
+ ORDERPATCH
181
+ PATCH
182
+ POST
183
+ PRI
184
+ PROPFIND
185
+ PROPPATCH
186
+ PUT
187
+ REBIND
188
+ REPORT
189
+ SEARCH
190
+ TRACE
191
+ UNBIND
192
+ UNCHECKOUT
193
+ UNLINK
194
+ UNLOCK
195
+ UPDATE
196
+ UPDATEREDIRECTREF
197
+ VERSION-CONTROL
198
+ ].freeze
199
+
200
+ # ETag is based on the apache standard of hex mtime-size-inode (inode is 0 on win32)
201
+ LINE_END = "\r\n"
202
+ REMOTE_ADDR = "REMOTE_ADDR"
203
+ HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR"
204
+ HTTP_X_FORWARDED_SSL = "HTTP_X_FORWARDED_SSL"
205
+ HTTP_X_FORWARDED_SCHEME = "HTTP_X_FORWARDED_SCHEME"
206
+ HTTP_X_FORWARDED_PROTO = "HTTP_X_FORWARDED_PROTO"
207
+
208
+ SERVER_NAME = "SERVER_NAME"
209
+ SERVER_PORT = "SERVER_PORT"
210
+ HTTP_HOST = "HTTP_HOST"
211
+ PORT_80 = "80"
212
+ PORT_443 = "443"
213
+ LOCALHOST = "localhost"
214
+ LOCALHOST_IPV4 = "127.0.0.1"
215
+ LOCALHOST_IPV6 = "::1"
216
+ UNSPECIFIED_IPV4 = "0.0.0.0"
217
+ UNSPECIFIED_IPV6 = "::"
218
+
219
+ SERVER_PROTOCOL = "SERVER_PROTOCOL"
220
+ HTTP_11 = "HTTP/1.1"
221
+
222
+ SERVER_SOFTWARE = "SERVER_SOFTWARE"
223
+ GATEWAY_INTERFACE = "GATEWAY_INTERFACE"
224
+ CGI_VER = "CGI/1.2"
225
+
226
+ STOP_COMMAND = "?"
227
+ HALT_COMMAND = "!"
228
+ RESTART_COMMAND = "R"
229
+
230
+ RACK_INPUT = "rack.input"
231
+ RACK_URL_SCHEME = "rack.url_scheme"
232
+ RACK_AFTER_REPLY = "rack.after_reply"
233
+ PUMA_SOCKET = "puma.socket"
234
+ PUMA_CONFIG = "puma.config"
235
+ PUMA_PEERCERT = "puma.peercert"
236
+
237
+ HTTP = "http"
238
+ HTTPS = "https"
239
+
240
+ HTTPS_KEY = "HTTPS"
241
+
242
+ HTTP_VERSION = "HTTP_VERSION"
243
+ HTTP_CONNECTION = "HTTP_CONNECTION"
244
+ HTTP_EXPECT = "HTTP_EXPECT"
245
+ CONTINUE = "100-continue"
246
+
247
+ HTTP_11_100 = "HTTP/1.1 100 Continue\r\n\r\n"
248
+ HTTP_11_200 = "HTTP/1.1 200 OK\r\n"
249
+ HTTP_10_200 = "HTTP/1.0 200 OK\r\n"
250
+
251
+ CLOSE = "close"
252
+ KEEP_ALIVE = "keep-alive"
253
+
254
+ CONTENT_LENGTH2 = "content-length"
255
+ CONTENT_LENGTH_S = "Content-Length: "
256
+ TRANSFER_ENCODING = "transfer-encoding"
257
+ TRANSFER_ENCODING2 = "HTTP_TRANSFER_ENCODING"
258
+
259
+ CONNECTION_CLOSE = "Connection: close\r\n"
260
+ CONNECTION_KEEP_ALIVE = "Connection: Keep-Alive\r\n"
261
+
262
+ TRANSFER_ENCODING_CHUNKED = "Transfer-Encoding: chunked\r\n"
263
+ CLOSE_CHUNKED = "0\r\n\r\n"
264
+
265
+ CHUNKED = "chunked"
266
+
267
+ COLON = ": "
268
+
269
+ NEWLINE = "\n"
270
+
271
+ HIJACK_P = "rack.hijack?"
272
+ HIJACK = "rack.hijack"
273
+ HIJACK_IO = "rack.hijack_io"
274
+
275
+ EARLY_HINTS = "rack.early_hints"
276
+
277
+ # Illegal character in the key or value of response header
278
+ DQUOTE = "\""
279
+ HTTP_HEADER_DELIMITER = Regexp.escape("(),/:;<=>?@[]{}\\").freeze
280
+ ILLEGAL_HEADER_KEY_REGEX = /[\x00-\x20#{DQUOTE}#{HTTP_HEADER_DELIMITER}]/.freeze
281
+ # header values can contain HTAB?
282
+ ILLEGAL_HEADER_VALUE_REGEX = /[\x00-\x08\x0A-\x1F]/.freeze
283
+
284
+ # The keys of headers that should not be convert to underscore
285
+ # normalized versions. These headers are ignored at the request reading layer,
286
+ # but if we normalize them after reading, it's just confusing for the application.
287
+ UNMASKABLE_HEADERS = {
288
+ "HTTP_TRANSFER,ENCODING" => true,
289
+ "HTTP_CONTENT,LENGTH" => true,
290
+ }
291
+
292
+ # Banned keys of response header
293
+ BANNED_HEADER_KEY = /\A(rack\.|status\z)/.freeze
294
+
295
+ PROXY_PROTOCOL_V1_REGEX = /^PROXY (?:TCP4|TCP6|UNKNOWN) ([^\r]+)\r\n/.freeze
296
+
297
+ module PipeRequest
298
+ WAKEUP = "!"
299
+ BOOT = "b"
300
+ FORK = "f"
301
+ EXTERNAL_TERM = "e"
302
+ TERM = "t"
303
+ PING = "p"
304
+ IDLE = "i"
305
+ end
306
+ end
307
+ end
@@ -0,0 +1,316 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+ require_relative 'const'
5
+ require_relative 'detect'
6
+ require 'uri'
7
+ require 'socket'
8
+
9
+ module Puma
10
+ class ControlCLI
11
+
12
+ # values must be string or nil
13
+ # value of `nil` means command cannot be processed via signal
14
+ # @version 5.0.3
15
+ CMD_PATH_SIG_MAP = {
16
+ 'gc' => nil,
17
+ 'gc-stats' => nil,
18
+ 'halt' => 'SIGQUIT',
19
+ 'info' => 'SIGINFO',
20
+ 'phased-restart' => 'SIGUSR1',
21
+ 'refork' => 'SIGURG',
22
+ 'reload-worker-directory' => nil,
23
+ 'reopen-log' => 'SIGHUP',
24
+ 'restart' => 'SIGUSR2',
25
+ 'start' => nil,
26
+ 'stats' => nil,
27
+ 'status' => '',
28
+ 'stop' => 'SIGTERM',
29
+ 'thread-backtraces' => nil,
30
+ 'worker-count-down' => 'SIGTTOU',
31
+ 'worker-count-up' => 'SIGTTIN'
32
+ }.freeze
33
+
34
+ # commands that cannot be used in a request
35
+ NO_REQ_COMMANDS = %w[info reopen-log worker-count-down worker-count-up].freeze
36
+
37
+ # @version 5.0.0
38
+ PRINTABLE_COMMANDS = %w[gc-stats stats thread-backtraces].freeze
39
+
40
+ def initialize(argv, stdout=STDOUT, stderr=STDERR, env: ENV)
41
+ @state = nil
42
+ @quiet = false
43
+ @pidfile = nil
44
+ @pid = nil
45
+ @control_url = nil
46
+ @control_auth_token = nil
47
+ @config_file = nil
48
+ @command = nil
49
+ @environment = env['APP_ENV'] || env['RACK_ENV'] || env['RAILS_ENV']
50
+
51
+ @argv = argv.dup
52
+ @stdout = stdout
53
+ @stderr = stderr
54
+ @cli_options = {}
55
+
56
+ opts = OptionParser.new do |o|
57
+ o.banner = "Usage: pumactl (-p PID | -P pidfile | -S status_file | -C url -T token | -F config.rb) (#{CMD_PATH_SIG_MAP.keys.join("|")})"
58
+
59
+ o.on "-S", "--state PATH", "Where the state file to use is" do |arg|
60
+ @state = arg
61
+ end
62
+
63
+ o.on "-Q", "--quiet", "Do not display messages" do |arg|
64
+ @quiet = true
65
+ end
66
+
67
+ o.on "-P", "--pidfile PATH", "Pid file" do |arg|
68
+ @pidfile = arg
69
+ end
70
+
71
+ o.on "-p", "--pid PID", "Pid" do |arg|
72
+ @pid = arg.to_i
73
+ end
74
+
75
+ o.on "-C", "--control-url URL", "The bind url to use for the control server" do |arg|
76
+ @control_url = arg
77
+ end
78
+
79
+ o.on "-T", "--control-token TOKEN", "The token to use as authentication for the control server" do |arg|
80
+ @control_auth_token = arg
81
+ end
82
+
83
+ o.on "-F", "--config-file PATH", "Puma config script" do |arg|
84
+ @config_file = arg
85
+ end
86
+
87
+ o.on "-e", "--environment ENVIRONMENT",
88
+ "The environment to run the Rack app on (default development)" do |arg|
89
+ @environment = arg
90
+ end
91
+
92
+ o.on_tail("-H", "--help", "Show this message") do
93
+ @stdout.puts o
94
+ exit
95
+ end
96
+
97
+ o.on_tail("-V", "--version", "Show version") do
98
+ @stdout.puts Const::PUMA_VERSION
99
+ exit
100
+ end
101
+ end
102
+
103
+ opts.order!(argv) { |a| opts.terminate a }
104
+ opts.parse!
105
+
106
+ @command = argv.shift
107
+
108
+ # check presence of command
109
+ unless @command
110
+ raise "Available commands: #{CMD_PATH_SIG_MAP.keys.join(", ")}"
111
+ end
112
+
113
+ unless CMD_PATH_SIG_MAP.key? @command
114
+ raise "Invalid command: #{@command}"
115
+ end
116
+
117
+ unless @config_file == '-'
118
+ environment = @environment || 'development'
119
+
120
+ if @config_file.nil?
121
+ @config_file = %W(config/puma/#{environment}.rb config/puma.rb).find do |f|
122
+ File.exist?(f)
123
+ end
124
+ end
125
+
126
+ if @config_file
127
+ require_relative 'configuration'
128
+ require_relative 'log_writer'
129
+
130
+ config = Puma::Configuration.new({ config_files: [@config_file] }, {} , env)
131
+ config.load
132
+ @state ||= config.options[:state]
133
+ @control_url ||= config.options[:control_url]
134
+ @control_auth_token ||= config.options[:control_auth_token]
135
+ @pidfile ||= config.options[:pidfile]
136
+ end
137
+ end
138
+ rescue => e
139
+ @stdout.puts e.message
140
+ exit 1
141
+ end
142
+
143
+ def message(msg)
144
+ @stdout.puts msg unless @quiet
145
+ end
146
+
147
+ def prepare_configuration
148
+ if @state
149
+ unless File.exist? @state
150
+ raise "State file not found: #{@state}"
151
+ end
152
+
153
+ require_relative 'state_file'
154
+
155
+ sf = Puma::StateFile.new
156
+ sf.load @state
157
+
158
+ @control_url = sf.control_url
159
+ @control_auth_token = sf.control_auth_token
160
+ @pid = sf.pid
161
+ elsif @pidfile
162
+ # get pid from pid_file
163
+ @pid = File.read(@pidfile, mode: 'rb:UTF-8').to_i
164
+ end
165
+ end
166
+
167
+ def send_request
168
+ uri = URI.parse @control_url
169
+
170
+ host = uri.host
171
+
172
+ # create server object by scheme
173
+ server =
174
+ case uri.scheme
175
+ when 'ssl'
176
+ require 'openssl'
177
+ host = host[1..-2] if host&.start_with? '['
178
+ OpenSSL::SSL::SSLSocket.new(
179
+ TCPSocket.new(host, uri.port),
180
+ OpenSSL::SSL::SSLContext.new)
181
+ .tap { |ssl| ssl.sync_close = true } # default is false
182
+ .tap(&:connect)
183
+ when 'tcp'
184
+ host = host[1..-2] if host&.start_with? '['
185
+ TCPSocket.new host, uri.port
186
+ when 'unix'
187
+ # check for abstract UNIXSocket
188
+ UNIXSocket.new(@control_url.start_with?('unix://@') ?
189
+ "\0#{host}#{uri.path}" : "#{host}#{uri.path}")
190
+ else
191
+ raise "Invalid scheme: #{uri.scheme}"
192
+ end
193
+
194
+ if @command == 'status'
195
+ message 'Puma is started'
196
+ else
197
+ url = "/#{@command}"
198
+
199
+ if @control_auth_token
200
+ url = url + "?token=#{@control_auth_token}"
201
+ end
202
+
203
+ server.syswrite "GET #{url} HTTP/1.0\r\n\r\n"
204
+
205
+ unless data = server.read
206
+ raise 'Server closed connection before responding'
207
+ end
208
+
209
+ response = data.split("\r\n")
210
+
211
+ if response.empty?
212
+ raise "Server sent empty response"
213
+ end
214
+
215
+ @http, @code, @message = response.first.split(' ',3)
216
+
217
+ if @code == '403'
218
+ raise 'Unauthorized access to server (wrong auth token)'
219
+ elsif @code == '404'
220
+ raise "Command error: #{response.last}"
221
+ elsif @code != '200'
222
+ raise "Bad response from server: #{@code}"
223
+ end
224
+
225
+ message "Command #{@command} sent success"
226
+ message response.last if PRINTABLE_COMMANDS.include?(@command)
227
+ end
228
+ ensure
229
+ if server
230
+ if uri.scheme == 'ssl'
231
+ server.sysclose
232
+ else
233
+ server.close unless server.closed?
234
+ end
235
+ end
236
+ end
237
+
238
+ def send_signal
239
+ unless @pid
240
+ raise 'Neither pid nor control url available'
241
+ end
242
+
243
+ begin
244
+ sig = CMD_PATH_SIG_MAP[@command]
245
+
246
+ if sig.nil?
247
+ @stdout.puts "'#{@command}' not available via pid only"
248
+ @stdout.flush unless @stdout.sync
249
+ return
250
+ elsif sig.start_with? 'SIG'
251
+ if Signal.list.key? sig.sub(/\ASIG/, '')
252
+ Process.kill sig, @pid
253
+ else
254
+ raise "Signal '#{sig}' not available'"
255
+ end
256
+ elsif @command == 'status'
257
+ begin
258
+ Process.kill 0, @pid
259
+ @stdout.puts 'Puma is started'
260
+ @stdout.flush unless @stdout.sync
261
+ rescue Errno::ESRCH
262
+ raise 'Puma is not running'
263
+ end
264
+ return
265
+ end
266
+ rescue SystemCallError
267
+ if @command == 'restart'
268
+ start
269
+ else
270
+ raise "No pid '#{@pid}' found"
271
+ end
272
+ end
273
+
274
+ message "Command #{@command} sent success"
275
+ end
276
+
277
+ def run
278
+ return start if @command == 'start'
279
+ prepare_configuration
280
+
281
+ if Puma.windows? || @control_url && !NO_REQ_COMMANDS.include?(@command)
282
+ send_request
283
+ else
284
+ send_signal
285
+ end
286
+
287
+ rescue => e
288
+ message e.message
289
+ exit 1
290
+ end
291
+
292
+ private
293
+ def start
294
+ require_relative 'cli'
295
+
296
+ run_args = []
297
+
298
+ run_args += ["-S", @state] if @state
299
+ run_args += ["-q"] if @quiet
300
+ run_args += ["--pidfile", @pidfile] if @pidfile
301
+ run_args += ["--control-url", @control_url] if @control_url
302
+ run_args += ["--control-token", @control_auth_token] if @control_auth_token
303
+ run_args += ["-C", @config_file] if @config_file
304
+ run_args += ["-e", @environment] if @environment
305
+
306
+ log_writer = Puma::LogWriter.new(@stdout, @stderr)
307
+
308
+ # replace $0 because puma use it to generate restart command
309
+ puma_cmd = $0.gsub(/pumactl$/, 'puma')
310
+ $0 = puma_cmd if File.exist?(puma_cmd)
311
+
312
+ cli = Puma::CLI.new run_args, log_writer
313
+ cli.run
314
+ end
315
+ end
316
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file can be loaded independently of puma.rb, so it cannot have any code
4
+ # that assumes puma.rb is loaded.
5
+
6
+
7
+ module Puma
8
+ # @version 5.2.1
9
+ HAS_FORK = ::Process.respond_to? :fork
10
+
11
+ HAS_NATIVE_IO_WAIT = ::IO.public_instance_methods(false).include? :wait_readable
12
+
13
+ IS_JRUBY = Object.const_defined? :JRUBY_VERSION
14
+
15
+ IS_OSX = RUBY_DESCRIPTION.include? 'darwin'
16
+
17
+ IS_WINDOWS = RUBY_DESCRIPTION.match?(/mswin|ming|cygwin/)
18
+
19
+ IS_LINUX = !(IS_OSX || IS_WINDOWS)
20
+
21
+ # @version 5.2.0
22
+ IS_MRI = RUBY_ENGINE == 'ruby'
23
+
24
+ def self.jruby?
25
+ IS_JRUBY
26
+ end
27
+
28
+ def self.osx?
29
+ IS_OSX
30
+ end
31
+
32
+ def self.windows?
33
+ IS_WINDOWS
34
+ end
35
+
36
+ # @version 5.0.0
37
+ def self.mri?
38
+ IS_MRI
39
+ end
40
+
41
+ # @version 5.0.0
42
+ def self.forkable?
43
+ HAS_FORK
44
+ end
45
+ end