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