Update to PSR-7 v1, update to match API changes

This commit is contained in:
Chris Boden 2015-05-22 09:49:14 -04:00
parent c2a51b62ef
commit c280d8137f
3 changed files with 26 additions and 33 deletions

View File

@ -22,8 +22,10 @@
}, },
"require": { "require": {
"php": ">=5.4.2", "php": ">=5.4.2",
"guzzlehttp/psr7": "dev-master", "guzzlehttp/psr7": "^1.0",
"psr/http-message": "0.9.*", "evenement/evenement": "^2.0"
"evenement/evenement": "~2.0" },
"require-dev": {
"react/http": "^0.4.1"
} }
} }

View File

@ -53,7 +53,7 @@ class Negotiator implements NegotiatorInterface {
return new Response(101, [ return new Response(101, [
'Upgrade' => 'websocket' 'Upgrade' => 'websocket'
, 'Connection' => 'Upgrade' , 'Connection' => 'Upgrade'
, 'Sec-WebSocket-Accept' => $this->sign((string)$request->getHeader('Sec-WebSocket-Key')) , 'Sec-WebSocket-Accept' => $this->sign((string)$request->getHeader('Sec-WebSocket-Key')[0])
]); ]);
} }

View File

@ -23,11 +23,11 @@ class RequestVerifier {
$passes += (int)$this->verifyMethod($request->getMethod()); $passes += (int)$this->verifyMethod($request->getMethod());
$passes += (int)$this->verifyHTTPVersion($request->getProtocolVersion()); $passes += (int)$this->verifyHTTPVersion($request->getProtocolVersion());
$passes += (int)$this->verifyRequestURI($request->getUri()->getPath()); $passes += (int)$this->verifyRequestURI($request->getUri()->getPath());
$passes += (int)$this->verifyHost((string)$request->getHeader('Host')); $passes += (int)$this->verifyHost($request->getHeader('Host'));
$passes += (int)$this->verifyUpgradeRequest((string)$request->getHeader('Upgrade')); $passes += (int)$this->verifyUpgradeRequest($request->getHeader('Upgrade'));
$passes += (int)$this->verifyConnection((string)$request->getHeader('Connection')); $passes += (int)$this->verifyConnection($request->getHeader('Connection'));
$passes += (int)$this->verifyKey((string)$request->getHeader('Sec-WebSocket-Key')); $passes += (int)$this->verifyKey($request->getHeader('Sec-WebSocket-Key'));
$passes += (int)$this->verifyVersion((string)$request->getHeader('Sec-WebSocket-Version')); $passes += (int)$this->verifyVersion($request->getHeader('Sec-WebSocket-Version'));
return (8 === $passes); return (8 === $passes);
} }
@ -71,59 +71,50 @@ class RequestVerifier {
} }
/** /**
* @param string|null * @param array $hostHeader
* @return bool * @return bool
* @todo Find out if I can find the master socket, ensure the port is attached to header if not 80 or 443 - not sure if this is possible, as I tried to hide it
* @todo Once I fix HTTP::getHeaders just verify this isn't NULL or empty...or maybe need to verify it's a valid domain??? Or should it equal $_SERVER['HOST'] ? * @todo Once I fix HTTP::getHeaders just verify this isn't NULL or empty...or maybe need to verify it's a valid domain??? Or should it equal $_SERVER['HOST'] ?
*/ */
public function verifyHost($val) { public function verifyHost(array $hostHeader) {
return (null !== $val); return (1 === count($hostHeader));
} }
/** /**
* Verify the Upgrade request to WebSockets. * Verify the Upgrade request to WebSockets.
* @param string $val MUST equal "websocket" * @param array $upgradeHeader MUST include "websocket"
* @return bool * @return bool
*/ */
public function verifyUpgradeRequest($val) { public function verifyUpgradeRequest(array $upgradeHeader) {
return ('websocket' === strtolower($val)); return (in_array('websocket', array_map('strtolower', $upgradeHeader)));
} }
/** /**
* Verify the Connection header * Verify the Connection header
* @param string $val MUST equal "Upgrade" * @param array $connectionHeader MUST include "Upgrade"
* @return bool * @return bool
*/ */
public function verifyConnection($val) { public function verifyConnection(array $connectionHeader) {
$val = strtolower($val); return in_array('upgrade', array_map('strtolower', $connectionHeader));
if ('upgrade' === $val) {
return true;
}
$vals = explode(',', str_replace(', ', ',', $val));
return (false !== array_search('upgrade', $vals));
} }
/** /**
* This function verifies the nonce is valid (64 big encoded, 16 bytes random string) * This function verifies the nonce is valid (64 big encoded, 16 bytes random string)
* @param string|null * @param array $keyHeader
* @return bool * @return bool
* @todo The spec says we don't need to base64_decode - can I just check if the length is 24 and not decode? * @todo The spec says we don't need to base64_decode - can I just check if the length is 24 and not decode?
* @todo Check the spec to see what the encoding of the key could be * @todo Check the spec to see what the encoding of the key could be
*/ */
public function verifyKey($val) { public function verifyKey(array $keyHeader) {
return (16 === strlen(base64_decode((string)$val))); return in_array(16, array_map('strlen', array_map('base64_decode', $keyHeader)));
} }
/** /**
* Verify the version passed matches this RFC * Verify the version passed matches this RFC
* @param string|int MUST equal 13|"13" * @param string|int $versionHeader MUST equal 13|"13"
* @return bool * @return bool
*/ */
public function verifyVersion($val) { public function verifyVersion($versionHeader) {
return (static::VERSION === (int)$val); return (1 === count($versionHeader) && static::VERSION === (int)$versionHeader[0]);
} }
/** /**