puma 3.5.0 → 3.12.0

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 (67) hide show
  1. checksums.yaml +5 -5
  2. data/{History.txt → History.md} +318 -75
  3. data/README.md +143 -227
  4. data/docs/architecture.md +36 -0
  5. data/{DEPLOYMENT.md → docs/deployment.md} +0 -0
  6. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  7. data/docs/images/puma-connection-flow.png +0 -0
  8. data/docs/images/puma-general-arch.png +0 -0
  9. data/docs/plugins.md +28 -0
  10. data/docs/restart.md +39 -0
  11. data/docs/signals.md +56 -3
  12. data/docs/systemd.md +124 -22
  13. data/ext/puma_http11/extconf.rb +2 -0
  14. data/ext/puma_http11/http11_parser.c +85 -84
  15. data/ext/puma_http11/http11_parser.h +1 -0
  16. data/ext/puma_http11/http11_parser.rl +10 -9
  17. data/ext/puma_http11/io_buffer.c +7 -7
  18. data/ext/puma_http11/mini_ssl.c +67 -6
  19. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +13 -16
  20. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +15 -2
  21. data/ext/puma_http11/puma_http11.c +1 -0
  22. data/lib/puma.rb +13 -5
  23. data/lib/puma/app/status.rb +8 -0
  24. data/lib/puma/binder.rb +22 -17
  25. data/lib/puma/cli.rb +49 -33
  26. data/lib/puma/client.rb +149 -4
  27. data/lib/puma/cluster.rb +54 -12
  28. data/lib/puma/commonlogger.rb +19 -20
  29. data/lib/puma/compat.rb +3 -7
  30. data/lib/puma/configuration.rb +133 -130
  31. data/lib/puma/const.rb +19 -37
  32. data/lib/puma/control_cli.rb +38 -35
  33. data/lib/puma/convenient.rb +3 -3
  34. data/lib/puma/detect.rb +3 -1
  35. data/lib/puma/dsl.rb +80 -58
  36. data/lib/puma/events.rb +6 -8
  37. data/lib/puma/io_buffer.rb +1 -1
  38. data/lib/puma/jruby_restart.rb +0 -1
  39. data/lib/puma/launcher.rb +61 -30
  40. data/lib/puma/minissl.rb +85 -4
  41. data/lib/puma/null_io.rb +6 -13
  42. data/lib/puma/plugin.rb +12 -1
  43. data/lib/puma/plugin/tmp_restart.rb +1 -2
  44. data/lib/puma/rack/builder.rb +3 -0
  45. data/lib/puma/rack/urlmap.rb +9 -8
  46. data/lib/puma/reactor.rb +135 -0
  47. data/lib/puma/runner.rb +27 -1
  48. data/lib/puma/server.rb +134 -32
  49. data/lib/puma/single.rb +17 -3
  50. data/lib/puma/thread_pool.rb +67 -20
  51. data/lib/puma/util.rb +1 -5
  52. data/lib/rack/handler/puma.rb +58 -17
  53. data/tools/jungle/README.md +12 -2
  54. data/tools/jungle/init.d/README.md +9 -2
  55. data/tools/jungle/init.d/puma +32 -62
  56. data/tools/jungle/init.d/run-puma +5 -1
  57. data/tools/jungle/rc.d/README.md +74 -0
  58. data/tools/jungle/rc.d/puma +61 -0
  59. data/tools/jungle/rc.d/puma.conf +10 -0
  60. data/tools/trickletest.rb +1 -1
  61. metadata +22 -92
  62. data/Gemfile +0 -13
  63. data/Manifest.txt +0 -77
  64. data/Rakefile +0 -158
  65. data/lib/puma/rack/backports/uri/common_18.rb +0 -59
  66. data/lib/puma/rack/backports/uri/common_192.rb +0 -55
  67. data/puma.gemspec +0 -52
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Copyright (c) 2005 Zed A. Shaw
3
3
  * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ * License 3-clause BSD
4
5
  */
5
6
 
6
7
  #ifndef http11_parser_h
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Copyright (c) 2005 Zed A. Shaw
3
3
  * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ * License 3-clause BSD
4
5
  */
5
6
  #include "http11_parser.h"
6
7
  #include <stdio.h>
@@ -28,7 +29,7 @@ static void snake_upcase_char(char *c)
28
29
  /** Machine **/
29
30
 
30
31
  %%{
31
-
32
+
32
33
  machine puma_parser;
33
34
 
34
35
  action mark { MARK(mark, fpc); }
@@ -36,7 +37,7 @@ static void snake_upcase_char(char *c)
36
37
 
37
38
  action start_field { MARK(field_start, fpc); }
38
39
  action snake_upcase_field { snake_upcase_char((char *)fpc); }
39
- action write_field {
40
+ action write_field {
40
41
  parser->field_len = LEN(field_start, fpc);
41
42
  }
42
43
 
@@ -44,10 +45,10 @@ static void snake_upcase_char(char *c)
44
45
  action write_value {
45
46
  parser->http_field(parser, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, fpc));
46
47
  }
47
- action request_method {
48
+ action request_method {
48
49
  parser->request_method(parser, PTR_TO(mark), LEN(mark, fpc));
49
50
  }
50
- action request_uri {
51
+ action request_uri {
51
52
  parser->request_uri(parser, PTR_TO(mark), LEN(mark, fpc));
52
53
  }
53
54
  action fragment {
@@ -55,11 +56,11 @@ static void snake_upcase_char(char *c)
55
56
  }
56
57
 
57
58
  action start_query { MARK(query_start, fpc); }
58
- action query_string {
59
+ action query_string {
59
60
  parser->query_string(parser, PTR_TO(query_start), LEN(query_start, fpc));
60
61
  }
61
62
 
62
- action http_version {
63
+ action http_version {
63
64
  parser->http_version(parser, PTR_TO(mark), LEN(mark, fpc));
64
65
  }
65
66
 
@@ -67,8 +68,8 @@ static void snake_upcase_char(char *c)
67
68
  parser->request_path(parser, PTR_TO(mark), LEN(mark,fpc));
68
69
  }
69
70
 
70
- action done {
71
- parser->body_start = fpc - buffer + 1;
71
+ action done {
72
+ parser->body_start = fpc - buffer + 1;
72
73
  parser->header_done(parser, fpc + 1, pe - fpc - 1);
73
74
  fbreak;
74
75
  }
@@ -108,7 +109,7 @@ size_t puma_parser_execute(puma_parser *parser, const char *buffer, size_t len,
108
109
  pe = buffer+len;
109
110
 
110
111
  /* assert(*pe == '\0' && "pointer does not end on NUL"); */
111
- assert(pe - p == len - off && "pointers aren't same distance");
112
+ assert((size_t) (pe - p) == len - off && "pointers aren't same distance");
112
113
 
113
114
  %% write exec;
114
115
 
@@ -14,8 +14,8 @@ struct buf_int {
14
14
  #define BUF_TOLERANCE 32
15
15
 
16
16
  static void buf_free(struct buf_int* internal) {
17
- free(internal->top);
18
- free(internal);
17
+ xfree(internal->top);
18
+ xfree(internal);
19
19
  }
20
20
 
21
21
  static VALUE buf_alloc(VALUE self) {
@@ -25,7 +25,7 @@ static VALUE buf_alloc(VALUE self) {
25
25
  buf = Data_Make_Struct(self, struct buf_int, 0, buf_free, internal);
26
26
 
27
27
  internal->size = BUF_DEFAULT_SIZE;
28
- internal->top = malloc(BUF_DEFAULT_SIZE);
28
+ internal->top = ALLOC_N(uint8_t, BUF_DEFAULT_SIZE);
29
29
  internal->cur = internal->top;
30
30
 
31
31
  return buf;
@@ -51,13 +51,13 @@ static VALUE buf_append(VALUE self, VALUE str) {
51
51
 
52
52
  new_size = (n > new_size ? n : new_size + BUF_TOLERANCE);
53
53
 
54
- top = malloc(new_size);
54
+ top = ALLOC_N(uint8_t, new_size);
55
55
  old = b->top;
56
56
  memcpy(top, old, used);
57
57
  b->top = top;
58
58
  b->cur = top + used;
59
59
  b->size = new_size;
60
- free(old);
60
+ xfree(old);
61
61
  }
62
62
 
63
63
  memcpy(b->cur, RSTRING_PTR(str), str_len);
@@ -92,13 +92,13 @@ static VALUE buf_append2(int argc, VALUE* argv, VALUE self) {
92
92
 
93
93
  new_size = (n > new_size ? n : new_size + BUF_TOLERANCE);
94
94
 
95
- top = malloc(new_size);
95
+ top = ALLOC_N(uint8_t, new_size);
96
96
  old = b->top;
97
97
  memcpy(top, old, used);
98
98
  b->top = top;
99
99
  b->cur = top + used;
100
100
  b->size = new_size;
101
- free(old);
101
+ xfree(old);
102
102
  }
103
103
 
104
104
  for(i = 0; i < argc; i++) {
@@ -87,6 +87,8 @@ DH *get_dh1024() {
87
87
 
88
88
  DH *dh;
89
89
  dh = DH_new();
90
+
91
+ #if OPENSSL_VERSION_NUMBER < 0x10100005L || defined(LIBRESSL_VERSION_NUMBER)
90
92
  dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
91
93
  dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
92
94
 
@@ -94,6 +96,18 @@ DH *get_dh1024() {
94
96
  DH_free(dh);
95
97
  return NULL;
96
98
  }
99
+ #else
100
+ BIGNUM *p, *g;
101
+ p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
102
+ g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
103
+
104
+ if (p == NULL || g == NULL || !DH_set0_pqg(dh, p, NULL, g)) {
105
+ DH_free(dh);
106
+ BN_free(p);
107
+ BN_free(g);
108
+ return NULL;
109
+ }
110
+ #endif
97
111
 
98
112
  return dh;
99
113
  }
@@ -134,15 +148,22 @@ VALUE engine_init_server(VALUE self, VALUE mini_ssl_ctx) {
134
148
  ID sym_key = rb_intern("key");
135
149
  VALUE key = rb_funcall(mini_ssl_ctx, sym_key, 0);
136
150
 
151
+ StringValue(key);
152
+
137
153
  ID sym_cert = rb_intern("cert");
138
154
  VALUE cert = rb_funcall(mini_ssl_ctx, sym_cert, 0);
139
155
 
156
+ StringValue(cert);
157
+
140
158
  ID sym_ca = rb_intern("ca");
141
159
  VALUE ca = rb_funcall(mini_ssl_ctx, sym_ca, 0);
142
160
 
143
161
  ID sym_verify_mode = rb_intern("verify_mode");
144
162
  VALUE verify_mode = rb_funcall(mini_ssl_ctx, sym_verify_mode, 0);
145
163
 
164
+ ID sym_ssl_cipher_filter = rb_intern("ssl_cipher_filter");
165
+ VALUE ssl_cipher_filter = rb_funcall(mini_ssl_ctx, sym_ssl_cipher_filter, 0);
166
+
146
167
  ctx = SSL_CTX_new(SSLv23_server_method());
147
168
  conn->ctx = ctx;
148
169
 
@@ -150,13 +171,20 @@ VALUE engine_init_server(VALUE self, VALUE mini_ssl_ctx) {
150
171
  SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM);
151
172
 
152
173
  if (!NIL_P(ca)) {
174
+ StringValue(ca);
153
175
  SSL_CTX_load_verify_locations(ctx, RSTRING_PTR(ca), NULL);
154
176
  }
155
-
177
+
156
178
  SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE | SSL_OP_NO_COMPRESSION);
157
179
  SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
158
180
 
159
- SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL@STRENGTH");
181
+ if (!NIL_P(ssl_cipher_filter)) {
182
+ StringValue(ssl_cipher_filter);
183
+ SSL_CTX_set_cipher_list(ctx, RSTRING_PTR(ssl_cipher_filter));
184
+ }
185
+ else {
186
+ SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL@STRENGTH");
187
+ }
160
188
 
161
189
  DH *dh = get_dh1024();
162
190
  SSL_CTX_set_tmp_dh(ctx, dh);
@@ -225,7 +253,7 @@ void raise_error(SSL* ssl, int result) {
225
253
  const char* err_str;
226
254
  int err = errno;
227
255
  int ssl_err = SSL_get_error(ssl, result);
228
- int verify_err = SSL_get_verify_result(ssl);
256
+ int verify_err = (int) SSL_get_verify_result(ssl);
229
257
 
230
258
  if(SSL_ERROR_SYSCALL == ssl_err) {
231
259
  snprintf(msg, sizeof(msg), "System error: %s - %d", strerror(err), err);
@@ -238,7 +266,7 @@ void raise_error(SSL* ssl, int result) {
238
266
  err_str, verify_err);
239
267
 
240
268
  } else {
241
- err = ERR_get_error();
269
+ err = (int) ERR_get_error();
242
270
  ERR_error_string_n(err, buf, sizeof(buf));
243
271
  snprintf(msg, sizeof(msg), "OpenSSL error: %s - %d", buf, err);
244
272
 
@@ -322,6 +350,30 @@ VALUE engine_extract(VALUE self) {
322
350
  return Qnil;
323
351
  }
324
352
 
353
+ VALUE engine_shutdown(VALUE self) {
354
+ ms_conn* conn;
355
+ int ok;
356
+
357
+ Data_Get_Struct(self, ms_conn, conn);
358
+
359
+ ERR_clear_error();
360
+
361
+ ok = SSL_shutdown(conn->ssl);
362
+ if (ok == 0) {
363
+ return Qfalse;
364
+ }
365
+
366
+ return Qtrue;
367
+ }
368
+
369
+ VALUE engine_init(VALUE self) {
370
+ ms_conn* conn;
371
+
372
+ Data_Get_Struct(self, ms_conn, conn);
373
+
374
+ return SSL_in_init(conn->ssl) ? Qtrue : Qfalse;
375
+ }
376
+
325
377
  VALUE engine_peercert(VALUE self) {
326
378
  ms_conn* conn;
327
379
  X509* cert;
@@ -368,11 +420,16 @@ VALUE noop(VALUE self) {
368
420
  void Init_mini_ssl(VALUE puma) {
369
421
  VALUE mod, eng;
370
422
 
423
+ /* Fake operation for documentation (RDoc, YARD) */
424
+ #if 0 == 1
425
+ puma = rb_define_module("Puma");
426
+ #endif
427
+
371
428
  SSL_library_init();
372
429
  OpenSSL_add_ssl_algorithms();
373
430
  SSL_load_error_strings();
374
431
  ERR_load_crypto_strings();
375
-
432
+
376
433
  mod = rb_define_module_under(puma, "MiniSSL");
377
434
  eng = rb_define_class_under(mod, "Engine", rb_cObject);
378
435
 
@@ -389,6 +446,10 @@ void Init_mini_ssl(VALUE puma) {
389
446
  rb_define_method(eng, "write", engine_write, 1);
390
447
  rb_define_method(eng, "extract", engine_extract, 0);
391
448
 
449
+ rb_define_method(eng, "shutdown", engine_shutdown, 0);
450
+
451
+ rb_define_method(eng, "init?", engine_init, 0);
452
+
392
453
  rb_define_method(eng, "peercert", engine_peercert, 0);
393
454
  }
394
455
 
@@ -400,7 +461,7 @@ VALUE raise_error(VALUE self) {
400
461
  }
401
462
 
402
463
  void Init_mini_ssl(VALUE puma) {
403
- VALUE mod, eng;
464
+ VALUE mod;
404
465
 
405
466
  mod = rb_define_module_under(puma, "MiniSSL");
406
467
  rb_define_class_under(mod, "SSLError", rb_eStandardError);
@@ -182,9 +182,6 @@ static final int puma_parser_start = 1;
182
182
  static final int puma_parser_first_final = 47;
183
183
  static final int puma_parser_error = 0;
184
184
 
185
- static final int puma_parser_en_main = 1;
186
-
187
-
188
185
  // line 69 "ext/puma_http11/http11_parser.java.rl"
189
186
 
190
187
  public static interface ElementCB {
@@ -220,7 +217,7 @@ static final int puma_parser_en_main = 1;
220
217
  public void init() {
221
218
  cs = 0;
222
219
 
223
-
220
+
224
221
  // line 225 "ext/puma_http11/org/jruby/puma/Http11Parser.java"
225
222
  {
226
223
  cs = puma_parser_start;
@@ -252,7 +249,7 @@ static final int puma_parser_en_main = 1;
252
249
  byte[] data = buffer.bytes();
253
250
  parser.buffer = buffer;
254
251
 
255
-
252
+
256
253
  // line 257 "ext/puma_http11/org/jruby/puma/Http11Parser.java"
257
254
  {
258
255
  int _klen;
@@ -347,7 +344,7 @@ case 1:
347
344
  break;
348
345
  case 3:
349
346
  // line 17 "ext/puma_http11/http11_parser.java.rl"
350
- {
347
+ {
351
348
  parser.field_len = p-parser.field_start;
352
349
  }
353
350
  break;
@@ -357,7 +354,7 @@ case 1:
357
354
  break;
358
355
  case 5:
359
356
  // line 22 "ext/puma_http11/http11_parser.java.rl"
360
- {
357
+ {
361
358
  if(parser.http_field != null) {
362
359
  parser.http_field.call(parser.data, parser.field_start, parser.field_len, parser.mark, p-parser.mark);
363
360
  }
@@ -365,21 +362,21 @@ case 1:
365
362
  break;
366
363
  case 6:
367
364
  // line 27 "ext/puma_http11/http11_parser.java.rl"
368
- {
369
- if(parser.request_method != null)
365
+ {
366
+ if(parser.request_method != null)
370
367
  parser.request_method.call(parser.data, parser.mark, p-parser.mark);
371
368
  }
372
369
  break;
373
370
  case 7:
374
371
  // line 31 "ext/puma_http11/http11_parser.java.rl"
375
- {
372
+ {
376
373
  if(parser.request_uri != null)
377
374
  parser.request_uri.call(parser.data, parser.mark, p-parser.mark);
378
375
  }
379
376
  break;
380
377
  case 8:
381
378
  // line 35 "ext/puma_http11/http11_parser.java.rl"
382
- {
379
+ {
383
380
  if(parser.fragment != null)
384
381
  parser.fragment.call(parser.data, parser.mark, p-parser.mark);
385
382
  }
@@ -390,14 +387,14 @@ case 1:
390
387
  break;
391
388
  case 10:
392
389
  // line 41 "ext/puma_http11/http11_parser.java.rl"
393
- {
390
+ {
394
391
  if(parser.query_string != null)
395
392
  parser.query_string.call(parser.data, parser.query_start, p-parser.query_start);
396
393
  }
397
394
  break;
398
395
  case 11:
399
396
  // line 46 "ext/puma_http11/http11_parser.java.rl"
400
- {
397
+ {
401
398
  if(parser.http_version != null)
402
399
  parser.http_version.call(parser.data, parser.mark, p-parser.mark);
403
400
  }
@@ -411,8 +408,8 @@ case 1:
411
408
  break;
412
409
  case 13:
413
410
  // line 56 "ext/puma_http11/http11_parser.java.rl"
414
- {
415
- parser.body_start = p + 1;
411
+ {
412
+ parser.body_start = p + 1;
416
413
  if(parser.header_done != null)
417
414
  parser.header_done.call(parser.data, p + 1, pe - p - 1);
418
415
  { p += 1; _goto_targ = 5; if (true) continue _goto;}
@@ -442,7 +439,7 @@ case 5:
442
439
 
443
440
  parser.cs = cs;
444
441
  parser.nread += (p - off);
445
-
442
+
446
443
  assert p <= pe : "buffer overflow after parsing execute";
447
444
  assert parser.nread <= len : "nread longer than length";
448
445
  assert parser.body_start <= len : "body starts after buffer end";
@@ -6,6 +6,7 @@ import org.jruby.RubyModule;
6
6
  import org.jruby.RubyObject;
7
7
  import org.jruby.RubyString;
8
8
  import org.jruby.anno.JRubyMethod;
9
+ import org.jruby.javasupport.JavaEmbedUtils;
9
10
  import org.jruby.runtime.Block;
10
11
  import org.jruby.runtime.ObjectAllocator;
11
12
  import org.jruby.runtime.ThreadContext;
@@ -18,6 +19,7 @@ import javax.net.ssl.SSLContext;
18
19
  import javax.net.ssl.SSLEngine;
19
20
  import javax.net.ssl.SSLEngineResult;
20
21
  import javax.net.ssl.SSLException;
22
+ import javax.net.ssl.SSLPeerUnverifiedException;
21
23
  import javax.net.ssl.SSLSession;
22
24
  import java.io.FileInputStream;
23
25
  import java.io.IOException;
@@ -27,6 +29,7 @@ import java.security.KeyStore;
27
29
  import java.security.KeyStoreException;
28
30
  import java.security.NoSuchAlgorithmException;
29
31
  import java.security.UnrecoverableKeyException;
32
+ import java.security.cert.CertificateEncodingException;
30
33
  import java.security.cert.CertificateException;
31
34
 
32
35
  import static javax.net.ssl.SSLEngineResult.Status;
@@ -167,6 +170,12 @@ public class MiniSSL extends RubyObject {
167
170
  engine.setNeedClientAuth(true);
168
171
  }
169
172
 
173
+ IRubyObject sslCipherListObject = miniSSLContext.callMethod(threadContext, "ssl_cipher_list");
174
+ if (!sslCipherListObject.isNil()) {
175
+ String[] sslCipherList = sslCipherListObject.convertToString().asJavaString().split(",");
176
+ engine.setEnabledCipherSuites(sslCipherList);
177
+ }
178
+
170
179
  SSLSession session = engine.getSession();
171
180
  inboundNetData = new MiniSSLBuffer(session.getPacketBufferSize());
172
181
  outboundAppData = new MiniSSLBuffer(session.getApplicationBufferSize());
@@ -333,7 +342,11 @@ public class MiniSSL extends RubyObject {
333
342
  }
334
343
 
335
344
  @JRubyMethod
336
- public IRubyObject peercert() {
337
- return getRuntime().getNil();
345
+ public IRubyObject peercert() throws CertificateEncodingException {
346
+ try {
347
+ return JavaEmbedUtils.javaToRuby(getRuntime(), engine.getSession().getPeerCertificates()[0].getEncoded());
348
+ } catch (SSLPeerUnverifiedException ex) {
349
+ return getRuntime().getNil();
350
+ }
338
351
  }
339
352
  }
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Copyright (c) 2005 Zed A. Shaw
3
3
  * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ * License 3-clause BSD
4
5
  */
5
6
 
6
7
  #define RSTRING_NOT_MODIFIED 1