Permalink
Browse files

Docs, refactoring

  • Loading branch information...
1 parent 64088c6 commit a7dab17a755743609d9fa6c761d56d9d76c2f45a @kakserpom committed Mar 20, 2013
View
@@ -49,7 +49,7 @@
* Context
* @var mixed
*/
- public $ctx; // @TODO: make it protected
+ protected $ctx;
/**
* URI
@@ -189,7 +189,7 @@ protected function initSSLContext() {
$this->errorneous = true;
return false;
}
- $this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, $a = [
+ $this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, [
EventSslContext::OPT_LOCAL_CERT => $this->certfile,
EventSslContext::OPT_LOCAL_PK => $this->pkfile,
EventSslContext::OPT_PASSPHRASE => $this->passphrase,
@@ -240,6 +240,7 @@ public function enable() {
-1,
$this->fd
);
+ //EventUtil::getSocketName($this->ev, $this->locHost, $this->locPort);
} else {
$this->ev->enable();
}
@@ -262,6 +263,9 @@ public function onListenerAcceptEv($listener, $fd, $addrPort, $ctx) {
$conn = new $class(null, $this->pool);
$conn->setParentSocket($this);
$conn->setPeername($addrPort[0], $addrPort[1]);
+ if ($this->ctx) {
+ $conn->setContext($this->ctx, EventBufferEvent::SSL_ACCEPTING);
+ }
$conn->setFd($fd);
}
@@ -353,7 +357,12 @@ public function accept() {
}
socket_set_nonblock($fd);
$class = $this->pool->connectionClass;
- return new $class($fd, $this->pool);
+ $conn = new $class(null, $this->pool);
+ if ($this->ctx) {
+ $conn->setContext($this->ctx, EventBufferEvent::SSL_ACCEPTING);
+ }
+ $conn->setFd($fd);
+ return $conn;
}
/**
View
@@ -100,45 +100,66 @@ class Connection extends IOStream {
protected $bevConnectEnabled = true;
/**
- * bevConnect used?
+ * SSL?
* @var boolean
*/
- protected $bevConnect = false;
+ protected $ssl = false;
/**
* URL
* @var string
*/
protected $url;
+ /**
+ * URI information
+ * @var hash
+ */
+ protected $uri;
+
/**
* Scheme
* @var string
*/
protected $scheme;
+
/**
- * Connected?
- * @return boolean
+ * Private key file
+ * @var string
*/
- public function isConnected() {
- return $this->connected;
- }
+ protected $pkfile;
+ /**
+ * Certificate file
+ * @var string
+ */
+ protected $certfile;
/**
- * Parses URL
- * @return hash URL info.
+ * Passphrase
+ * @var string
*/
- public function parseUrl($url) {
- $u = Daemon_Config::parseCfgUri($url);
- if (!$u) {
- return false;
- }
- if (!isset($u['port']) && isset($this->pool->config->port->value)) {
- $u['port'] = $this->pool->config->port->value;
- }
- return $u;
+ protected $passphrase;
+
+ /**
+ * Verify peer?
+ * @var boolean
+ */
+ protected $verifypeer = false;
+
+ /**
+ * Allow self-signed?
+ * @var boolean
+ */
+ protected $allowselfsigned = true;
+
+ /**
+ * Connected?
+ * @return boolean
+ */
+ public function isConnected() {
+ return $this->connected;
}
/**
@@ -293,8 +314,68 @@ public function onConnected($cb) {
}
$this->onConnected->push($cb);
}
+ }
+
+ protected function importParams() {
+
+ foreach ($this->uri['params'] as $key => $val) {
+ if (isset($this->{$key}) && is_bool($this->{$key})) {
+ $this->{$key} = (bool) $val;
+ continue;
+ }
+ if (!property_exists($this, $key)) {
+ Daemon::log(get_class($this).': unrecognized setting \'' . $key . '\'');
+ continue;
+ }
+ $this->{$key} = $val;
+ }
+ if (!$this->ctxname) {
+ return;
+ }
+ if (!isset(Daemon::$config->{'TransportContext:' . $this->ctxname})) {
+ Daemon::log(get_class($this).': undefined transport context \'' . $this->ctxname . '\'');
+ return;
+ }
+ $ctx = Daemon::$config->{'TransportContext:' . $this->ctxname};
+ foreach ($ctx as $key => $entry) {
+ $value = ($entry instanceof Daemon_ConfigEntry) ? $entry->value : $entry;
+ if (isset($this->{$key}) && is_bool($this->{$key})) {
+ $this->{$key} = (bool) $value;
+ continue;
+ }
+ if (!property_exists($this, $key)) {
+ Daemon::log(get_class($this).': unrecognized setting in transport context \'' . $this->ctxname . '\': \'' . $key . '\'');
+ continue;
+ }
+ $this->{$key} = $value;
+ }
+
+ }
+
+ /**
+ * Initialize SSL context
+ * @return object|false Context
+ */
+ protected function initSSLContext() {
+ if (!EventUtil::sslRandPoll()) {
+ Daemon::$process->log(get_class($this->pool) . ': EventUtil::sslRandPoll failed');
+ return false;
+ }
+ $params = [
+ EventSslContext::OPT_VERIFY_PEER => $this->verifypeer,
+ EventSslContext::OPT_ALLOW_SELF_SIGNED => $this->allowselfsigned,
+ ];
+ if ($this->certfile !== null) {
+ $params[EventSslContext::OPT_LOCAL_CERT] = $this->certfile;
+ }
+ if ($this->pkfile !== null) {
+ $params[EventSslContext::OPT_LOCAL_PK] = $this->pkfile;
+ }
+ if ($this->passphrase !== null) {
+ $params[EventSslContext::OPT_PASSPHRASE] = $this->passphrase;
+ }
+ return new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, $params);
}
-
/**
* Connects to URL
@@ -303,10 +384,22 @@ public function onConnected($cb) {
* @return void
*/
public function connect($url, $cb = null) {
- $u = $this->parseUrl($url);
+ $this->uri = Daemon_Config::parseCfgUri($url);
+ $u =& $this->uri;
+ if (!$u) {
+ return false;
+ }
+ if (!isset($u['port']) && isset($this->pool->config->port->value)) {
+ $u['port'] = $this->pool->config->port->value;
+ }
if (isset($u['user'])) {
$this->user = $u['user'];
}
+ $this->importParams();
+
+ if ($this->ssl) {
+ $this->setContext($this->initSSLContext(), EventBufferEvent::SSL_CONNECTING);
+ }
$this->url = $url;
$this->scheme = strtolower($u['scheme']);
View
@@ -139,7 +139,7 @@ public static function init($configFile = null) {
}
if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') {
- setlocale(LC_ALL,explode(',', Daemon::$config->locale->value));
+ setlocale(LC_ALL,array_map('trim', explode(',', Daemon::$config->locale->value)));
}
if (
Oops, something went wrong.

0 comments on commit a7dab17

Please sign in to comment.