Skip to content

Commit 9c74a41

Browse files
authored
Merge pull request #35 from haugli92/master
Adding getClientHeaders
2 parents 5c596b2 + 73a1b65 commit 9c74a41

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Connection.php

+20-7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class Connection
4949
* @var string $dataBuffer
5050
*/
5151
private string $dataBuffer = '';
52+
53+
/**
54+
* @var array $headers
55+
*/
56+
private array $headers = [];
5257

5358
/**
5459
* @param Server $server
@@ -104,16 +109,15 @@ private function handshake(string $data): bool
104109
$this->application = $this->server->getApplication($applicationKey);
105110

106111
// generate headers array:
107-
$headers = [];
108112
foreach ($lines as $line) {
109113
$line = chop($line);
110114
if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
111-
$headers[ strtolower($matches[1])] = $matches[2];
115+
$this->headers[ strtolower($matches[1])] = $matches[2];
112116
}
113117
}
114118

115119
// check for supported websocket version:
116-
if (!isset($headers['sec-websocket-version']) || $headers['sec-websocket-version'] < 6) {
120+
if (!isset($this->headers['sec-websocket-version']) || $this->headers['sec-websocket-version'] < 6) {
117121
$this->log('Unsupported websocket version.');
118122
$this->sendHttpResponse(501);
119123
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
@@ -123,8 +127,8 @@ private function handshake(string $data): bool
123127

124128
// check origin:
125129
if ($this->server->getCheckOrigin() === true) {
126-
$origin = (isset($headers['sec-websocket-origin'])) ? $headers['sec-websocket-origin'] : '';
127-
$origin = (isset($headers['origin'])) ? $headers['origin'] : $origin;
130+
$origin = (isset($this->headers['sec-websocket-origin'])) ? $this->headers['sec-websocket-origin'] : '';
131+
$origin = (isset($this->headers['origin'])) ? $this->headers['origin'] : $origin;
128132
if (empty($origin)) {
129133
$this->log('No origin provided.');
130134
$this->sendHttpResponse(401);
@@ -143,13 +147,13 @@ private function handshake(string $data): bool
143147
}
144148

145149
// do handyshake: (hybi-10)
146-
$secKey = $headers['sec-websocket-key'];
150+
$secKey = $this->headers['sec-websocket-key'];
147151
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
148152
$response = "HTTP/1.1 101 Switching Protocols\r\n";
149153
$response .= "Upgrade: websocket\r\n";
150154
$response .= "Connection: Upgrade\r\n";
151155
$response .= "Sec-WebSocket-Accept: " . $secAccept . "\r\n";
152-
if (isset($headers['sec-websocket-protocol']) && !empty($headers['sec-websocket-protocol'])) {
156+
if (isset($this->headers['sec-websocket-protocol']) && !empty($this->headers['sec-websocket-protocol'])) {
153157
$response .= "Sec-WebSocket-Protocol: " . substr($path, 1) . "\r\n";
154158
}
155159
$response .= "\r\n";
@@ -588,6 +592,15 @@ public function getClientSocket()
588592
{
589593
return $this->socket;
590594
}
595+
596+
/**
597+
* Return the headers of the connection
598+
* @return array
599+
*/
600+
public function getClientHeaders(): array
601+
{
602+
return $this->headers;
603+
}
591604

592605
/**
593606
* Returns the application the client is connected to.

0 commit comments

Comments
 (0)