puma 8.1.4 → 8.2.0.pre1
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.
- checksums.yaml +4 -4
- data/History.md +114 -19
- data/README.md +4 -3
- data/docs/GLOSSARY.md +160 -0
- data/docs/architecture.md +41 -42
- data/docs/binding.md +3 -0
- data/docs/cluster_mode.md +21 -21
- data/docs/control_server.md +4 -4
- data/docs/deployment/nginx.md +2 -0
- data/docs/deployment/systemd.md +15 -0
- data/docs/puma_support.md +12 -0
- data/docs/restart.md +2 -2
- data/docs/signals.md +3 -3
- data/docs/ssl.md +65 -0
- data/docs/upgrading/5.0-Upgrade.md +1 -1
- data/docs/upgrading/6.0-Upgrade.md +1 -1
- data/docs/upgrading/7.0-Upgrade.md +1 -1
- data/docs/upgrading/8.0-Upgrade.md +1 -1
- data/ext/puma_http11/extconf.rb +7 -5
- data/ext/puma_http11/http11_parser.c +66 -84
- data/ext/puma_http11/http11_parser.h +0 -5
- data/ext/puma_http11/mini_ssl.c +17 -61
- data/ext/puma_http11/puma_http11.c +125 -7
- data/lib/puma/app/status.rb +22 -7
- data/lib/puma/binder.rb +0 -1
- data/lib/puma/cli.rb +1 -3
- data/lib/puma/client.rb +114 -56
- data/lib/puma/client_env.rb +9 -11
- data/lib/puma/cluster/worker.rb +0 -6
- data/lib/puma/cluster.rb +3 -14
- data/lib/puma/cluster_accept_loop_delay.rb +18 -7
- data/lib/puma/commonlogger.rb +3 -4
- data/lib/puma/configuration.rb +4 -1
- data/lib/puma/const.rb +5 -5
- data/lib/puma/control_cli.rb +5 -7
- data/lib/puma/detect.rb +1 -1
- data/lib/puma/dsl.rb +70 -14
- data/lib/puma/http_parser_error.rb +8 -0
- data/lib/puma/launcher/bundle_pruner.rb +3 -21
- data/lib/puma/launcher.rb +13 -5
- data/lib/puma/minissl/context_builder.rb +3 -2
- data/lib/puma/minissl.rb +3 -12
- data/lib/puma/plugin/systemd.rb +69 -3
- data/lib/puma/rack/urlmap.rb +2 -2
- data/lib/puma/reactor.rb +138 -25
- data/lib/puma/response.rb +52 -33
- data/lib/puma/runner.rb +4 -1
- data/lib/puma/sd_notify.rb +21 -0
- data/lib/puma/server.rb +14 -14
- data/lib/puma/single.rb +0 -1
- data/lib/puma/thread_pool.rb +87 -14
- data/lib/puma/util.rb +5 -16
- data/lib/puma.rb +22 -5
- data/lib/rack/handler/puma.rb +1 -5
- metadata +4 -1
|
@@ -4,13 +4,10 @@
|
|
|
4
4
|
* License 3-clause BSD
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
#define RSTRING_NOT_MODIFIED 1
|
|
8
|
-
|
|
9
7
|
#include "ruby.h"
|
|
10
8
|
#include "ruby/encoding.h"
|
|
11
9
|
#include <assert.h>
|
|
12
10
|
#include <string.h>
|
|
13
|
-
#include <ctype.h>
|
|
14
11
|
#include "http11_parser.h"
|
|
15
12
|
|
|
16
13
|
#ifdef HAVE_PRCTL
|
|
@@ -30,6 +27,7 @@ static VALUE global_fragment;
|
|
|
30
27
|
static VALUE global_query_string;
|
|
31
28
|
static VALUE global_server_protocol;
|
|
32
29
|
static VALUE global_request_path;
|
|
30
|
+
static VALUE global_http_host;
|
|
33
31
|
|
|
34
32
|
/** Defines common length and error messages for input length validation. */
|
|
35
33
|
#define QUOTE(s) #s
|
|
@@ -135,16 +133,120 @@ static void init_common_fields(void)
|
|
|
135
133
|
} else {
|
|
136
134
|
memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1);
|
|
137
135
|
cf->value = rb_enc_interned_str(tmp, HTTP_PREFIX_LEN + cf->len, rb_utf8_encoding());
|
|
136
|
+
if (cf->len == 4 && !memcmp(cf->name, "HOST", 4)) {
|
|
137
|
+
global_http_host = cf->value;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static char snake_upcase_table[256];
|
|
144
|
+
static unsigned char host_char_table[256];
|
|
145
|
+
|
|
146
|
+
static void init_snake_upcase_table(void)
|
|
147
|
+
{
|
|
148
|
+
unsigned i;
|
|
149
|
+
for (i = 0; i < 256; i++) {
|
|
150
|
+
if (i >= 'a' && i <= 'z')
|
|
151
|
+
snake_upcase_table[i] = (char)(i & ~0x20);
|
|
152
|
+
else if (i == '_')
|
|
153
|
+
snake_upcase_table[i] = ',';
|
|
154
|
+
else if (i == '-')
|
|
155
|
+
snake_upcase_table[i] = '_';
|
|
156
|
+
else
|
|
157
|
+
snake_upcase_table[i] = (char)i;
|
|
158
|
+
|
|
159
|
+
unsigned char flags = 0;
|
|
160
|
+
if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') ||
|
|
161
|
+
(i >= '0' && i <= '9') || i == '.' || i == '_' || i == '~' || i == '-') {
|
|
162
|
+
flags |= 1;
|
|
163
|
+
}
|
|
164
|
+
if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F')) {
|
|
165
|
+
flags |= 2;
|
|
166
|
+
}
|
|
167
|
+
if (i >= '0' && i <= '9') {
|
|
168
|
+
flags |= 4;
|
|
138
169
|
}
|
|
170
|
+
host_char_table[i] = flags;
|
|
139
171
|
}
|
|
140
172
|
}
|
|
141
173
|
|
|
174
|
+
static void validate_ascii(const char *at, size_t length)
|
|
175
|
+
{
|
|
176
|
+
const unsigned char *p = (const unsigned char *)at;
|
|
177
|
+
unsigned char acc = 0;
|
|
178
|
+
size_t i;
|
|
179
|
+
for (i = 0; i < length; i++) {
|
|
180
|
+
acc |= p[i];
|
|
181
|
+
}
|
|
182
|
+
if (acc & 0x80) {
|
|
183
|
+
rb_raise(eHttpParserError, "Invalid HTTP format, non-ASCII byte in request URI");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static void validate_host(const char *s, size_t len)
|
|
188
|
+
{
|
|
189
|
+
size_t i = 0;
|
|
190
|
+
if (s[0] == '[') {
|
|
191
|
+
i = 1;
|
|
192
|
+
while (i < len && s[i] != ']') {
|
|
193
|
+
unsigned char c = (unsigned char)s[i];
|
|
194
|
+
if (!(host_char_table[c] & 2) && c != ':' && c != '.') {
|
|
195
|
+
rb_raise(eHttpParserError, "Invalid Host header");
|
|
196
|
+
}
|
|
197
|
+
i++;
|
|
198
|
+
}
|
|
199
|
+
if (i == 1 || i >= len || s[i] != ']') {
|
|
200
|
+
rb_raise(eHttpParserError, "Invalid Host header");
|
|
201
|
+
}
|
|
202
|
+
i++;
|
|
203
|
+
} else {
|
|
204
|
+
while (i < len && s[i] != ':') {
|
|
205
|
+
unsigned char c = (unsigned char)s[i];
|
|
206
|
+
if (host_char_table[c] & 1) {
|
|
207
|
+
i++;
|
|
208
|
+
} else if (c == '%' && i + 2 < len &&
|
|
209
|
+
(host_char_table[(unsigned char)s[i + 1]] & 2) &&
|
|
210
|
+
(host_char_table[(unsigned char)s[i + 2]] & 2)) {
|
|
211
|
+
i += 3;
|
|
212
|
+
} else {
|
|
213
|
+
rb_raise(eHttpParserError, "Invalid Host header");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (i == 0) {
|
|
217
|
+
rb_raise(eHttpParserError, "Invalid Host header");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (i < len) {
|
|
221
|
+
if (s[i] != ':') {
|
|
222
|
+
rb_raise(eHttpParserError, "Invalid Host header");
|
|
223
|
+
}
|
|
224
|
+
i++;
|
|
225
|
+
while (i < len) {
|
|
226
|
+
if (!(host_char_table[(unsigned char)s[i]] & 4)) {
|
|
227
|
+
rb_raise(eHttpParserError, "Invalid Host header");
|
|
228
|
+
}
|
|
229
|
+
i++;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
static inline int field_name_eql(const char *expected, const char *raw, size_t len)
|
|
235
|
+
{
|
|
236
|
+
size_t i;
|
|
237
|
+
for (i = 0; i < len; i++) {
|
|
238
|
+
if (expected[i] != snake_upcase_table[(unsigned char)raw[i]])
|
|
239
|
+
return 0;
|
|
240
|
+
}
|
|
241
|
+
return 1;
|
|
242
|
+
}
|
|
243
|
+
|
|
142
244
|
static VALUE find_common_field_value(const char *field, size_t flen)
|
|
143
245
|
{
|
|
144
246
|
unsigned i;
|
|
145
247
|
struct common_field *cf = common_http_fields;
|
|
146
248
|
for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) {
|
|
147
|
-
if (cf->len == flen &&
|
|
249
|
+
if (cf->len == flen && field_name_eql(cf->name, field, flen))
|
|
148
250
|
return cf->value;
|
|
149
251
|
}
|
|
150
252
|
return Qnil;
|
|
@@ -168,14 +270,18 @@ static void http_field(puma_parser* hp, const char *field, size_t flen,
|
|
|
168
270
|
if (f == Qnil) {
|
|
169
271
|
/*
|
|
170
272
|
* We got a strange header that we don't have a memoized value for.
|
|
171
|
-
*
|
|
273
|
+
* Normalize the field name into the parser's scratch buffer, after the
|
|
274
|
+
* HTTP_ prefix, leaving the caller's String untouched.
|
|
172
275
|
*/
|
|
173
|
-
|
|
276
|
+
size_t i;
|
|
174
277
|
size_t new_size = HTTP_PREFIX_LEN + flen;
|
|
278
|
+
char *name = hp->buf + HTTP_PREFIX_LEN;
|
|
175
279
|
assert(new_size < BUFFER_LEN);
|
|
176
280
|
|
|
177
281
|
memcpy(hp->buf, HTTP_PREFIX, HTTP_PREFIX_LEN);
|
|
178
|
-
|
|
282
|
+
for (i = 0; i < flen; i++) {
|
|
283
|
+
name[i] = snake_upcase_table[(unsigned char)field[i]];
|
|
284
|
+
}
|
|
179
285
|
|
|
180
286
|
f = rb_enc_interned_str(hp->buf, new_size, rb_utf8_encoding());
|
|
181
287
|
}
|
|
@@ -189,6 +295,15 @@ static void http_field(puma_parser* hp, const char *field, size_t flen,
|
|
|
189
295
|
/* check for duplicate header */
|
|
190
296
|
v = rb_hash_aref(hp->request, f);
|
|
191
297
|
|
|
298
|
+
if (f == global_http_host) {
|
|
299
|
+
if (v != Qnil) {
|
|
300
|
+
rb_raise(eHttpParserError, "Invalid Host header");
|
|
301
|
+
}
|
|
302
|
+
if (vlen > 0) {
|
|
303
|
+
validate_host(value, vlen);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
192
307
|
if (v == Qnil) {
|
|
193
308
|
v = rb_str_new(value, vlen);
|
|
194
309
|
rb_hash_aset(hp->request, f, v);
|
|
@@ -212,6 +327,7 @@ static void request_uri(puma_parser* hp, const char *at, size_t length)
|
|
|
212
327
|
VALUE val = Qnil;
|
|
213
328
|
|
|
214
329
|
VALIDATE_MAX_LENGTH(length, REQUEST_URI);
|
|
330
|
+
validate_ascii(at, length);
|
|
215
331
|
|
|
216
332
|
val = rb_str_new(at, length);
|
|
217
333
|
rb_hash_aset(hp->request, global_request_uri, val);
|
|
@@ -222,6 +338,7 @@ static void fragment(puma_parser* hp, const char *at, size_t length)
|
|
|
222
338
|
VALUE val = Qnil;
|
|
223
339
|
|
|
224
340
|
VALIDATE_MAX_LENGTH(length, FRAGMENT);
|
|
341
|
+
validate_ascii(at, length);
|
|
225
342
|
|
|
226
343
|
val = rb_str_new(at, length);
|
|
227
344
|
rb_hash_aset(hp->request, global_fragment, val);
|
|
@@ -516,6 +633,7 @@ RUBY_FUNC_EXPORTED void Init_puma_http11(void)
|
|
|
516
633
|
rb_define_method(cHttpParser, "finished?", HttpParser_is_finished, 0);
|
|
517
634
|
rb_define_method(cHttpParser, "nread", HttpParser_nread, 0);
|
|
518
635
|
rb_define_method(cHttpParser, "body", HttpParser_body, 0);
|
|
636
|
+
init_snake_upcase_table();
|
|
519
637
|
init_common_fields();
|
|
520
638
|
|
|
521
639
|
#ifdef HAVE_OPENSSL_BIO_H
|
data/lib/puma/app/status.rb
CHANGED
|
@@ -12,23 +12,31 @@ module Puma
|
|
|
12
12
|
# @param launcher [::Puma::Launcher]
|
|
13
13
|
# @param token [String, nil] the token used for authentication
|
|
14
14
|
# @param data_only [Boolean] if true, restrict to read-only data commands
|
|
15
|
+
# @param data_token [String, Symbol, nil] optional token (or 'none' / :none) for read-only data commands
|
|
15
16
|
#
|
|
16
|
-
def initialize(launcher, token: nil, data_only: false)
|
|
17
|
+
def initialize(launcher, token: nil, data_only: false, data_token: nil)
|
|
17
18
|
@launcher = launcher
|
|
18
19
|
@auth_token = token
|
|
19
20
|
@enabled_commands = READ_ONLY_COMMANDS if data_only
|
|
21
|
+
@data_token =
|
|
22
|
+
case data_token
|
|
23
|
+
when '' then nil
|
|
24
|
+
when 'none' then :none
|
|
25
|
+
else data_token
|
|
26
|
+
end
|
|
20
27
|
end
|
|
21
28
|
|
|
22
29
|
# most commands call methods in `::Puma::Launcher` based on command in
|
|
23
30
|
# `env['PATH_INFO']`
|
|
24
31
|
def call(env)
|
|
25
|
-
unless authenticate(env)
|
|
26
|
-
return rack_response(403, 'Invalid auth token', 'text/plain')
|
|
27
|
-
end
|
|
28
|
-
|
|
29
32
|
# resp_type is processed by following case statement, return
|
|
30
33
|
# is a number (status) or a string used as the body of a 200 response
|
|
31
34
|
command = env['PATH_INFO'][/\/([^\/]+)$/, 1]
|
|
35
|
+
|
|
36
|
+
unless authenticate(env, command)
|
|
37
|
+
return rack_response(403, 'Invalid auth token', 'text/plain')
|
|
38
|
+
end
|
|
39
|
+
|
|
32
40
|
if @enabled_commands && !@enabled_commands.include?(command)
|
|
33
41
|
return rack_response(404, "Command #{command.inspect} unavailable", 'text/plain')
|
|
34
42
|
end
|
|
@@ -86,9 +94,16 @@ module Puma
|
|
|
86
94
|
|
|
87
95
|
private
|
|
88
96
|
|
|
89
|
-
def authenticate(env)
|
|
97
|
+
def authenticate(env, command)
|
|
90
98
|
return true unless @auth_token
|
|
91
|
-
env
|
|
99
|
+
return true if token_matches?(env, @auth_token)
|
|
100
|
+
return false unless @data_token && READ_ONLY_COMMANDS.include?(command)
|
|
101
|
+
|
|
102
|
+
@data_token == :none || token_matches?(env, @data_token)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def token_matches?(env, token)
|
|
106
|
+
env['QUERY_STRING'].to_s.split(/[&;]/).include?("token=#{token}")
|
|
92
107
|
end
|
|
93
108
|
|
|
94
109
|
def rack_response(status, body, content_type='application/json')
|
data/lib/puma/binder.rb
CHANGED
|
@@ -234,7 +234,6 @@ module Puma
|
|
|
234
234
|
# If key and certs are not defined and localhost gem is required.
|
|
235
235
|
# localhost gem will be used for self signed
|
|
236
236
|
# Load localhost authority if not loaded.
|
|
237
|
-
# Ruby 3 `values_at` accepts an array, earlier do not
|
|
238
237
|
if params.values_at(*cert_key).all? { |v| v.to_s.empty? }
|
|
239
238
|
ctx = localhost_authority && localhost_authority_context(params)
|
|
240
239
|
end
|
data/lib/puma/cli.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'optparse'
|
|
4
|
-
require 'uri'
|
|
5
4
|
|
|
6
5
|
require_relative '../puma'
|
|
7
6
|
require_relative 'configuration'
|
|
@@ -25,7 +24,6 @@ module Puma
|
|
|
25
24
|
# arguments.
|
|
26
25
|
#
|
|
27
26
|
def initialize(argv, log_writer = LogWriter.stdio, events = Events.new, env: ENV)
|
|
28
|
-
@debug = false
|
|
29
27
|
@argv = argv.dup
|
|
30
28
|
@log_writer = log_writer
|
|
31
29
|
@events = events
|
|
@@ -92,7 +90,7 @@ module Puma
|
|
|
92
90
|
|
|
93
91
|
def setup_options(env = ENV)
|
|
94
92
|
@conf = Configuration.new({}, { events: @events }, env) do |user_config, file_config|
|
|
95
|
-
|
|
93
|
+
OptionParser.new do |o|
|
|
96
94
|
o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
|
|
97
95
|
user_config.bind arg
|
|
98
96
|
end
|
data/lib/puma/client.rb
CHANGED
|
@@ -3,16 +3,10 @@
|
|
|
3
3
|
require_relative 'detect'
|
|
4
4
|
require_relative 'io_buffer'
|
|
5
5
|
require_relative 'client_env'
|
|
6
|
+
require_relative 'http_parser_error'
|
|
6
7
|
require_relative 'null_io'
|
|
7
8
|
require 'tempfile'
|
|
8
9
|
|
|
9
|
-
if Puma::IS_JRUBY
|
|
10
|
-
# We have to work around some OpenSSL buffer/io-readiness bugs
|
|
11
|
-
# so we pull it in regardless of if the user is binding
|
|
12
|
-
# to an SSL socket
|
|
13
|
-
require 'openssl'
|
|
14
|
-
end
|
|
15
|
-
|
|
16
10
|
module Puma
|
|
17
11
|
|
|
18
12
|
class ConnectionError < RuntimeError; end
|
|
@@ -57,10 +51,16 @@ module Puma
|
|
|
57
51
|
# this tests all values but the last, which must be chunked
|
|
58
52
|
ALLOWED_TRANSFER_ENCODING = %w[compress deflate gzip].freeze
|
|
59
53
|
|
|
60
|
-
# chunked body validation
|
|
54
|
+
# chunked body validation (RFC 9112 §7.1, §7.1.1)
|
|
61
55
|
CHUNK_SIZE_VALID = /\A\h+\z/.freeze
|
|
56
|
+
CHUNK_EXT_TCHAR = /[!#$%&'*+\-.^_`|~0-9A-Za-z]/.freeze
|
|
57
|
+
CHUNK_EXT_TOKEN = /#{CHUNK_EXT_TCHAR}+/.freeze
|
|
58
|
+
CHUNK_EXT_QUOTED = /"(?:[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*"/n.freeze
|
|
59
|
+
CHUNK_EXT_VALID = /\A(?:[ \t]*;[ \t]*#{CHUNK_EXT_TOKEN}(?:[ \t]*=[ \t]*(?:#{CHUNK_EXT_TOKEN}|#{CHUNK_EXT_QUOTED}))?)+\z/n.freeze
|
|
62
60
|
CHUNK_VALID_ENDING = Const::LINE_END
|
|
63
61
|
CHUNK_VALID_ENDING_SIZE = CHUNK_VALID_ENDING.bytesize
|
|
62
|
+
BARE_LF = /(?<!\r)\n/.freeze
|
|
63
|
+
private_constant :CHUNK_EXT_TCHAR, :CHUNK_EXT_TOKEN, :CHUNK_EXT_QUOTED, :CHUNK_EXT_VALID, :BARE_LF
|
|
64
64
|
|
|
65
65
|
# The maximum number of bytes we'll buffer looking for a valid
|
|
66
66
|
# chunk header, and the maximum size of a trailer section.
|
|
@@ -94,27 +94,30 @@ module Puma
|
|
|
94
94
|
:allow_underscore_headers, :continue_callback
|
|
95
95
|
|
|
96
96
|
attr_accessor :remote_addr_header, :listener, :env_set_http_version,
|
|
97
|
-
:reactor_error
|
|
97
|
+
:reactor_error, :empty_rack_input
|
|
98
98
|
|
|
99
|
-
def initialize(io, env=nil)
|
|
99
|
+
def initialize(io, env=nil, http_parser: nil)
|
|
100
100
|
@io = io
|
|
101
101
|
@to_io = io.to_io
|
|
102
102
|
@io_buffer = IOBuffer.new
|
|
103
103
|
@proto_env = env
|
|
104
104
|
@env = env&.dup
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
http_parser ||= Puma.default_http_parser
|
|
107
|
+
@parser = http_parser.respond_to?(:call) ? http_parser.call : http_parser.new
|
|
107
108
|
@parsed_bytes = 0
|
|
108
109
|
@read_header = true
|
|
109
110
|
@read_proxy = false
|
|
110
111
|
@ready = false
|
|
111
112
|
|
|
112
113
|
@body = nil
|
|
114
|
+
@empty_rack_input = EmptyBody
|
|
113
115
|
@body_read_start = nil
|
|
114
116
|
@buffer = nil
|
|
115
117
|
@tempfile = nil
|
|
116
118
|
|
|
117
119
|
@timeout_at = nil
|
|
120
|
+
@header_deadline = nil
|
|
118
121
|
|
|
119
122
|
@requests_served = 0
|
|
120
123
|
@hijacked = false
|
|
@@ -142,7 +145,6 @@ module Puma
|
|
|
142
145
|
@read_buffer = String.new # rubocop: disable Performance/UnfreezeString
|
|
143
146
|
end
|
|
144
147
|
|
|
145
|
-
# Remove in Puma 7?
|
|
146
148
|
def closed?
|
|
147
149
|
@to_io.closed?
|
|
148
150
|
end
|
|
@@ -174,6 +176,14 @@ module Puma
|
|
|
174
176
|
@timeout_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + val
|
|
175
177
|
end
|
|
176
178
|
|
|
179
|
+
def set_read_timeout(val)
|
|
180
|
+
set_timeout(val)
|
|
181
|
+
return if in_data_phase
|
|
182
|
+
|
|
183
|
+
@header_deadline ||= @timeout_at
|
|
184
|
+
@timeout_at = @header_deadline
|
|
185
|
+
end
|
|
186
|
+
|
|
177
187
|
# Number of seconds until the timeout elapses.
|
|
178
188
|
# @!attribute [r] timeout
|
|
179
189
|
def timeout
|
|
@@ -193,6 +203,7 @@ module Puma
|
|
|
193
203
|
@in_last_chunk = false
|
|
194
204
|
@http_content_length_limit_exceeded = nil
|
|
195
205
|
@error_status_code = nil
|
|
206
|
+
@header_deadline = nil
|
|
196
207
|
end
|
|
197
208
|
|
|
198
209
|
# only used with back-to-back requests contained in the buffer
|
|
@@ -318,12 +329,13 @@ module Puma
|
|
|
318
329
|
|
|
319
330
|
def finish(timeout, &deadline)
|
|
320
331
|
return if @ready
|
|
321
|
-
|
|
332
|
+
set_read_timeout timeout
|
|
322
333
|
read = @bytes_read
|
|
323
334
|
until try_to_finish(&deadline)
|
|
324
335
|
if @bytes_read > read
|
|
325
|
-
|
|
336
|
+
set_read_timeout timeout
|
|
326
337
|
read = @bytes_read
|
|
338
|
+
timeout! if !in_data_phase && self.timeout == 0
|
|
327
339
|
elsif self.timeout == 0
|
|
328
340
|
timeout!
|
|
329
341
|
end
|
|
@@ -360,6 +372,12 @@ module Puma
|
|
|
360
372
|
def parser_execute
|
|
361
373
|
ret = @parser.execute(@env, @buffer, @parsed_bytes)
|
|
362
374
|
|
|
375
|
+
if (proto = @env[SERVER_PROTOCOL]) && proto != HTTP_11 && !proto.start_with?('HTTP/1.')
|
|
376
|
+
@error_status_code = 505
|
|
377
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
378
|
+
raise HttpParserError, "HTTP Version Not Supported: #{proto}"
|
|
379
|
+
end
|
|
380
|
+
|
|
363
381
|
if @env[REQUEST_METHOD] && @supported_http_methods != :any && !@supported_http_methods.key?(@env[REQUEST_METHOD])
|
|
364
382
|
raise HttpParserError501, "#{@env[REQUEST_METHOD]} method is not supported"
|
|
365
383
|
end
|
|
@@ -392,10 +410,12 @@ module Puma
|
|
|
392
410
|
# protocol is bad
|
|
393
411
|
text = request_line[/[^ ]*\z/]
|
|
394
412
|
raise HttpParserError, "Invalid HTTP format, parsing fails. Bad protocol #{text}"
|
|
395
|
-
elsif !headers.empty?
|
|
413
|
+
elsif headers && !headers.empty?
|
|
396
414
|
# headers are bad
|
|
397
415
|
hdrs = headers.split("\r\n").map { |h| h.gsub "\n", '\n'}.join "\n"
|
|
398
416
|
raise HttpParserError, "Invalid HTTP format, parsing fails. Bad headers\n#{hdrs}"
|
|
417
|
+
else
|
|
418
|
+
raise e
|
|
399
419
|
end
|
|
400
420
|
end
|
|
401
421
|
|
|
@@ -525,24 +545,20 @@ module Puma
|
|
|
525
545
|
|
|
526
546
|
@body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
|
527
547
|
|
|
528
|
-
if @env[HTTP_EXPECT] == CONTINUE
|
|
529
|
-
if @continue_callback && (response = @continue_callback.call(@env))
|
|
530
|
-
reject_continue response
|
|
531
|
-
end
|
|
532
|
-
write_interim HTTP_11_100
|
|
533
|
-
end
|
|
534
|
-
|
|
535
|
-
@read_header = false
|
|
536
|
-
|
|
537
|
-
parser_body = @parser.body
|
|
538
|
-
|
|
539
548
|
te = @env[TRANSFER_ENCODING2]
|
|
549
|
+
cl = @env[CONTENT_LENGTH]
|
|
550
|
+
|
|
540
551
|
if te
|
|
541
552
|
if @env.key?(CONTENT_LENGTH)
|
|
542
553
|
@error_status_code = 400
|
|
543
554
|
@env[HTTP_CONNECTION] = CLOSE
|
|
544
555
|
raise HttpParserError, "#{TE_ERR_MSG}, cannot be combined with Content-Length"
|
|
545
556
|
end
|
|
557
|
+
if te.empty?
|
|
558
|
+
@error_status_code = 400
|
|
559
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
560
|
+
raise HttpParserError, "#{TE_ERR_MSG}, empty value"
|
|
561
|
+
end
|
|
546
562
|
te_lwr = te.downcase
|
|
547
563
|
if te.include? ','
|
|
548
564
|
te_ary = te_lwr.split(',').each { |te| te.gsub!(STRIP_OWS, "") }
|
|
@@ -555,30 +571,38 @@ module Puma
|
|
|
555
571
|
elsif !te_valid
|
|
556
572
|
raise HttpParserError501, "#{TE_ERR_MSG}, unknown value: '#{te}'"
|
|
557
573
|
end
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
raise HttpParserError , "#{TE_ERR_MSG}, single value must be chunked: '#{te}'"
|
|
565
|
-
else
|
|
566
|
-
raise HttpParserError501 , "#{TE_ERR_MSG}, unknown value: '#{te}'"
|
|
574
|
+
elsif te_lwr != CHUNKED
|
|
575
|
+
if ALLOWED_TRANSFER_ENCODING.include? te_lwr
|
|
576
|
+
raise HttpParserError , "#{TE_ERR_MSG}, single value must be chunked: '#{te}'"
|
|
577
|
+
else
|
|
578
|
+
raise HttpParserError501, "#{TE_ERR_MSG}, unknown value: '#{te}'"
|
|
579
|
+
end
|
|
567
580
|
end
|
|
581
|
+
elsif cl && (CONTENT_LENGTH_VALUE_INVALID.match?(cl) || cl.empty?)
|
|
582
|
+
@error_status_code = 400
|
|
583
|
+
@env[HTTP_CONNECTION] = 'close'
|
|
584
|
+
raise HttpParserError, "Invalid Content-Length: #{cl.inspect}"
|
|
568
585
|
end
|
|
569
586
|
|
|
570
|
-
@
|
|
587
|
+
if @env[HTTP_EXPECT] == CONTINUE && @env[SERVER_PROTOCOL] != HTTP_10
|
|
588
|
+
if @continue_callback && (response = @continue_callback.call(@env))
|
|
589
|
+
reject_continue response
|
|
590
|
+
end
|
|
591
|
+
write_interim HTTP_11_100
|
|
592
|
+
end
|
|
571
593
|
|
|
572
|
-
|
|
594
|
+
@read_header = false
|
|
573
595
|
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
596
|
+
parser_body = @parser.body
|
|
597
|
+
|
|
598
|
+
if te
|
|
599
|
+
@env.delete TRANSFER_ENCODING2
|
|
600
|
+
return setup_chunked_body parser_body
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
@chunked_body = false
|
|
604
|
+
|
|
605
|
+
unless cl
|
|
582
606
|
@buffer = parser_body.empty? ? nil : parser_body
|
|
583
607
|
@body = EmptyBody
|
|
584
608
|
set_ready
|
|
@@ -715,13 +739,13 @@ module Puma
|
|
|
715
739
|
@partial_part_left = 0
|
|
716
740
|
@prev_chunk = ""
|
|
717
741
|
@excess_cr = 0
|
|
718
|
-
|
|
719
|
-
@body = Tempfile.create(Const::PUMA_TMP_BASE)
|
|
720
|
-
File.unlink @body.path unless IS_WINDOWS
|
|
721
|
-
@body.binmode
|
|
722
|
-
@tempfile = @body
|
|
742
|
+
@tempfile = nil
|
|
723
743
|
@chunked_content_length = 0
|
|
724
744
|
|
|
745
|
+
# The body[0,0] trick is to get an empty string in the same
|
|
746
|
+
# encoding as body.
|
|
747
|
+
@body = StringIO.new body[0,0]
|
|
748
|
+
|
|
725
749
|
if decode_chunk(body)
|
|
726
750
|
@env[CONTENT_LENGTH] = @chunked_content_length.to_s
|
|
727
751
|
return true
|
|
@@ -730,10 +754,23 @@ module Puma
|
|
|
730
754
|
|
|
731
755
|
# @version 5.0.0
|
|
732
756
|
def write_chunk(str)
|
|
733
|
-
|
|
757
|
+
remain = @chunked_content_length + str.bytesize
|
|
758
|
+
|
|
759
|
+
if @http_content_length_limit&.< remain
|
|
734
760
|
raise_above_http_content_limit
|
|
735
761
|
end
|
|
736
762
|
|
|
763
|
+
# The length of a chunked body is unknown up front, so it starts in memory
|
|
764
|
+
# and moves to a Tempfile once it grows past MAX_BODY, similar to Content-Length.
|
|
765
|
+
if @tempfile.nil? && remain > MAX_BODY
|
|
766
|
+
@tempfile = Tempfile.create(Const::PUMA_TMP_BASE)
|
|
767
|
+
File.unlink @tempfile.path unless IS_WINDOWS
|
|
768
|
+
@tempfile.binmode
|
|
769
|
+
@tempfile.write @body.string
|
|
770
|
+
@body = @tempfile
|
|
771
|
+
@env[RACK_INPUT] = @body
|
|
772
|
+
end
|
|
773
|
+
|
|
737
774
|
@chunked_content_length += @body.write(str)
|
|
738
775
|
end
|
|
739
776
|
|
|
@@ -783,11 +820,19 @@ module Puma
|
|
|
783
820
|
while !io.eof?
|
|
784
821
|
line = io.gets
|
|
785
822
|
if line.end_with?(CHUNK_VALID_ENDING)
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
823
|
+
header = line.b.byteslice(0, line.bytesize - CHUNK_VALID_ENDING_SIZE)
|
|
824
|
+
if semi = header.index(';')
|
|
825
|
+
chunk_hex = header.byteslice(0, semi).sub(/[ \t]+\z/, '')
|
|
826
|
+
chunk_ext = header.byteslice(semi, header.bytesize - semi)
|
|
827
|
+
unless CHUNK_EXT_VALID.match?(chunk_ext)
|
|
828
|
+
raise HttpParserError, "Invalid chunk extension"
|
|
829
|
+
end
|
|
830
|
+
else
|
|
831
|
+
chunk_hex = header
|
|
832
|
+
end
|
|
833
|
+
|
|
834
|
+
unless CHUNK_SIZE_VALID.match?(chunk_hex)
|
|
835
|
+
err_msg = !chunk_hex.empty? ?
|
|
791
836
|
"Invalid chunk size: '#{chunk_hex}'" :
|
|
792
837
|
"Chunk size cannot be empty or nil"
|
|
793
838
|
raise HttpParserError, err_msg
|
|
@@ -842,6 +887,8 @@ module Puma
|
|
|
842
887
|
write_chunk(part[0..-2])
|
|
843
888
|
@partial_part_left = len - part.size
|
|
844
889
|
end
|
|
890
|
+
elsif line.end_with?("\n")
|
|
891
|
+
raise HttpParserError, "Invalid chunk header: bare LF"
|
|
845
892
|
else
|
|
846
893
|
if @prev_chunk.size + line.size >= MAX_CHUNK_HEADER_SIZE
|
|
847
894
|
raise HttpParserError, "maximum size of chunk header exceeded"
|
|
@@ -860,10 +907,21 @@ module Puma
|
|
|
860
907
|
# CRLF*2. Puma does not surface trailers, so we skip past them. Bytes
|
|
861
908
|
# past the terminator belong to the next pipelined request.
|
|
862
909
|
def finish_last_chunk(rest)
|
|
910
|
+
rest = rest.b
|
|
863
911
|
if rest.start_with?(CHUNK_VALID_ENDING)
|
|
864
912
|
trailer_end = CHUNK_VALID_ENDING_SIZE
|
|
913
|
+
elsif rest.start_with?("\n")
|
|
914
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
915
|
+
raise HttpParserError, "Invalid chunk trailer: bare LF"
|
|
865
916
|
elsif idx = rest.index(CHUNK_VALID_ENDING * 2)
|
|
917
|
+
if BARE_LF.match?(rest.byteslice(0, idx))
|
|
918
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
919
|
+
raise HttpParserError, "Invalid chunk trailer: bare LF"
|
|
920
|
+
end
|
|
866
921
|
trailer_end = idx + CHUNK_VALID_ENDING_SIZE * 2
|
|
922
|
+
elsif BARE_LF.match?(rest)
|
|
923
|
+
@env[HTTP_CONNECTION] = CLOSE
|
|
924
|
+
raise HttpParserError, "Invalid chunk trailer: bare LF"
|
|
867
925
|
elsif rest.bytesize < MAX_CHUNK_HEADER_SIZE
|
|
868
926
|
@prev_chunk = rest
|
|
869
927
|
return false
|
data/lib/puma/client_env.rb
CHANGED
|
@@ -13,25 +13,23 @@ module Puma
|
|
|
13
13
|
include Puma::Const
|
|
14
14
|
|
|
15
15
|
def validate_env
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
unless @env.key? HTTP_HOST
|
|
16
|
+
if @env[REQUEST_URI] == '*' && @env[REQUEST_METHOD] != 'OPTIONS'
|
|
19
17
|
@error_status_code = 400
|
|
20
18
|
@env[HTTP_CONNECTION] = CLOSE
|
|
21
|
-
raise HttpParserError, "
|
|
19
|
+
raise HttpParserError, "Invalid request-target '*' for #{@env[REQUEST_METHOD]}"
|
|
22
20
|
end
|
|
23
21
|
|
|
24
|
-
if @env[
|
|
22
|
+
return if @env[SERVER_PROTOCOL] == HTTP_10
|
|
23
|
+
|
|
24
|
+
unless @env.key?(HTTP_HOST)
|
|
25
25
|
@error_status_code = 400
|
|
26
26
|
@env[HTTP_CONNECTION] = CLOSE
|
|
27
|
-
raise HttpParserError, "
|
|
27
|
+
raise HttpParserError, "Missing Host header"
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
# Given a Hash
|
|
31
|
+
# Given a Hash +@env+ for the request, add
|
|
32
32
|
# and fixup keys to comply with Rack's env guidelines.
|
|
33
|
-
# @param env [Hash] see Puma::Client#env, from request
|
|
34
|
-
# @param client [Puma::Client] only needed for Client#peerip
|
|
35
33
|
#
|
|
36
34
|
def normalize_env
|
|
37
35
|
if host = @env[HTTP_HOST]
|
|
@@ -110,7 +108,8 @@ module Puma
|
|
|
110
108
|
@env[HIJACK_P] = true
|
|
111
109
|
@env[HIJACK] = method(:full_hijack).to_proc
|
|
112
110
|
|
|
113
|
-
|
|
111
|
+
input = @body.nil? || @body.equal?(Client::EmptyBody) ? @empty_rack_input : @body
|
|
112
|
+
@env[RACK_INPUT] = input if input
|
|
114
113
|
@env[RACK_URL_SCHEME] ||= default_server_port == PORT_443 ? HTTPS : HTTP
|
|
115
114
|
end
|
|
116
115
|
|
|
@@ -126,7 +125,6 @@ module Puma
|
|
|
126
125
|
# ignore the `,` version. This prevents clobbering headers managed by
|
|
127
126
|
# proxies but not by clients (Like X-Forwarded-For).
|
|
128
127
|
#
|
|
129
|
-
# @param env [Hash] see Puma::Client#env, from request, modifies in place
|
|
130
128
|
# @version 5.0.3
|
|
131
129
|
#
|
|
132
130
|
def req_env_post_parse
|
data/lib/puma/cluster/worker.rb
CHANGED
|
@@ -244,12 +244,6 @@ module Puma
|
|
|
244
244
|
new_worker.run
|
|
245
245
|
end
|
|
246
246
|
|
|
247
|
-
if !pid
|
|
248
|
-
log "! Complete inability to spawn new workers detected"
|
|
249
|
-
log "! Seppuku is the only choice."
|
|
250
|
-
exit! 1
|
|
251
|
-
end
|
|
252
|
-
|
|
253
247
|
@worker_pids << pid
|
|
254
248
|
@config.run_hooks(:after_worker_fork, idx, @log_writer, @hook_data)
|
|
255
249
|
pid
|