Skip to content

Commit

Permalink
ext_help.h, minissl.c, puma_http11.c - change Data -> TypedData (#2430)
Browse files Browse the repository at this point in the history
'Data_' macros have been 'deprecated' since Ruby 2.3 or earlier, but are still supported.

Update c source to use the 'TypedData_' macros.
  • Loading branch information
MSP-Greg committed Oct 16, 2020
1 parent 2f90396 commit 04eb8bd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion ext/puma_http11/ext_help.h
Expand Up @@ -2,7 +2,7 @@
#define ext_help_h

#define RAISE_NOT_NULL(T) if(T == NULL) rb_raise(rb_eArgError, "%s", "NULL found for " # T " when shouldn't be.");
#define DATA_GET(from,type,name) Data_Get_Struct(from,type,name); RAISE_NOT_NULL(name);
#define DATA_GET(from,type,data_type,name) TypedData_Get_Struct(from,type,data_type,name); RAISE_NOT_NULL(name);
#define REQUIRE_TYPE(V, T) if(TYPE(V) != T) rb_raise(rb_eTypeError, "%s", "Wrong argument type for " # V " required " # T);
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))

Expand Down
76 changes: 39 additions & 37 deletions ext/puma_http11/mini_ssl.c
Expand Up @@ -2,12 +2,7 @@

#include <ruby.h>
#include <ruby/version.h>

#if RUBY_API_VERSION_MAJOR == 1
#include <rubyio.h>
#else
#include <ruby/io.h>
#endif

#ifdef HAVE_OPENSSL_BIO_H

Expand All @@ -33,7 +28,8 @@ typedef struct {
int bytes;
} ms_cert_buf;

void engine_free(ms_conn* conn) {
void engine_free(void *ptr) {
ms_conn *conn = ptr;
ms_cert_buf* cert_buf = (ms_cert_buf*)SSL_get_app_data(conn->ssl);
if(cert_buf) {
OPENSSL_free(cert_buf->buf);
Expand All @@ -45,10 +41,16 @@ void engine_free(ms_conn* conn) {
free(conn);
}

const rb_data_type_t engine_data_type = {
"MiniSSL/ENGINE",
{ 0, engine_free, 0 },
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
};

ms_conn* engine_alloc(VALUE klass, VALUE* obj) {
ms_conn* conn;

*obj = Data_Make_Struct(klass, ms_conn, 0, engine_free, conn);
*obj = TypedData_Make_Struct(klass, ms_conn, &engine_data_type, conn);

conn->read = BIO_new(BIO_s_mem());
BIO_set_nbio(conn->read, 1);
Expand Down Expand Up @@ -198,7 +200,7 @@ VALUE engine_init_server(VALUE self, VALUE mini_ssl_ctx) {
else {
min = TLS1_VERSION;
}

SSL_CTX_set_min_proto_version(ctx, min);

SSL_CTX_set_options(ctx, ssl_options);
Expand Down Expand Up @@ -281,7 +283,7 @@ VALUE engine_inject(VALUE self, VALUE str) {
ms_conn* conn;
long used;

Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);

StringValue(str);

Expand Down Expand Up @@ -334,7 +336,7 @@ VALUE engine_read(VALUE self) {
char buf[512];
int bytes, error;

Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);

ERR_clear_error();

Expand All @@ -361,7 +363,7 @@ VALUE engine_write(VALUE self, VALUE str) {
ms_conn* conn;
int bytes;

Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);

StringValue(str);

Expand All @@ -385,7 +387,7 @@ VALUE engine_extract(VALUE self) {
size_t pending;
char buf[512];

Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);

pending = BIO_pending(conn->write);
if(pending > 0) {
Expand All @@ -404,7 +406,7 @@ VALUE engine_shutdown(VALUE self) {
ms_conn* conn;
int ok;

Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);

ERR_clear_error();

Expand All @@ -419,7 +421,7 @@ VALUE engine_shutdown(VALUE self) {
VALUE engine_init(VALUE self) {
ms_conn* conn;

Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);

return SSL_in_init(conn->ssl) ? Qtrue : Qfalse;
}
Expand All @@ -432,7 +434,7 @@ VALUE engine_peercert(VALUE self) {
ms_cert_buf* cert_buf = NULL;
VALUE rb_cert_buf;

Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);

cert = SSL_get_peer_certificate(conn->ssl);
if(!cert) {
Expand Down Expand Up @@ -469,7 +471,7 @@ VALUE engine_peercert(VALUE self) {
static VALUE
engine_ssl_vers_st(VALUE self) {
ms_conn* conn;
Data_Get_Struct(self, ms_conn, conn);
TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
return rb_ary_new3(2, rb_str_new2(SSL_get_version(conn->ssl)), rb_str_new2(SSL_state_string(conn->ssl)));
}

Expand Down Expand Up @@ -504,27 +506,27 @@ void Init_mini_ssl(VALUE puma) {
#else
rb_define_const(mod, "OPENSSL_LIBRARY_VERSION", rb_str_new2(SSLeay_version(SSLEAY_VERSION)));
#endif
#if defined(OPENSSL_NO_SSL3) || defined(OPENSSL_NO_SSL3_METHOD)
/* True if SSL3 is not available */
rb_define_const(mod, "OPENSSL_NO_SSL3", Qtrue);
#else
rb_define_const(mod, "OPENSSL_NO_SSL3", Qfalse);
#endif

#if defined(OPENSSL_NO_TLS1) || defined(OPENSSL_NO_TLS1_METHOD)
/* True if TLS1 is not available */
rb_define_const(mod, "OPENSSL_NO_TLS1", Qtrue);
#else
rb_define_const(mod, "OPENSSL_NO_TLS1", Qfalse);
#endif

#if defined(OPENSSL_NO_TLS1_1) || defined(OPENSSL_NO_TLS1_1_METHOD)
/* True if TLS1_1 is not available */
rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qtrue);
#else
rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qfalse);
#endif

#if defined(OPENSSL_NO_SSL3) || defined(OPENSSL_NO_SSL3_METHOD)
/* True if SSL3 is not available */
rb_define_const(mod, "OPENSSL_NO_SSL3", Qtrue);
#else
rb_define_const(mod, "OPENSSL_NO_SSL3", Qfalse);
#endif

#if defined(OPENSSL_NO_TLS1) || defined(OPENSSL_NO_TLS1_METHOD)
/* True if TLS1 is not available */
rb_define_const(mod, "OPENSSL_NO_TLS1", Qtrue);
#else
rb_define_const(mod, "OPENSSL_NO_TLS1", Qfalse);
#endif

#if defined(OPENSSL_NO_TLS1_1) || defined(OPENSSL_NO_TLS1_1_METHOD)
/* True if TLS1_1 is not available */
rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qtrue);
#else
rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qfalse);
#endif

rb_define_singleton_method(mod, "check", noop, 0);

Expand Down
27 changes: 17 additions & 10 deletions ext/puma_http11/puma_http11.c
Expand Up @@ -253,11 +253,18 @@ void HttpParser_free(void *data) {
}
}

void HttpParser_mark(puma_parser* hp) {
void HttpParser_mark(void *ptr) {
puma_parser *hp = ptr;
if(hp->request) rb_gc_mark(hp->request);
if(hp->body) rb_gc_mark(hp->body);
}

const rb_data_type_t HttpParser_data_type = {
"HttpParser",
{ HttpParser_mark, HttpParser_free, 0 },
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
};

VALUE HttpParser_alloc(VALUE klass)
{
puma_parser *hp = ALLOC_N(puma_parser, 1);
Expand All @@ -274,7 +281,7 @@ VALUE HttpParser_alloc(VALUE klass)

puma_parser_init(hp);

return Data_Wrap_Struct(klass, HttpParser_mark, HttpParser_free, hp);
return TypedData_Wrap_Struct(klass, &HttpParser_data_type, hp);
}

/**
Expand All @@ -286,7 +293,7 @@ VALUE HttpParser_alloc(VALUE klass)
VALUE HttpParser_init(VALUE self)
{
puma_parser *http = NULL;
DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);
puma_parser_init(http);

return self;
Expand All @@ -303,7 +310,7 @@ VALUE HttpParser_init(VALUE self)
VALUE HttpParser_reset(VALUE self)
{
puma_parser *http = NULL;
DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);
puma_parser_init(http);

return Qnil;
Expand All @@ -320,7 +327,7 @@ VALUE HttpParser_reset(VALUE self)
VALUE HttpParser_finish(VALUE self)
{
puma_parser *http = NULL;
DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);
puma_parser_finish(http);

return puma_parser_is_finished(http) ? Qtrue : Qfalse;
Expand Down Expand Up @@ -351,7 +358,7 @@ VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
char *dptr = NULL;
long dlen = 0;

DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);

from = FIX2INT(start);
dptr = rb_extract_chars(data, &dlen);
Expand Down Expand Up @@ -385,7 +392,7 @@ VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
VALUE HttpParser_has_error(VALUE self)
{
puma_parser *http = NULL;
DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);

return puma_parser_has_error(http) ? Qtrue : Qfalse;
}
Expand All @@ -400,7 +407,7 @@ VALUE HttpParser_has_error(VALUE self)
VALUE HttpParser_is_finished(VALUE self)
{
puma_parser *http = NULL;
DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);

return puma_parser_is_finished(http) ? Qtrue : Qfalse;
}
Expand All @@ -416,7 +423,7 @@ VALUE HttpParser_is_finished(VALUE self)
VALUE HttpParser_nread(VALUE self)
{
puma_parser *http = NULL;
DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);

return INT2FIX(http->nread);
}
Expand All @@ -429,7 +436,7 @@ VALUE HttpParser_nread(VALUE self)
*/
VALUE HttpParser_body(VALUE self) {
puma_parser *http = NULL;
DATA_GET(self, puma_parser, http);
DATA_GET(self, puma_parser, &HttpParser_data_type, http);

return http->body;
}
Expand Down

0 comments on commit 04eb8bd

Please sign in to comment.