init ssl roots only once

pull/20903/head
Hannah Shi 5 years ago
parent ab7e5e8dec
commit 35c6369daa
  1. 16
      src/php/ext/grpc/channel_credentials.c
  2. 2
      src/php/lib/Grpc/BaseStub.php

@ -100,6 +100,17 @@ PHP_METHOD(ChannelCredentials, setDefaultRootsPem) {
memcpy(default_pem_root_certs, pem_roots, pem_roots_length + 1); memcpy(default_pem_root_certs, pem_roots, pem_roots_length + 1);
} }
/**
* if default roots pem is set
* @return TRUE/FALSE
*/
PHP_METHOD(ChannelCredentials, isDefaultRootsPemSet) {
if (default_pem_root_certs) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/** /**
* Create a default channel credentials object. * Create a default channel credentials object.
* @return ChannelCredentials The new default channel credentials object * @return ChannelCredentials The new default channel credentials object
@ -214,6 +225,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_setDefaultRootsPem, 0, 0, 1)
ZEND_ARG_INFO(0, pem_roots) ZEND_ARG_INFO(0, pem_roots)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_isDefaultRootsPemSet, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_createDefault, 0, 0, 0) ZEND_BEGIN_ARG_INFO_EX(arginfo_createDefault, 0, 0, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
@ -234,6 +248,8 @@ ZEND_END_ARG_INFO()
static zend_function_entry channel_credentials_methods[] = { static zend_function_entry channel_credentials_methods[] = {
PHP_ME(ChannelCredentials, setDefaultRootsPem, arginfo_setDefaultRootsPem, PHP_ME(ChannelCredentials, setDefaultRootsPem, arginfo_setDefaultRootsPem,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(ChannelCredentials, isDefaultRootsPemSet, arginfo_isDefaultRootsPemSet,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(ChannelCredentials, createDefault, arginfo_createDefault, PHP_ME(ChannelCredentials, createDefault, arginfo_createDefault,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(ChannelCredentials, createSsl, arginfo_createSsl, PHP_ME(ChannelCredentials, createSsl, arginfo_createSsl,

@ -43,10 +43,12 @@ class BaseStub
*/ */
public function __construct($hostname, $opts, $channel = null) public function __construct($hostname, $opts, $channel = null)
{ {
if (!ChannelCredentials::isDefaultRootsPemSet()) {
$ssl_roots = file_get_contents( $ssl_roots = file_get_contents(
dirname(__FILE__).'/../../../../etc/roots.pem' dirname(__FILE__).'/../../../../etc/roots.pem'
); );
ChannelCredentials::setDefaultRootsPem($ssl_roots); ChannelCredentials::setDefaultRootsPem($ssl_roots);
}
$this->hostname = $hostname; $this->hostname = $hostname;
$this->update_metadata = null; $this->update_metadata = null;

Loading…
Cancel
Save