From 04eb8bdb39e9603152e39fbec1afb3802d46ebba Mon Sep 17 00:00:00 2001 From: MSP-Greg Date: Fri, 16 Oct 2020 08:36:08 -0500 Subject: [PATCH] ext_help.h, minissl.c, puma_http11.c - change Data -> TypedData (#2430) 'Data_' macros have been 'deprecated' since Ruby 2.3 or earlier, but are still supported. Update c source to use the 'TypedData_' macros. --- ext/puma_http11/ext_help.h | 2 +- ext/puma_http11/mini_ssl.c | 72 ++++++++++++++++++----------------- ext/puma_http11/puma_http11.c | 27 ++++++++----- 3 files changed, 55 insertions(+), 46 deletions(-) diff --git a/ext/puma_http11/ext_help.h b/ext/puma_http11/ext_help.h index 80b433f0..ba09e6dc 100644 --- a/ext/puma_http11/ext_help.h +++ b/ext/puma_http11/ext_help.h @@ -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])) diff --git a/ext/puma_http11/mini_ssl.c b/ext/puma_http11/mini_ssl.c index fd964cf3..b67ea3e2 100644 --- a/ext/puma_http11/mini_ssl.c +++ b/ext/puma_http11/mini_ssl.c @@ -2,12 +2,7 @@ #include #include - -#if RUBY_API_VERSION_MAJOR == 1 -#include -#else #include -#endif #ifdef HAVE_OPENSSL_BIO_H @@ -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); @@ -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); @@ -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); @@ -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); @@ -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(); @@ -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); @@ -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) { @@ -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(); @@ -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; } @@ -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) { @@ -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))); } @@ -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_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_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_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); diff --git a/ext/puma_http11/puma_http11.c b/ext/puma_http11/puma_http11.c index b27b2534..4969b137 100644 --- a/ext/puma_http11/puma_http11.c +++ b/ext/puma_http11/puma_http11.c @@ -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); @@ -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); } /** @@ -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; @@ -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; @@ -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; @@ -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); @@ -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; } @@ -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; } @@ -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); } @@ -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; }