puma 4.3.10 → 5.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puma might be problematic. Click here for more details.

Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +58 -31
  3. data/LICENSE +23 -20
  4. data/README.md +17 -11
  5. data/docs/deployment.md +3 -1
  6. data/docs/fork_worker.md +31 -0
  7. data/docs/jungle/README.md +13 -0
  8. data/{tools → docs}/jungle/rc.d/README.md +0 -0
  9. data/{tools → docs}/jungle/rc.d/puma +0 -0
  10. data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
  11. data/{tools → docs}/jungle/upstart/README.md +0 -0
  12. data/{tools → docs}/jungle/upstart/puma-manager.conf +0 -0
  13. data/{tools → docs}/jungle/upstart/puma.conf +0 -0
  14. data/docs/signals.md +1 -0
  15. data/docs/systemd.md +1 -63
  16. data/ext/puma_http11/PumaHttp11Service.java +2 -4
  17. data/ext/puma_http11/extconf.rb +3 -2
  18. data/ext/puma_http11/http11_parser.c +11 -26
  19. data/ext/puma_http11/http11_parser.rl +1 -3
  20. data/ext/puma_http11/http11_parser_common.rl +1 -1
  21. data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
  22. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +46 -48
  23. data/ext/puma_http11/puma_http11.c +2 -38
  24. data/lib/puma/app/status.rb +16 -5
  25. data/lib/puma/binder.rb +62 -60
  26. data/lib/puma/cli.rb +7 -15
  27. data/lib/puma/client.rb +35 -32
  28. data/lib/puma/cluster.rb +179 -74
  29. data/lib/puma/configuration.rb +30 -42
  30. data/lib/puma/const.rb +2 -3
  31. data/lib/puma/control_cli.rb +27 -17
  32. data/lib/puma/detect.rb +8 -0
  33. data/lib/puma/dsl.rb +70 -34
  34. data/lib/puma/io_buffer.rb +9 -2
  35. data/lib/puma/jruby_restart.rb +0 -58
  36. data/lib/puma/launcher.rb +41 -29
  37. data/lib/puma/minissl.rb +13 -8
  38. data/lib/puma/null_io.rb +1 -1
  39. data/lib/puma/plugin.rb +1 -10
  40. data/lib/puma/rack/builder.rb +0 -4
  41. data/lib/puma/reactor.rb +6 -1
  42. data/lib/puma/runner.rb +5 -34
  43. data/lib/puma/server.rb +70 -190
  44. data/lib/puma/single.rb +7 -64
  45. data/lib/puma/state_file.rb +5 -2
  46. data/lib/puma/thread_pool.rb +85 -47
  47. data/lib/puma.rb +4 -0
  48. data/lib/rack/handler/puma.rb +1 -3
  49. data/tools/{docker/Dockerfile → Dockerfile} +0 -0
  50. metadata +22 -26
  51. data/docs/tcp_mode.md +0 -96
  52. data/ext/puma_http11/io_buffer.c +0 -155
  53. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +0 -72
  54. data/lib/puma/tcp_logger.rb +0 -41
  55. data/tools/jungle/README.md +0 -19
  56. data/tools/jungle/init.d/README.md +0 -61
  57. data/tools/jungle/init.d/puma +0 -421
  58. data/tools/jungle/init.d/run-puma +0 -18
@@ -34,9 +34,9 @@ private static short[] init__puma_parser_key_offsets_0()
34
34
  {
35
35
  return new short [] {
36
36
  0, 0, 8, 17, 27, 29, 30, 31, 32, 33, 34, 36,
37
- 39, 41, 44, 45, 61, 62, 78, 85, 91, 99, 107, 117,
38
- 125, 134, 142, 150, 159, 168, 177, 186, 195, 204, 213, 222,
39
- 231, 240, 249, 258, 267, 276, 285, 294, 303, 312, 313
37
+ 39, 41, 44, 45, 61, 62, 78, 80, 81, 89, 97, 107,
38
+ 115, 124, 132, 140, 149, 158, 167, 176, 185, 194, 203, 212,
39
+ 221, 230, 239, 248, 257, 266, 275, 284, 293, 302, 303
40
40
  };
41
41
  }
42
42
 
@@ -52,27 +52,26 @@ private static char[] init__puma_parser_trans_keys_0()
52
52
  46, 48, 57, 48, 57, 13, 48, 57, 10, 13, 33, 124,
53
53
  126, 35, 39, 42, 43, 45, 46, 48, 57, 65, 90, 94,
54
54
  122, 10, 33, 58, 124, 126, 35, 39, 42, 43, 45, 46,
55
- 48, 57, 65, 90, 94, 122, 13, 32, 127, 0, 8, 10,
56
- 31, 13, 127, 0, 8, 10, 31, 32, 60, 62, 127, 0,
57
- 31, 34, 35, 32, 60, 62, 127, 0, 31, 34, 35, 43,
58
- 58, 45, 46, 48, 57, 65, 90, 97, 122, 32, 34, 35,
59
- 60, 62, 127, 0, 31, 32, 34, 35, 60, 62, 63, 127,
60
- 0, 31, 32, 34, 35, 60, 62, 127, 0, 31, 32, 34,
61
- 35, 60, 62, 127, 0, 31, 32, 36, 95, 45, 46, 48,
62
- 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90,
63
- 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95,
64
- 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48,
65
- 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90,
66
- 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95,
67
- 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48,
68
- 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90,
69
- 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95,
70
- 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48,
71
- 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90,
72
- 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95,
73
- 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48,
74
- 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90,
75
- 32, 0
55
+ 48, 57, 65, 90, 94, 122, 13, 32, 13, 32, 60, 62,
56
+ 127, 0, 31, 34, 35, 32, 60, 62, 127, 0, 31, 34,
57
+ 35, 43, 58, 45, 46, 48, 57, 65, 90, 97, 122, 32,
58
+ 34, 35, 60, 62, 127, 0, 31, 32, 34, 35, 60, 62,
59
+ 63, 127, 0, 31, 32, 34, 35, 60, 62, 127, 0, 31,
60
+ 32, 34, 35, 60, 62, 127, 0, 31, 32, 36, 95, 45,
61
+ 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57,
62
+ 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32,
63
+ 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45,
64
+ 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57,
65
+ 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32,
66
+ 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45,
67
+ 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57,
68
+ 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32,
69
+ 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45,
70
+ 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57,
71
+ 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32,
72
+ 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45,
73
+ 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57,
74
+ 65, 90, 32, 0
76
75
  };
77
76
  }
78
77
 
@@ -83,7 +82,7 @@ private static byte[] init__puma_parser_single_lengths_0()
83
82
  {
84
83
  return new byte [] {
85
84
  0, 2, 3, 4, 2, 1, 1, 1, 1, 1, 0, 1,
86
- 0, 1, 1, 4, 1, 4, 3, 2, 4, 4, 2, 6,
85
+ 0, 1, 1, 4, 1, 4, 2, 1, 4, 4, 2, 6,
87
86
  7, 6, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3,
88
87
  3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 0
89
88
  };
@@ -96,7 +95,7 @@ private static byte[] init__puma_parser_range_lengths_0()
96
95
  {
97
96
  return new byte [] {
98
97
  0, 3, 3, 3, 0, 0, 0, 0, 0, 0, 1, 1,
99
- 1, 1, 0, 6, 0, 6, 2, 2, 2, 2, 4, 1,
98
+ 1, 1, 0, 6, 0, 6, 0, 0, 2, 2, 4, 1,
100
99
  1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3,
101
100
  3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0
102
101
  };
@@ -109,9 +108,9 @@ private static short[] init__puma_parser_index_offsets_0()
109
108
  {
110
109
  return new short [] {
111
110
  0, 0, 6, 13, 21, 24, 26, 28, 30, 32, 34, 36,
112
- 39, 41, 44, 46, 57, 59, 70, 76, 81, 88, 95, 102,
113
- 110, 119, 127, 135, 142, 149, 156, 163, 170, 177, 184, 191,
114
- 198, 205, 212, 219, 226, 233, 240, 247, 254, 261, 263
111
+ 39, 41, 44, 46, 57, 59, 70, 73, 75, 82, 89, 96,
112
+ 104, 113, 121, 129, 136, 143, 150, 157, 164, 171, 178, 185,
113
+ 192, 199, 206, 213, 220, 227, 234, 241, 248, 255, 257
115
114
  };
116
115
  }
117
116
 
@@ -127,23 +126,22 @@ private static byte[] init__puma_parser_indicies_0()
127
126
  16, 15, 1, 17, 1, 18, 17, 1, 19, 1, 20, 21,
128
127
  21, 21, 21, 21, 21, 21, 21, 21, 1, 22, 1, 23,
129
128
  24, 23, 23, 23, 23, 23, 23, 23, 23, 1, 26, 27,
130
- 1, 1, 1, 25, 29, 1, 1, 1, 28, 30, 1, 1,
131
- 1, 1, 1, 31, 32, 1, 1, 1, 1, 1, 33, 34,
132
- 35, 34, 34, 34, 34, 1, 8, 1, 9, 1, 1, 1,
133
- 1, 35, 36, 1, 38, 1, 1, 39, 1, 1, 37, 40,
134
- 1, 42, 1, 1, 1, 1, 41, 43, 1, 45, 1, 1,
135
- 1, 1, 44, 2, 46, 46, 46, 46, 46, 1, 2, 47,
136
- 47, 47, 47, 47, 1, 2, 48, 48, 48, 48, 48, 1,
137
- 2, 49, 49, 49, 49, 49, 1, 2, 50, 50, 50, 50,
138
- 50, 1, 2, 51, 51, 51, 51, 51, 1, 2, 52, 52,
139
- 52, 52, 52, 1, 2, 53, 53, 53, 53, 53, 1, 2,
140
- 54, 54, 54, 54, 54, 1, 2, 55, 55, 55, 55, 55,
141
- 1, 2, 56, 56, 56, 56, 56, 1, 2, 57, 57, 57,
142
- 57, 57, 1, 2, 58, 58, 58, 58, 58, 1, 2, 59,
143
- 59, 59, 59, 59, 1, 2, 60, 60, 60, 60, 60, 1,
144
- 2, 61, 61, 61, 61, 61, 1, 2, 62, 62, 62, 62,
145
- 62, 1, 2, 63, 63, 63, 63, 63, 1, 2, 1, 1,
146
- 0
129
+ 25, 29, 28, 30, 1, 1, 1, 1, 1, 31, 32, 1,
130
+ 1, 1, 1, 1, 33, 34, 35, 34, 34, 34, 34, 1,
131
+ 8, 1, 9, 1, 1, 1, 1, 35, 36, 1, 38, 1,
132
+ 1, 39, 1, 1, 37, 40, 1, 42, 1, 1, 1, 1,
133
+ 41, 43, 1, 45, 1, 1, 1, 1, 44, 2, 46, 46,
134
+ 46, 46, 46, 1, 2, 47, 47, 47, 47, 47, 1, 2,
135
+ 48, 48, 48, 48, 48, 1, 2, 49, 49, 49, 49, 49,
136
+ 1, 2, 50, 50, 50, 50, 50, 1, 2, 51, 51, 51,
137
+ 51, 51, 1, 2, 52, 52, 52, 52, 52, 1, 2, 53,
138
+ 53, 53, 53, 53, 1, 2, 54, 54, 54, 54, 54, 1,
139
+ 2, 55, 55, 55, 55, 55, 1, 2, 56, 56, 56, 56,
140
+ 56, 1, 2, 57, 57, 57, 57, 57, 1, 2, 58, 58,
141
+ 58, 58, 58, 1, 2, 59, 59, 59, 59, 59, 1, 2,
142
+ 60, 60, 60, 60, 60, 1, 2, 61, 61, 61, 61, 61,
143
+ 1, 2, 62, 62, 62, 62, 62, 1, 2, 63, 63, 63,
144
+ 63, 63, 1, 2, 1, 1, 0
147
145
  };
148
146
  }
149
147
 
@@ -219,7 +217,7 @@ static final int puma_parser_en_main = 1;
219
217
  cs = puma_parser_start;
220
218
  }
221
219
 
222
- // line 88 "ext/puma_http11/http11_parser.java.rl"
220
+ // line 90 "ext/puma_http11/http11_parser.java.rl"
223
221
 
224
222
  body_start = 0;
225
223
  content_len = 0;
@@ -422,7 +420,7 @@ case 5:
422
420
  break; }
423
421
  }
424
422
 
425
- // line 114 "ext/puma_http11/http11_parser.java.rl"
423
+ // line 116 "ext/puma_http11/http11_parser.java.rl"
426
424
 
427
425
  parser.cs = cs;
428
426
  parser.nread += (p - off);
@@ -54,7 +54,7 @@ DEF_MAX_LENGTH(FIELD_NAME, 256);
54
54
  DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
55
55
  DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12);
56
56
  DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
57
- DEF_MAX_LENGTH(REQUEST_PATH, 2048);
57
+ DEF_MAX_LENGTH(REQUEST_PATH, 8196);
58
58
  DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10));
59
59
  DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32)));
60
60
 
@@ -112,21 +112,6 @@ static struct common_field common_http_fields[] = {
112
112
  # undef f
113
113
  };
114
114
 
115
- /*
116
- * qsort(3) and bsearch(3) improve average performance slightly, but may
117
- * not be worth it for lack of portability to certain platforms...
118
- */
119
- #if defined(HAVE_QSORT_BSEARCH)
120
- /* sort by length, then by name if there's a tie */
121
- static int common_field_cmp(const void *a, const void *b)
122
- {
123
- struct common_field *cfa = (struct common_field *)a;
124
- struct common_field *cfb = (struct common_field *)b;
125
- signed long diff = cfa->len - cfb->len;
126
- return diff ? diff : memcmp(cfa->name, cfb->name, cfa->len);
127
- }
128
- #endif /* HAVE_QSORT_BSEARCH */
129
-
130
115
  static void init_common_fields(void)
131
116
  {
132
117
  unsigned i;
@@ -143,28 +128,10 @@ static void init_common_fields(void)
143
128
  }
144
129
  rb_global_variable(&cf->value);
145
130
  }
146
-
147
- #if defined(HAVE_QSORT_BSEARCH)
148
- qsort(common_http_fields,
149
- ARRAY_SIZE(common_http_fields),
150
- sizeof(struct common_field),
151
- common_field_cmp);
152
- #endif /* HAVE_QSORT_BSEARCH */
153
131
  }
154
132
 
155
133
  static VALUE find_common_field_value(const char *field, size_t flen)
156
134
  {
157
- #if defined(HAVE_QSORT_BSEARCH)
158
- struct common_field key;
159
- struct common_field *found;
160
- key.name = field;
161
- key.len = (signed long)flen;
162
- found = (struct common_field *)bsearch(&key, common_http_fields,
163
- ARRAY_SIZE(common_http_fields),
164
- sizeof(struct common_field),
165
- common_field_cmp);
166
- return found ? found->value : Qnil;
167
- #else /* !HAVE_QSORT_BSEARCH */
168
135
  unsigned i;
169
136
  struct common_field *cf = common_http_fields;
170
137
  for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) {
@@ -172,7 +139,6 @@ static VALUE find_common_field_value(const char *field, size_t flen)
172
139
  return cf->value;
173
140
  }
174
141
  return Qnil;
175
- #endif /* !HAVE_QSORT_BSEARCH */
176
142
  }
177
143
 
178
144
  void http_field(puma_parser* hp, const char *field, size_t flen,
@@ -401,7 +367,7 @@ VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
401
367
  VALIDATE_MAX_LENGTH(puma_parser_nread(http), HEADER);
402
368
 
403
369
  if(puma_parser_has_error(http)) {
404
- rb_raise(eHttpParserError, "%s", "Invalid HTTP format, parsing fails.");
370
+ rb_raise(eHttpParserError, "%s", "Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?");
405
371
  } else {
406
372
  return INT2FIX(puma_parser_nread(http));
407
373
  }
@@ -468,7 +434,6 @@ VALUE HttpParser_body(VALUE self) {
468
434
  return http->body;
469
435
  }
470
436
 
471
- void Init_io_buffer(VALUE puma);
472
437
  void Init_mini_ssl(VALUE mod);
473
438
 
474
439
  void Init_puma_http11()
@@ -498,6 +463,5 @@ void Init_puma_http11()
498
463
  rb_define_method(cHttpParser, "body", HttpParser_body, 0);
499
464
  init_common_fields();
500
465
 
501
- Init_io_buffer(mPuma);
502
466
  Init_mini_ssl(mPuma);
503
467
  }
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
4
+
3
5
  module Puma
4
6
  module App
5
7
  # Check out {#call}'s source code to see what actions this web application
@@ -17,10 +19,6 @@ module Puma
17
19
  return rack_response(403, 'Invalid auth token', 'text/plain')
18
20
  end
19
21
 
20
- if env['PATH_INFO'] =~ /\/(gc-stats|stats|thread-backtraces)$/
21
- require 'json'
22
- end
23
-
24
22
  case env['PATH_INFO']
25
23
  when /\/stop$/
26
24
  @cli.stop
@@ -56,7 +54,20 @@ module Puma
56
54
  rack_response(200, GC.stat.to_json)
57
55
 
58
56
  when /\/stats$/
59
- rack_response(200, @cli.stats)
57
+ rack_response(200, @cli.stats.to_json)
58
+
59
+ when /\/thread-backtraces$/
60
+ backtraces = []
61
+ @cli.thread_status do |name, backtrace|
62
+ backtraces << { name: name, backtrace: backtrace }
63
+ end
64
+
65
+ rack_response(200, backtraces.to_json)
66
+
67
+ when /\/refork$/
68
+ Process.kill "SIGURG", $$
69
+ rack_response(200, OK_STATUS)
70
+
60
71
  else
61
72
  rack_response 404, "Unsupported action", 'text/plain'
62
73
  end
data/lib/puma/binder.rb CHANGED
@@ -11,7 +11,7 @@ module Puma
11
11
  class Binder
12
12
  include Puma::Const
13
13
 
14
- RACK_VERSION = [1,3].freeze
14
+ RACK_VERSION = [1,6].freeze
15
15
 
16
16
  def initialize(events)
17
17
  @events = events
@@ -43,7 +43,8 @@ module Puma
43
43
  @ios = []
44
44
  end
45
45
 
46
- attr_reader :ios
46
+ attr_reader :ios, :listeners, :unix_paths, :proto_env, :envs, :activated_sockets, :inherited_fds
47
+ attr_writer :ios, :listeners
47
48
 
48
49
  def env(sock)
49
50
  @envs.fetch(sock, @proto_env)
@@ -53,40 +54,39 @@ module Puma
53
54
  @ios.each { |i| i.close }
54
55
  end
55
56
 
56
- def import_from_env
57
- remove = []
58
-
59
- ENV.each do |k,v|
60
- if k =~ /PUMA_INHERIT_\d+/
61
- fd, url = v.split(":", 2)
62
- @inherited_fds[url] = fd.to_i
63
- remove << k
64
- elsif k == 'LISTEN_FDS' && ENV['LISTEN_PID'].to_i == $$
65
- v.to_i.times do |num|
66
- fd = num + 3
67
- sock = TCPServer.for_fd(fd)
68
- begin
69
- key = [ :unix, Socket.unpack_sockaddr_un(sock.getsockname) ]
70
- rescue ArgumentError
71
- port, addr = Socket.unpack_sockaddr_in(sock.getsockname)
72
- if addr =~ /\:/
73
- addr = "[#{addr}]"
74
- end
75
- key = [ :tcp, addr, port ]
76
- end
77
- @activated_sockets[key] = sock
78
- @events.debug "Registered #{key.join ':'} for activation from LISTEN_FDS"
79
- end
80
- remove << k << 'LISTEN_PID'
81
- end
82
- end
57
+ def connected_ports
58
+ ios.map { |io| io.addr[1] }.uniq
59
+ end
60
+
61
+ def create_inherited_fds(env_hash)
62
+ env_hash.select {|k,v| k =~ /PUMA_INHERIT_\d+/}.each do |_k, v|
63
+ fd, url = v.split(":", 2)
64
+ @inherited_fds[url] = fd.to_i
65
+ end.keys # pass keys back for removal
66
+ end
83
67
 
84
- remove.each do |k|
85
- ENV.delete k
68
+ # systemd socket activation.
69
+ # LISTEN_FDS = number of listening sockets. e.g. 2 means accept on 2 sockets w/descriptors 3 and 4.
70
+ # LISTEN_PID = PID of the service process, aka us
71
+ # see https://www.freedesktop.org/software/systemd/man/systemd-socket-activate.html
72
+ def create_activated_fds(env_hash)
73
+ return [] unless env_hash['LISTEN_FDS'] && env_hash['LISTEN_PID'].to_i == $$
74
+ env_hash['LISTEN_FDS'].to_i.times do |index|
75
+ sock = TCPServer.for_fd(socket_activation_fd(index))
76
+ key = begin # Try to parse as a path
77
+ [:unix, Socket.unpack_sockaddr_un(sock.getsockname)]
78
+ rescue ArgumentError # Try to parse as a port/ip
79
+ port, addr = Socket.unpack_sockaddr_in(sock.getsockname)
80
+ addr = "[#{addr}]" if addr =~ /\:/
81
+ [:tcp, addr, port]
82
+ end
83
+ @activated_sockets[key] = sock
84
+ @events.debug "Registered #{key.join ':'} for activation from LISTEN_FDS"
86
85
  end
86
+ ["LISTEN_FDS", "LISTEN_PID"] # Signal to remove these keys from ENV
87
87
  end
88
88
 
89
- def parse(binds, logger)
89
+ def parse(binds, logger, log_msg = 'Listening')
90
90
  binds.each do |str|
91
91
  uri = URI.parse str
92
92
  case uri.scheme
@@ -113,7 +113,7 @@ module Puma
113
113
  i.local_address.ip_unpack.join(':')
114
114
  end
115
115
 
116
- logger.log "* Listening on tcp://#{addr}"
116
+ logger.log "* #{log_msg} on tcp://#{addr}"
117
117
  end
118
118
  end
119
119
 
@@ -149,7 +149,7 @@ module Puma
149
149
  end
150
150
 
151
151
  io = add_unix_listener path, umask, mode, backlog
152
- logger.log "* Listening on #{str}"
152
+ logger.log "* #{log_msg} on #{str}"
153
153
  end
154
154
 
155
155
  @listeners << [str, io]
@@ -204,12 +204,6 @@ module Puma
204
204
  end
205
205
  end
206
206
 
207
- def loopback_addresses
208
- Socket.ip_address_list.select do |addrinfo|
209
- addrinfo.ipv6_loopback? || addrinfo.ipv4_loopback?
210
- end.map { |addrinfo| addrinfo.ip_address }.uniq
211
- end
212
-
213
207
  # Tell the server to listen on host +host+, port +port+.
214
208
  # If +optimize_for_latency+ is true (the default) then clients connecting
215
209
  # will be optimized for latency over throughput.
@@ -226,20 +220,17 @@ module Puma
226
220
  end
227
221
 
228
222
  host = host[1..-2] if host and host[0..0] == '['
229
- s = TCPServer.new(host, port)
223
+ tcp_server = TCPServer.new(host, port)
230
224
  if optimize_for_latency
231
- s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
225
+ tcp_server.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
232
226
  end
233
- s.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
234
- s.listen backlog
235
- @connected_port = s.addr[1]
227
+ tcp_server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
228
+ tcp_server.listen backlog
236
229
 
237
- @ios << s
238
- s
230
+ @ios << tcp_server
231
+ tcp_server
239
232
  end
240
233
 
241
- attr_reader :connected_port
242
-
243
234
  def inherit_tcp_listener(host, port, fd)
244
235
  if fd.kind_of? TCPServer
245
236
  s = fd
@@ -360,26 +351,37 @@ module Puma
360
351
  end
361
352
 
362
353
  def close_listeners
363
- @listeners.each do |l, io|
364
- io.close
354
+ listeners.each do |l, io|
355
+ io.close unless io.closed? # Ruby 2.2 issue
365
356
  uri = URI.parse(l)
366
357
  next unless uri.scheme == 'unix'
367
358
  unix_path = "#{uri.host}#{uri.path}"
368
- File.unlink unix_path if @unix_paths.include? unix_path
359
+ File.unlink unix_path if unix_paths.include? unix_path
369
360
  end
370
361
  end
371
362
 
372
- def close_unix_paths
373
- @unix_paths.each { |up| File.unlink(up) if File.exist? up }
363
+ def redirects_for_restart
364
+ redirects = listeners.map { |a| [a[1].to_i, a[1].to_i] }.to_h
365
+ redirects[:close_others] = true
366
+ redirects
374
367
  end
375
368
 
376
- def redirects_for_restart
377
- redirects = {:close_others => true}
378
- @listeners.each_with_index do |(l, io), i|
379
- ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
380
- redirects[io.to_i] = io.to_i
369
+ def redirects_for_restart_env
370
+ listeners.each_with_object({}).with_index do |(listen, memo), i|
371
+ memo["PUMA_INHERIT_#{i}"] = "#{listen[1].to_i}:#{listen[0]}"
381
372
  end
382
- redirects
373
+ end
374
+
375
+ private
376
+
377
+ def loopback_addresses
378
+ Socket.ip_address_list.select do |addrinfo|
379
+ addrinfo.ipv6_loopback? || addrinfo.ipv4_loopback?
380
+ end.map { |addrinfo| addrinfo.ip_address }.uniq
381
+ end
382
+
383
+ def socket_activation_fd(int)
384
+ int + 3 # 3 is the magic number you add to follow the SA protocol
383
385
  end
384
386
  end
385
387
  end
data/lib/puma/cli.rb CHANGED
@@ -80,7 +80,7 @@ module Puma
80
80
  @launcher.run
81
81
  end
82
82
 
83
- private
83
+ private
84
84
  def unsupported(str)
85
85
  @events.error(str)
86
86
  raise UnsupportedOption
@@ -112,21 +112,11 @@ module Puma
112
112
  configure_control_url(arg)
113
113
  end
114
114
 
115
- # alias --control-url for backwards-compatibility
116
- o.on "--control URL", "DEPRECATED alias for --control-url" do |arg|
117
- configure_control_url(arg)
118
- end
119
-
120
115
  o.on "--control-token TOKEN",
121
116
  "The token to use as authentication for the control server" do |arg|
122
117
  @control_options[:auth_token] = arg
123
118
  end
124
119
 
125
- o.on "-d", "--daemon", "Daemonize the server into the background" do
126
- user_config.daemonize
127
- user_config.quiet
128
- end
129
-
130
120
  o.on "--debug", "Log lowlevel debugging information" do
131
121
  user_config.debug
132
122
  end
@@ -140,6 +130,12 @@ module Puma
140
130
  user_config.environment arg
141
131
  end
142
132
 
133
+ o.on "-f", "--fork-worker=[REQUESTS]", OptionParser::DecimalInteger,
134
+ "Fork new workers from existing worker. Cluster mode only",
135
+ "Auto-refork after REQUESTS (default 1000)" do |*args|
136
+ user_config.fork_worker(*args.compact)
137
+ end
138
+
143
139
  o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
144
140
  $LOAD_PATH.unshift(*arg.split(':'))
145
141
  end
@@ -192,10 +188,6 @@ module Puma
192
188
  end
193
189
  end
194
190
 
195
- o.on "--tcp-mode", "Run the app in raw TCP mode instead of HTTP mode" do
196
- user_config.tcp_mode!
197
- end
198
-
199
191
  o.on "--early-hints", "Enable early hints support" do
200
192
  user_config.early_hints
201
193
  end
data/lib/puma/client.rb CHANGED
@@ -238,12 +238,23 @@ module Puma
238
238
  return false unless IO.select([@to_io], nil, nil, 0)
239
239
  try_to_finish
240
240
  end
241
+
242
+ # For documentation, see https://github.com/puma/puma/issues/1754
243
+ send(:alias_method, :jruby_eagerly_finish, :eagerly_finish)
241
244
  end # IS_JRUBY
242
245
 
243
- def finish
246
+ def finish(timeout)
244
247
  return true if @ready
245
248
  until try_to_finish
246
- IO.select([@to_io], nil, nil)
249
+ can_read = begin
250
+ IO.select([@to_io], nil, nil, timeout)
251
+ rescue ThreadPool::ForceShutdown
252
+ nil
253
+ end
254
+ unless can_read
255
+ write_error(408) if in_data_phase
256
+ raise ConnectionError
257
+ end
247
258
  end
248
259
  true
249
260
  end
@@ -259,7 +270,7 @@ module Puma
259
270
  return @peerip if @peerip
260
271
 
261
272
  if @remote_addr_header
262
- hdr = (@env[@remote_addr_header] || LOCALHOST_ADDR).split(/[\s,]/).first
273
+ hdr = (@env[@remote_addr_header] || LOCALHOST_IP).split(/[\s,]/).first
263
274
  @peerip = hdr
264
275
  return hdr
265
276
  end
@@ -267,6 +278,18 @@ module Puma
267
278
  @peerip ||= @io.peeraddr.last
268
279
  end
269
280
 
281
+ # Returns true if the persistent connection can be closed immediately
282
+ # without waiting for the configured idle/shutdown timeout.
283
+ def can_close?
284
+ # Allow connection to close if it's received at least one full request
285
+ # and hasn't received any data for a future request.
286
+ #
287
+ # From RFC 2616 section 8.1.4:
288
+ # Servers SHOULD always respond to at least one request per connection,
289
+ # if at all possible.
290
+ @requests_served > 0 && @parsed_bytes == 0
291
+ end
292
+
270
293
  private
271
294
 
272
295
  def setup_body
@@ -285,16 +308,8 @@ module Puma
285
308
 
286
309
  te = @env[TRANSFER_ENCODING2]
287
310
 
288
- if te
289
- if te.include?(",")
290
- te.split(",").each do |part|
291
- if CHUNKED.casecmp(part.strip) == 0
292
- return setup_chunked_body(body)
293
- end
294
- end
295
- elsif CHUNKED.casecmp(te) == 0
296
- return setup_chunked_body(body)
297
- end
311
+ if te && CHUNKED.casecmp(te) == 0
312
+ return setup_chunked_body(body)
298
313
  end
299
314
 
300
315
  @chunked_body = false
@@ -397,10 +412,7 @@ module Puma
397
412
  raise EOFError
398
413
  end
399
414
 
400
- if decode_chunk(chunk)
401
- @env[CONTENT_LENGTH] = @chunked_content_length
402
- return true
403
- end
415
+ return true if decode_chunk(chunk)
404
416
  end
405
417
  end
406
418
 
@@ -413,28 +425,19 @@ module Puma
413
425
  @body.binmode
414
426
  @tempfile = @body
415
427
 
416
- @chunked_content_length = 0
417
-
418
- if decode_chunk(body)
419
- @env[CONTENT_LENGTH] = @chunked_content_length
420
- return true
421
- end
422
- end
423
-
424
- def write_chunk(str)
425
- @chunked_content_length += @body.write(str)
428
+ return decode_chunk(body)
426
429
  end
427
430
 
428
431
  def decode_chunk(chunk)
429
432
  if @partial_part_left > 0
430
433
  if @partial_part_left <= chunk.size
431
434
  if @partial_part_left > 2
432
- write_chunk(chunk[0..(@partial_part_left-3)]) # skip the \r\n
435
+ @body << chunk[0..(@partial_part_left-3)] # skip the \r\n
433
436
  end
434
437
  chunk = chunk[@partial_part_left..-1]
435
438
  @partial_part_left = 0
436
439
  else
437
- write_chunk(chunk) if @partial_part_left > 2 # don't include the last \r\n
440
+ @body << chunk if @partial_part_left > 2 # don't include the last \r\n
438
441
  @partial_part_left -= chunk.size
439
442
  return false
440
443
  end
@@ -481,12 +484,12 @@ module Puma
481
484
 
482
485
  case
483
486
  when got == len
484
- write_chunk(part[0..-3]) # to skip the ending \r\n
487
+ @body << part[0..-3] # to skip the ending \r\n
485
488
  when got <= len - 2
486
- write_chunk(part)
489
+ @body << part
487
490
  @partial_part_left = len - part.size
488
491
  when got == len - 1 # edge where we get just \r but not \n
489
- write_chunk(part[0..-2])
492
+ @body << part[0..-2]
490
493
  @partial_part_left = len - part.size
491
494
  end
492
495
  else