polyphony 0.93 → 0.94

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb7ee7e43bec0bfffbcff8c78cdcdcc7d93f4283014bd9caebfcd94c3da6b96f
4
- data.tar.gz: add731011308495bc5ec0f4f5bc1f7152cfaa1dddd0f53f8027b4a64ea0ba08f
3
+ metadata.gz: 02f56fa13c8397d86a0ceae20dd253461809397e8ad9eb5827a04650f3dc5020
4
+ data.tar.gz: f956d6e226e4a2b4239dc367abe9b4c1de89173df1b9968748f0da730b88410d
5
5
  SHA512:
6
- metadata.gz: 108f69257c57caab18780bad60dbd6bed240dd8a45e05e54e54cbb4b90d31382c10c8f25d3c8bb92f8753e134054782eb4b9d47e79ec79da8938ac4c3d14f4a6
7
- data.tar.gz: be78ce7748655d18014074fbb95bedecccab8af94b53e7a2db797cdb503a4f67c25a81b9e9b9154d6e1187efbc3c5fd1b4454e604ad068d51a0c3f411ee7c8ea
6
+ metadata.gz: 262b6272e6f2cdc86c09eca16b60e31713139ef37b7203c77e858d0c991a5754ee842311caac9ee76e09ce71a1f473e18f102f0ce460e903f31acb0317b6a0b5
7
+ data.tar.gz: d628e38d39ee4bbbeb1f5f9e7b5ba9d2e406911be1cb9af809a310f9cdb0315beb417e41462b68ed58e23e2c94435a5702192fd5946d5c51a9d4ae392ee50872
data/Gemfile.lock CHANGED
@@ -14,7 +14,7 @@ GEM
14
14
  httparty (0.17.1)
15
15
  mime-types (~> 3.0)
16
16
  multi_xml (>= 0.5.2)
17
- json (2.6.1)
17
+ json (2.6.2)
18
18
  localhost (1.1.9)
19
19
  method_source (1.0.0)
20
20
  mime-types (3.4.1)
@@ -28,8 +28,8 @@ GEM
28
28
  ruby-progressbar
29
29
  msgpack (1.4.2)
30
30
  multi_xml (0.6.0)
31
- parallel (1.21.0)
32
- parser (3.1.0.0)
31
+ parallel (1.22.1)
32
+ parser (3.1.2.1)
33
33
  ast (~> 2.4.1)
34
34
  pry (0.13.1)
35
35
  coderay (~> 1.1)
@@ -38,7 +38,7 @@ GEM
38
38
  rake (13.0.6)
39
39
  rake-compiler (1.1.1)
40
40
  rake
41
- regexp_parser (2.2.0)
41
+ regexp_parser (2.6.0)
42
42
  rexml (3.2.5)
43
43
  rubocop (0.85.1)
44
44
  parallel (~> 1.10)
@@ -49,8 +49,8 @@ GEM
49
49
  rubocop-ast (>= 0.0.3)
50
50
  ruby-progressbar (~> 1.7)
51
51
  unicode-display_width (>= 1.4.0, < 2.0)
52
- rubocop-ast (1.15.1)
53
- parser (>= 3.0.1.1)
52
+ rubocop-ast (1.21.0)
53
+ parser (>= 3.1.1.0)
54
54
  ruby-progressbar (1.11.0)
55
55
  simplecov (0.17.1)
56
56
  docile (~> 1.1)
@@ -60,11 +60,7 @@ GEM
60
60
  unicode-display_width (1.8.0)
61
61
 
62
62
  PLATFORMS
63
- ruby
64
- universal-darwin
65
- universal-freebsd
66
- universal-linux
67
- universal-unknown
63
+ x86_64-linux
68
64
 
69
65
  DEPENDENCIES
70
66
  httparty (= 0.17.1)
@@ -79,4 +75,4 @@ DEPENDENCIES
79
75
  simplecov (= 0.17.1)
80
76
 
81
77
  BUNDLED WITH
82
- 2.3.3
78
+ 2.3.7
@@ -17,4 +17,4 @@ def handle_client(conn)
17
17
  end
18
18
 
19
19
  puts "Serving echo on port 1234..."
20
- TCPServer.new('127.0.0.1', 1234).accept_loop { |c| handle_client(c) }
20
+ TCPServer.new('0.0.0.0', 1234).accept_loop { |c| handle_client(c) }
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ gem 'h1p'
7
+ gem 'polyphony', path: '.'
8
+ end
9
+
10
+ require 'polyphony'
11
+ require 'h1p'
12
+
13
+ def handle_client(conn)
14
+ spin do
15
+ parser = H1P::Parser.new(conn, :server)
16
+
17
+ while true # assuming persistent connection
18
+ headers = parser.parse_headers
19
+ break unless headers
20
+
21
+ parser.read_body unless parser.complete?
22
+
23
+ conn << "HTTP/1.1 200 OK\r\nContent-Length: 14\r\n\r\nHello, world!\n"
24
+ end
25
+ rescue H1P::Error
26
+ puts 'Got invalid request, closing connection...'
27
+ ensure
28
+ conn.close
29
+ end
30
+ end
31
+
32
+ puts "Serving HTTP on port 1234..."
33
+ TCPServer.new('0.0.0.0', 1234).accept_loop { |c| handle_client(c) }
@@ -479,10 +479,10 @@ int backend_getaddrinfo(VALUE host, VALUE port, struct sockaddr **ai_addr) {
479
479
  return addrinfo_result->ai_addrlen;
480
480
  }
481
481
 
482
- struct io_buffer get_io_buffer(VALUE in, int rw) {
482
+ inline struct backend_buffer_spec backend_get_buffer_spec(VALUE in, int rw) {
483
483
  if (FIXNUM_P(in)) {
484
- struct raw_buffer *raw = FIX2PTR(in);
485
- return (struct io_buffer){ raw->ptr, raw->len, 1 };
484
+ struct buffer_spec *spec = FIX2PTR(in);
485
+ return (struct backend_buffer_spec){ spec->ptr, spec->len, 1 };
486
486
  }
487
487
 
488
488
  #ifdef HAVE_RUBY_IO_BUFFER_H
@@ -493,11 +493,56 @@ struct io_buffer get_io_buffer(VALUE in, int rw) {
493
493
  rb_io_buffer_get_bytes_for_reading(in, (const void **)&ptr, &len);
494
494
  else
495
495
  rb_io_buffer_get_bytes_for_writing(in, &ptr, &len);
496
- return (struct io_buffer){ ptr, len, 1 };
496
+ return (struct backend_buffer_spec){ ptr, len, 1 };
497
497
  }
498
498
  #endif
499
499
 
500
- return (struct io_buffer){ (unsigned char *)RSTRING_PTR(in), RSTRING_LEN(in), 0 };
500
+ return (struct backend_buffer_spec){ (unsigned char *)RSTRING_PTR(in), RSTRING_LEN(in), 0 };
501
+ }
502
+
503
+ inline void backend_prepare_read_buffer(VALUE buffer, VALUE length, struct backend_buffer_spec *buffer_spec, int pos) {
504
+ buffer_spec->pos = pos;
505
+ buffer_spec->expandable = 0;
506
+ buffer_spec->shrinkable = 0;
507
+ if (buffer_spec->raw) {
508
+ if (buffer_spec->pos < 0 || buffer_spec->pos > buffer_spec->len)
509
+ buffer_spec->pos = buffer_spec->len;
510
+ buffer_spec->ptr += buffer_spec->pos;
511
+ buffer_spec->len -= buffer_spec->pos;
512
+ }
513
+ else {
514
+ buffer_spec->expandable = length == Qnil;
515
+ long expected_read_length = buffer_spec->expandable ? 4096 : FIX2INT(length);
516
+ long string_cap = rb_str_capacity(buffer);
517
+ if (buffer_spec->pos < 0 || buffer_spec->pos > buffer_spec->len)
518
+ buffer_spec->pos = buffer_spec->len;
519
+
520
+ if (string_cap < expected_read_length + buffer_spec->pos) {
521
+ buffer_spec->shrinkable = io_setstrbuf(&buffer, expected_read_length + buffer_spec->pos);
522
+ buffer_spec->ptr = (unsigned char *)RSTRING_PTR(buffer) + buffer_spec->pos;
523
+ buffer_spec->len = expected_read_length;
524
+ }
525
+ else {
526
+ buffer_spec->ptr += buffer_spec->pos;
527
+ buffer_spec->len = string_cap - buffer_spec->pos;
528
+ if (buffer_spec->len > expected_read_length)
529
+ buffer_spec->len = expected_read_length;
530
+ }
531
+ }
532
+ }
533
+
534
+ inline void backend_grow_string_buffer(VALUE buffer, struct backend_buffer_spec *buffer_spec, int total) {
535
+ // resize buffer to double its capacity
536
+ rb_str_resize(buffer, total + buffer_spec->pos);
537
+ rb_str_modify_expand(buffer, rb_str_capacity(buffer));
538
+ buffer_spec->shrinkable = 0;
539
+ buffer_spec->ptr = (unsigned char *)RSTRING_PTR(buffer) + total + buffer_spec->pos;
540
+ buffer_spec->len = rb_str_capacity(buffer) - total - buffer_spec->pos;
541
+ }
542
+
543
+ inline void backend_finalize_string_buffer(VALUE buffer, struct backend_buffer_spec *buffer_spec, int total, rb_io_t *fptr) {
544
+ io_set_read_length(buffer, buffer_spec->pos + total, buffer_spec->shrinkable);
545
+ if (fptr) io_enc_str(buffer, fptr);
501
546
  }
502
547
 
503
548
  VALUE coerce_io_string_or_buffer(VALUE buf) {
@@ -55,23 +55,31 @@ struct backend_stats backend_base_stats(struct Backend_base *base);
55
55
  }
56
56
  #define COND_TRACE(base, ...) if (SHOULD_TRACE(base)) { TRACE(base, __VA_ARGS__); }
57
57
 
58
- // raw buffers
58
+ // buffers
59
59
 
60
- struct raw_buffer {
60
+ struct buffer_spec {
61
61
  unsigned char *ptr;
62
62
  int len;
63
63
  };
64
64
 
65
- struct io_buffer {
65
+ struct backend_buffer_spec {
66
66
  unsigned char *ptr;
67
67
  int len;
68
68
  int raw;
69
+ int pos;
70
+ int expandable:1;
71
+ int shrinkable:1;
72
+ int reserved:30;
69
73
  };
70
74
 
71
75
  #define FIX2PTR(v) ((void *)(FIX2LONG(v)))
72
76
  #define PTR2FIX(p) LONG2FIX((long)p)
73
77
 
74
- struct io_buffer get_io_buffer(VALUE in, int rw);
78
+ struct backend_buffer_spec backend_get_buffer_spec(VALUE in, int rw);
79
+ void backend_prepare_read_buffer(VALUE buffer, VALUE length, struct backend_buffer_spec *buffer_spec, int pos);
80
+ void backend_grow_string_buffer(VALUE buffer, struct backend_buffer_spec *buffer_spec, int total);
81
+ void backend_finalize_string_buffer(VALUE buffer, struct backend_buffer_spec *buffer_spec, int total, rb_io_t *fptr);
82
+
75
83
  VALUE coerce_io_string_or_buffer(VALUE buf);
76
84
 
77
85
  #ifdef POLYPHONY_USE_PIDFD_OPEN
@@ -106,23 +114,23 @@ VALUE backend_snooze(struct Backend_base *backend);
106
114
 
107
115
  // macros for doing read loops
108
116
  #define READ_LOOP_PREPARE_STR() { \
109
- str = Qnil; \
110
- shrinkable = io_setstrbuf(&str, len); \
111
- buf = RSTRING_PTR(str); \
117
+ buffer = Qnil; \
118
+ shrinkable = io_setstrbuf(&buffer, len); \
119
+ ptr = RSTRING_PTR(buffer); \
112
120
  total = 0; \
113
121
  }
114
122
 
115
123
  #define READ_LOOP_YIELD_STR() { \
116
- io_set_read_length(str, total, shrinkable); \
117
- if (fptr) io_enc_str(str, fptr); \
118
- rb_yield(str); \
124
+ io_set_read_length(buffer, total, shrinkable); \
125
+ if (fptr) io_enc_str(buffer, fptr); \
126
+ rb_yield(buffer); \
119
127
  READ_LOOP_PREPARE_STR(); \
120
128
  }
121
129
 
122
130
  #define READ_LOOP_PASS_STR_TO_RECEIVER(receiver, method_id) { \
123
- io_set_read_length(str, total, shrinkable); \
124
- if (fptr) io_enc_str(str, fptr); \
125
- rb_funcall_passing_block(receiver, method_id, 1, &str); \
131
+ io_set_read_length(buffer, total, shrinkable); \
132
+ if (fptr) io_enc_str(buffer, fptr); \
133
+ rb_funcall_passing_block(receiver, method_id, 1, &buffer); \
126
134
  READ_LOOP_PREPARE_STR(); \
127
135
  }
128
136