Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding getClientHeaders #35

Merged
merged 5 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Connection
* @var string $dataBuffer
*/
private string $dataBuffer = '';

/**
* @var array $headers
*/
private array $headers = [];

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

// generate headers array:
$headers = [];
foreach ($lines as $line) {
$line = chop($line);
if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
$headers[ strtolower($matches[1])] = $matches[2];
$this->headers[ strtolower($matches[1])] = $matches[2];
}
}

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

// check origin:
if ($this->server->getCheckOrigin() === true) {
$origin = (isset($headers['sec-websocket-origin'])) ? $headers['sec-websocket-origin'] : '';
$origin = (isset($headers['origin'])) ? $headers['origin'] : $origin;
$origin = (isset($this->headers['sec-websocket-origin'])) ? $this->headers['sec-websocket-origin'] : '';
$origin = (isset($this->headers['origin'])) ? $this->headers['origin'] : $origin;
if (empty($origin)) {
$this->log('No origin provided.');
$this->sendHttpResponse(401);
Expand All @@ -143,13 +147,13 @@ private function handshake(string $data): bool
}

// do handyshake: (hybi-10)
$secKey = $headers['sec-websocket-key'];
$secKey = $this->headers['sec-websocket-key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$response = "HTTP/1.1 101 Switching Protocols\r\n";
$response .= "Upgrade: websocket\r\n";
$response .= "Connection: Upgrade\r\n";
$response .= "Sec-WebSocket-Accept: " . $secAccept . "\r\n";
if (isset($headers['sec-websocket-protocol']) && !empty($headers['sec-websocket-protocol'])) {
if (isset($this->headers['sec-websocket-protocol']) && !empty($this->headers['sec-websocket-protocol'])) {
$response .= "Sec-WebSocket-Protocol: " . substr($path, 1) . "\r\n";
}
$response .= "\r\n";
Expand Down Expand Up @@ -588,6 +592,15 @@ public function getClientSocket()
{
return $this->socket;
}

/**
* Return the headers of the connection
* @return array
*/
public function getClientHeaders(): array
{
return $this->headers;
}

/**
* Returns the application the client is connected to.
Expand Down
2 changes: 1 addition & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function __construct(
$this->host = $host;
$this->port = $port;
$this->ipcSocketPath = $ipcSocketPath;
$this->timers = new TimerCollection();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line moved? Seems to have nothing to do with the headers...

Copy link
Contributor Author

@haugli92 haugli92 Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this fix to my fork, to make timers work. Ref #40.
So this change will not make any difference.

}

/**
Expand All @@ -137,7 +138,6 @@ public function run(): void
ob_implicit_flush();
$this->createSocket($this->host, $this->port);
$this->openIPCSocket($this->ipcSocketPath);
$this->timers = new TimerCollection();
$this->log('Server created');

while (true) {
Expand Down