iodine 0.8.2 → 0.8.4

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.
data/ext/iodine/iodine.c CHANGED
@@ -240,6 +240,7 @@ void Init_iodine(void) {
240
240
  Init_Iodine_PubSub_Message();
241
241
  Init_Iodine_TLS();
242
242
  Init_Iodine_Connection();
243
+ Init_Iodine_Params();
243
244
  /* make sure the Iodine.run / async methods are available pre-start */
244
245
  fio_io_async_attach(&IODINE_THREAD_POOL, (uint32_t)fio_cli_get_i("-t"));
245
246
  #ifdef SIGUSR1
data/ext/iodine/iodine.h CHANGED
@@ -99,7 +99,7 @@ static ID IODINE_JOIN_ID;
99
99
  static ID IODINE_TO_PATH_ID;
100
100
  static ID IODINE_TO_S_ID;
101
101
  static ID IODINE_TO_JSON_ID;
102
- static ID IODINE_RACK_HIJACK_ID;
102
+ static ID IODINE_RACK_HIJACK_ID; /* rack_hijack */
103
103
  static ID IODINE_ON_AUTHENTICATE_ID;
104
104
  static ID IODINE_ON_AUTHENTICATE_SSE_ID;
105
105
  static ID IODINE_ON_AUTHENTICATE_WEBSOCKET_ID;
@@ -135,6 +135,10 @@ static ID IODINE_DELETE_ID;
135
135
  name = rb_intern(value); \
136
136
  STORE.hold(RB_ID2SYM(name));
137
137
 
138
+ #define IODINE_CONST_IDSYM_STORE(name, value) \
139
+ name##_ID = rb_intern(value); \
140
+ STORE.hold((name##_SYM = RB_ID2SYM(name##_ID)));
141
+
138
142
  static VALUE iodine_rb_IODINE;
139
143
  static VALUE iodine_rb_IODINE_BASE;
140
144
  static VALUE iodine_rb_IODINE_BASE_APP404;
@@ -150,9 +154,9 @@ static rb_encoding *IodineBinaryEncoding;
150
154
  static VALUE IODINE_CONNECTION_ENV_TEMPLATE = Qnil;
151
155
  static VALUE IODINE_CONNECTION_ENV_TEMPLATE_HTTPS = Qnil;
152
156
  static VALUE IODINE_RACK_PROTOCOL_STR;
153
- static VALUE IODINE_RACK_HIJACK_ID_SYM;
154
- static VALUE IODINE_RACK_HIJACK_STR;
155
- static VALUE IODINE_RACK_HIJACK_SYM;
157
+ static VALUE IODINE_RACK_HIJACK_ID_SYM; /* rack_hijack */
158
+ static VALUE IODINE_RACK_HIJACK_STR; /* rack.hijack */
159
+ static VALUE IODINE_RACK_HIJACK_SYM; /* rack.hijack */
156
160
  static VALUE IODINE_RACK_UPGRADE_STR;
157
161
  static VALUE IODINE_RACK_UPGRADE_Q_STR;
158
162
  static VALUE IODINE_RACK_UPGRADE_WS_SYM;
@@ -252,6 +256,7 @@ static VALUE iodine_handler_method_injection__inner(VALUE self,
252
256
  #include "iodine_tls.h"
253
257
  /* layer 3 modules */
254
258
  #include "iodine_connection.h"
259
+ #include "iodine_params.h"
255
260
 
256
261
  /* layer 4 modules */
257
262
  #include "iodine_core.h"
@@ -149,7 +149,11 @@ typedef enum {
149
149
  IODINE_CONNECTION_STORE_opath, /**< Cached original path string */
150
150
  IODINE_CONNECTION_STORE_query, /**< Cached query string */
151
151
  IODINE_CONNECTION_STORE_version, /**< Cached HTTP version string */
152
- IODINE_CONNECTION_STORE_tmp, /**< Temporary storage during GC-sensitive ops */
152
+ IODINE_CONNECTION_STORE_tmp, /**< Active upload / temporary GC root */
153
+ IODINE_CONNECTION_STORE_params, /**< Memoized params Hash (iodine_params.h) */
154
+ IODINE_CONNECTION_STORE_params_stage, /**< Body parse staging Hash */
155
+ IODINE_CONNECTION_STORE_params_input, /**< Guarded JSON input copy */
156
+ IODINE_CONNECTION_STORE_params_exception, /**< Pending parser exception */
153
157
  IODINE_CONNECTION_STORE_FINISH, /**< Sentinel value (array size) */
154
158
  } iodine_connection_store_e;
155
159
 
@@ -164,6 +168,9 @@ typedef enum {
164
168
  IODINE_CONNECTION_UPGRADE = (2 | 4), /**< Any upgrade (SSE or WS) */
165
169
  IODINE_CONNECTION_CLOSED = 8, /**< Connection close scheduled */
166
170
  IODINE_CONNECTION_CLIENT = 16, /**< This is a client connection */
171
+ IODINE_CONNECTION_PARAMS_QUERY = 32, /**< Query merged into params Hash */
172
+ IODINE_CONNECTION_PARAMS_BODY = 64, /**< Body merged into params Hash */
173
+ IODINE_CONNECTION_PARAMS_HAS_UPLOAD = 128, /**< Upload cleanup may be needed */
167
174
  } iodine_connection_flags_e;
168
175
 
169
176
  /**
@@ -181,9 +188,14 @@ typedef struct iodine_connection_s {
181
188
  fio_http_s *http; /**< facil.io HTTP handle */
182
189
  VALUE store[IODINE_CONNECTION_STORE_FINISH]; /**< Cached Ruby objects */
183
190
  iodine_minimap_s map; /**< Header storage map */
191
+ struct iodine_upload_s *open_uploads; /**< Params cleanup list */
184
192
  iodine_connection_flags_e flags; /**< Connection state flags */
193
+ size_t params_spent; /**< Params field budget spent (iodine_params.h) */
185
194
  } iodine_connection_s;
186
195
 
196
+ /* Implemented in iodine_params.h (included after this header). */
197
+ static void iodine_params_on_connection_free(iodine_connection_s *c);
198
+
187
199
  static size_t iodine_connection_data_size(const void *ptr_) {
188
200
  iodine_connection_s *c = (iodine_connection_s *)ptr_;
189
201
  return sizeof(*c) +
@@ -192,6 +204,7 @@ static size_t iodine_connection_data_size(const void *ptr_) {
192
204
 
193
205
  static void iodine_connection_free(void *ptr_) {
194
206
  iodine_connection_s *c = (iodine_connection_s *)ptr_;
207
+ iodine_params_on_connection_free(c);
195
208
  fio_http_free(c->http);
196
209
  iodine_hmap_destroy(&c->map.map);
197
210
  FIO_LEAK_COUNTER_ON_FREE(iodine_connection);
@@ -252,6 +265,9 @@ static iodine_connection_s *iodine_connection_ptr(VALUE self) {
252
265
  return c;
253
266
  }
254
267
 
268
+ /* Defined in iodine_params.h (included after this header). */
269
+ static void iodine_params_on_finish(iodine_connection_s *c);
270
+
255
271
  /** Creates (and allocates) a new Iodine::Connection object. */
256
272
  static VALUE iodine_connection_create_from_io(fio_io_s *io) {
257
273
  VALUE m = rb_obj_alloc(iodine_rb_IODINE_CONNECTION);
@@ -1927,6 +1943,7 @@ static void iodine_io_http_on_http(fio_http_s *h) {
1927
1943
  IODINE_ON_FINISH_ID, \
1928
1944
  1, \
1929
1945
  &connection); \
1946
+ iodine_params_on_finish(c); \
1930
1947
  fio_http_free(c->http); \
1931
1948
  c->http = NULL; \
1932
1949
  c->io = NULL; \
@@ -1965,6 +1982,7 @@ IODINE_CONNECTION_DEF_CB(on_close, IODINE_ON_CLOSE_ID, 1);
1965
1982
  IODINE_ON_FINISH_ID, \
1966
1983
  1, \
1967
1984
  &connection); \
1985
+ iodine_params_on_finish(c); \
1968
1986
  fio_http_free(c->http); \
1969
1987
  c->http = NULL; \
1970
1988
  c->io = NULL; \
@@ -2113,6 +2131,7 @@ static void iodine_io_http_on_finish(fio_http_s *h) {
2113
2131
  }
2114
2132
  fio_http_free(h);
2115
2133
  }
2134
+ iodine_params_on_finish(c);
2116
2135
  c->http = NULL;
2117
2136
  c->io = NULL;
2118
2137
  STORE.release(connection);
@@ -2588,6 +2607,8 @@ static VALUE iodine_connection_initialize(int argc, VALUE *argv, VALUE self) {
2588
2607
  (VALUE)h);
2589
2608
  // args.settings.on_finish =
2590
2609
  c->io = fio_http_connect FIO_NOOP(args.url.buf, h, args.settings);
2610
+ /* fio_http_connect duplicates its TLS reference; release our own. */
2611
+ fio_io_tls_free(args.settings.tls);
2591
2612
  c->http = h;
2592
2613
  fio_http_udata2_set(h, (void *)self);
2593
2614
 
@@ -2912,7 +2933,7 @@ static VALUE iodine_connection_peer_addr(VALUE self) {
2912
2933
  fio_buf_info_s buf = FIO_BUF_INFO0;
2913
2934
  iodine_connection_s *c = iodine_connection_ptr(self);
2914
2935
  if (c->io)
2915
- buf = fio_sock_peer_addr(fio_io_fd(c->io));
2936
+ buf = fio_sock_peer_addr(fio_io_fd(c->io), false);
2916
2937
  if (!buf.len)
2917
2938
  return Qnil;
2918
2939
  return rb_str_new(buf.buf, buf.len);
@@ -2927,7 +2948,7 @@ static VALUE iodine_connection_from(VALUE self) {
2927
2948
  fio_http_from(&addr, c->http);
2928
2949
  buf = FIO_STR2BUF_INFO(addr);
2929
2950
  } else if (c->io)
2930
- buf = fio_sock_peer_addr(fio_io_fd(c->io));
2951
+ buf = fio_sock_peer_addr(fio_io_fd(c->io), false);
2931
2952
  if (!buf.len)
2932
2953
  return Qnil;
2933
2954
  return rb_str_new(buf.buf, buf.len);
@@ -3292,7 +3313,7 @@ static void *iodine_tcp_listen(iodine_connection_args_s args) {
3292
3313
  return fio_io_listen(.url = args.url.buf,
3293
3314
  .protocol = protocol,
3294
3315
  .udata = args.settings.udata,
3295
- .tls = args.settings.tls,
3316
+ .tls = fio_io_tls_dup(args.settings.tls),
3296
3317
  .on_stop = iodine_tcp_on_stop,
3297
3318
  .queue_for_accept = 0);
3298
3319
  }
@@ -64,13 +64,14 @@ FIO_SFUNC VALUE iodine_crypto_chacha_encrypt(int argc,
64
64
  VALUE ciphertext = rb_str_new(data.buf, data.len);
65
65
  uint8_t mac[16];
66
66
 
67
- fio_chacha20_poly1305_enc(mac,
68
- RSTRING_PTR(ciphertext),
69
- data.len,
70
- ad.buf,
71
- ad.len,
72
- (void *)key.buf,
73
- (void *)nonce.buf);
67
+ if (fio_chacha20_poly1305_enc(mac,
68
+ RSTRING_PTR(ciphertext),
69
+ data.len,
70
+ ad.buf,
71
+ ad.len,
72
+ (void *)key.buf,
73
+ (void *)nonce.buf))
74
+ rb_raise(rb_eRuntimeError, "Encryption failed");
74
75
 
75
76
  VALUE mac_str = rb_str_new((const char *)mac, 16);
76
77
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -172,13 +173,14 @@ FIO_SFUNC VALUE iodine_crypto_xchacha_encrypt(int argc,
172
173
  VALUE ciphertext = rb_str_new(data.buf, data.len);
173
174
  uint8_t mac[16];
174
175
 
175
- fio_xchacha20_poly1305_enc(mac,
176
- RSTRING_PTR(ciphertext),
177
- data.len,
178
- ad.buf,
179
- ad.len,
180
- (void *)key.buf,
181
- (void *)nonce.buf);
176
+ if (fio_xchacha20_poly1305_enc(mac,
177
+ RSTRING_PTR(ciphertext),
178
+ data.len,
179
+ ad.buf,
180
+ ad.len,
181
+ (void *)key.buf,
182
+ (void *)nonce.buf))
183
+ rb_raise(rb_eRuntimeError, "Encryption failed");
182
184
 
183
185
  VALUE mac_str = rb_str_new((const char *)mac, 16);
184
186
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -277,13 +279,14 @@ FIO_SFUNC VALUE iodine_crypto_aes128gcm_encrypt(int argc,
277
279
  VALUE ciphertext = rb_str_new(data.buf, data.len);
278
280
  uint8_t mac[16];
279
281
 
280
- fio_aes128_gcm_enc(mac,
281
- RSTRING_PTR(ciphertext),
282
- data.len,
283
- ad.buf,
284
- ad.len,
285
- (void *)key.buf,
286
- (void *)nonce.buf);
282
+ if (fio_aes128_gcm_enc(mac,
283
+ RSTRING_PTR(ciphertext),
284
+ data.len,
285
+ ad.buf,
286
+ ad.len,
287
+ (void *)key.buf,
288
+ (void *)nonce.buf))
289
+ rb_raise(rb_eRuntimeError, "Encryption failed");
287
290
 
288
291
  VALUE mac_str = rb_str_new((const char *)mac, 16);
289
292
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -382,13 +385,14 @@ FIO_SFUNC VALUE iodine_crypto_aes256gcm_encrypt(int argc,
382
385
  VALUE ciphertext = rb_str_new(data.buf, data.len);
383
386
  uint8_t mac[16];
384
387
 
385
- fio_aes256_gcm_enc(mac,
386
- RSTRING_PTR(ciphertext),
387
- data.len,
388
- ad.buf,
389
- ad.len,
390
- (void *)key.buf,
391
- (void *)nonce.buf);
388
+ if (fio_aes256_gcm_enc(mac,
389
+ RSTRING_PTR(ciphertext),
390
+ data.len,
391
+ ad.buf,
392
+ ad.len,
393
+ (void *)key.buf,
394
+ (void *)nonce.buf))
395
+ rb_raise(rb_eRuntimeError, "Encryption failed");
392
396
 
393
397
  VALUE mac_str = rb_str_new((const char *)mac, 16);
394
398
  return rb_ary_new_from_args(2, ciphertext, mac_str);
@@ -1004,15 +1008,16 @@ FIO_SFUNC VALUE iodine_crypto_hkdf_derive(int argc, VALUE *argv, VALUE self) {
1004
1008
  VALUE okm = rb_str_buf_new(length);
1005
1009
  rb_str_set_len(okm, length);
1006
1010
 
1007
- fio_hkdf(RSTRING_PTR(okm),
1008
- (size_t)length,
1009
- salt.buf,
1010
- salt.len,
1011
- ikm.buf,
1012
- ikm.len,
1013
- info.buf,
1014
- info.len,
1015
- (int)sha384);
1011
+ if (fio_hkdf(RSTRING_PTR(okm),
1012
+ (size_t)length,
1013
+ salt.buf,
1014
+ salt.len,
1015
+ ikm.buf,
1016
+ ikm.len,
1017
+ info.buf,
1018
+ info.len,
1019
+ (int)sha384))
1020
+ rb_raise(rb_eRuntimeError, "HKDF derivation failed");
1016
1021
 
1017
1022
  return okm;
1018
1023
  (void)self;