@@ -49,6 +49,11 @@ class Connection
49
49
* @var string $dataBuffer
50
50
*/
51
51
private string $ dataBuffer = '' ;
52
+
53
+ /**
54
+ * @var array $headers
55
+ */
56
+ private array $ headers = [];
52
57
53
58
/**
54
59
* @param Server $server
@@ -104,16 +109,15 @@ private function handshake(string $data): bool
104
109
$ this ->application = $ this ->server ->getApplication ($ applicationKey );
105
110
106
111
// generate headers array:
107
- $ headers = [];
108
112
foreach ($ lines as $ line ) {
109
113
$ line = chop ($ line );
110
114
if (preg_match ('/\A(\S+): (.*)\z/ ' , $ line , $ matches )) {
111
- $ headers [ strtolower ($ matches [1 ])] = $ matches [2 ];
115
+ $ this -> headers [ strtolower ($ matches [1 ])] = $ matches [2 ];
112
116
}
113
117
}
114
118
115
119
// 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 ) {
117
121
$ this ->log ('Unsupported websocket version. ' );
118
122
$ this ->sendHttpResponse (501 );
119
123
stream_socket_shutdown ($ this ->socket , STREAM_SHUT_RDWR );
@@ -123,8 +127,8 @@ private function handshake(string $data): bool
123
127
124
128
// check origin:
125
129
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 ;
128
132
if (empty ($ origin )) {
129
133
$ this ->log ('No origin provided. ' );
130
134
$ this ->sendHttpResponse (401 );
@@ -143,13 +147,13 @@ private function handshake(string $data): bool
143
147
}
144
148
145
149
// do handyshake: (hybi-10)
146
- $ secKey = $ headers ['sec-websocket-key ' ];
150
+ $ secKey = $ this -> headers ['sec-websocket-key ' ];
147
151
$ secAccept = base64_encode (pack ('H* ' , sha1 ($ secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11 ' )));
148
152
$ response = "HTTP/1.1 101 Switching Protocols \r\n" ;
149
153
$ response .= "Upgrade: websocket \r\n" ;
150
154
$ response .= "Connection: Upgrade \r\n" ;
151
155
$ 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 ' ])) {
153
157
$ response .= "Sec-WebSocket-Protocol: " . substr ($ path , 1 ) . "\r\n" ;
154
158
}
155
159
$ response .= "\r\n" ;
@@ -588,6 +592,15 @@ public function getClientSocket()
588
592
{
589
593
return $ this ->socket ;
590
594
}
595
+
596
+ /**
597
+ * Return the headers of the connection
598
+ * @return array
599
+ */
600
+ public function getClientHeaders (): array
601
+ {
602
+ return $ this ->headers ;
603
+ }
591
604
592
605
/**
593
606
* Returns the application the client is connected to.
0 commit comments