puma 7.1.0-java → 8.0.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +116 -0
  3. data/README.md +18 -11
  4. data/docs/5.0-Upgrade.md +98 -0
  5. data/docs/6.0-Upgrade.md +56 -0
  6. data/docs/7.0-Upgrade.md +52 -0
  7. data/docs/8.0-Upgrade.md +100 -0
  8. data/docs/deployment.md +58 -23
  9. data/docs/grpc.md +62 -0
  10. data/docs/images/favicon.svg +1 -0
  11. data/docs/images/running-puma.svg +1 -0
  12. data/docs/images/standard-logo.svg +1 -0
  13. data/docs/jungle/README.md +1 -1
  14. data/docs/kubernetes.md +3 -10
  15. data/docs/plugins.md +2 -2
  16. data/docs/signals.md +10 -10
  17. data/docs/stats.md +1 -1
  18. data/docs/systemd.md +3 -3
  19. data/ext/puma_http11/http11_parser.java.rl +51 -65
  20. data/ext/puma_http11/org/jruby/puma/EnvKey.java +241 -0
  21. data/ext/puma_http11/org/jruby/puma/Http11.java +168 -104
  22. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +71 -85
  23. data/ext/puma_http11/puma_http11.c +101 -109
  24. data/lib/puma/app/status.rb +10 -2
  25. data/lib/puma/cli.rb +1 -1
  26. data/lib/puma/client.rb +90 -66
  27. data/lib/puma/client_env.rb +171 -0
  28. data/lib/puma/cluster/worker.rb +10 -9
  29. data/lib/puma/cluster.rb +3 -4
  30. data/lib/puma/configuration.rb +85 -16
  31. data/lib/puma/const.rb +2 -2
  32. data/lib/puma/control_cli.rb +1 -1
  33. data/lib/puma/detect.rb +11 -0
  34. data/lib/puma/dsl.rb +90 -14
  35. data/lib/puma/launcher.rb +7 -7
  36. data/lib/puma/puma_http11.jar +0 -0
  37. data/lib/puma/reactor.rb +3 -12
  38. data/lib/puma/{request.rb → response.rb} +25 -194
  39. data/lib/puma/runner.rb +1 -1
  40. data/lib/puma/server.rb +72 -37
  41. data/lib/puma/server_plugin_control.rb +32 -0
  42. data/lib/puma/single.rb +2 -2
  43. data/lib/puma/thread_pool.rb +129 -23
  44. data/lib/rack/handler/puma.rb +1 -1
  45. data/tools/Dockerfile +13 -5
  46. metadata +17 -7
  47. data/ext/puma_http11/ext_help.h +0 -15
@@ -0,0 +1,241 @@
1
+ package org.jruby.puma;
2
+
3
+ import org.jruby.util.ByteList;
4
+
5
+ import java.nio.charset.StandardCharsets;
6
+
7
+ /**
8
+ * A set of keys representing HTTP/1.1 header fields and CGI env keys.
9
+ *
10
+ * The set of header fields is derived from RFC 2616 with some additions
11
+ * common to mainstream browsers.
12
+ *
13
+ * This enum is used by {@link Http11 }to access pre-allocated strings for
14
+ * Puma's version of these keys and includes a perfect hash function to quickly
15
+ * find the key for a given range of bytes.
16
+ */
17
+ public enum EnvKey {
18
+ ACCEPT,
19
+ ACCEPT_CHARSET,
20
+ ACCEPT_ENCODING,
21
+ ACCEPT_LANGUAGE,
22
+ ACCEPT_RANGES,
23
+ AGE,
24
+ ALLOW,
25
+ AUTHORIZATION,
26
+ CACHE_CONTROL,
27
+ CONNECTION,
28
+ CONTENT_ENCODING,
29
+ CONTENT_LANGUAGE,
30
+ CONTENT_LENGTH(true),
31
+ CONTENT_LOCATION,
32
+ CONTENT_MD5,
33
+ CONTENT_RANGE,
34
+ CONTENT_TYPE(true),
35
+ DATE,
36
+ ETAG,
37
+ EXPECT,
38
+ EXPIRES,
39
+ FRAGMENT(true),
40
+ FROM,
41
+ HOST,
42
+ IF_MATCH,
43
+ IF_MODIFIED_SINCE,
44
+ IF_NONE_MATCH,
45
+ IF_RANGE,
46
+ IF_UNMODIFIED_SINCE,
47
+ KEEP_ALIVE,
48
+ LAST_MODIFIED,
49
+ LOCATION,
50
+ MAX_FORWARDS,
51
+ PRAGMA,
52
+ PROXY_AUTHENTICATE,
53
+ PROXY_AUTHORIZATION,
54
+ QUERY_STRING(true),
55
+ RANGE,
56
+ REFERER,
57
+ REQUEST_METHOD(true),
58
+ REQUEST_PATH(true),
59
+ REQUEST_URI(true),
60
+ RETRY_AFTER,
61
+ SERVER,
62
+ SERVER_PROTOCOL(true),
63
+ TE,
64
+ TRAILER,
65
+ TRANSFER_ENCODING,
66
+ UPGRADE,
67
+ USER_AGENT,
68
+ VARY,
69
+ VIA,
70
+ X_FORWARDED_FOR,
71
+ X_REAL_IP,
72
+ WARNING,
73
+ WWW_AUTHENTICATE;
74
+
75
+ final ByteList httpName;
76
+
77
+ EnvKey() {
78
+ this(false);
79
+ }
80
+
81
+ EnvKey(boolean raw) {
82
+ String httpName = raw ? name() : "HTTP_" + name();
83
+ this.httpName = new ByteList(ByteList.plain(httpName), false);
84
+ }
85
+
86
+ /**
87
+ * Using a perfect hash, find the EnvKey for a given field name, or null if none matches.
88
+ *
89
+ * Generated by the gperf tool as C and adapted to Java. Must be regenerated if the set of keys changes.
90
+ *
91
+ * @param buffer the buffer containing the field name
92
+ * @param start the starting offset of the field name
93
+ * @param len the length of the field name
94
+ * @return the matching EnvKey, or else null
95
+ */
96
+ public static EnvKey keyForField(byte[] buffer, int start, int len) {
97
+ int key = hash(buffer, start, len);
98
+
99
+ if (key <= MAX_HASH_VALUE) {
100
+ EnvKey match = keyList[key];
101
+
102
+ if (match != null && equalsIgnoreCase(buffer, start, len, match.name())) {
103
+ return match;
104
+ }
105
+ }
106
+ return null;
107
+ }
108
+
109
+ private static boolean equalsIgnoreCase(byte[] buf, int start, int len, String key) {
110
+ int slen = key.length();
111
+ if (len != slen) return false;
112
+ for (int i = 0; i < slen; i++) {
113
+ byte b1 = buf[start + i];
114
+ byte b2 = (byte) key.charAt(i);
115
+ if (Http11.snakeUpcase(b1) != b2) return false;
116
+ }
117
+ return true;
118
+ }
119
+
120
+ private static int hash(byte[] buffer, int start, int len) {
121
+ return len
122
+ + asso_values[Byte.toUnsignedInt(Http11.snakeUpcase(buffer[start + len - 1]))]
123
+ + asso_values[Byte.toUnsignedInt(Http11.snakeUpcase(buffer[start]))];
124
+ }
125
+
126
+ private static final int MAX_HASH_VALUE = 94;
127
+
128
+ private final static int[] asso_values = {
129
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
130
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
131
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
132
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
133
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
134
+ 95, 95, 95, 10, 95, 95, 95, 95, 95, 95,
135
+ 95, 95, 95, 95, 95, 5, 95, 15, 40, 0,
136
+ 0, 10, 10, 15, 95, 0, 20, 5, 25, 95,
137
+ 50, 40, 0, 40, 10, 35, 5, 35, 0, 10,
138
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
139
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
140
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
141
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
142
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
143
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
144
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
145
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
146
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
147
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
148
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
149
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
150
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
151
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
152
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
153
+ 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
154
+ 95, 95, 95, 95, 95, 95
155
+ };
156
+
157
+ private static final EnvKey[] keyList = {
158
+ null, null, null, null, null,
159
+ EnvKey.RANGE,
160
+ null,
161
+ EnvKey.REFERER,
162
+ EnvKey.AGE,
163
+ EnvKey.FROM,
164
+ EnvKey.KEEP_ALIVE,
165
+ EnvKey.RETRY_AFTER,
166
+ EnvKey.TE,
167
+ EnvKey.VIA,
168
+ EnvKey.ETAG,
169
+ EnvKey.X_FORWARDED_FOR,
170
+ EnvKey.EXPECT,
171
+ EnvKey.TRAILER,
172
+ EnvKey.FRAGMENT,
173
+ EnvKey.VARY,
174
+ EnvKey.ACCEPT_LANGUAGE,
175
+ EnvKey.ACCEPT,
176
+ EnvKey.REQUEST_PATH,
177
+ EnvKey.IF_RANGE,
178
+ EnvKey.HOST,
179
+ null,
180
+ EnvKey.REQUEST_URI,
181
+ EnvKey.CONTENT_TYPE,
182
+ EnvKey.CONTENT_RANGE,
183
+ EnvKey.ACCEPT_CHARSET,
184
+ EnvKey.ACCEPT_ENCODING,
185
+ EnvKey.CONTENT_LANGUAGE,
186
+ EnvKey.IF_MODIFIED_SINCE,
187
+ EnvKey.IF_MATCH,
188
+ EnvKey.IF_UNMODIFIED_SINCE,
189
+ null,
190
+ EnvKey.CONTENT_MD5,
191
+ EnvKey.TRANSFER_ENCODING,
192
+ EnvKey.IF_NONE_MATCH,
193
+ EnvKey.CONTENT_LENGTH,
194
+ null,
195
+ EnvKey.CONTENT_ENCODING,
196
+ EnvKey.UPGRADE,
197
+ EnvKey.AUTHORIZATION,
198
+ EnvKey.DATE,
199
+ EnvKey.ALLOW,
200
+ EnvKey.SERVER,
201
+ EnvKey.EXPIRES,
202
+ EnvKey.CACHE_CONTROL,
203
+ null,
204
+ EnvKey.CONNECTION,
205
+ EnvKey.WWW_AUTHENTICATE,
206
+ EnvKey.WARNING,
207
+ EnvKey.LOCATION,
208
+ EnvKey.REQUEST_METHOD,
209
+ EnvKey.USER_AGENT,
210
+ EnvKey.CONTENT_LOCATION,
211
+ EnvKey.MAX_FORWARDS,
212
+ EnvKey.ACCEPT_RANGES,
213
+ EnvKey.X_REAL_IP,
214
+ null,
215
+ EnvKey.PRAGMA,
216
+ EnvKey.QUERY_STRING,
217
+ null, null, null, null, null,
218
+ EnvKey.PROXY_AUTHENTICATE,
219
+ null, null, null, null,
220
+ EnvKey.LAST_MODIFIED,
221
+ null,
222
+ EnvKey.SERVER_PROTOCOL,
223
+ null, null, null, null, null, null, null, null, null,
224
+ null, null, null, null, null, null, null, null, null,
225
+ EnvKey.PROXY_AUTHORIZATION
226
+ };
227
+
228
+ static {
229
+ // The perfect hash function must be regenerated if the list above changes
230
+ for (EnvKey key : values()) {
231
+ EnvKey found;
232
+ assert (found = keyForKey(key)) == key : key + " was not matched by perfect hash and found " + found;
233
+ }
234
+ }
235
+
236
+ // Only used for verification of the perfect hash, at boot with asserts enabled.
237
+ private static EnvKey keyForKey(EnvKey key) {
238
+ byte[] bytes = key.name().getBytes(StandardCharsets.ISO_8859_1);
239
+ return keyList[hash(bytes, 0, bytes.length)];
240
+ }
241
+ }
@@ -10,43 +10,22 @@ import org.jruby.RubyString;
10
10
 
11
11
  import org.jruby.anno.JRubyMethod;
12
12
 
13
- import org.jruby.runtime.ObjectAllocator;
13
+ import org.jruby.runtime.ThreadContext;
14
14
  import org.jruby.runtime.builtin.IRubyObject;
15
15
 
16
16
  import org.jruby.exceptions.RaiseException;
17
17
 
18
18
  import org.jruby.util.ByteList;
19
19
 
20
+ import java.util.Arrays;
21
+
22
+ import static java.util.Arrays.stream;
23
+
20
24
  /**
21
25
  * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
22
26
  * @author <a href="mailto:headius@headius.com">Charles Oliver Nutter</a>
23
27
  */
24
28
  public class Http11 extends RubyObject {
25
- public final static int MAX_FIELD_NAME_LENGTH = 256;
26
- public final static String MAX_FIELD_NAME_LENGTH_ERR = "HTTP element FIELD_NAME is longer than the 256 allowed length.";
27
- public final static int MAX_FIELD_VALUE_LENGTH = 80 * 1024;
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 = 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
- public final static int MAX_FRAGMENT_LENGTH = 1024;
32
- public final static String MAX_FRAGMENT_LENGTH_ERR = "HTTP element FRAGMENT is longer than the 1024 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
- public final static int MAX_HEADER_LENGTH = 1024 * (80 + 32);
38
- public final static String MAX_HEADER_LENGTH_ERR = "HTTP element HEADER is longer than the 114688 allowed length.";
39
-
40
- public static final ByteList CONTENT_TYPE_BYTELIST = new ByteList(ByteList.plain("CONTENT_TYPE"));
41
- public static final ByteList CONTENT_LENGTH_BYTELIST = new ByteList(ByteList.plain("CONTENT_LENGTH"));
42
- public static final ByteList HTTP_PREFIX_BYTELIST = new ByteList(ByteList.plain("HTTP_"));
43
- public static final ByteList COMMA_SPACE_BYTELIST = new ByteList(ByteList.plain(", "));
44
- public static final ByteList REQUEST_METHOD_BYTELIST = new ByteList(ByteList.plain("REQUEST_METHOD"));
45
- public static final ByteList REQUEST_URI_BYTELIST = new ByteList(ByteList.plain("REQUEST_URI"));
46
- public static final ByteList FRAGMENT_BYTELIST = new ByteList(ByteList.plain("FRAGMENT"));
47
- public static final ByteList REQUEST_PATH_BYTELIST = new ByteList(ByteList.plain("REQUEST_PATH"));
48
- public static final ByteList QUERY_STRING_BYTELIST = new ByteList(ByteList.plain("QUERY_STRING"));
49
- public static final ByteList SERVER_PROTOCOL_BYTELIST = new ByteList(ByteList.plain("SERVER_PROTOCOL"));
50
29
 
51
30
  public static String getEnvOrProperty(String name) {
52
31
  String envValue = System.getenv(name);
@@ -69,29 +48,35 @@ public class Http11 extends RubyObject {
69
48
  }
70
49
  }
71
50
 
72
- private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
73
- public IRubyObject allocate(Ruby runtime, RubyClass klass) {
74
- return new Http11(runtime, klass);
75
- }
76
- };
77
-
78
51
  public static void createHttp11(Ruby runtime) {
79
52
  RubyModule mPuma = runtime.defineModule("Puma");
80
53
  mPuma.defineClassUnder("HttpParserError",runtime.getClass("StandardError"),runtime.getClass("StandardError").getAllocator());
81
54
 
82
- RubyClass cHttpParser = mPuma.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
55
+ // Set up pre-allocated strings for HTTP/1.1 headers and CGI variables
56
+ RubyString[] envStrings =
57
+ stream(EnvKey.values())
58
+ .map((key) -> internRubyString(runtime, key))
59
+ .toArray(RubyString[]::new);
60
+
61
+ RubyClass cHttpParser = mPuma.defineClassUnder("HttpParser",runtime.getObject(),(r, c) -> new Http11(r, c, envStrings));
83
62
  cHttpParser.defineAnnotatedMethods(Http11.class);
84
63
  }
85
64
 
86
- private Ruby runtime;
87
- private Http11Parser hp;
65
+ private static RubyString internRubyString(Ruby runtime, EnvKey key) {
66
+ return runtime.freezeAndDedupString(RubyString.newStringShared(runtime, key.httpName));
67
+ }
68
+
69
+ private final Ruby runtime;
70
+ private final Http11Parser hp;
71
+ final RubyString[] envStrings;
88
72
  private RubyString body;
89
73
 
90
- public Http11(Ruby runtime, RubyClass clazz) {
74
+ public Http11(Ruby runtime, RubyClass clazz, RubyString[] envStrings) {
91
75
  super(runtime,clazz);
92
76
  this.runtime = runtime;
93
77
  this.hp = new Http11Parser();
94
- this.hp.parser.init();
78
+ this.envStrings = envStrings;
79
+ this.hp.init();
95
80
  }
96
81
 
97
82
  public static void validateMaxLength(Ruby runtime, int len, int max, String msg) {
@@ -109,149 +94,228 @@ public class Http11 extends RubyObject {
109
94
  return (RubyClass)runtime.getModule("Puma").getConstant("HttpParserError");
110
95
  }
111
96
 
97
+ private static RubyString fstringForField(RubyString[] envStrings, byte[] buffer, int field, int flen) {
98
+ EnvKey key = EnvKey.keyForField(buffer, field, flen);
99
+ if (key != null) return envStrings[key.ordinal()];
100
+ return null;
101
+ }
102
+
112
103
  private static boolean is_ows(int c) {
113
104
  return c == ' ' || c == '\t';
114
105
  }
115
106
 
116
- public static void http_field(Ruby runtime, RubyHash req, ByteList buffer, int field, int flen, int value, int vlen) {
107
+ public static final byte[] HTTP_PREFIX_BYTELIST = ByteList.plain("HTTP_");
108
+ private static final int HTTP_PREFIX_LENGTH = 5;
109
+ public static final byte[] COMMA_SPACE_BYTELIST = ByteList.plain(", ");
110
+
111
+ public static void http_field(Ruby runtime, RubyString[] envStrings, Http11Parser hp, int vlen) {
117
112
  RubyString f;
118
113
  IRubyObject v;
119
- validateMaxLength(runtime, flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
120
- validateMaxLength(runtime, vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);
121
-
122
- ByteList b = new ByteList(buffer,field,flen);
123
- for(int i = 0,j = b.length();i<j;i++) {
124
- int bite = b.get(i) & 0xFF;
125
- if(bite == '-') {
126
- b.set(i, (byte)'_');
127
- } else if(bite == '_') {
128
- b.set(i, (byte)',');
129
- } else {
130
- b.set(i, (byte)Character.toUpperCase(bite));
131
- }
114
+ int field_len = hp.field_len;
115
+ validateFieldNameLength(runtime, field_len);
116
+ validateFieldValueLength(runtime, vlen);
117
+
118
+ byte[] buffer = hp.buffer;
119
+ int field_start = hp.field_start;
120
+ f = fstringForField(envStrings, buffer, field_start, field_len);
121
+
122
+ if (f == null) {
123
+ f = newHttpHeader(runtime, buffer, field_start, field_len);
132
124
  }
133
125
 
134
- while (vlen > 0 && is_ows(buffer.get(value + vlen - 1))) vlen--;
135
- while (vlen > 0 && is_ows(buffer.get(value))) {
126
+ int mark = hp.mark;
127
+ while (vlen > 0) {
128
+ if (!is_ows(buffer[mark + vlen - 1])) break;
136
129
  vlen--;
137
- value++;
138
130
  }
139
-
140
- if (b.equals(CONTENT_LENGTH_BYTELIST) || b.equals(CONTENT_TYPE_BYTELIST)) {
141
- f = RubyString.newString(runtime, b);
142
- } else {
143
- f = RubyString.newStringShared(runtime, HTTP_PREFIX_BYTELIST);
144
- f.cat(b);
131
+ while (vlen > 0 && is_ows(buffer[mark])) {
132
+ vlen--;
133
+ mark++;
145
134
  }
146
135
 
147
- b = new ByteList(buffer, value, vlen);
136
+ RubyHash req = hp.data;
148
137
  v = req.fastARef(f);
149
138
  if (v == null || v.isNil()) {
150
- req.fastASet(f, RubyString.newString(runtime, b));
139
+ req.fastASet(f, RubyString.newStringShared(runtime, buffer, mark, vlen));
151
140
  } else {
152
141
  RubyString vs = v.convertToString();
153
142
  vs.cat(COMMA_SPACE_BYTELIST);
154
- vs.cat(b);
143
+ vs.cat(buffer, mark, vlen);
155
144
  }
156
145
  }
157
146
 
158
- public static void request_method(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
159
- RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
160
- req.fastASet(RubyString.newStringShared(runtime, REQUEST_METHOD_BYTELIST),val);
147
+ private static RubyString newHttpHeader(Ruby runtime, byte[] buffer, int field_start, int field_len) {
148
+ byte[] bytes = new byte[HTTP_PREFIX_LENGTH + field_len];
149
+ System.arraycopy(HTTP_PREFIX_BYTELIST, 0, bytes, 0, HTTP_PREFIX_LENGTH);
150
+ snakeCaseCopy(buffer, field_start, field_len, bytes, HTTP_PREFIX_LENGTH);
151
+ return RubyString.newStringNoCopy(runtime, bytes);
161
152
  }
162
153
 
163
- public static void request_uri(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
164
- validateMaxLength(runtime, length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
165
- RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
166
- req.fastASet(RubyString.newStringShared(runtime, REQUEST_URI_BYTELIST),val);
154
+ static void snakeCaseCopy(byte[] s, int soff, int slen, byte[] d, int doff) {
155
+ for (int i = 0; i < slen; i++) {
156
+ int si = soff + i;
157
+ byte sch = s[si];
158
+ int di = doff + i;
159
+ byte dch = snakeUpcase(sch);
160
+ d[di] = dch;
161
+ }
167
162
  }
168
163
 
169
- public static void fragment(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
170
- validateMaxLength(runtime, length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
171
- RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
172
- req.fastASet(RubyString.newStringShared(runtime, FRAGMENT_BYTELIST),val);
164
+ static byte snakeUpcase(byte ch) {
165
+ if (ch >= 'a' && ch <= 'z')
166
+ ch = (byte) (ch & ~0x20);
167
+ else if (ch == '_')
168
+ ch = ',';
169
+ else if (ch == '-')
170
+ ch = '_';
171
+ return ch;
173
172
  }
174
173
 
175
- public static void request_path(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
176
- validateMaxLength(runtime, length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
177
- RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
178
- req.fastASet(RubyString.newStringShared(runtime, REQUEST_PATH_BYTELIST),val);
174
+ public static void request_method(Ruby runtime, RubyString[] envStrings, Http11Parser hp, int length) {
175
+ hp.data.fastASet(envStrings[EnvKey.REQUEST_METHOD.ordinal()], newValueString(runtime, hp, length));
179
176
  }
180
177
 
181
- public static void query_string(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
182
- validateMaxLength(runtime, length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
183
- RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
184
- req.fastASet(RubyString.newStringShared(runtime, QUERY_STRING_BYTELIST),val);
178
+ public static void request_uri(Ruby runtime, RubyString[] envStrings, Http11Parser hp, int length) {
179
+ validateRequestURILength(runtime, length);
180
+ hp.data.fastASet(envStrings[EnvKey.REQUEST_URI.ordinal()], newValueString(runtime, hp, length));
181
+ }
182
+
183
+ public static void fragment(Ruby runtime, RubyString[] envStrings, Http11Parser hp, int length) {
184
+ validateFragmentLength(runtime, length);
185
+ hp.data.fastASet(envStrings[EnvKey.FRAGMENT.ordinal()], newValueString(runtime, hp, length));
186
+ }
187
+
188
+ public static void request_path(Ruby runtime, RubyString[] envStrings, Http11Parser hp, int length) {
189
+ validateRequestPathLength(runtime, length);
190
+ hp.data.fastASet(envStrings[EnvKey.REQUEST_PATH.ordinal()], newValueString(runtime, hp, length));
191
+ }
192
+
193
+ public static void query_string(Ruby runtime, RubyString[] envStrings, Http11Parser hp, int length) {
194
+ validateQueryStringLength(runtime, length);
195
+ hp.data.fastASet(envStrings[EnvKey.QUERY_STRING.ordinal()], newQueryString(runtime, hp, length));
185
196
  }
186
197
 
187
- public static void server_protocol(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
188
- RubyString val = RubyString.newString(runtime,new ByteList(buffer,at,length));
189
- req.fastASet(RubyString.newStringShared(runtime, SERVER_PROTOCOL_BYTELIST),val);
198
+ public static void server_protocol(Ruby runtime, RubyString[] envStrings, Http11Parser hp, int length) {
199
+ hp.data.fastASet(envStrings[EnvKey.SERVER_PROTOCOL.ordinal()], newValueString(runtime, hp, length));
190
200
  }
191
201
 
192
- public void header_done(Ruby runtime, RubyHash req, ByteList buffer, int at, int length) {
193
- body = RubyString.newStringShared(runtime, new ByteList(buffer, at, length));
202
+ public void header_done(Ruby runtime, Http11Parser hp, int at, int length) {
203
+ body = RubyString.newStringShared(runtime, hp.buffer, at, length);
194
204
  }
195
205
 
196
206
  @JRubyMethod
197
207
  public IRubyObject initialize() {
198
- this.hp.parser.init();
208
+ this.hp.init();
199
209
  return this;
200
210
  }
201
211
 
202
212
  @JRubyMethod
203
- public IRubyObject reset() {
204
- this.hp.parser.init();
205
- return runtime.getNil();
213
+ public IRubyObject reset(ThreadContext context) {
214
+ this.hp.init();
215
+ return context.nil;
206
216
  }
207
217
 
208
218
  @JRubyMethod
209
- public IRubyObject finish() {
210
- this.hp.finish();
211
- return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
219
+ public IRubyObject finish(ThreadContext context) {
220
+ Http11Parser hp = this.hp;
221
+ hp.finish();
222
+ return hp.is_finished() ? context.tru : context.fals;
212
223
  }
213
224
 
214
225
  @JRubyMethod
215
226
  public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) {
227
+ Ruby runtime = this.runtime;
216
228
  int from = RubyNumeric.fix2int(start);
217
- ByteList d = ((RubyString)data).getByteList();
229
+ RubyString dataString = (RubyString) data;
230
+ dataString.setByteListShared();
231
+ ByteList d = dataString.getByteList();
218
232
  if(from >= d.length()) {
219
233
  throw newHTTPParserError(runtime, "Requested start is after data buffer end.");
220
234
  } else {
221
235
  Http11Parser hp = this.hp;
222
- Http11Parser.HttpParser parser = hp.parser;
223
236
 
224
- parser.data = (RubyHash) req_hash;
237
+ hp.data = (RubyHash) req_hash;
225
238
 
226
239
  hp.execute(runtime, this, d,from);
227
240
 
228
- validateMaxLength(runtime, parser.nread,MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR);
241
+ validateMaxHeaderLength(runtime, hp);
229
242
 
230
243
  if(hp.has_error()) {
231
244
  throw newHTTPParserError(runtime, "Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?");
232
245
  } else {
233
- return runtime.newFixnum(parser.nread);
246
+ return runtime.newFixnum(hp.nread);
234
247
  }
235
248
  }
236
249
  }
237
250
 
238
251
  @JRubyMethod(name = "error?")
239
- public IRubyObject has_error() {
240
- return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse();
252
+ public IRubyObject has_error(ThreadContext context) {
253
+ return this.hp.has_error() ? context.tru : context.fals;
241
254
  }
242
255
 
243
256
  @JRubyMethod(name = "finished?")
244
- public IRubyObject is_finished() {
245
- return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
257
+ public IRubyObject is_finished(ThreadContext context) {
258
+ return this.hp.is_finished() ? context.tru : context.fals;
246
259
  }
247
260
 
248
261
  @JRubyMethod
249
262
  public IRubyObject nread() {
250
- return runtime.newFixnum(this.hp.parser.nread);
263
+ return runtime.newFixnum(this.hp.nread);
251
264
  }
252
265
 
253
266
  @JRubyMethod
254
267
  public IRubyObject body() {
255
268
  return body;
256
269
  }
270
+
271
+ public final static int MAX_FIELD_NAME_LENGTH = 256;
272
+ public final static String MAX_FIELD_NAME_LENGTH_ERR = "HTTP element FIELD_NAME is longer than the 256 allowed length.";
273
+ public final static int MAX_FIELD_VALUE_LENGTH = 80 * 1024;
274
+ public final static String MAX_FIELD_VALUE_LENGTH_ERR = "HTTP element FIELD_VALUE is longer than the 81920 allowed length.";
275
+ public final static int MAX_REQUEST_URI_LENGTH = getConstLength("PUMA_REQUEST_URI_MAX_LENGTH", 1024 * 12);
276
+ public final static String MAX_REQUEST_URI_LENGTH_ERR = "HTTP element REQUEST_URI is longer than the " + MAX_REQUEST_URI_LENGTH + " allowed length.";
277
+ public final static int MAX_FRAGMENT_LENGTH = 1024;
278
+ public final static String MAX_FRAGMENT_LENGTH_ERR = "HTTP element FRAGMENT is longer than the 1024 allowed length.";
279
+ public final static int MAX_REQUEST_PATH_LENGTH = getConstLength("PUMA_REQUEST_PATH_MAX_LENGTH", 8192);
280
+ public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the " + MAX_REQUEST_PATH_LENGTH + " allowed length.";
281
+ public final static int MAX_QUERY_STRING_LENGTH = getConstLength("PUMA_QUERY_STRING_MAX_LENGTH", 10 * 1024);
282
+ public final static String MAX_QUERY_STRING_LENGTH_ERR = "HTTP element QUERY_STRING is longer than the " + MAX_QUERY_STRING_LENGTH +" allowed length.";
283
+ public final static int MAX_HEADER_LENGTH = 1024 * (80 + 32);
284
+ public final static String MAX_HEADER_LENGTH_ERR = "HTTP element HEADER is longer than the 114688 allowed length.";
285
+
286
+ private static void validateFieldNameLength(Ruby runtime, int field_len) {
287
+ validateMaxLength(runtime, field_len, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
288
+ }
289
+
290
+ private static void validateFieldValueLength(Ruby runtime, int field_len) {
291
+ validateMaxLength(runtime, field_len, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);
292
+ }
293
+
294
+ private static void validateRequestURILength(Ruby runtime, int length) {
295
+ validateMaxLength(runtime, length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
296
+ }
297
+
298
+ private static void validateFragmentLength(Ruby runtime, int length) {
299
+ validateMaxLength(runtime, length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
300
+ }
301
+
302
+ private static void validateRequestPathLength(Ruby runtime, int length) {
303
+ validateMaxLength(runtime, length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
304
+ }
305
+
306
+ private static void validateQueryStringLength(Ruby runtime, int length) {
307
+ validateMaxLength(runtime, length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
308
+ }
309
+
310
+ private static RubyString newValueString(Ruby runtime, Http11Parser hp, int length) {
311
+ return RubyString.newStringShared(runtime, hp.buffer, hp.mark, length);
312
+ }
313
+
314
+ private static RubyString newQueryString(Ruby runtime, Http11Parser hp, int length) {
315
+ return RubyString.newStringShared(runtime, hp.buffer, hp.query_start, length);
316
+ }
317
+
318
+ private static void validateMaxHeaderLength(Ruby runtime, Http11Parser hp) {
319
+ validateMaxLength(runtime, hp.nread, MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR);
320
+ }
257
321
  }// Http11