puma 5.6.9-java → 6.6.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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +465 -18
  3. data/README.md +152 -42
  4. data/bin/puma-wild +1 -1
  5. data/docs/compile_options.md +34 -0
  6. data/docs/fork_worker.md +12 -4
  7. data/docs/java_options.md +54 -0
  8. data/docs/kubernetes.md +12 -0
  9. data/docs/nginx.md +1 -1
  10. data/docs/plugins.md +4 -0
  11. data/docs/restart.md +1 -0
  12. data/docs/signals.md +2 -2
  13. data/docs/stats.md +8 -3
  14. data/docs/systemd.md +13 -7
  15. data/docs/testing_benchmarks_local_files.md +150 -0
  16. data/docs/testing_test_rackup_ci_files.md +36 -0
  17. data/ext/puma_http11/extconf.rb +27 -17
  18. data/ext/puma_http11/http11_parser.c +1 -1
  19. data/ext/puma_http11/http11_parser.h +1 -1
  20. data/ext/puma_http11/http11_parser.java.rl +2 -2
  21. data/ext/puma_http11/http11_parser.rl +2 -2
  22. data/ext/puma_http11/http11_parser_common.rl +2 -2
  23. data/ext/puma_http11/mini_ssl.c +137 -19
  24. data/ext/puma_http11/org/jruby/puma/Http11.java +31 -10
  25. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +1 -1
  26. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +157 -53
  27. data/ext/puma_http11/puma_http11.c +21 -10
  28. data/lib/puma/app/status.rb +4 -4
  29. data/lib/puma/binder.rb +60 -55
  30. data/lib/puma/cli.rb +22 -20
  31. data/lib/puma/client.rb +93 -30
  32. data/lib/puma/cluster/worker.rb +27 -17
  33. data/lib/puma/cluster/worker_handle.rb +8 -6
  34. data/lib/puma/cluster.rb +121 -47
  35. data/lib/puma/commonlogger.rb +21 -14
  36. data/lib/puma/configuration.rb +101 -65
  37. data/lib/puma/const.rb +141 -93
  38. data/lib/puma/control_cli.rb +19 -15
  39. data/lib/puma/detect.rb +7 -4
  40. data/lib/puma/dsl.rb +521 -88
  41. data/lib/puma/error_logger.rb +22 -13
  42. data/lib/puma/events.rb +6 -126
  43. data/lib/puma/io_buffer.rb +39 -4
  44. data/lib/puma/jruby_restart.rb +0 -15
  45. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  46. data/lib/puma/launcher.rb +121 -181
  47. data/lib/puma/log_writer.rb +147 -0
  48. data/lib/puma/minissl/context_builder.rb +27 -12
  49. data/lib/puma/minissl.rb +105 -11
  50. data/lib/puma/null_io.rb +42 -2
  51. data/lib/puma/plugin/systemd.rb +90 -0
  52. data/lib/puma/plugin/tmp_restart.rb +1 -1
  53. data/lib/puma/puma_http11.jar +0 -0
  54. data/lib/puma/rack/builder.rb +6 -6
  55. data/lib/puma/rack/urlmap.rb +1 -1
  56. data/lib/puma/rack_default.rb +19 -4
  57. data/lib/puma/reactor.rb +19 -10
  58. data/lib/puma/request.rb +368 -169
  59. data/lib/puma/runner.rb +65 -22
  60. data/lib/puma/sd_notify.rb +146 -0
  61. data/lib/puma/server.rb +161 -102
  62. data/lib/puma/single.rb +13 -11
  63. data/lib/puma/state_file.rb +3 -6
  64. data/lib/puma/thread_pool.rb +71 -21
  65. data/lib/puma/util.rb +1 -12
  66. data/lib/puma.rb +9 -10
  67. data/lib/rack/handler/puma.rb +116 -86
  68. data/tools/Dockerfile +2 -2
  69. metadata +17 -12
  70. data/lib/puma/queue_close.rb +0 -26
  71. data/lib/puma/systemd.rb +0 -46
  72. data/lib/rack/version_restriction.rb +0 -15
@@ -0,0 +1,150 @@
1
+ # Testing - benchmark/local files
2
+
3
+ These files generate data that shows request-per-second (RPS), etc. Typically, files are in
4
+ pairs, a shell script and a Ruby script. The shell script starts the server, then runs the
5
+ Ruby file, which starts client request stream(s), then collects and logs metrics.
6
+
7
+ ## response_time_wrk.sh
8
+
9
+ This uses [wrk] for generating data. One or more wrk runs are performed. Summarizes RPS and
10
+ wrk latency times. The default for the `-b` argument runs 28 different client request streams,
11
+ and takes a bit over 5 minutes. See 'Request Stream Configuration' below for `-b` argument
12
+ description.
13
+
14
+ <details>
15
+ <summary>Summary output for<br/><code>benchmarks/local/response_time_wrk.sh -w2 -t5:5 -s tcp6</code>:</summary>
16
+
17
+ ```
18
+ Type req/sec 50% 75% 90% 99% 100% Resp Size
19
+ ───────────────────────────────────────────────────────────────── 1kB
20
+ array 13710 0.74 2.52 5.23 7.76 37.45 1024
21
+ chunk 13502 0.76 2.55 5.28 7.84 11.23 1042
22
+ string 13794 0.74 2.51 5.20 7.75 14.07 1024
23
+ io 9615 1.16 3.45 7.13 10.57 15.75 1024
24
+ ───────────────────────────────────────────────────────────────── 10kB
25
+ array 13458 0.76 2.57 5.31 7.93 13.94 10239
26
+ chunk 13066 0.78 2.64 5.46 8.18 38.48 10320
27
+ string 13500 0.76 2.55 5.29 7.88 11.42 10240
28
+ io 9293 1.18 3.59 7.39 10.94 16.99 10240
29
+ ───────────────────────────────────────────────────────────────── 100kB
30
+ array 11315 0.96 3.06 6.33 9.49 17.69 102424
31
+ chunk 9916 1.10 3.48 7.20 10.73 15.14 103075
32
+ string 10948 1.00 3.17 6.57 9.83 17.88 102378
33
+ io 8901 1.21 3.72 7.48 11.27 59.98 102407
34
+ ───────────────────────────────────────────────────────────────── 256kB
35
+ array 9217 1.15 3.82 7.88 11.74 17.12 262212
36
+ chunk 7339 1.45 4.76 9.81 14.63 22.70 264007
37
+ string 8574 1.19 3.81 7.73 11.21 15.80 262147
38
+ io 8911 1.19 3.80 7.55 15.25 60.01 262183
39
+ ───────────────────────────────────────────────────────────────── 512kB
40
+ array 6951 1.49 5.03 10.28 15.90 25.08 524378
41
+ chunk 5234 2.03 6.56 13.57 20.46 32.15 527862
42
+ string 6438 1.55 5.04 10.12 16.28 72.87 524275
43
+ io 8533 1.15 4.62 8.79 48.15 70.51 524327
44
+ ───────────────────────────────────────────────────────────────── 1024kB
45
+ array 4122 1.80 15.59 41.87 67.79 121.00 1048565
46
+ chunk 3158 2.82 15.22 31.00 71.39 99.90 1055654
47
+ string 4710 2.24 6.66 13.65 20.38 70.44 1048575
48
+ io 8355 1.23 3.95 7.94 14.08 68.54 1048498
49
+ ───────────────────────────────────────────────────────────────── 2048kB
50
+ array 2454 4.12 14.02 27.70 43.48 88.89 2097415
51
+ chunk 1743 6.26 17.65 36.98 55.78 92.10 2111358
52
+ string 2479 4.38 12.52 25.65 38.44 95.62 2097502
53
+ io 8264 1.25 3.83 7.76 11.73 65.69 2097090
54
+
55
+ Body ────────── req/sec ────────── ─────── req 50% times ───────
56
+ KB array chunk string io array chunk string io
57
+ 1 13710 13502 13794 9615 0.745 0.757 0.741 1.160
58
+ 10 13458 13066 13500 9293 0.760 0.784 0.759 1.180
59
+ 100 11315 9916 10948 8901 0.960 1.100 1.000 1.210
60
+ 256 9217 7339 8574 8911 1.150 1.450 1.190 1.190
61
+ 512 6951 5234 6438 8533 1.490 2.030 1.550 1.150
62
+ 1024 4122 3158 4710 8355 1.800 2.820 2.240 1.230
63
+ 2048 2454 1743 2479 8264 4.120 6.260 4.380 1.250
64
+ ─────────────────────────────────────────────────────────────────────
65
+ wrk -t8 -c16 -d10s
66
+ benchmarks/local/response_time_wrk.sh -w2 -t5:5 -s tcp6 -Y
67
+ Server cluster mode -w2 -t5:5, bind: tcp6
68
+ Puma repo branch 00-response-refactor
69
+ ruby 3.2.0dev (2022-06-14T01:21:55Z master 048f14221c) +YJIT [x86_64-linux]
70
+
71
+ [2136] - Gracefully shutting down workers...
72
+ [2136] === puma shutdown: 2022-06-13 21:16:13 -0500 ===
73
+ [2136] - Goodbye!
74
+
75
+ 5:15 Total Time
76
+ ```
77
+ </details><br/>
78
+
79
+ ## bench_base.sh, bench_base.rb
80
+
81
+ These two files setup parameters for the Puma server, which is normally started in a shell
82
+ script. It then starts a Ruby file (a subclass of BenchBase), passing arguments to it. The
83
+ Ruby file is normally used to generate a client request stream(s).
84
+
85
+ ### Puma Configuration
86
+
87
+ The following arguments are used for the Puma server:
88
+
89
+ * **`-C`** - configuration file
90
+ * **`-d`** - app delay
91
+ * **`-r`** - rackup file, often defaults to test/rackup/ci_select.ru
92
+ * **`-s`** - bind socket type, default is tcp/tcp4, also tcp6, ssl/ssl4, ssl6, unix, or aunix
93
+ (unix & abstract unix are not available with wrk).
94
+ * **`-t`** - threads, expressed as '5:5', same as Puma --thread
95
+ * **`-w`** - workers, same as Puma --worker
96
+ * **`-Y`** - enable Ruby YJIT
97
+
98
+ ### Request Stream Configuration
99
+
100
+ The following arguments are used for request streams:
101
+
102
+ * **`-b`** - response body configuration. Body type options are a array, c chunked, s string,
103
+ and i for File/IO. None or any combination can be specified, they should start the option.
104
+ Then, any combination of comma separated integers can be used for the response body size
105
+ in kB. The string 'ac50,100' would create four runs, 50kb array, 50kB chunked, 100kB array,
106
+ and 100kB chunked. See 'Testing - test/rackup/ci-*.ru files' for more info.
107
+ * **`-c`** - connections per client request stream thread, defaults to 2 for wrk.
108
+ * **`-D`** - duration of client request stream in seconds.
109
+ * **`-T`** - number of threads in the client request stream. For wrk, this defaults to
110
+ 80% of Puma workers * max_threads.
111
+
112
+ ### Notes - Configuration
113
+
114
+ The above lists script arguments.
115
+
116
+ `bench_base.sh` contains most server defaults. Many can be set via ENV variables.
117
+
118
+ `bench_base.rb` contains the client request stream defaults. The default value for
119
+ `-b` is `acsi1,10,100,256,512,1024,2048`, which is a 4 x 7 matrix, and hence, runs
120
+ 28 jobs. Also, the i body type (File/IO) generates files, they are placed in the
121
+ `"#{Dir.tmpdir}/.puma_response_body_io"` directory, which is created.
122
+
123
+ ### Notes - wrk
124
+
125
+ The shell scripts use `-T` for wrk's thread count, since `-t` is used for Puma
126
+ server threads. Regarding the `-c` argument, wrk has an interesting behavior.
127
+ The total number of connections is set by `(connections/threads).to_i`. The scripts
128
+ here use `-c` as connections per thread. Hence, using `-T4 -c2` will yield a total
129
+ of eight wrk connections, two per thread. The equivalent wrk arguments would be `-t4 -c8`.
130
+
131
+ Puma can only process so many requests, and requests will queue in the backlog
132
+ until Puma can respond to them. With wrk, if the number of total connections is
133
+ too high, one will see the upper latency times increase, pushing into the lower
134
+ latency times as the connections are increased. The default values for wrk's
135
+ threads and connections were chosen to minimize requests' time in the backlog.
136
+
137
+ An example with four wrk runs using `-b s10`. Notice that `req/sec` varies by
138
+ less than 1%, but the `75%` times increase by an order of magnitude:
139
+ ```
140
+ req/sec 50% 75% 90% 99% 100% Resp Size wrk cmd line
141
+ ─────────────────────────────────────────────────────────────────────────────
142
+ 13597 0.755 2.550 5.260 7.800 13.310 12040 wrk -t8 -c16 -d10
143
+ 13549 0.793 4.430 8.140 11.220 16.600 12002 wrk -t10 -c20 -d10
144
+ 13570 1.040 25.790 40.010 49.070 58.300 11982 wrk -t8 -c64 -d10
145
+ 13684 1.050 25.820 40.080 49.160 66.190 12033 wrk -t16 -c64 -d10
146
+ ```
147
+ Finally, wrk's output may cause rounding errors, so the response body size calculation is
148
+ imprecise.
149
+
150
+ [wrk]: <https://github.com/ioquatix/wrk>
@@ -0,0 +1,36 @@
1
+ # Testing - test/rackup/ci-*.ru files
2
+
3
+ ## Overview
4
+
5
+ Puma should efficiently handle a variety of response bodies, varying both by size
6
+ and by the type of object used for the body.
7
+
8
+ Five rackup files are located in 'test/rackup' that can be used. All have their
9
+ request body size (in kB) set via `Body-Conf` header or with `ENV['CI_BODY_CONF']`.
10
+ Additionally, the ci_select.ru file can have it's body type set via a starting
11
+ character.
12
+
13
+ * **ci_array.ru** - body is an `Array` of 1kB strings. `Content-Length` is not set.
14
+ * **ci_chunked.ru** - body is an `Enumerator` of 1kB strings. `Content-Length` is not set.
15
+ * **ci_io.ru** - body is a File/IO object. `Content-Length` is set.
16
+ * **ci_string.ru** - body is a single string. `Content-Length` is set.
17
+ * **ci_select.ru** - can be any of the above.
18
+
19
+ All responses have 25 headers, total length approx 1kB. ci_array.ru and ci_chunked.ru
20
+ contain 1kB items.
21
+
22
+ All can be delayed by a float value (seconds) specified by the `Dly` header
23
+
24
+ Note that rhe `Body-Conf` header takes precedence, and `ENV['CI_BODY_CONF']` is
25
+ only read on load.
26
+
27
+ ## ci_select.ru
28
+
29
+ The ci_select.ru file allows a starting character to specify the body type in the
30
+ `Body-Conf` header or with `ENV['CI_BODY_CONF']`.
31
+ * **a** - array of strings
32
+ * **c** - chunked (enum)
33
+ * **s** - single string
34
+ * **i** - File/IO
35
+
36
+ A value of `a100` would return a body as an array of 100 1kB strings.
@@ -2,19 +2,23 @@ require 'mkmf'
2
2
 
3
3
  dir_config("puma_http11")
4
4
 
5
- if $mingw && RUBY_VERSION >= '2.4'
5
+ if $mingw
6
6
  append_cflags '-fstack-protector-strong -D_FORTIFY_SOURCE=2'
7
7
  append_ldflags '-fstack-protector-strong -l:libssp.a'
8
8
  have_library 'ssp'
9
9
  end
10
10
 
11
- unless ENV["DISABLE_SSL"]
11
+ unless ENV["PUMA_DISABLE_SSL"]
12
12
  # don't use pkg_config('openssl') if '--with-openssl-dir' is used
13
- has_openssl_dir = dir_config('openssl').any?
13
+ has_openssl_dir = dir_config('openssl').any? ||
14
+ RbConfig::CONFIG['configure_args']&.include?('openssl')
15
+
14
16
  found_pkg_config = !has_openssl_dir && pkg_config('openssl')
15
17
 
16
- found_ssl = if (!$mingw || RUBY_VERSION >= '2.4') && found_pkg_config
17
- puts 'using OpenSSL pkgconfig (openssl.pc)'
18
+ found_ssl = if !$mingw && found_pkg_config
19
+ puts '──── Using OpenSSL pkgconfig (openssl.pc) ────'
20
+ true
21
+ elsif have_library('libcrypto', 'BIO_read') && have_library('libssl', 'SSL_CTX_new')
18
22
  true
19
23
  elsif %w'crypto libeay32'.find {|crypto| have_library(crypto, 'BIO_read')} &&
20
24
  %w'ssl ssleay32'.find {|ssl| have_library(ssl, 'SSL_CTX_new')}
@@ -27,21 +31,27 @@ unless ENV["DISABLE_SSL"]
27
31
  if found_ssl
28
32
  have_header "openssl/bio.h"
29
33
 
30
- # below is yes for 1.0.2 & later
31
- have_func "DTLS_method" , "openssl/ssl.h"
34
+ ssl_h = "openssl/ssl.h".freeze
35
+
36
+ puts "\n──── Below are yes for 1.0.2 & later ────"
37
+ have_func "DTLS_method" , ssl_h
38
+ have_func "SSL_CTX_set_session_cache_mode(NULL, 0)", ssl_h
39
+
40
+ puts "\n──── Below are yes for 1.1.0 & later ────"
41
+ have_func "TLS_server_method" , ssl_h
42
+ have_func "SSL_CTX_set_min_proto_version(NULL, 0)" , ssl_h
32
43
 
33
- # below are yes for 1.1.0 & later
34
- have_func "TLS_server_method" , "openssl/ssl.h"
35
- have_func "SSL_CTX_set_min_proto_version(NULL, 0)", "openssl/ssl.h"
44
+ puts "\n──── Below is yes for 1.1.0 and later, but isn't documented until 3.0.0 ────"
45
+ # https://github.com/openssl/openssl/blob/OpenSSL_1_1_0/include/openssl/ssl.h#L1159
46
+ have_func "SSL_CTX_set_dh_auto(NULL, 0)" , ssl_h
36
47
 
37
- have_func "X509_STORE_up_ref"
38
- have_func "SSL_CTX_set_ecdh_auto(NULL, 0)" , "openssl/ssl.h"
48
+ puts "\n──── Below is yes for 1.1.1 & later ────"
49
+ have_func "SSL_CTX_set_ciphersuites(NULL, \"\")" , ssl_h
39
50
 
40
- # below exists in 1.1.0 and later, but isn't documented until 3.0.0
41
- have_func "SSL_CTX_set_dh_auto(NULL, 0)" , "openssl/ssl.h"
51
+ puts "\n──── Below is yes for 3.0.0 & later ────"
52
+ have_func "SSL_get1_peer_certificate" , ssl_h
42
53
 
43
- # below is yes for 3.0.0 & later
44
- have_func "SSL_get1_peer_certificate" , "openssl/ssl.h"
54
+ puts ''
45
55
 
46
56
  # Random.bytes available in Ruby 2.5 and later, Random::DEFAULT deprecated in 3.0
47
57
  if Random.respond_to?(:bytes)
@@ -53,7 +63,7 @@ unless ENV["DISABLE_SSL"]
53
63
  end
54
64
  end
55
65
 
56
- if ENV["MAKE_WARNINGS_INTO_ERRORS"]
66
+ if ENV["PUMA_MAKE_WARNINGS_INTO_ERRORS"]
57
67
  # Make all warnings into errors
58
68
  # Except `implicit-fallthrough` since most failures comes from ragel state machine generated code
59
69
  if respond_to?(:append_cflags, true) # Ruby 2.5 and later
@@ -297,7 +297,7 @@ case 13:
297
297
  tr18:
298
298
  #line 65 "ext/puma_http11/http11_parser.rl"
299
299
  {
300
- parser->http_version(parser, PTR_TO(mark), LEN(mark, p));
300
+ parser->server_protocol(parser, PTR_TO(mark), LEN(mark, p));
301
301
  }
302
302
  goto st14;
303
303
  tr26:
@@ -46,7 +46,7 @@ typedef struct puma_parser {
46
46
  element_cb fragment;
47
47
  element_cb request_path;
48
48
  element_cb query_string;
49
- element_cb http_version;
49
+ element_cb server_protocol;
50
50
  element_cb header_done;
51
51
 
52
52
  char buf[BUFFER_LEN];
@@ -39,8 +39,8 @@ public class Http11Parser {
39
39
  Http11.query_string(runtime, parser.data, parser.buffer, parser.query_start, fpc-parser.query_start);
40
40
  }
41
41
 
42
- action http_version {
43
- Http11.http_version(runtime, parser.data, parser.buffer, parser.mark, fpc-parser.mark);
42
+ action server_protocol {
43
+ Http11.server_protocol(runtime, parser.data, parser.buffer, parser.mark, fpc-parser.mark);
44
44
  }
45
45
 
46
46
  action request_path {
@@ -62,8 +62,8 @@ static void snake_upcase_char(char *c)
62
62
  parser->query_string(parser, PTR_TO(query_start), LEN(query_start, fpc));
63
63
  }
64
64
 
65
- action http_version {
66
- parser->http_version(parser, PTR_TO(mark), LEN(mark, fpc));
65
+ action server_protocol {
66
+ parser->server_protocol(parser, PTR_TO(mark), LEN(mark, fpc));
67
67
  }
68
68
 
69
69
  action request_path {
@@ -38,8 +38,8 @@
38
38
  Method = ( upper | digit | safe ){1,20} >mark %request_method;
39
39
 
40
40
  http_number = ( digit+ "." digit+ ) ;
41
- HTTP_Version = ( "HTTP/" http_number ) >mark %http_version ;
42
- Request_Line = ( Method " " Request_URI ("#" Fragment){0,1} " " HTTP_Version CRLF ) ;
41
+ Server_Protocol = ( "HTTP/" http_number ) >mark %server_protocol ;
42
+ Request_Line = ( Method " " Request_URI ("#" Fragment){0,1} " " Server_Protocol CRLF ) ;
43
43
 
44
44
  field_name = ( token -- ":" )+ >start_field $snake_upcase_field %write_field;
45
45
 
@@ -36,6 +36,12 @@ void raise_file_error(const char* caller, const char *filename) {
36
36
  rb_raise(eError, "%s: error in file '%s': %s", caller, filename, ERR_error_string(ERR_get_error(), NULL));
37
37
  }
38
38
 
39
+ NORETURN(void raise_param_error(const char* caller, const char *param));
40
+
41
+ void raise_param_error(const char* caller, const char *param) {
42
+ rb_raise(eError, "%s: error with parameter '%s': %s", caller, param, ERR_error_string(ERR_get_error(), NULL));
43
+ }
44
+
39
45
  void engine_free(void *ptr) {
40
46
  ms_conn *conn = ptr;
41
47
  ms_cert_buf* cert_buf = (ms_cert_buf*)SSL_get_app_data(conn->ssl);
@@ -185,6 +191,18 @@ static int engine_verify_callback(int preverify_ok, X509_STORE_CTX* ctx) {
185
191
  return preverify_ok;
186
192
  }
187
193
 
194
+ static int password_callback(char *buf, int size, int rwflag, void *userdata) {
195
+ const char *password = (const char *) userdata;
196
+ size_t len = strlen(password);
197
+
198
+ if (len > (size_t) size) {
199
+ return 0;
200
+ }
201
+
202
+ memcpy(buf, password, len);
203
+ return (int) len;
204
+ }
205
+
188
206
  static VALUE
189
207
  sslctx_alloc(VALUE klass) {
190
208
  SSL_CTX *ctx;
@@ -210,28 +228,35 @@ sslctx_alloc(VALUE klass) {
210
228
  VALUE
211
229
  sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
212
230
  SSL_CTX* ctx;
213
-
231
+ int ssl_options;
232
+ VALUE key, cert, ca, verify_mode, ssl_cipher_filter, ssl_ciphersuites, no_tlsv1, no_tlsv1_1,
233
+ verification_flags, session_id_bytes, cert_pem, key_pem, key_password_command, key_password;
234
+ BIO *bio;
235
+ X509 *x509 = NULL;
236
+ EVP_PKEY *pkey;
237
+ pem_password_cb *password_cb = NULL;
238
+ const char *password = NULL;
214
239
  #ifdef HAVE_SSL_CTX_SET_MIN_PROTO_VERSION
215
240
  int min;
216
241
  #endif
217
- int ssl_options;
218
- VALUE key, cert, ca, verify_mode, ssl_cipher_filter, no_tlsv1, no_tlsv1_1,
219
- verification_flags, session_id_bytes, cert_pem, key_pem;
220
242
  #ifndef HAVE_SSL_CTX_SET_DH_AUTO
221
243
  DH *dh;
222
244
  #endif
223
- BIO *bio;
224
- X509 *x509;
225
- EVP_PKEY *pkey;
226
-
227
245
  #if OPENSSL_VERSION_NUMBER < 0x10002000L
228
246
  EC_KEY *ecdh;
229
247
  #endif
248
+ #ifdef HAVE_SSL_CTX_SET_SESSION_CACHE_MODE
249
+ VALUE reuse, reuse_cache_size, reuse_timeout;
230
250
 
231
- TypedData_Get_Struct(self, SSL_CTX, &sslctx_type, ctx);
251
+ reuse = rb_funcall(mini_ssl_ctx, rb_intern_const("reuse"), 0);
252
+ reuse_cache_size = rb_funcall(mini_ssl_ctx, rb_intern_const("reuse_cache_size"), 0);
253
+ reuse_timeout = rb_funcall(mini_ssl_ctx, rb_intern_const("reuse_timeout"), 0);
254
+ #endif
232
255
 
233
256
  key = rb_funcall(mini_ssl_ctx, rb_intern_const("key"), 0);
234
257
 
258
+ key_password_command = rb_funcall(mini_ssl_ctx, rb_intern_const("key_password_command"), 0);
259
+
235
260
  cert = rb_funcall(mini_ssl_ctx, rb_intern_const("cert"), 0);
236
261
 
237
262
  ca = rb_funcall(mini_ssl_ctx, rb_intern_const("ca"), 0);
@@ -244,10 +269,14 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
244
269
 
245
270
  ssl_cipher_filter = rb_funcall(mini_ssl_ctx, rb_intern_const("ssl_cipher_filter"), 0);
246
271
 
272
+ ssl_ciphersuites = rb_funcall(mini_ssl_ctx, rb_intern_const("ssl_ciphersuites"), 0);
273
+
247
274
  no_tlsv1 = rb_funcall(mini_ssl_ctx, rb_intern_const("no_tlsv1"), 0);
248
275
 
249
276
  no_tlsv1_1 = rb_funcall(mini_ssl_ctx, rb_intern_const("no_tlsv1_1"), 0);
250
277
 
278
+ TypedData_Get_Struct(self, SSL_CTX, &sslctx_type, ctx);
279
+
251
280
  if (!NIL_P(cert)) {
252
281
  StringValue(cert);
253
282
 
@@ -256,6 +285,18 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
256
285
  }
257
286
  }
258
287
 
288
+ if (!NIL_P(key_password_command)) {
289
+ key_password = rb_funcall(mini_ssl_ctx, rb_intern_const("key_password"), 0);
290
+
291
+ if (!NIL_P(key_password)) {
292
+ StringValue(key_password);
293
+ password_cb = password_callback;
294
+ password = RSTRING_PTR(key_password);
295
+ SSL_CTX_set_default_passwd_cb(ctx, password_cb);
296
+ SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *) password);
297
+ }
298
+ }
299
+
259
300
  if (!NIL_P(key)) {
260
301
  StringValue(key);
261
302
 
@@ -265,23 +306,78 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
265
306
  }
266
307
 
267
308
  if (!NIL_P(cert_pem)) {
309
+ X509 *ca = NULL;
310
+ unsigned long err;
311
+
268
312
  bio = BIO_new(BIO_s_mem());
269
313
  BIO_puts(bio, RSTRING_PTR(cert_pem));
314
+
315
+ /**
316
+ * Much of this pulled as a simplified version of the `use_certificate_chain_file` method
317
+ * from openssl's `ssl_rsa.c` file.
318
+ */
319
+
320
+ /* first read the cert as the first item in the pem file */
270
321
  x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
322
+ if (NULL == x509) {
323
+ BIO_free_all(bio);
324
+ raise_param_error("PEM_read_bio_X509", "cert_pem");
325
+ }
326
+
327
+ /* Add the cert to the context */
328
+ /* 1 is success - otherwise check the error codes */
329
+ if (1 != SSL_CTX_use_certificate(ctx, x509)) {
330
+ BIO_free_all(bio);
331
+ raise_param_error("SSL_CTX_use_certificate", "cert_pem");
332
+ }
333
+
334
+ X509_free(x509); /* no longer need our reference */
271
335
 
272
- if (SSL_CTX_use_certificate(ctx, x509) != 1) {
273
- raise_file_error("SSL_CTX_use_certificate", RSTRING_PTR(cert_pem));
336
+ /* Now lets load up the rest of the certificate chain */
337
+ /* 1 is success 0 is error */
338
+ if (0 == SSL_CTX_clear_chain_certs(ctx)) {
339
+ BIO_free_all(bio);
340
+ raise_param_error("SSL_CTX_clear_chain_certs","cert_pem");
341
+ }
342
+
343
+ while (1) {
344
+ ca = PEM_read_bio_X509(bio, NULL, NULL, NULL);
345
+
346
+ if (NULL == ca) {
347
+ break;
348
+ }
349
+
350
+ if (0 == SSL_CTX_add0_chain_cert(ctx, ca)) {
351
+ BIO_free_all(bio);
352
+ raise_param_error("SSL_CTX_add0_chain_cert","cert_pem");
353
+ }
354
+ /* don't free ca - its now owned by the context */
355
+ }
356
+
357
+ /* ca is NULL - so its either the end of the file or an error */
358
+ err = ERR_peek_last_error();
359
+
360
+ /* If its the end of the file - then we are done, in any case free the bio */
361
+ BIO_free_all(bio);
362
+
363
+ if ((ERR_GET_LIB(err) == ERR_LIB_PEM) && (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
364
+ ERR_clear_error();
365
+ } else {
366
+ raise_param_error("PEM_read_bio_X509","cert_pem");
274
367
  }
275
368
  }
276
369
 
277
370
  if (!NIL_P(key_pem)) {
278
371
  bio = BIO_new(BIO_s_mem());
279
372
  BIO_puts(bio, RSTRING_PTR(key_pem));
280
- pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
373
+ pkey = PEM_read_bio_PrivateKey(bio, NULL, password_cb, (void *) password);
281
374
 
282
375
  if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1) {
376
+ BIO_free(bio);
283
377
  raise_file_error("SSL_CTX_use_PrivateKey", RSTRING_PTR(key_pem));
284
378
  }
379
+ EVP_PKEY_free(pkey);
380
+ BIO_free(bio);
285
381
  }
286
382
 
287
383
  verification_flags = rb_funcall(mini_ssl_ctx, rb_intern_const("verification_flags"), 0);
@@ -314,8 +410,6 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
314
410
 
315
411
  SSL_CTX_set_min_proto_version(ctx, min);
316
412
 
317
- SSL_CTX_set_options(ctx, ssl_options);
318
-
319
413
  #else
320
414
  /* As of 1.0.2f, SSL_OP_SINGLE_DH_USE key use is always on */
321
415
  ssl_options |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_SINGLE_DH_USE;
@@ -326,10 +420,23 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
326
420
  if(RTEST(no_tlsv1_1)) {
327
421
  ssl_options |= SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1;
328
422
  }
329
- SSL_CTX_set_options(ctx, ssl_options);
330
423
  #endif
331
424
 
332
- SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
425
+ #ifdef HAVE_SSL_CTX_SET_SESSION_CACHE_MODE
426
+ if (!NIL_P(reuse)) {
427
+ SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER);
428
+ if (!NIL_P(reuse_cache_size)) {
429
+ SSL_CTX_sess_set_cache_size(ctx, NUM2INT(reuse_cache_size));
430
+ }
431
+ if (!NIL_P(reuse_timeout)) {
432
+ SSL_CTX_set_timeout(ctx, NUM2INT(reuse_timeout));
433
+ }
434
+ } else {
435
+ SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
436
+ }
437
+ #endif
438
+
439
+ SSL_CTX_set_options(ctx, ssl_options);
333
440
 
334
441
  if (!NIL_P(ssl_cipher_filter)) {
335
442
  StringValue(ssl_cipher_filter);
@@ -339,9 +446,16 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
339
446
  SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL@STRENGTH");
340
447
  }
341
448
 
449
+ #if HAVE_SSL_CTX_SET_CIPHERSUITES
450
+ // Only override OpenSSL default ciphersuites if config option is supplied.
451
+ if (!NIL_P(ssl_ciphersuites)) {
452
+ StringValue(ssl_ciphersuites);
453
+ SSL_CTX_set_ciphersuites(ctx, RSTRING_PTR(ssl_ciphersuites));
454
+ }
455
+ #endif
456
+
342
457
  #if OPENSSL_VERSION_NUMBER < 0x10002000L
343
- // Remove this case if OpenSSL 1.0.1 (now EOL) support is no
344
- // longer needed.
458
+ // Remove this case if OpenSSL 1.0.1 (now EOL) support is no longer needed.
345
459
  ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
346
460
  if (ecdh) {
347
461
  SSL_CTX_set_tmp_ecdh(ctx, ecdh);
@@ -442,7 +556,7 @@ NORETURN(void raise_error(SSL* ssl, int result));
442
556
 
443
557
  void raise_error(SSL* ssl, int result) {
444
558
  char buf[512];
445
- char msg[512];
559
+ char msg[768];
446
560
  const char* err_str;
447
561
  int err = errno;
448
562
  int mask = 4095;
@@ -700,6 +814,10 @@ void Init_mini_ssl(VALUE puma) {
700
814
 
701
815
  rb_define_method(eng, "init?", engine_init, 0);
702
816
 
817
+ /* @!attribute [r] peercert
818
+ * Returns `nil` when `MiniSSL::Context#verify_mode` is set to `VERIFY_NONE`.
819
+ * @return [String, nil] DER encoded cert
820
+ */
703
821
  rb_define_method(eng, "peercert", engine_peercert, 0);
704
822
 
705
823
  rb_define_method(eng, "ssl_vers_st", engine_ssl_vers_st, 0);
@@ -26,14 +26,14 @@ public class Http11 extends RubyObject {
26
26
  public final static String MAX_FIELD_NAME_LENGTH_ERR = "HTTP element FIELD_NAME is longer than the 256 allowed length.";
27
27
  public final static int MAX_FIELD_VALUE_LENGTH = 80 * 1024;
28
28
  public final static String MAX_FIELD_VALUE_LENGTH_ERR = "HTTP element FIELD_VALUE is longer than the 81920 allowed length.";
29
- public final static int MAX_REQUEST_URI_LENGTH = 1024 * 12;
30
- public final static String MAX_REQUEST_URI_LENGTH_ERR = "HTTP element REQUEST_URI is longer than the 12288 allowed length.";
29
+ public final static int MAX_REQUEST_URI_LENGTH = getConstLength("PUMA_REQUEST_URI_MAX_LENGTH", 1024 * 12);
30
+ public final static String MAX_REQUEST_URI_LENGTH_ERR = "HTTP element REQUEST_URI is longer than the " + MAX_REQUEST_URI_LENGTH + " allowed length.";
31
31
  public final static int MAX_FRAGMENT_LENGTH = 1024;
32
32
  public final static String MAX_FRAGMENT_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 1024 allowed length.";
33
- public final static int MAX_REQUEST_PATH_LENGTH = 8192;
34
- public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 8192 allowed length.";
35
- public final static int MAX_QUERY_STRING_LENGTH = 1024 * 10;
36
- public final static String MAX_QUERY_STRING_LENGTH_ERR = "HTTP element QUERY_STRING is longer than the 10240 allowed length.";
33
+ public final static int MAX_REQUEST_PATH_LENGTH = getConstLength("PUMA_REQUEST_PATH_MAX_LENGTH", 8192);
34
+ public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the " + MAX_REQUEST_PATH_LENGTH + " allowed length.";
35
+ public final static int MAX_QUERY_STRING_LENGTH = getConstLength("PUMA_QUERY_STRING_MAX_LENGTH", 10 * 1024);
36
+ public final static String MAX_QUERY_STRING_LENGTH_ERR = "HTTP element QUERY_STRING is longer than the " + MAX_QUERY_STRING_LENGTH +" allowed length.";
37
37
  public final static int MAX_HEADER_LENGTH = 1024 * (80 + 32);
38
38
  public final static String MAX_HEADER_LENGTH_ERR = "HTTP element HEADER is longer than the 114688 allowed length.";
39
39
 
@@ -46,7 +46,28 @@ public class Http11 extends RubyObject {
46
46
  public static final ByteList FRAGMENT_BYTELIST = new ByteList(ByteList.plain("FRAGMENT"));
47
47
  public static final ByteList REQUEST_PATH_BYTELIST = new ByteList(ByteList.plain("REQUEST_PATH"));
48
48
  public static final ByteList QUERY_STRING_BYTELIST = new ByteList(ByteList.plain("QUERY_STRING"));
49
- public static final ByteList HTTP_VERSION_BYTELIST = new ByteList(ByteList.plain("HTTP_VERSION"));
49
+ public static final ByteList SERVER_PROTOCOL_BYTELIST = new ByteList(ByteList.plain("SERVER_PROTOCOL"));
50
+
51
+ public static String getEnvOrProperty(String name) {
52
+ String envValue = System.getenv(name);
53
+ return (envValue != null) ? envValue : System.getProperty(name);
54
+ }
55
+
56
+ public static int getConstLength(String name, Integer defaultValue) {
57
+ String stringValue = getEnvOrProperty(name);
58
+ if (stringValue == null || stringValue.isEmpty()) return defaultValue;
59
+
60
+ try {
61
+ int value = Integer.parseUnsignedInt(stringValue);
62
+ if (value <= 0) {
63
+ throw new NumberFormatException("The number is not positive.");
64
+ }
65
+ return value;
66
+ } catch (NumberFormatException e) {
67
+ System.err.println(String.format("The value %s for %s is invalid. Using default value %d instead.", stringValue, name, defaultValue));
68
+ return defaultValue;
69
+ }
70
+ }
50
71
 
51
72
  private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
52
73
  public IRubyObject allocate(Ruby runtime, RubyClass klass) {
@@ -56,7 +77,7 @@ public class Http11 extends RubyObject {
56
77
 
57
78
  public static void createHttp11(Ruby runtime) {
58
79
  RubyModule mPuma = runtime.defineModule("Puma");
59
- mPuma.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator());
80
+ mPuma.defineClassUnder("HttpParserError",runtime.getClass("StandardError"),runtime.getClass("StandardError").getAllocator());
60
81
 
61
82
  RubyClass cHttpParser = mPuma.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
62
83
  cHttpParser.defineAnnotatedMethods(Http11.class);
@@ -155,9 +176,9 @@ public class Http11 extends RubyObject {
155
176
  req.fastASet(RubyString.newStringShared(runtime, QUERY_STRING_BYTELIST),val);
156
177
  }
157
178
 
158
- public static void http_version(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
179
+ public static void server_protocol(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
159
180
  RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
160
- req.fastASet(RubyString.newStringShared(runtime, HTTP_VERSION_BYTELIST),val);
181
+ req.fastASet(RubyString.newStringShared(runtime, SERVER_PROTOCOL_BYTELIST),val);
161
182
  }
162
183
 
163
184
  public void header_done(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
@@ -383,7 +383,7 @@ case 1:
383
383
  case 11:
384
384
  // line 42 "ext/puma_http11/http11_parser.java.rl"
385
385
  {
386
- Http11.http_version(runtime, parser.data, parser.buffer, parser.mark, p-parser.mark);
386
+ Http11.server_protocol(runtime, parser.data, parser.buffer, parser.mark, p-parser.mark);
387
387
  }
388
388
  break;
389
389
  case 12: