tiny-fast-gem 0.0.1

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 (99) hide show
  1. checksums.yaml +7 -0
  2. data/puma-8.0.2/History.md +3334 -0
  3. data/puma-8.0.2/LICENSE +29 -0
  4. data/puma-8.0.2/README.md +484 -0
  5. data/puma-8.0.2/bin/puma +10 -0
  6. data/puma-8.0.2/bin/puma-wild +25 -0
  7. data/puma-8.0.2/bin/pumactl +12 -0
  8. data/puma-8.0.2/docs/5.0-Upgrade.md +98 -0
  9. data/puma-8.0.2/docs/6.0-Upgrade.md +56 -0
  10. data/puma-8.0.2/docs/7.0-Upgrade.md +52 -0
  11. data/puma-8.0.2/docs/8.0-Upgrade.md +100 -0
  12. data/puma-8.0.2/docs/architecture.md +74 -0
  13. data/puma-8.0.2/docs/compile_options.md +55 -0
  14. data/puma-8.0.2/docs/deployment.md +137 -0
  15. data/puma-8.0.2/docs/fork_worker.md +41 -0
  16. data/puma-8.0.2/docs/grpc.md +62 -0
  17. data/puma-8.0.2/docs/images/favicon.svg +1 -0
  18. data/puma-8.0.2/docs/images/puma-connection-flow-no-reactor.png +0 -0
  19. data/puma-8.0.2/docs/images/puma-connection-flow.png +0 -0
  20. data/puma-8.0.2/docs/images/puma-general-arch.png +0 -0
  21. data/puma-8.0.2/docs/images/running-puma.svg +1 -0
  22. data/puma-8.0.2/docs/images/standard-logo.svg +1 -0
  23. data/puma-8.0.2/docs/java_options.md +54 -0
  24. data/puma-8.0.2/docs/jungle/README.md +9 -0
  25. data/puma-8.0.2/docs/jungle/rc.d/README.md +74 -0
  26. data/puma-8.0.2/docs/jungle/rc.d/puma +61 -0
  27. data/puma-8.0.2/docs/jungle/rc.d/puma.conf +10 -0
  28. data/puma-8.0.2/docs/kubernetes.md +73 -0
  29. data/puma-8.0.2/docs/nginx.md +80 -0
  30. data/puma-8.0.2/docs/plugins.md +42 -0
  31. data/puma-8.0.2/docs/rails_dev_mode.md +28 -0
  32. data/puma-8.0.2/docs/restart.md +65 -0
  33. data/puma-8.0.2/docs/signals.md +98 -0
  34. data/puma-8.0.2/docs/stats.md +148 -0
  35. data/puma-8.0.2/docs/systemd.md +253 -0
  36. data/puma-8.0.2/docs/testing_benchmarks_local_files.md +150 -0
  37. data/puma-8.0.2/docs/testing_test_rackup_ci_files.md +36 -0
  38. data/puma-8.0.2/ext/puma_http11/PumaHttp11Service.java +17 -0
  39. data/puma-8.0.2/ext/puma_http11/extconf.rb +65 -0
  40. data/puma-8.0.2/ext/puma_http11/http11_parser.c +1057 -0
  41. data/puma-8.0.2/ext/puma_http11/http11_parser.h +65 -0
  42. data/puma-8.0.2/ext/puma_http11/http11_parser.java.rl +131 -0
  43. data/puma-8.0.2/ext/puma_http11/http11_parser.rl +149 -0
  44. data/puma-8.0.2/ext/puma_http11/http11_parser_common.rl +54 -0
  45. data/puma-8.0.2/ext/puma_http11/mini_ssl.c +852 -0
  46. data/puma-8.0.2/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  47. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/EnvKey.java +241 -0
  48. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/Http11.java +321 -0
  49. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/Http11Parser.java +441 -0
  50. data/puma-8.0.2/ext/puma_http11/org/jruby/puma/MiniSSL.java +509 -0
  51. data/puma-8.0.2/ext/puma_http11/puma_http11.c +499 -0
  52. data/puma-8.0.2/lib/puma/app/status.rb +104 -0
  53. data/puma-8.0.2/lib/puma/binder.rb +511 -0
  54. data/puma-8.0.2/lib/puma/cli.rb +245 -0
  55. data/puma-8.0.2/lib/puma/client.rb +756 -0
  56. data/puma-8.0.2/lib/puma/client_env.rb +171 -0
  57. data/puma-8.0.2/lib/puma/cluster/worker.rb +183 -0
  58. data/puma-8.0.2/lib/puma/cluster/worker_handle.rb +127 -0
  59. data/puma-8.0.2/lib/puma/cluster.rb +634 -0
  60. data/puma-8.0.2/lib/puma/cluster_accept_loop_delay.rb +91 -0
  61. data/puma-8.0.2/lib/puma/commonlogger.rb +115 -0
  62. data/puma-8.0.2/lib/puma/configuration.rb +522 -0
  63. data/puma-8.0.2/lib/puma/const.rb +308 -0
  64. data/puma-8.0.2/lib/puma/control_cli.rb +320 -0
  65. data/puma-8.0.2/lib/puma/detect.rb +58 -0
  66. data/puma-8.0.2/lib/puma/dsl.rb +1562 -0
  67. data/puma-8.0.2/lib/puma/error_logger.rb +115 -0
  68. data/puma-8.0.2/lib/puma/events.rb +72 -0
  69. data/puma-8.0.2/lib/puma/io_buffer.rb +50 -0
  70. data/puma-8.0.2/lib/puma/jruby_restart.rb +11 -0
  71. data/puma-8.0.2/lib/puma/json_serialization.rb +96 -0
  72. data/puma-8.0.2/lib/puma/launcher/bundle_pruner.rb +102 -0
  73. data/puma-8.0.2/lib/puma/launcher.rb +501 -0
  74. data/puma-8.0.2/lib/puma/log_writer.rb +153 -0
  75. data/puma-8.0.2/lib/puma/minissl/context_builder.rb +96 -0
  76. data/puma-8.0.2/lib/puma/minissl.rb +458 -0
  77. data/puma-8.0.2/lib/puma/null_io.rb +101 -0
  78. data/puma-8.0.2/lib/puma/plugin/systemd.rb +90 -0
  79. data/puma-8.0.2/lib/puma/plugin/tmp_restart.rb +36 -0
  80. data/puma-8.0.2/lib/puma/plugin.rb +111 -0
  81. data/puma-8.0.2/lib/puma/rack/builder.rb +297 -0
  82. data/puma-8.0.2/lib/puma/rack/urlmap.rb +93 -0
  83. data/puma-8.0.2/lib/puma/rack_default.rb +24 -0
  84. data/puma-8.0.2/lib/puma/reactor.rb +131 -0
  85. data/puma-8.0.2/lib/puma/response.rb +532 -0
  86. data/puma-8.0.2/lib/puma/runner.rb +211 -0
  87. data/puma-8.0.2/lib/puma/sd_notify.rb +146 -0
  88. data/puma-8.0.2/lib/puma/server.rb +773 -0
  89. data/puma-8.0.2/lib/puma/server_plugin_control.rb +32 -0
  90. data/puma-8.0.2/lib/puma/single.rb +72 -0
  91. data/puma-8.0.2/lib/puma/state_file.rb +69 -0
  92. data/puma-8.0.2/lib/puma/thread_pool.rb +517 -0
  93. data/puma-8.0.2/lib/puma/util.rb +134 -0
  94. data/puma-8.0.2/lib/puma.rb +88 -0
  95. data/puma-8.0.2/lib/rack/handler/puma.rb +144 -0
  96. data/puma-8.0.2/tools/Dockerfile +26 -0
  97. data/puma-8.0.2/tools/trickletest.rb +44 -0
  98. data/tiny-fast-gem.gemspec +12 -0
  99. metadata +138 -0
@@ -0,0 +1,499 @@
1
+ /**
2
+ * Copyright (c) 2005 Zed A. Shaw
3
+ * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ * License 3-clause BSD
5
+ */
6
+
7
+ #define RSTRING_NOT_MODIFIED 1
8
+
9
+ #include "ruby.h"
10
+ #include "ruby/encoding.h"
11
+ #include <assert.h>
12
+ #include <string.h>
13
+ #include <ctype.h>
14
+ #include "http11_parser.h"
15
+
16
+ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
17
+
18
+ static VALUE eHttpParserError;
19
+
20
+ #define HTTP_PREFIX "HTTP_"
21
+ #define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1)
22
+
23
+ static VALUE global_request_method;
24
+ static VALUE global_request_uri;
25
+ static VALUE global_fragment;
26
+ static VALUE global_query_string;
27
+ static VALUE global_server_protocol;
28
+ static VALUE global_request_path;
29
+
30
+ /** Defines common length and error messages for input length validation. */
31
+ #define QUOTE(s) #s
32
+ #define EXPAND_MAX_LENGTH_VALUE(s) QUOTE(s)
33
+ #define DEF_MAX_LENGTH(N,length) const size_t MAX_##N##_LENGTH = length; const char *MAX_##N##_LENGTH_ERR = "HTTP element " # N " is longer than the " EXPAND_MAX_LENGTH_VALUE(length) " allowed length (was %d)"
34
+
35
+ /** Validates the max length of given input and throws an HttpParserError exception if over. */
36
+ #define VALIDATE_MAX_LENGTH(len, N) if(len > MAX_##N##_LENGTH) { rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR, len); }
37
+
38
+ /** Defines global strings in the init method. */
39
+ static inline void DEF_GLOBAL(VALUE *var, const char *cstr)
40
+ {
41
+ rb_global_variable(var);
42
+ *var = rb_enc_interned_str_cstr(cstr, rb_utf8_encoding());
43
+ }
44
+
45
+ /* Defines the maximum allowed lengths for various input elements.*/
46
+ #ifndef PUMA_REQUEST_URI_MAX_LENGTH
47
+ #define PUMA_REQUEST_URI_MAX_LENGTH (1024 * 12)
48
+ #endif
49
+
50
+ #ifndef PUMA_REQUEST_PATH_MAX_LENGTH
51
+ #define PUMA_REQUEST_PATH_MAX_LENGTH (8192)
52
+ #endif
53
+
54
+ #ifndef PUMA_QUERY_STRING_MAX_LENGTH
55
+ #define PUMA_QUERY_STRING_MAX_LENGTH (1024 * 10)
56
+ #endif
57
+
58
+ DEF_MAX_LENGTH(FIELD_NAME, 256);
59
+ DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
60
+ DEF_MAX_LENGTH(REQUEST_URI, PUMA_REQUEST_URI_MAX_LENGTH);
61
+ DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
62
+ DEF_MAX_LENGTH(REQUEST_PATH, PUMA_REQUEST_PATH_MAX_LENGTH);
63
+ DEF_MAX_LENGTH(QUERY_STRING, PUMA_QUERY_STRING_MAX_LENGTH);
64
+ DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32)));
65
+
66
+ struct common_field {
67
+ const size_t len;
68
+ const char *name;
69
+ int raw;
70
+ VALUE value;
71
+ };
72
+
73
+ /*
74
+ * A list of common HTTP headers we expect to receive.
75
+ * This allows us to avoid repeatedly creating identical string
76
+ * objects to be used with rb_hash_aset().
77
+ */
78
+ static struct common_field common_http_fields[] = {
79
+ # define f(N) { (sizeof(N) - 1), N, 0, Qnil }
80
+ # define fr(N) { (sizeof(N) - 1), N, 1, Qnil }
81
+ f("ACCEPT"),
82
+ f("ACCEPT_CHARSET"),
83
+ f("ACCEPT_ENCODING"),
84
+ f("ACCEPT_LANGUAGE"),
85
+ f("ALLOW"),
86
+ f("AUTHORIZATION"),
87
+ f("CACHE_CONTROL"),
88
+ f("CONNECTION"),
89
+ f("CONTENT_ENCODING"),
90
+ fr("CONTENT_LENGTH"),
91
+ fr("CONTENT_TYPE"),
92
+ f("COOKIE"),
93
+ f("DATE"),
94
+ f("EXPECT"),
95
+ f("FROM"),
96
+ f("HOST"),
97
+ f("IF_MATCH"),
98
+ f("IF_MODIFIED_SINCE"),
99
+ f("IF_NONE_MATCH"),
100
+ f("IF_RANGE"),
101
+ f("IF_UNMODIFIED_SINCE"),
102
+ f("KEEP_ALIVE"), /* Firefox sends this */
103
+ f("MAX_FORWARDS"),
104
+ f("PRAGMA"),
105
+ f("PROXY_AUTHORIZATION"),
106
+ f("RANGE"),
107
+ f("REFERER"),
108
+ f("TE"),
109
+ f("TRAILER"),
110
+ f("TRANSFER_ENCODING"),
111
+ f("UPGRADE"),
112
+ f("USER_AGENT"),
113
+ f("VIA"),
114
+ f("X_FORWARDED_FOR"), /* common for proxies */
115
+ f("X_REAL_IP"), /* common for proxies */
116
+ f("WARNING")
117
+ # undef f
118
+ };
119
+
120
+ static void init_common_fields(void)
121
+ {
122
+ unsigned i;
123
+ struct common_field *cf = common_http_fields;
124
+ char tmp[256]; /* MAX_FIELD_NAME_LENGTH */
125
+ memcpy(tmp, HTTP_PREFIX, HTTP_PREFIX_LEN);
126
+
127
+ for(i = 0; i < ARRAY_SIZE(common_http_fields); cf++, i++) {
128
+ rb_global_variable(&cf->value);
129
+ if(cf->raw) {
130
+ cf->value = rb_enc_interned_str(cf->name, cf->len, rb_utf8_encoding());
131
+ } else {
132
+ memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1);
133
+ cf->value = rb_enc_interned_str(tmp, HTTP_PREFIX_LEN + cf->len, rb_utf8_encoding());
134
+ }
135
+ }
136
+ }
137
+
138
+ static VALUE find_common_field_value(const char *field, size_t flen)
139
+ {
140
+ unsigned i;
141
+ struct common_field *cf = common_http_fields;
142
+ for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) {
143
+ if (cf->len == flen && !memcmp(cf->name, field, flen))
144
+ return cf->value;
145
+ }
146
+ return Qnil;
147
+ }
148
+
149
+ static int is_ows(const char c) {
150
+ return c == ' ' || c == '\t';
151
+ }
152
+
153
+ static void http_field(puma_parser* hp, const char *field, size_t flen,
154
+ const char *value, size_t vlen)
155
+ {
156
+ VALUE f = Qnil;
157
+ VALUE v;
158
+
159
+ VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
160
+ VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);
161
+
162
+ f = find_common_field_value(field, flen);
163
+
164
+ if (f == Qnil) {
165
+ /*
166
+ * We got a strange header that we don't have a memoized value for.
167
+ * Fallback to creating a new string to use as a hash key.
168
+ */
169
+
170
+ size_t new_size = HTTP_PREFIX_LEN + flen;
171
+ assert(new_size < BUFFER_LEN);
172
+
173
+ memcpy(hp->buf, HTTP_PREFIX, HTTP_PREFIX_LEN);
174
+ memcpy(hp->buf + HTTP_PREFIX_LEN, field, flen);
175
+
176
+ f = rb_enc_interned_str(hp->buf, new_size, rb_utf8_encoding());
177
+ }
178
+
179
+ while (vlen > 0 && is_ows(value[vlen - 1])) vlen--;
180
+ while (vlen > 0 && is_ows(value[0])) {
181
+ vlen--;
182
+ value++;
183
+ }
184
+
185
+ /* check for duplicate header */
186
+ v = rb_hash_aref(hp->request, f);
187
+
188
+ if (v == Qnil) {
189
+ v = rb_str_new(value, vlen);
190
+ rb_hash_aset(hp->request, f, v);
191
+ } else {
192
+ /* if duplicate header, normalize to comma-separated values */
193
+ rb_str_cat2(v, ", ");
194
+ rb_str_cat(v, value, vlen);
195
+ }
196
+ }
197
+
198
+ static void request_method(puma_parser* hp, const char *at, size_t length)
199
+ {
200
+ VALUE val = Qnil;
201
+
202
+ val = rb_str_new(at, length);
203
+ rb_hash_aset(hp->request, global_request_method, val);
204
+ }
205
+
206
+ static void request_uri(puma_parser* hp, const char *at, size_t length)
207
+ {
208
+ VALUE val = Qnil;
209
+
210
+ VALIDATE_MAX_LENGTH(length, REQUEST_URI);
211
+
212
+ val = rb_str_new(at, length);
213
+ rb_hash_aset(hp->request, global_request_uri, val);
214
+ }
215
+
216
+ static void fragment(puma_parser* hp, const char *at, size_t length)
217
+ {
218
+ VALUE val = Qnil;
219
+
220
+ VALIDATE_MAX_LENGTH(length, FRAGMENT);
221
+
222
+ val = rb_str_new(at, length);
223
+ rb_hash_aset(hp->request, global_fragment, val);
224
+ }
225
+
226
+ static void request_path(puma_parser* hp, const char *at, size_t length)
227
+ {
228
+ VALUE val = Qnil;
229
+
230
+ VALIDATE_MAX_LENGTH(length, REQUEST_PATH);
231
+
232
+ val = rb_str_new(at, length);
233
+ rb_hash_aset(hp->request, global_request_path, val);
234
+ }
235
+
236
+ static void query_string(puma_parser* hp, const char *at, size_t length)
237
+ {
238
+ VALUE val = Qnil;
239
+
240
+ VALIDATE_MAX_LENGTH(length, QUERY_STRING);
241
+
242
+ val = rb_str_new(at, length);
243
+ rb_hash_aset(hp->request, global_query_string, val);
244
+ }
245
+
246
+ static void server_protocol(puma_parser* hp, const char *at, size_t length)
247
+ {
248
+ VALUE val = rb_str_new(at, length);
249
+ rb_hash_aset(hp->request, global_server_protocol, val);
250
+ }
251
+
252
+ /** Finalizes the request header to have a bunch of stuff that's
253
+ needed. */
254
+
255
+ static void header_done(puma_parser* hp, const char *at, size_t length)
256
+ {
257
+ hp->body = rb_str_new(at, length);
258
+ }
259
+
260
+
261
+ static void HttpParser_mark(void *ptr) {
262
+ puma_parser *hp = ptr;
263
+ rb_gc_mark_movable(hp->request);
264
+ rb_gc_mark_movable(hp->body);
265
+ }
266
+
267
+ static size_t HttpParser_size(const void *ptr) {
268
+ return sizeof(puma_parser);
269
+ }
270
+
271
+ static void HttpParser_compact(void *ptr) {
272
+ puma_parser *hp = ptr;
273
+ hp->request = rb_gc_location(hp->request);
274
+ hp->body = rb_gc_location(hp->body);
275
+ }
276
+
277
+ static const rb_data_type_t HttpParser_data_type = {
278
+ .wrap_struct_name = "Puma::HttpParser",
279
+ .function = {
280
+ .dmark = HttpParser_mark,
281
+ .dfree = RUBY_TYPED_DEFAULT_FREE,
282
+ .dsize = HttpParser_size,
283
+ .dcompact = HttpParser_compact,
284
+ },
285
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
286
+ };
287
+
288
+ static VALUE HttpParser_alloc(VALUE klass)
289
+ {
290
+ puma_parser *hp = ALLOC_N(puma_parser, 1);
291
+ hp->http_field = http_field;
292
+ hp->request_method = request_method;
293
+ hp->request_uri = request_uri;
294
+ hp->fragment = fragment;
295
+ hp->request_path = request_path;
296
+ hp->query_string = query_string;
297
+ hp->server_protocol = server_protocol;
298
+ hp->header_done = header_done;
299
+ hp->request = Qnil;
300
+
301
+ puma_parser_init(hp);
302
+
303
+ return TypedData_Wrap_Struct(klass, &HttpParser_data_type, hp);
304
+ }
305
+
306
+ static inline puma_parser *HttpParser_unwrap(VALUE self)
307
+ {
308
+ puma_parser *http;
309
+ TypedData_Get_Struct(self, puma_parser, &HttpParser_data_type, http);
310
+ if (http == NULL) {
311
+ rb_raise(rb_eArgError, "%s", "NULL http_parser found");
312
+ }
313
+ return http;
314
+ }
315
+
316
+ /**
317
+ * call-seq:
318
+ * parser.new -> parser
319
+ *
320
+ * Creates a new parser.
321
+ */
322
+ static VALUE HttpParser_init(VALUE self)
323
+ {
324
+ puma_parser *http = HttpParser_unwrap(self);
325
+ puma_parser_init(http);
326
+
327
+ return self;
328
+ }
329
+
330
+
331
+ /**
332
+ * call-seq:
333
+ * parser.reset -> nil
334
+ *
335
+ * Resets the parser to it's initial state so that you can reuse it
336
+ * rather than making new ones.
337
+ */
338
+ static VALUE HttpParser_reset(VALUE self)
339
+ {
340
+ puma_parser *http = HttpParser_unwrap(self);
341
+ puma_parser_init(http);
342
+
343
+ return Qnil;
344
+ }
345
+
346
+
347
+ /**
348
+ * call-seq:
349
+ * parser.finish -> true/false
350
+ *
351
+ * Finishes a parser early which could put in a "good" or bad state.
352
+ * You should call reset after finish it or bad things will happen.
353
+ */
354
+ static VALUE HttpParser_finish(VALUE self)
355
+ {
356
+ puma_parser *http = HttpParser_unwrap(self);
357
+ puma_parser_finish(http);
358
+
359
+ return puma_parser_is_finished(http) ? Qtrue : Qfalse;
360
+ }
361
+
362
+
363
+ /**
364
+ * call-seq:
365
+ * parser.execute(req_hash, data, start) -> Integer
366
+ *
367
+ * Takes a Hash and a String of data, parses the String of data filling in the Hash
368
+ * returning an Integer to indicate how much of the data has been read. No matter
369
+ * what the return value, you should call HttpParser#finished? and HttpParser#error?
370
+ * to figure out if it's done parsing or there was an error.
371
+ *
372
+ * This function now throws an exception when there is a parsing error. This makes
373
+ * the logic for working with the parser much easier. You can still test for an
374
+ * error, but now you need to wrap the parser with an exception handling block.
375
+ *
376
+ * The third argument allows for parsing a partial request and then continuing
377
+ * the parsing from that position. It needs all of the original data as well
378
+ * so you have to append to the data buffer as you read.
379
+ */
380
+ static VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
381
+ {
382
+ puma_parser *http = HttpParser_unwrap(self);
383
+ int from = 0;
384
+ char *dptr = NULL;
385
+ long dlen = 0;
386
+
387
+ from = FIX2INT(start);
388
+ RSTRING_GETMEM(data, dptr, dlen);
389
+
390
+ if(from >= dlen) {
391
+ rb_raise(eHttpParserError, "%s", "Requested start is after data buffer end.");
392
+ } else {
393
+ http->request = req_hash;
394
+ puma_parser_execute(http, dptr, dlen, from);
395
+
396
+ VALIDATE_MAX_LENGTH(puma_parser_nread(http), HEADER);
397
+
398
+ if(puma_parser_has_error(http)) {
399
+ rb_raise(eHttpParserError, "%s", "Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?");
400
+ } else {
401
+ return INT2FIX(puma_parser_nread(http));
402
+ }
403
+ }
404
+ }
405
+
406
+
407
+
408
+ /**
409
+ * call-seq:
410
+ * parser.error? -> true/false
411
+ *
412
+ * Tells you whether the parser is in an error state.
413
+ */
414
+ static VALUE HttpParser_has_error(VALUE self)
415
+ {
416
+ puma_parser *http = HttpParser_unwrap(self);
417
+
418
+ return puma_parser_has_error(http) ? Qtrue : Qfalse;
419
+ }
420
+
421
+
422
+ /**
423
+ * call-seq:
424
+ * parser.finished? -> true/false
425
+ *
426
+ * Tells you whether the parser is finished or not and in a good state.
427
+ */
428
+ static VALUE HttpParser_is_finished(VALUE self)
429
+ {
430
+ puma_parser *http = HttpParser_unwrap(self);
431
+
432
+ return puma_parser_is_finished(http) ? Qtrue : Qfalse;
433
+ }
434
+
435
+
436
+ /**
437
+ * call-seq:
438
+ * parser.nread -> Integer
439
+ *
440
+ * Returns the amount of data processed so far during this processing cycle. It is
441
+ * set to 0 on initialize or reset calls and is incremented each time execute is called.
442
+ */
443
+ static VALUE HttpParser_nread(VALUE self)
444
+ {
445
+ puma_parser *http = HttpParser_unwrap(self);
446
+
447
+ return INT2FIX(http->nread);
448
+ }
449
+
450
+ /**
451
+ * call-seq:
452
+ * parser.body -> nil or String
453
+ *
454
+ * If the request included a body, returns it.
455
+ */
456
+ static VALUE HttpParser_body(VALUE self) {
457
+ puma_parser *http = HttpParser_unwrap(self);
458
+
459
+ return http->body;
460
+ }
461
+
462
+ #ifdef HAVE_OPENSSL_BIO_H
463
+ void Init_mini_ssl(VALUE mod);
464
+ #endif
465
+
466
+ RUBY_FUNC_EXPORTED void Init_puma_http11(void)
467
+ {
468
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
469
+ rb_ext_ractor_safe(true);
470
+ #endif
471
+
472
+ VALUE mPuma = rb_define_module("Puma");
473
+ VALUE cHttpParser = rb_define_class_under(mPuma, "HttpParser", rb_cObject);
474
+
475
+ DEF_GLOBAL(&global_request_method, "REQUEST_METHOD");
476
+ DEF_GLOBAL(&global_request_uri, "REQUEST_URI");
477
+ DEF_GLOBAL(&global_fragment, "FRAGMENT");
478
+ DEF_GLOBAL(&global_query_string, "QUERY_STRING");
479
+ DEF_GLOBAL(&global_server_protocol, "SERVER_PROTOCOL");
480
+ DEF_GLOBAL(&global_request_path, "REQUEST_PATH");
481
+
482
+ rb_global_variable(&eHttpParserError);
483
+ eHttpParserError = rb_define_class_under(mPuma, "HttpParserError", rb_eStandardError);
484
+
485
+ rb_define_alloc_func(cHttpParser, HttpParser_alloc);
486
+ rb_define_method(cHttpParser, "initialize", HttpParser_init, 0);
487
+ rb_define_method(cHttpParser, "reset", HttpParser_reset, 0);
488
+ rb_define_method(cHttpParser, "finish", HttpParser_finish, 0);
489
+ rb_define_method(cHttpParser, "execute", HttpParser_execute, 3);
490
+ rb_define_method(cHttpParser, "error?", HttpParser_has_error, 0);
491
+ rb_define_method(cHttpParser, "finished?", HttpParser_is_finished, 0);
492
+ rb_define_method(cHttpParser, "nread", HttpParser_nread, 0);
493
+ rb_define_method(cHttpParser, "body", HttpParser_body, 0);
494
+ init_common_fields();
495
+
496
+ #ifdef HAVE_OPENSSL_BIO_H
497
+ Init_mini_ssl(mPuma);
498
+ #endif
499
+ }
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../json_serialization'
3
+
4
+ module Puma
5
+ module App
6
+ # Check out {#call}'s source code to see what actions this web application
7
+ # can respond to.
8
+ class Status
9
+ OK_STATUS = '{ "status": "ok" }'.freeze
10
+ READ_ONLY_COMMANDS = %w[gc-stats stats].freeze
11
+
12
+ # @param launcher [::Puma::Launcher]
13
+ # @param token [String, nil] the token used for authentication
14
+ # @param data_only [Boolean] if true, restrict to read-only data commands
15
+ #
16
+ def initialize(launcher, token: nil, data_only: false)
17
+ @launcher = launcher
18
+ @auth_token = token
19
+ @enabled_commands = READ_ONLY_COMMANDS if data_only
20
+ end
21
+
22
+ # most commands call methods in `::Puma::Launcher` based on command in
23
+ # `env['PATH_INFO']`
24
+ def call(env)
25
+ unless authenticate(env)
26
+ return rack_response(403, 'Invalid auth token', 'text/plain')
27
+ end
28
+
29
+ # resp_type is processed by following case statement, return
30
+ # is a number (status) or a string used as the body of a 200 response
31
+ command = env['PATH_INFO'][/\/([^\/]+)$/, 1]
32
+ if @enabled_commands && !@enabled_commands.include?(command)
33
+ return rack_response(404, "Command #{command.inspect} unavailable", 'text/plain')
34
+ end
35
+
36
+ resp_type =
37
+ case command
38
+ when 'stop'
39
+ @launcher.stop ; 200
40
+
41
+ when 'halt'
42
+ @launcher.halt ; 200
43
+
44
+ when 'restart'
45
+ @launcher.restart ; 200
46
+
47
+ when 'phased-restart'
48
+ @launcher.phased_restart ? 200 : 404
49
+
50
+ when 'refork'
51
+ @launcher.refork ? 200 : 404
52
+
53
+ when 'reload-worker-directory'
54
+ @launcher.send(:reload_worker_directory) ? 200 : 404
55
+
56
+ when 'gc'
57
+ GC.start ; 200
58
+
59
+ when 'gc-stats'
60
+ Puma::JSONSerialization.generate GC.stat
61
+
62
+ when 'stats'
63
+ Puma::JSONSerialization.generate @launcher.stats
64
+
65
+ when 'thread-backtraces'
66
+ backtraces = []
67
+ @launcher.thread_status do |name, backtrace|
68
+ backtraces << { name: name, backtrace: backtrace }
69
+ end
70
+ Puma::JSONSerialization.generate backtraces
71
+
72
+ else
73
+ return rack_response(404, "Unsupported action", 'text/plain')
74
+ end
75
+
76
+ case resp_type
77
+ when String
78
+ rack_response 200, resp_type
79
+ when 200
80
+ rack_response 200, OK_STATUS
81
+ when 404
82
+ str = env['PATH_INFO'][/\/(\S+)/, 1].tr '-', '_'
83
+ rack_response 404, "{ \"error\": \"#{str} not available\" }"
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def authenticate(env)
90
+ return true unless @auth_token
91
+ env['QUERY_STRING'].to_s.split(/[&;]/).include? "token=#{@auth_token}"
92
+ end
93
+
94
+ def rack_response(status, body, content_type='application/json')
95
+ headers = {
96
+ 'content-type' => content_type,
97
+ 'content-length' => body.bytesize.to_s
98
+ }
99
+
100
+ [status, headers, [body]]
101
+ end
102
+ end
103
+ end
104
+ end