uringmachine 0.28.3 → 0.29.0

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/um/um_ssl.c CHANGED
@@ -38,17 +38,14 @@ static long um_bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
38
38
  BIO_METHOD *um_ssl_create_bio_method(void)
39
39
  {
40
40
  BIO_METHOD *m = BIO_meth_new(BIO_TYPE_MEM, "UringMachine BIO");
41
- if(m) {
42
- BIO_meth_set_write(m, &um_bio_write);
43
- BIO_meth_set_read(m, &um_bio_read);
44
- BIO_meth_set_ctrl(m, &um_bio_ctrl);
45
- }
46
- else
47
- rb_raise(eUMError, "Failed to set SSL BIO");
41
+ if (!m) rb_raise(eUMError, "Failed to set SSL BIO");
42
+
43
+ BIO_meth_set_write(m, &um_bio_write);
44
+ BIO_meth_set_read(m, &um_bio_read);
45
+ BIO_meth_set_ctrl(m, &um_bio_ctrl);
48
46
  return m;
49
47
  }
50
48
 
51
-
52
49
  static BIO_METHOD *um_bio_method = NULL;
53
50
  static ID id_ivar_um_bio;
54
51
 
@@ -58,21 +55,19 @@ void um_ssl_set_bio(struct um *machine, VALUE ssl_obj)
58
55
  um_bio_method = um_ssl_create_bio_method();
59
56
  id_ivar_um_bio = rb_intern_const("@__um_bio__");
60
57
  }
58
+ VALUE value = rb_ivar_get(ssl_obj, id_ivar_um_bio);
59
+ if (value == Qtrue) return;
61
60
 
62
- long fd = NUM2LONG(rb_funcall(ssl_obj, rb_intern_const("fileno"), 0));
63
61
  rb_ivar_set(ssl_obj, id_ivar_um_bio, Qtrue);
64
-
62
+ long fd = NUM2LONG(rb_funcall(ssl_obj, rb_intern_const("fileno"), 0));
65
63
  BIO *bio = BIO_new(um_bio_method);
66
- if(!bio)
67
- rb_raise(eUMError, "Failed to create custom BIO");
64
+ if(!bio) rb_raise(eUMError, "Failed to create custom BIO");
68
65
 
69
66
  int ret = BIO_set_ex_data(bio, IDX_BIO_DATA_MACHINE, (void *)machine);
70
- if (!ret)
71
- rb_raise(eUMError, "Failed to set BIO metadata");
67
+ if (!ret) rb_raise(eUMError, "Failed to set BIO metadata");
72
68
 
73
69
  ret = BIO_set_ex_data(bio, IDX_BIO_DATA_FD, (void *)fd);
74
- if (!ret)
75
- rb_raise(eUMError, "Failed to set BIO metadata");
70
+ if (!ret) rb_raise(eUMError, "Failed to set BIO metadata");
76
71
 
77
72
  SSL *ssl = RTYPEDDATA_GET_DATA(ssl_obj);
78
73
  BIO_up_ref(bio);
@@ -80,16 +75,19 @@ void um_ssl_set_bio(struct um *machine, VALUE ssl_obj)
80
75
  SSL_set0_wbio(ssl, bio);
81
76
  }
82
77
 
83
- int um_ssl_read(struct um *machine, VALUE ssl_obj, VALUE buf, int maxlen) {
78
+ int um_ssl_read_raw(struct um *machine, VALUE ssl_obj, char *ptr, int maxlen) {
84
79
  SSL *ssl = RTYPEDDATA_GET_DATA(ssl_obj);
85
- void *ptr = um_prepare_read_buffer(buf, maxlen, 0);
80
+
86
81
  int ret = SSL_read(ssl, ptr, maxlen);
87
- if (ret > 0) {
88
- um_update_read_buffer(buf, 0, ret);
89
- }
90
- else {
91
- rb_raise(eUMError, "Failed to read");
92
- }
82
+ if (ret < 0) rb_raise(eUMError, "Failed to read");
83
+
84
+ return ret;
85
+ }
86
+
87
+ int um_ssl_read(struct um *machine, VALUE ssl_obj, VALUE buf, int maxlen) {
88
+ void *ptr = um_prepare_read_buffer(buf, maxlen, 0);
89
+ int ret = um_ssl_read_raw(machine, ssl_obj, ptr, maxlen);
90
+ um_update_read_buffer(buf, 0, ret);
93
91
  return ret;
94
92
  }
95
93
 
@@ -102,8 +100,7 @@ int um_ssl_write(struct um *machine, VALUE ssl_obj, VALUE buf, int len) {
102
100
  if (unlikely(!len)) return INT2NUM(0);
103
101
 
104
102
  int ret = SSL_write(ssl, base, len);
105
- if (ret <= 0) {
106
- rb_raise(eUMError, "Failed to read");
107
- }
103
+ if (ret <= 0) rb_raise(eUMError, "Failed to write");
104
+
108
105
  return ret;
109
106
  }