Skip to content
This repository
Browse code

Docs, refactoring

  • Loading branch information...
commit a7dab17a755743609d9fa6c761d56d9d76c2f45a 1 parent 64088c6
Vasily Zorin authored March 20, 2013
15  lib/BoundSocket.php
@@ -49,7 +49,7 @@
49 49
 	 * Context
50 50
 	 * @var mixed
51 51
 	 */
52  
-	public $ctx; // @TODO: make it protected
  52
+	protected $ctx;
53 53
 
54 54
 	/**
55 55
 	 * URI
@@ -189,7 +189,7 @@ protected function initSSLContext() {
189 189
 	 		$this->errorneous = true;
190 190
 	 		return false;
191 191
 	 	}
192  
-	 	$this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, $a = [
  192
+	 	$this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, [
193 193
  			EventSslContext::OPT_LOCAL_CERT  => $this->certfile,
194 194
  			EventSslContext::OPT_LOCAL_PK    => $this->pkfile,
195 195
  			EventSslContext::OPT_PASSPHRASE  => $this->passphrase,
@@ -240,6 +240,7 @@ public function enable() {
240 240
 					-1,
241 241
 					$this->fd
242 242
 				);
  243
+				//EventUtil::getSocketName($this->ev, $this->locHost, $this->locPort);
243 244
 			} else {
244 245
 				$this->ev->enable();
245 246
 			}
@@ -262,6 +263,9 @@ public function onListenerAcceptEv($listener, $fd, $addrPort, $ctx)  {
262 263
 		$conn = new $class(null, $this->pool);
263 264
 		$conn->setParentSocket($this);
264 265
 		$conn->setPeername($addrPort[0], $addrPort[1]);
  266
+		if ($this->ctx) {
  267
+			$conn->setContext($this->ctx, EventBufferEvent::SSL_ACCEPTING);
  268
+		}
265 269
 		$conn->setFd($fd);
266 270
 	}
267 271
 
@@ -353,7 +357,12 @@ public function accept() {
353 357
 		}
354 358
 		socket_set_nonblock($fd);
355 359
 		$class = $this->pool->connectionClass;
356  
- 		return new $class($fd, $this->pool);
  360
+ 		$conn = new $class(null, $this->pool);
  361
+ 		if ($this->ctx) {
  362
+			$conn->setContext($this->ctx, EventBufferEvent::SSL_ACCEPTING);
  363
+		}
  364
+		$conn->setFd($fd);
  365
+		return $conn;
357 366
 	}
358 367
 
359 368
 	/**
133  lib/Connection.php
@@ -100,10 +100,10 @@ class Connection extends IOStream {
100 100
 	protected $bevConnectEnabled = true;
101 101
 
102 102
 	/**
103  
-	 * bevConnect used?
  103
+	 * SSL?
104 104
 	 * @var boolean
105 105
 	 */
106  
-	protected $bevConnect = false;
  106
+	protected $ssl = false;
107 107
 
108 108
 	/**
109 109
 	 * URL
@@ -112,33 +112,54 @@ class Connection extends IOStream {
112 112
 	protected $url;
113 113
 
114 114
 	/**
  115
+	 * URI information
  116
+	 * @var hash
  117
+	 */
  118
+	protected $uri;
  119
+
  120
+	/**
115 121
 	 * Scheme
116 122
 	 * @var string
117 123
 	 */
118 124
 	protected $scheme;
119 125
 
  126
+
120 127
 	/**
121  
-	 * Connected?
122  
-	 * @return boolean
  128
+	 * Private key file
  129
+	 * @var string
123 130
 	 */
124  
-	public function isConnected() {
125  
-		return $this->connected;
126  
-	}
  131
+	protected $pkfile;
127 132
 
  133
+	/**
  134
+	 * Certificate file
  135
+	 * @var string
  136
+	 */
  137
+	protected $certfile;
128 138
 
129 139
 	/**
130  
-	 * Parses URL
131  
-	 * @return hash URL info.
  140
+	 * Passphrase
  141
+	 * @var string
132 142
 	 */
133  
-	public function parseUrl($url) {
134  
-		$u = Daemon_Config::parseCfgUri($url);
135  
-		if (!$u) {
136  
-			return false;
137  
-		}
138  
-		if (!isset($u['port']) && isset($this->pool->config->port->value)) {
139  
-			$u['port'] = $this->pool->config->port->value;
140  
-		}
141  
-		return $u;
  143
+	protected $passphrase;
  144
+
  145
+	/**
  146
+	 * Verify peer?
  147
+	 * @var boolean
  148
+	 */
  149
+	protected $verifypeer = false;
  150
+
  151
+	/**
  152
+	 * Allow self-signed?
  153
+	 * @var boolean
  154
+	 */
  155
+	protected $allowselfsigned = true;
  156
+
  157
+	/**
  158
+	 * Connected?
  159
+	 * @return boolean
  160
+	 */
  161
+	public function isConnected() {
  162
+		return $this->connected;
142 163
 	}
143 164
 
144 165
 	/**
@@ -293,8 +314,68 @@ public function onConnected($cb) {
293 314
 			}
294 315
 			$this->onConnected->push($cb);
295 316
 		}
  317
+	}	
  318
+
  319
+	protected function importParams() {
  320
+
  321
+		foreach ($this->uri['params'] as $key => $val) {
  322
+			if (isset($this->{$key}) && is_bool($this->{$key})) {
  323
+				$this->{$key} = (bool) $val;
  324
+				continue;
  325
+			}
  326
+			if (!property_exists($this, $key)) {
  327
+				Daemon::log(get_class($this).': unrecognized setting \'' . $key . '\'');
  328
+				continue;
  329
+			}
  330
+			$this->{$key} = $val;
  331
+		}
  332
+		if (!$this->ctxname) {
  333
+			return;
  334
+		}
  335
+		if (!isset(Daemon::$config->{'TransportContext:' . $this->ctxname})) {
  336
+			Daemon::log(get_class($this).': undefined transport context \'' . $this->ctxname . '\'');
  337
+			return;
  338
+		}
  339
+		$ctx = Daemon::$config->{'TransportContext:' . $this->ctxname};
  340
+		foreach ($ctx as $key => $entry) {
  341
+			$value = ($entry instanceof Daemon_ConfigEntry) ? $entry->value : $entry;
  342
+			if (isset($this->{$key}) && is_bool($this->{$key})) {
  343
+			$this->{$key} = (bool) $value;
  344
+				continue;
  345
+			}
  346
+			if (!property_exists($this, $key)) {
  347
+				Daemon::log(get_class($this).': unrecognized setting in transport context \'' . $this->ctxname . '\': \'' . $key . '\'');
  348
+				continue;
  349
+			}
  350
+			$this->{$key} = $value;	
  351
+		}
  352
+
  353
+	}
  354
+
  355
+	/**
  356
+	 * Initialize SSL context
  357
+	 * @return object|false Context
  358
+	 */
  359
+	protected function initSSLContext() {
  360
+		if (!EventUtil::sslRandPoll()) {
  361
+	 		Daemon::$process->log(get_class($this->pool) . ': EventUtil::sslRandPoll failed');
  362
+	 		return false;
  363
+	 	}
  364
+	 	$params = [
  365
+ 			EventSslContext::OPT_VERIFY_PEER => $this->verifypeer,
  366
+ 			EventSslContext::OPT_ALLOW_SELF_SIGNED => $this->allowselfsigned,
  367
+		];
  368
+		 if ($this->certfile !== null) {
  369
+		 	$params[EventSslContext::OPT_LOCAL_CERT] = $this->certfile;
  370
+		 }
  371
+		 if ($this->pkfile !== null) {
  372
+		 	$params[EventSslContext::OPT_LOCAL_PK] = $this->pkfile;
  373
+		 }
  374
+		 if ($this->passphrase !== null) {
  375
+		 	$params[EventSslContext::OPT_PASSPHRASE] = $this->passphrase;
  376
+		 }
  377
+	 	return new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, $params);
296 378
 	}
297  
-	
298 379
 
299 380
 	/**
300 381
 	 * Connects to URL
@@ -303,10 +384,22 @@ public function onConnected($cb) {
303 384
 	 * @return void
304 385
 	 */
305 386
 	public function connect($url, $cb = null) {
306  
-		$u = $this->parseUrl($url);
  387
+		$this->uri = Daemon_Config::parseCfgUri($url);
  388
+		$u =& $this->uri;
  389
+		if (!$u) {
  390
+			return false;
  391
+		}
  392
+		if (!isset($u['port']) && isset($this->pool->config->port->value)) {
  393
+			$u['port'] = $this->pool->config->port->value;
  394
+		}
307 395
 		if (isset($u['user'])) {
308 396
 			$this->user = $u['user'];
309 397
 		}
  398
+		$this->importParams();
  399
+
  400
+		if ($this->ssl) {
  401
+			$this->setContext($this->initSSLContext(), EventBufferEvent::SSL_CONNECTING);
  402
+		}
310 403
 			
311 404
 		$this->url = $url;
312 405
 		$this->scheme = strtolower($u['scheme']);
2  lib/Daemon_Bootstrap.php
@@ -139,7 +139,7 @@ public static function init($configFile = null) {
139 139
 		}
140 140
 		
141 141
 		if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') {
142  
-			setlocale(LC_ALL,explode(',', Daemon::$config->locale->value));
  142
+			setlocale(LC_ALL,array_map('trim', explode(',', Daemon::$config->locale->value)));
143 143
 		}
144 144
 		
145 145
 		if (
298  lib/Daemon_Config.php
@@ -10,76 +10,268 @@
10 10
  */
11 11
 class Daemon_Config implements ArrayAccess {
12 12
 
13  
-	// Worker graceful restarting:
  13
+	/**
  14
+	 * Maximum memory usage
  15
+	 * @var size
  16
+	 */
14 17
 	public $maxmemoryusage = '0b';
  18
+
  19
+	/**
  20
+	 * Maximum idle time
  21
+	 * @var time
  22
+	 */
15 23
 	public $maxidle        = '0s';
16 24
 			
17  
-	// Main Pathes
  25
+	/**
  26
+	 * PID file
  27
+	 * @var path
  28
+	 */
18 29
 	public $pidfile        = '/var/run/phpd.pid';
  30
+
  31
+	/**
  32
+	 * Default PID file
  33
+	 * @var path
  34
+	 */
19 35
 	public $defaultpidfile = '/var/run/phpd.pid';
  36
+
  37
+	/**
  38
+	 * Config file
  39
+	 * @var path
  40
+	 */
20 41
 	public $configfile     = '/etc/phpdaemon/phpd.conf;/etc/phpd/phpd.conf;./conf/phpd.conf';
  42
+
  43
+	/**
  44
+	 * Application resolver
  45
+	 * @var path
  46
+	 */
21 47
 	public $path           = '/etc/phpdaemon/AppResolver.php;./conf/AppResolver.php';
  48
+
  49
+	/**
  50
+	 * Path to applications
  51
+	 * @var path
  52
+	 */
22 53
 	public $appfilepath    = '{app-*,applications}/%s.php';
23  
-	public $autoload   	 	= NULL;
  54
+
  55
+	/**
  56
+	 * Autoload path
  57
+	 * @var path
  58
+	 */
  59
+	public $autoload   	 	= null;
24 60
 			
25  
-	// Master-related
  61
+	/**
  62
+	 * Multi-Process Manager delay
  63
+	 * @var time
  64
+	 */
26 65
 	public $mpmdelay        = '0.1s';
  66
+
  67
+	/**
  68
+	 * Start workers
  69
+	 * @var number
  70
+	 */
27 71
 	public $startworkers    = 4;
  72
+
  73
+	/**
  74
+	 * Minimum number of workers
  75
+	 * @var number
  76
+	 */
28 77
 	public $minworkers      = 4;
  78
+
  79
+	/**
  80
+	 * Maximum number of workers
  81
+	 * @var number
  82
+	 */
29 83
 	public $maxworkers      = 8;
  84
+
  85
+	/**
  86
+	 * Minimum number of spare workes
  87
+	 * @var number
  88
+	 */
30 89
 	public $minspareworkers = 2;
  90
+
  91
+	/**
  92
+	 * Maximum number of spare workes
  93
+	 * @var number
  94
+	 */
31 95
 	public $maxspareworkers = 0;
  96
+
  97
+	/**
  98
+	 * Master thread priority
  99
+	 * @var integer
  100
+	 */
32 101
 	public $masterpriority  = 100;
  102
+
  103
+	/**
  104
+	 * IPC thread priority
  105
+	 * @var integer
  106
+	 */
33 107
 	public $ipcthreadpriority = 100;
34 108
 			 
35  
-	// Requests
  109
+	/**
  110
+	 * IPC thread priority
  111
+	 * @var boolean
  112
+	 */
36 113
 	public $obfilterauto                   = 1;
37  
-	public $maxconcurrentrequestsperworker = 1000;
38 114
 			
39  
-	// Worker-related
40  
-	public $user                     = NULL;
41  
-	public $group                    = NULL;
  115
+	/**
  116
+	 * System user (setuid)
  117
+	 * @var string
  118
+	 */	
  119
+	public $user                     = null;
  120
+
  121
+	/**
  122
+	 * System group (setgid)
  123
+	 * @var string
  124
+	 */	
  125
+	public $group                    = null;
  126
+
  127
+	/**
  128
+	 * Automatic garbage collector, number of iterations between GC call
  129
+	 * @var number
  130
+	 */	
42 131
 	public $autogc                   = '1k';
  132
+
  133
+	/**
  134
+	 * Chroot
  135
+	 * @var string
  136
+	 */	
43 137
 	public $chroot                   = '/';
  138
+
  139
+	/**
  140
+	 * Current directory
  141
+	 * @var string
  142
+	 */	
44 143
 	public $cwd                      = '.';
  144
+
  145
+	/**
  146
+	 * Autoreload interval. Time interval between checks.
  147
+	 * @var time
  148
+	 */	
45 149
 	public $autoreload               = '0s';
  150
+
  151
+	/**
  152
+	 * Try to import updated code (runkit required)
  153
+	 * @var boolean
  154
+	 */	
46 155
 	public $autoreimport             = 0;
  156
+
  157
+	/**
  158
+	 * Worker thread priority
  159
+	 * @var integer
  160
+	 */	
47 161
 	public $workerpriority           = 4;
  162
+
  163
+	/**
  164
+	 * Throw exception on shutdown?
  165
+	 * @var boolean
  166
+	 */	
48 167
 	public $throwexceptiononshutdown = 0;
  168
+
  169
+	/**
  170
+	 * Comma-separated list of locales
  171
+	 * @var string
  172
+	 */	
49 173
 	public $locale                   = '';
  174
+
  175
+	/**
  176
+	 * Restrict usage of error-control functions (like @ operator), useful in debugging
  177
+	 * @var boolean
  178
+	 */	
50 179
 	public $restricterrorcontrol = false; 
51 180
 			
52 181
 	// Logging-related
  182
+
  183
+	/**
  184
+	 * Logging?
  185
+	 * @var boolean
  186
+	 */	
53 187
 	public $logging            = 1;
  188
+	
  189
+	/**
  190
+	 * Log storage
  191
+	 * @var boolean
  192
+	 */	
54 193
 	public $logstorage         = '/var/log/phpdaemon.log';
  194
+
  195
+	/**
  196
+	 * Log errors?
  197
+	 * @var boolean
  198
+	 */	
55 199
 	public $logerrors          = 1;
  200
+
  201
+	/**
  202
+	 * Log Worker->setState() ?
  203
+	 * @var boolean
  204
+	 */	
56 205
 	public $logworkersetstate = 0;
  206
+
  207
+	/**
  208
+	 * Log events?
  209
+	 * @var boolean
  210
+	 */	
57 211
 	public $logevents          = 0;
58  
-	public $logqueue           = 0;
59  
-	public $logreads           = 0;
  212
+
  213
+	/**
  214
+	 * Log signals?
  215
+	 * @var boolean
  216
+	 */	
60 217
 	public $logsignals         = 0;
61  
-	public $verbose = 0;
  218
+
  219
+	/**
  220
+	 * Do not close STDOUT and STDERR pipes and send log messages there
  221
+	 * @var boolean
  222
+	 */	
62 223
 	public $verbosetty = 0;
63 224
 	
64  
-	// eio
  225
+	/**
  226
+	 * EIO enabled?
  227
+	 * @var boolean
  228
+	 */	
65 229
 	public $eioenabled = 1;
  230
+
  231
+	/**
  232
+	 * EIO maximum idle time
  233
+	 * @var time
  234
+	 */	
66 235
 	public $eiosetmaxidle = null;
  236
+
  237
+	/**
  238
+	 * EIO maximum parallel threads
  239
+	 * @var number
  240
+	 */	
67 241
 	public $eiosetmaxparallel = null;
  242
+
  243
+	/**
  244
+	 * EIO maximum poll requests
  245
+	 * @var number
  246
+	 */
68 247
 	public $eiosetmaxpollreqs = null;
  248
+
  249
+	/**
  250
+	 * EIO maximum poll time
  251
+	 * @var time
  252
+	 */
69 253
 	public $eiosetmaxpolltime = null;
  254
+
  255
+	/**
  256
+	 * EIO minimum parallel threads
  257
+	 * @var number
  258
+	 */
70 259
 	public $eiosetminparallel = null;
71 260
 	
72 261
 	public static $lastRevision = 0;
73 262
 	
74  
-	// @todo phpdoc missed
  263
+	/**
  264
+	 * Constructor
  265
+	 * @return object
  266
+	 */
75 267
 	
76 268
 	public function __construct() {
77  
-		static $sizes = array('maxmemoryusage');
78  
-		static $times = array('maxidle', 'autoreload', 'mpmdelay', 'eiosetmaxpolltime');
79  
-		static $numbers = array(
  269
+		static $sizes = ['maxmemoryusage'];
  270
+		static $times = ['maxidle', 'autoreload', 'mpmdelay', 'eiosetmaxpolltime'];
  271
+		static $numbers = [
80 272
 			'maxrequests', 'autogc','minworkers','maxworkers','minspareworkers','maxspareworkers','masterpriority', 'ipcthreadpriority',
81 273
 			'eiosetmaxidle', 'eiosetmaxparallel', 'eiosetmaxpollreqs', 'eiosetminparallel', 'verbose', 'verbosetty'
82  
-		);
  274
+		];
83 275
 
84 276
 		foreach ($this as $name => $value) {
85 277
 			if (in_array($name, $sizes)) {
@@ -103,12 +295,21 @@ public function __construct() {
103 295
 		}
104 296
 	}
105 297
 
  298
+	/**
  299
+	 * Load config file
  300
+	 * @param string Path
  301
+	 * @return boolean Success
  302
+	 */
106 303
 	public function loadFile($path) {
107 304
 		$parser = new Daemon_ConfigParser($path,$this);
108 305
 		$this->onLoad();
109 306
 		return !$parser->isErrorneous();
110 307
 	}
111  
-	
  308
+
  309
+	/**
  310
+	 * Called when config is loaded
  311
+	 * @return void
  312
+	 */	
112 313
 	protected function onLoad() {
113 314
 		if (
114 315
 			isset($this->minspareworkers->value) 
@@ -132,30 +333,63 @@ protected function onLoad() {
132 333
 		}
133 334
 	}
134 335
 
135  
-	public function getRealOffsetName($offset) {
  336
+
  337
+	/**
  338
+	 * Get real property name
  339
+	 * @param string Property name
  340
+	 * @return string Real property name
  341
+	 */
  342
+	public function getRealPropertyName($prop) {
136 343
 		return str_replace('-', '', strtolower($offset));
137 344
 	}
  345
+
  346
+	/**
  347
+	 * Checks if property exists
  348
+	 * @param string Property name
  349
+	 * @return boolean Exists?
  350
+	 */
138 351
 	
139  
-	public function offsetExists($offset) {
140  
-		return $this->offsetGet($offset) !== NULL;
  352
+	public function offsetExists($prop) {
  353
+		$prop = $this->getRealPropertyName($prop);
  354
+		return propery_exists($this, $prop);
141 355
 	}
142 356
 
143  
-	public function offsetGet($offset) {
144  
-		$offset = $this->getRealOffsetName($offset);
145  
-
146  
-		return isset($this->{$offset}) ? $this->{$offset}->value : NULL;
  357
+	/**
  358
+	 * Get property by name
  359
+	 * @param string Property name
  360
+	 * @return mixed
  361
+	 */
  362
+	public function offsetGet($prop) {
  363
+		$prop = $this->getRealPropertyName($prop);
  364
+		return isset($this->{$prop}) ? $this->{$prop}->value : null;
147 365
 	}
148  
-	
149  
-	public function offsetSet($offset,$value) {
150  
-		$offset = $this->getRealOffsetName($offset);
151 366
 
152  
-		$this->{$offset} = $value;
  367
+	/**
  368
+	 * Set property
  369
+	 * @param string Property name
  370
+	 * @param mixed Value
  371
+	 * @return void
  372
+	 */
  373
+	public function offsetSet($prop,$value) {
  374
+		$prop = $this->getRealPropertyName($prop);
  375
+		$this->{$prop} = $value;
153 376
 	}
154 377
 
155  
-	public function offsetUnset($offset) {
156  
-		unset($this->{$this->getRealOffsetName($offset)});
  378
+	/**
  379
+	 * Unset property
  380
+	 * @param string Property name
  381
+	 * @return void
  382
+	 */
  383
+	public function offsetUnset($prop) {
  384
+		$prop = $this->getRealPropertyName($prop);
  385
+		unset($this->{$prop});
157 386
 	}
158 387
 
  388
+	/**
  389
+	 * Checks if property exists
  390
+	 * @param string Property name
  391
+	 * @return boolean Exists?
  392
+	 */
159 393
 	public static function parseCfgUri($uri, $source = null) {
160 394
 		if (strpos($uri, '://') === false) {
161 395
 			if (strncmp($uri, 'unix:', 5) === 0) {
8  lib/Daemon_ConfigParser.php
@@ -90,7 +90,7 @@ public function __construct($file, $config, $included = false) {
90 90
 			self::T_ALL => function($c) {
91 91
 				if (ctype_space($c)) { }
92 92
 				elseif ($c === '#') {
93  
-					$this->state[] = array(Daemon_ConfigParser::T_COMMENT);
  93
+					$this->state[] = [Daemon_ConfigParser::T_COMMENT];
94 94
 				}
95 95
 				elseif ($c === '}') {
96 96
 					if (sizeof($this->state) > 1) {
@@ -108,7 +108,7 @@ public function __construct($file, $config, $included = false) {
108 108
 					$newLineDetected = null;
109 109
 
110 110
 					for (;$this->p < $this->len; ++$this->p) {
111  
-						$prePoint = array($this->line, $this->col - 1);
  111
+						$prePoint = [$this->line, $this->col - 1];
112 112
 						$c = $this->getCurrentChar();
113 113
 
114 114
 						if (ctype_space($c) || $c === '=' || $c === ',') {
@@ -246,10 +246,10 @@ public function __construct($file, $config, $included = false) {
246 246
 						}
247 247
 						$scope->{$sectionName}->source = 'config';
248 248
 						$scope->{$sectionName}->revision = $this->revision;
249  
-						$this->state[] = array(
  249
+						$this->state[] = [
250 250
 							Daemon_ConfigParser::T_ALL,
251 251
 							$scope->{$sectionName},
252  
-						);
  252
+						];
253 253
 					}
254 254
 				} else {
255 255
 					$this->raiseError('Unexpected char \''.Debug::exportBytes($c).'\'');
6  lib/Daemon_ConfigSection.php
@@ -13,7 +13,7 @@ class Daemon_ConfigSection implements ArrayAccess, Countable {
13 13
 	public $source;
14 14
 	public $revision;
15 15
 	
16  
-	public function __construct($arr = array()) {
  16
+	public function __construct($arr = []) {
17 17
 		foreach ($arr as $k => $v) {
18 18
 			if (!is_object($v)) {
19 19
 				$e = new Daemon_ConfigEntry;
@@ -36,7 +36,7 @@ public function count() {
36 36
 	}
37 37
 	
38 38
 	public function toArray() {
39  
-		$arr = array();
  39
+		$arr = [];
40 40
 		foreach ($this as $k => $entry) {
41 41
 			if (!$entry instanceof Daemon_ConfigEntry)	{
42 42
 				continue;
@@ -51,7 +51,7 @@ public function getRealOffsetName($offset) {
51 51
 	}
52 52
 
53 53
 	public function offsetExists($offset) {
54  
-		return $this->offsetGet($offset) !== NULL;
  54
+		return $this->offsetGet($offset) !== null;
55 55
 	}
56 56
 
57 57
 	public function offsetGet($offset) {
14  lib/Daemon_IPCThread.php
@@ -128,7 +128,7 @@ protected function prepareSystemEnv() {
128 128
 		}
129 129
 
130 130
 		if (isset(Daemon::$config->group->value)) {
131  
-			if ($sg === FALSE) {
  131
+			if ($sg === false) {
132 132
 				$this->log('Couldn\'t change group to \'' . Daemon::$config->group->value . '\'. You must replace config-variable \'group\' with existing group.');
133 133
 				exit(0);
134 134
 			}
@@ -142,7 +142,7 @@ protected function prepareSystemEnv() {
142 142
 		}
143 143
 
144 144
 		if (isset(Daemon::$config->user->value)) {
145  
-			if ($su === FALSE) {
  145
+			if ($su === false) {
146 146
 				$this->log('Couldn\'t change user to \'' . Daemon::$config->user->value . '\', user not found. You must replace config-variable \'user\' with existing username.');
147 147
 				exit(0);
148 148
 			}
@@ -189,7 +189,7 @@ protected function update() {
189 189
 	 * @param boolean - Hard? If hard, we shouldn't wait for graceful shutdown of the running applications.
190 190
 	 * @return boolean - Ready?
191 191
 	 */
192  
-	public function shutdown($hard = FALSE) {
  192
+	public function shutdown($hard = false) {
193 193
 		$error = error_get_last(); 
194 194
 		if ($error) {
195 195
 			if ($error['type'] === E_ERROR) {
@@ -207,7 +207,7 @@ public function shutdown($hard = FALSE) {
207 207
 
208 208
 		@ob_flush();
209 209
 
210  
-		if ($this->terminated === TRUE) {
  210
+		if ($this->terminated === true) {
211 211
 			if ($hard) {
212 212
 				exit(0);
213 213
 			}
@@ -215,7 +215,7 @@ public function shutdown($hard = FALSE) {
215 215
 			return;
216 216
 		}
217 217
 
218  
-		$this->terminated = TRUE;
  218
+		$this->terminated = true;
219 219
 		if ($hard) {
220 220
 			exit(0);
221 221
 		}
@@ -233,7 +233,7 @@ protected function sigint() {
233 233
 			$this->log('caught SIGINT.');
234 234
 		}
235 235
 
236  
-		$this->shutdown(TRUE);
  236
+		$this->shutdown(true);
237 237
 	}
238 238
 
239 239
 	/**
@@ -273,7 +273,7 @@ public function sighup() {
273 273
 			Daemon::loadConfig(Daemon::$config->configfile->value);
274 274
 		}
275 275
 
276  
-		$this->update = TRUE;
  276
+		$this->update = true;
277 277
 	}
278 278
 
279 279
 	/**
25  lib/HTTPServerConnection.php
@@ -23,6 +23,8 @@ class HTTPServerConnection extends Connection {
23 23
 	protected $EOL = "\r\n";
24 24
 	protected $currentHeader;
25 25
 
  26
+	protected $policyReqNotFound = false;
  27
+
26 28
 	public function checkSendfileCap() { // @DISCUSS
27 29
 		return true;
28 30
 	}
@@ -146,15 +148,9 @@ protected function httpProcessHeaders() {
146 148
 	 */
147 149
 	
148 150
 	protected function onRead() {
149  
-		start:
150  
-		if ($this->finished) {
151  
-			return;
152  
-		}
153  
-		if ($this->state === self::STATE_ROOT) {
154  
-			if ($this->req !== null) { // we have to wait the current request.
155  
-				return;
156  
-			}
157  
-			if (($d = $this->drainIfMatch("<policy-file-request/>\x00")) === null) { // partially match
  151
+		if (!$this->policyReqNotFound) {
  152
+			$d = $this->drainIfMatch("<policy-file-request/>\x00");
  153
+			if ($d === null) { // partially match
158 154
 				return;
159 155
 			}
160 156
 			if ($d) {
@@ -163,6 +159,17 @@ protected function onRead() {
163 159
 				}
164 160
 				$this->finish();
165 161
 				return;
  162
+			} else {
  163
+				$this->policyReqNotFound = true;
  164
+			}
  165
+		}
  166
+		start:
  167
+		if ($this->finished) {
  168
+			return;
  169
+		}
  170
+		if ($this->state === self::STATE_ROOT) {
  171
+			if ($this->req !== null) { // we have to wait the current request.
  172
+				return;
166 173
 			}
167 174
 			if (!$this->req = $this->newRequest()) {
168 175
 				$this->finish();
60  lib/IOStream.php
@@ -146,6 +146,24 @@
146 146
 	protected $freed = false;
147 147
 
148 148
 	/**
  149
+	 * Context
  150
+	 * @var object
  151
+	 */
  152
+	protected $ctx;
  153
+
  154
+	/**
  155
+	 * Context name
  156
+	 * @var object
  157
+	 */
  158
+	protected $ctxname;
  159
+
  160
+	/**
  161
+	 * Defines context-related flag
  162
+	 * @var integer
  163
+	 */
  164
+	protected $ctxMode;
  165
+
  166
+	/**
149 167
 	 * IOStream constructor
150 168
  	 * @param resource File descriptor. Optional.
151 169
 	 * @param object Pool. Optional.
@@ -207,6 +225,19 @@ public function setReadPacketSize($n) {
207 225
 		return $this;
208 226
 	}
209 227
 
  228
+
  229
+	/**
  230
+	 * Sets context mode
  231
+	 * @param object Context
  232
+	 * @param integer Mode
  233
+	 * @return void
  234
+	 */
  235
+
  236
+	public function setContext($ctx, $mode) {
  237
+		$this->ctx = $ctx;
  238
+		$this->ctxMode = $mode;
  239
+	}
  240
+
210 241
 	/**
211 242
 	 * Sets fd
212 243
 	 * @param mixed File descriptor
@@ -230,9 +261,17 @@ public function setFd($fd, $bev = null) {
230 261
 			$this->alive = true;
231 262
 		} else {
232 263
 			$flags = !is_resource($this->fd) ? EventBufferEvent::OPT_CLOSE_ON_FREE : 0 /*| EventBufferEvent::OPT_DEFER_CALLBACKS /* buggy option */;
233  
-			if (isset($this->parentSocket->ctx) && $this->parentSocket->ctx instanceof EventSslContext) {
234  
-				$this->bev = EventBufferEvent::sslSocket(Daemon::$process->eventBase, $this->fd,$this->parentSocket->ctx, EventBufferEvent::SSL_ACCEPTING, $flags);
235  
-				$this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']);
  264
+			if ($this->ctx) {
  265
+				if ($this->ctx instanceof EventSslContext) {
  266
+					$this->bev = EventBufferEvent::sslSocket(Daemon::$process->eventBase, $this->fd, $this->ctx, $this->ctxMode, $flags);
  267
+					if ($this->bev) {
  268
+						$this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']);
  269
+					}
  270
+					$this->ssl = true;
  271
+				} else {
  272
+					$this->log('unsupported type of context: '.($this->ctx ? get_class($this->ctx) : 'undefined'));
  273
+					return;
  274
+				}
236 275
 			} else {
237 276
 				$this->bev = new EventBufferEvent(Daemon::$process->eventBase, $this->fd, $flags, [$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']);
238 277
 			}
@@ -336,25 +375,24 @@ public function readLine($eol = null) {
336 375
 	}
337 376
 
338 377
 	/* Drains buffer it matches the string
339  
-	 * @param string String
  378
+	 * @param string Data
340 379
 	 * @return boolean|null Success
341 380
 	 */
342 381
 	public function drainIfMatch($str) {
343 382
 		if (!isset($this->bev)) {
344 383
 			return false;
345 384
 		}
  385
+		$in = $this->bev->input;
346 386
 		$l = strlen($str);
347  
-		$ll = $this->bev->input->length;
  387
+		$ll = $in->length;
  388
+		// @TODO: add End to search()
348 389
 		if ($ll < $l) {
349  
-			$read = $this->read($ll);
350  
-			$this->bev->input->prepend($read);
351  
-			return strncmp($read, $str, $ll) === 0;
  390
+			return $in->search(substr($str, 0, $ll), 0) === 0 ? null : false;
352 391
 		}
353  
-		$read = $this->read($l);
354  
-		if ($read === $str) {
  392
+		if ($in->search($str, 0) === 0) {
  393
+			$in->drain($l);
355 394
 			return true;
356 395
 		}
357  
-		$this->bev->input->prepend($read);
358 396
 		return false;
359 397
 	}
360 398
 

0 notes on commit a7dab17

Please sign in to comment.
Something went wrong with that request. Please try again.