@@ -4,17 +4,17 @@ |
||
| 4 | 4 | $SocketClient = new \EasyHttp\WebSocket(); |
| 5 | 5 | |
| 6 | 6 | $SocketClient->onOpen = function (\EasyHttp\WebSocket $socket) { |
| 7 | - echo "Connected to server<br>"; |
|
| 8 | - $socket->send("Hello World"); |
|
| 7 | + echo "Connected to server<br>"; |
|
| 8 | + $socket->send("Hello World"); |
|
| 9 | 9 | }; |
| 10 | 10 | |
| 11 | 11 | $SocketClient->onMessage = function (\EasyHttp\WebSocket $socket, $message) { |
| 12 | - echo $message . "<br>"; |
|
| 13 | - $socket->close(); |
|
| 12 | + echo $message . "<br>"; |
|
| 13 | + $socket->close(); |
|
| 14 | 14 | }; |
| 15 | 15 | |
| 16 | 16 | $SocketClient->onClose = function (\EasyHttp\WebSocket $socket, int $closeStatus) { |
| 17 | - echo "Disconnected with status: $closeStatus<br>"; |
|
| 17 | + echo "Disconnected with status: $closeStatus<br>"; |
|
| 18 | 18 | }; |
| 19 | 19 | |
| 20 | 20 | $SocketClient->connect('wss://socket.litehex.com/', new \EasyHttp\WebSocketConfig()); |
| 21 | 21 | \ No newline at end of file |
@@ -12,58 +12,58 @@ |
||
| 12 | 12 | interface CommonsContract |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - // DADA types |
|
| 16 | - public const EVENT_TYPE_PING = 'ping'; |
|
| 17 | - public const EVENT_TYPE_PONG = 'pong'; |
|
| 18 | - public const EVENT_TYPE_TEXT = 'text'; |
|
| 19 | - public const EVENT_TYPE_CLOSE = 'close'; |
|
| 20 | - public const EVENT_TYPE_BINARY = 'binary'; |
|
| 21 | - public const EVENT_TYPE_CONTINUATION = 'continuation'; |
|
| 15 | + // DADA types |
|
| 16 | + public const EVENT_TYPE_PING = 'ping'; |
|
| 17 | + public const EVENT_TYPE_PONG = 'pong'; |
|
| 18 | + public const EVENT_TYPE_TEXT = 'text'; |
|
| 19 | + public const EVENT_TYPE_CLOSE = 'close'; |
|
| 20 | + public const EVENT_TYPE_BINARY = 'binary'; |
|
| 21 | + public const EVENT_TYPE_CONTINUATION = 'continuation'; |
|
| 22 | 22 | |
| 23 | - public const MAP_EVENT_TYPE_TO_METHODS = [ |
|
| 24 | - self::EVENT_TYPE_TEXT => 'onMessage', |
|
| 25 | - self::EVENT_TYPE_PING => 'onPing', |
|
| 26 | - self::EVENT_TYPE_PONG => 'onPong', |
|
| 27 | - ]; |
|
| 23 | + public const MAP_EVENT_TYPE_TO_METHODS = [ |
|
| 24 | + self::EVENT_TYPE_TEXT => 'onMessage', |
|
| 25 | + self::EVENT_TYPE_PING => 'onPing', |
|
| 26 | + self::EVENT_TYPE_PONG => 'onPong', |
|
| 27 | + ]; |
|
| 28 | 28 | |
| 29 | - // DECODE FRAMES |
|
| 30 | - public const DECODE_TEXT = 1; |
|
| 31 | - public const DECODE_BINARY = 2; |
|
| 32 | - public const DECODE_CLOSE = 8; |
|
| 33 | - public const DECODE_PING = 9; |
|
| 34 | - public const DECODE_PONG = 10; |
|
| 29 | + // DECODE FRAMES |
|
| 30 | + public const DECODE_TEXT = 1; |
|
| 31 | + public const DECODE_BINARY = 2; |
|
| 32 | + public const DECODE_CLOSE = 8; |
|
| 33 | + public const DECODE_PING = 9; |
|
| 34 | + public const DECODE_PONG = 10; |
|
| 35 | 35 | |
| 36 | - // ENCODE FRAMES |
|
| 37 | - public const ENCODE_TEXT = 129; |
|
| 38 | - public const ENCODE_CLOSE = 136; |
|
| 39 | - public const ENCODE_PING = 137; |
|
| 40 | - public const ENCODE_PONG = 138; |
|
| 36 | + // ENCODE FRAMES |
|
| 37 | + public const ENCODE_TEXT = 129; |
|
| 38 | + public const ENCODE_CLOSE = 136; |
|
| 39 | + public const ENCODE_PING = 137; |
|
| 40 | + public const ENCODE_PONG = 138; |
|
| 41 | 41 | |
| 42 | - // MASKS |
|
| 43 | - public const MASK_125 = 125; |
|
| 44 | - public const MASK_126 = 126; |
|
| 45 | - public const MASK_127 = 127; |
|
| 46 | - public const MASK_128 = 128; |
|
| 47 | - public const MASK_254 = 254; |
|
| 48 | - public const MASK_255 = 255; |
|
| 42 | + // MASKS |
|
| 43 | + public const MASK_125 = 125; |
|
| 44 | + public const MASK_126 = 126; |
|
| 45 | + public const MASK_127 = 127; |
|
| 46 | + public const MASK_128 = 128; |
|
| 47 | + public const MASK_254 = 254; |
|
| 48 | + public const MASK_255 = 255; |
|
| 49 | 49 | |
| 50 | - // PAYLOADS |
|
| 51 | - public const PAYLOAD_CHUNK = 8; |
|
| 52 | - public const PAYLOAD_MAX_BITS = 65535; |
|
| 50 | + // PAYLOADS |
|
| 51 | + public const PAYLOAD_CHUNK = 8; |
|
| 52 | + public const PAYLOAD_MAX_BITS = 65535; |
|
| 53 | 53 | |
| 54 | - // transfer protocol-level errors |
|
| 55 | - public const SERVER_COULD_NOT_BIND_TO_SOCKET = 101; |
|
| 56 | - public const SERVER_SELECT_ERROR = 102; |
|
| 57 | - public const SERVER_HEADERS_NOT_SET = 103; |
|
| 58 | - public const CLIENT_COULD_NOT_OPEN_SOCKET = 104; |
|
| 59 | - public const CLIENT_INCORRECT_SCHEME = 105; |
|
| 60 | - public const CLIENT_INVALID_UPGRADE_RESPONSE = 106; |
|
| 61 | - public const CLIENT_INVALID_STREAM_CONTEXT = 107; |
|
| 62 | - public const CLIENT_BAD_OPCODE = 108; |
|
| 63 | - public const CLIENT_COULD_ONLY_WRITE_LESS = 109; |
|
| 64 | - public const CLIENT_BROKEN_FRAME = 110; |
|
| 65 | - public const CLIENT_EMPTY_READ = 111; |
|
| 66 | - public const SERVER_INVALID_STREAM_CONTEXT = 112; |
|
| 67 | - public const CLIENT_CONNECTION_NOT_ESTABLISHED = 113; |
|
| 54 | + // transfer protocol-level errors |
|
| 55 | + public const SERVER_COULD_NOT_BIND_TO_SOCKET = 101; |
|
| 56 | + public const SERVER_SELECT_ERROR = 102; |
|
| 57 | + public const SERVER_HEADERS_NOT_SET = 103; |
|
| 58 | + public const CLIENT_COULD_NOT_OPEN_SOCKET = 104; |
|
| 59 | + public const CLIENT_INCORRECT_SCHEME = 105; |
|
| 60 | + public const CLIENT_INVALID_UPGRADE_RESPONSE = 106; |
|
| 61 | + public const CLIENT_INVALID_STREAM_CONTEXT = 107; |
|
| 62 | + public const CLIENT_BAD_OPCODE = 108; |
|
| 63 | + public const CLIENT_COULD_ONLY_WRITE_LESS = 109; |
|
| 64 | + public const CLIENT_BROKEN_FRAME = 110; |
|
| 65 | + public const CLIENT_EMPTY_READ = 111; |
|
| 66 | + public const SERVER_INVALID_STREAM_CONTEXT = 112; |
|
| 67 | + public const CLIENT_CONNECTION_NOT_ESTABLISHED = 113; |
|
| 68 | 68 | |
| 69 | 69 | } |
| 70 | 70 | \ No newline at end of file |
@@ -12,25 +12,25 @@ |
||
| 12 | 12 | interface WscCommonsContract |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - public const TCP_SCHEME = 'tcp://'; |
|
| 15 | + public const TCP_SCHEME = 'tcp://'; |
|
| 16 | 16 | |
| 17 | - public const MAX_BYTES_READ = 65535; |
|
| 18 | - public const DEFAULT_TIMEOUT = 5; |
|
| 19 | - public const DEFAULT_FRAGMENT_SIZE = 4096; |
|
| 20 | - public const DEFAULT_RESPONSE_HEADER = 8192; |
|
| 21 | - public const SEC_WEBSOCKET_ACCEPT_PTTRN = '/Sec-WebSocket-Accept:\s(.*)$/mUi'; |
|
| 22 | - public const SERVER_KEY_ACCEPT = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; |
|
| 23 | - public const PROXY_MATCH_RESP = '/^HTTP\/\d\.\d 200/'; |
|
| 17 | + public const MAX_BYTES_READ = 65535; |
|
| 18 | + public const DEFAULT_TIMEOUT = 5; |
|
| 19 | + public const DEFAULT_FRAGMENT_SIZE = 4096; |
|
| 20 | + public const DEFAULT_RESPONSE_HEADER = 8192; |
|
| 21 | + public const SEC_WEBSOCKET_ACCEPT_PTTRN = '/Sec-WebSocket-Accept:\s(.*)$/mUi'; |
|
| 22 | + public const SERVER_KEY_ACCEPT = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; |
|
| 23 | + public const PROXY_MATCH_RESP = '/^HTTP\/\d\.\d 200/'; |
|
| 24 | 24 | |
| 25 | - // MASKS |
|
| 26 | - public const MASK_125 = 125; |
|
| 27 | - public const MASK_126 = 126; |
|
| 28 | - public const MASK_127 = 127; |
|
| 29 | - public const MASK_128 = 128; |
|
| 30 | - public const MASK_254 = 254; |
|
| 31 | - public const MASK_255 = 255; |
|
| 25 | + // MASKS |
|
| 26 | + public const MASK_125 = 125; |
|
| 27 | + public const MASK_126 = 126; |
|
| 28 | + public const MASK_127 = 127; |
|
| 29 | + public const MASK_128 = 128; |
|
| 30 | + public const MASK_254 = 254; |
|
| 31 | + public const MASK_255 = 255; |
|
| 32 | 32 | |
| 33 | - public const KEY_GEN_LENGTH = 16; |
|
| 34 | - public const DEFAULT_PING_INTERVAL = 5; |
|
| 33 | + public const KEY_GEN_LENGTH = 16; |
|
| 34 | + public const DEFAULT_PING_INTERVAL = 5; |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -15,11 +15,11 @@ |
||
| 15 | 15 | interface MessageContract |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @param WebSocket $socket |
|
| 20 | - * @param string $message |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function onMessage(WebSocket $socket, string $message): void; |
|
| 18 | + /** |
|
| 19 | + * @param WebSocket $socket |
|
| 20 | + * @param string $message |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function onMessage(WebSocket $socket, string $message): void; |
|
| 24 | 24 | |
| 25 | 25 | } |
@@ -15,24 +15,24 @@ |
||
| 15 | 15 | interface WebSocketContract |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @param WebSocket $socket |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 22 | - public function onOpen(WebSocket $socket): void; |
|
| 18 | + /** |
|
| 19 | + * @param WebSocket $socket |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | + public function onOpen(WebSocket $socket): void; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param WebSocket $socket |
|
| 26 | - * @param int $closeStatus |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public function onClose(WebSocket $socket, int $closeStatus): void; |
|
| 24 | + /** |
|
| 25 | + * @param WebSocket $socket |
|
| 26 | + * @param int $closeStatus |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public function onClose(WebSocket $socket, int $closeStatus): void; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param WebSocket $socket |
|
| 33 | - * @param WebSocketException $exception |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - public function onError(WebSocket $socket, WebSocketException $exception): void; |
|
| 31 | + /** |
|
| 32 | + * @param WebSocket $socket |
|
| 33 | + * @param WebSocketException $exception |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + public function onError(WebSocket $socket, WebSocketException $exception): void; |
|
| 37 | 37 | |
| 38 | 38 | } |
@@ -14,336 +14,336 @@ |
||
| 14 | 14 | class WebSocketConfig |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - private string $scheme; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - private string $host; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - private string $user; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - private string $password; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - private string $port; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private int $timeout = WscCommonsContract::DEFAULT_TIMEOUT; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - private array $headers = []; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var int |
|
| 54 | - */ |
|
| 55 | - private int $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var null|resource |
|
| 59 | - */ |
|
| 60 | - private $context; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var bool |
|
| 64 | - */ |
|
| 65 | - private bool $hasProxy = false; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - private string $proxyIp; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - private string $proxyPort; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @var string|null |
|
| 79 | - */ |
|
| 80 | - private ?string $proxyAuth; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 85 | - private array $contextOptions = []; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @var int |
|
| 89 | - */ |
|
| 90 | - private int $pingInterval = WscCommonsContract::DEFAULT_PING_INTERVAL; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return int |
|
| 94 | - */ |
|
| 95 | - public function getTimeout(): int |
|
| 96 | - { |
|
| 97 | - return $this->timeout; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param int $timeout |
|
| 102 | - * @return WebSocketConfig |
|
| 103 | - */ |
|
| 104 | - public function setTimeout(int $timeout): WebSocketConfig |
|
| 105 | - { |
|
| 106 | - $this->timeout = $timeout; |
|
| 107 | - return $this; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param int $int |
|
| 112 | - * @return $this |
|
| 113 | - */ |
|
| 114 | - public function setPingInterval(int $int): WebSocketConfig |
|
| 115 | - { |
|
| 116 | - $this->pingInterval = $int; |
|
| 117 | - return $this; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return int |
|
| 122 | - */ |
|
| 123 | - public function getPingInterval(): int |
|
| 124 | - { |
|
| 125 | - return $this->pingInterval; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @return array |
|
| 130 | - */ |
|
| 131 | - public function getHeaders(): array |
|
| 132 | - { |
|
| 133 | - return $this->headers; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @param array $headers |
|
| 138 | - * @return WebSocketConfig |
|
| 139 | - */ |
|
| 140 | - public function setHeaders(array $headers): WebSocketConfig |
|
| 141 | - { |
|
| 142 | - $this->headers = $headers; |
|
| 143 | - return $this; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @return int |
|
| 148 | - */ |
|
| 149 | - public function getFragmentSize(): int |
|
| 150 | - { |
|
| 151 | - return $this->fragmentSize; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @param int $fragmentSize |
|
| 156 | - * @return WebSocketConfig |
|
| 157 | - */ |
|
| 158 | - public function setFragmentSize(int $fragmentSize): WebSocketConfig |
|
| 159 | - { |
|
| 160 | - $this->fragmentSize = $fragmentSize; |
|
| 161 | - return $this; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @return mixed |
|
| 166 | - */ |
|
| 167 | - public function getContext(): mixed |
|
| 168 | - { |
|
| 169 | - return $this->context; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @param mixed $context |
|
| 174 | - * @return WebSocketConfig |
|
| 175 | - */ |
|
| 176 | - public function setContext(mixed $context): WebSocketConfig |
|
| 177 | - { |
|
| 178 | - $this->context = $context; |
|
| 179 | - return $this; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @return mixed |
|
| 184 | - */ |
|
| 185 | - public function getScheme(): string |
|
| 186 | - { |
|
| 187 | - return $this->scheme; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @param string $scheme |
|
| 192 | - * @return WebSocketConfig |
|
| 193 | - */ |
|
| 194 | - public function setScheme(string $scheme): WebSocketConfig |
|
| 195 | - { |
|
| 196 | - $this->scheme = $scheme; |
|
| 197 | - return $this; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @return string |
|
| 202 | - */ |
|
| 203 | - public function getHost(): string |
|
| 204 | - { |
|
| 205 | - return $this->host; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @param string $host |
|
| 210 | - * @return WebSocketConfig |
|
| 211 | - */ |
|
| 212 | - public function setHost(string $host): WebSocketConfig |
|
| 213 | - { |
|
| 214 | - $this->host = $host; |
|
| 215 | - return $this; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @return string |
|
| 220 | - */ |
|
| 221 | - public function getUser(): string |
|
| 222 | - { |
|
| 223 | - return $this->user; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param array $urlParts |
|
| 228 | - * @return WebSocketConfig |
|
| 229 | - */ |
|
| 230 | - public function setUser(array $urlParts): WebSocketConfig |
|
| 231 | - { |
|
| 232 | - $this->user = $urlParts['user'] ?? ''; |
|
| 233 | - return $this; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @return string |
|
| 238 | - */ |
|
| 239 | - public function getPassword(): string |
|
| 240 | - { |
|
| 241 | - return $this->password; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * @param array $urlParts |
|
| 246 | - * @return WebSocketConfig |
|
| 247 | - */ |
|
| 248 | - public function setPassword(array $urlParts): WebSocketConfig |
|
| 249 | - { |
|
| 250 | - $this->password = $urlParts['pass'] ?? ''; |
|
| 251 | - return $this; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * @return string |
|
| 256 | - */ |
|
| 257 | - public function getPort(): string |
|
| 258 | - { |
|
| 259 | - return $this->port; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * @param array $urlParts |
|
| 264 | - * @return WebSocketConfig |
|
| 265 | - */ |
|
| 266 | - public function setPort(array $urlParts): WebSocketConfig |
|
| 267 | - { |
|
| 268 | - $this->port = $urlParts['port'] ?? ($this->scheme === 'wss' ? '443' : '80'); |
|
| 269 | - return $this; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * @return array |
|
| 274 | - */ |
|
| 275 | - public function getContextOptions(): array |
|
| 276 | - { |
|
| 277 | - return $this->contextOptions; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * @param array $contextOptions |
|
| 282 | - * @return WebSocketConfig |
|
| 283 | - */ |
|
| 284 | - public function setContextOptions(array $contextOptions): WebSocketConfig |
|
| 285 | - { |
|
| 286 | - $this->contextOptions = $contextOptions; |
|
| 287 | - return $this; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @param string $ip |
|
| 292 | - * @param string $port |
|
| 293 | - * @return WebSocketConfig |
|
| 294 | - */ |
|
| 295 | - public function setProxy(string $ip, string $port): WebSocketConfig |
|
| 296 | - { |
|
| 297 | - $this->hasProxy = true; |
|
| 298 | - $this->proxyIp = $ip; |
|
| 299 | - $this->proxyPort = $port; |
|
| 300 | - |
|
| 301 | - return $this; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Sets auth for proxy |
|
| 306 | - * |
|
| 307 | - * @param string $userName |
|
| 308 | - * @param string $password |
|
| 309 | - * @return WebSocketConfig |
|
| 310 | - */ |
|
| 311 | - public function setProxyAuth(string $userName, string $password): WebSocketConfig |
|
| 312 | - { |
|
| 313 | - $this->proxyAuth = (empty($userName) === false && empty($password) === false) ? base64_encode($userName . ':' . $password) : null; |
|
| 314 | - return $this; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * @return bool |
|
| 319 | - */ |
|
| 320 | - public function hasProxy(): bool |
|
| 321 | - { |
|
| 322 | - return $this->hasProxy; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * @return string|null |
|
| 327 | - */ |
|
| 328 | - public function getProxyIp(): ?string |
|
| 329 | - { |
|
| 330 | - return $this->proxyIp; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * @return string|null |
|
| 335 | - */ |
|
| 336 | - public function getProxyPort(): ?string |
|
| 337 | - { |
|
| 338 | - return $this->proxyPort; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @return string|null |
|
| 343 | - */ |
|
| 344 | - public function getProxyAuth(): ?string |
|
| 345 | - { |
|
| 346 | - return $this->proxyAuth; |
|
| 347 | - } |
|
| 17 | + /** |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + private string $scheme; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + private string $host; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + private string $user; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + private string $password; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + private string $port; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private int $timeout = WscCommonsContract::DEFAULT_TIMEOUT; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + private array $headers = []; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var int |
|
| 54 | + */ |
|
| 55 | + private int $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var null|resource |
|
| 59 | + */ |
|
| 60 | + private $context; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var bool |
|
| 64 | + */ |
|
| 65 | + private bool $hasProxy = false; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + private string $proxyIp; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + private string $proxyPort; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @var string|null |
|
| 79 | + */ |
|
| 80 | + private ?string $proxyAuth; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | + private array $contextOptions = []; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @var int |
|
| 89 | + */ |
|
| 90 | + private int $pingInterval = WscCommonsContract::DEFAULT_PING_INTERVAL; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return int |
|
| 94 | + */ |
|
| 95 | + public function getTimeout(): int |
|
| 96 | + { |
|
| 97 | + return $this->timeout; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param int $timeout |
|
| 102 | + * @return WebSocketConfig |
|
| 103 | + */ |
|
| 104 | + public function setTimeout(int $timeout): WebSocketConfig |
|
| 105 | + { |
|
| 106 | + $this->timeout = $timeout; |
|
| 107 | + return $this; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param int $int |
|
| 112 | + * @return $this |
|
| 113 | + */ |
|
| 114 | + public function setPingInterval(int $int): WebSocketConfig |
|
| 115 | + { |
|
| 116 | + $this->pingInterval = $int; |
|
| 117 | + return $this; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return int |
|
| 122 | + */ |
|
| 123 | + public function getPingInterval(): int |
|
| 124 | + { |
|
| 125 | + return $this->pingInterval; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @return array |
|
| 130 | + */ |
|
| 131 | + public function getHeaders(): array |
|
| 132 | + { |
|
| 133 | + return $this->headers; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @param array $headers |
|
| 138 | + * @return WebSocketConfig |
|
| 139 | + */ |
|
| 140 | + public function setHeaders(array $headers): WebSocketConfig |
|
| 141 | + { |
|
| 142 | + $this->headers = $headers; |
|
| 143 | + return $this; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @return int |
|
| 148 | + */ |
|
| 149 | + public function getFragmentSize(): int |
|
| 150 | + { |
|
| 151 | + return $this->fragmentSize; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @param int $fragmentSize |
|
| 156 | + * @return WebSocketConfig |
|
| 157 | + */ |
|
| 158 | + public function setFragmentSize(int $fragmentSize): WebSocketConfig |
|
| 159 | + { |
|
| 160 | + $this->fragmentSize = $fragmentSize; |
|
| 161 | + return $this; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @return mixed |
|
| 166 | + */ |
|
| 167 | + public function getContext(): mixed |
|
| 168 | + { |
|
| 169 | + return $this->context; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @param mixed $context |
|
| 174 | + * @return WebSocketConfig |
|
| 175 | + */ |
|
| 176 | + public function setContext(mixed $context): WebSocketConfig |
|
| 177 | + { |
|
| 178 | + $this->context = $context; |
|
| 179 | + return $this; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @return mixed |
|
| 184 | + */ |
|
| 185 | + public function getScheme(): string |
|
| 186 | + { |
|
| 187 | + return $this->scheme; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @param string $scheme |
|
| 192 | + * @return WebSocketConfig |
|
| 193 | + */ |
|
| 194 | + public function setScheme(string $scheme): WebSocketConfig |
|
| 195 | + { |
|
| 196 | + $this->scheme = $scheme; |
|
| 197 | + return $this; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @return string |
|
| 202 | + */ |
|
| 203 | + public function getHost(): string |
|
| 204 | + { |
|
| 205 | + return $this->host; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @param string $host |
|
| 210 | + * @return WebSocketConfig |
|
| 211 | + */ |
|
| 212 | + public function setHost(string $host): WebSocketConfig |
|
| 213 | + { |
|
| 214 | + $this->host = $host; |
|
| 215 | + return $this; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @return string |
|
| 220 | + */ |
|
| 221 | + public function getUser(): string |
|
| 222 | + { |
|
| 223 | + return $this->user; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param array $urlParts |
|
| 228 | + * @return WebSocketConfig |
|
| 229 | + */ |
|
| 230 | + public function setUser(array $urlParts): WebSocketConfig |
|
| 231 | + { |
|
| 232 | + $this->user = $urlParts['user'] ?? ''; |
|
| 233 | + return $this; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @return string |
|
| 238 | + */ |
|
| 239 | + public function getPassword(): string |
|
| 240 | + { |
|
| 241 | + return $this->password; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * @param array $urlParts |
|
| 246 | + * @return WebSocketConfig |
|
| 247 | + */ |
|
| 248 | + public function setPassword(array $urlParts): WebSocketConfig |
|
| 249 | + { |
|
| 250 | + $this->password = $urlParts['pass'] ?? ''; |
|
| 251 | + return $this; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * @return string |
|
| 256 | + */ |
|
| 257 | + public function getPort(): string |
|
| 258 | + { |
|
| 259 | + return $this->port; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * @param array $urlParts |
|
| 264 | + * @return WebSocketConfig |
|
| 265 | + */ |
|
| 266 | + public function setPort(array $urlParts): WebSocketConfig |
|
| 267 | + { |
|
| 268 | + $this->port = $urlParts['port'] ?? ($this->scheme === 'wss' ? '443' : '80'); |
|
| 269 | + return $this; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * @return array |
|
| 274 | + */ |
|
| 275 | + public function getContextOptions(): array |
|
| 276 | + { |
|
| 277 | + return $this->contextOptions; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * @param array $contextOptions |
|
| 282 | + * @return WebSocketConfig |
|
| 283 | + */ |
|
| 284 | + public function setContextOptions(array $contextOptions): WebSocketConfig |
|
| 285 | + { |
|
| 286 | + $this->contextOptions = $contextOptions; |
|
| 287 | + return $this; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @param string $ip |
|
| 292 | + * @param string $port |
|
| 293 | + * @return WebSocketConfig |
|
| 294 | + */ |
|
| 295 | + public function setProxy(string $ip, string $port): WebSocketConfig |
|
| 296 | + { |
|
| 297 | + $this->hasProxy = true; |
|
| 298 | + $this->proxyIp = $ip; |
|
| 299 | + $this->proxyPort = $port; |
|
| 300 | + |
|
| 301 | + return $this; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Sets auth for proxy |
|
| 306 | + * |
|
| 307 | + * @param string $userName |
|
| 308 | + * @param string $password |
|
| 309 | + * @return WebSocketConfig |
|
| 310 | + */ |
|
| 311 | + public function setProxyAuth(string $userName, string $password): WebSocketConfig |
|
| 312 | + { |
|
| 313 | + $this->proxyAuth = (empty($userName) === false && empty($password) === false) ? base64_encode($userName . ':' . $password) : null; |
|
| 314 | + return $this; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * @return bool |
|
| 319 | + */ |
|
| 320 | + public function hasProxy(): bool |
|
| 321 | + { |
|
| 322 | + return $this->hasProxy; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * @return string|null |
|
| 327 | + */ |
|
| 328 | + public function getProxyIp(): ?string |
|
| 329 | + { |
|
| 330 | + return $this->proxyIp; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * @return string|null |
|
| 335 | + */ |
|
| 336 | + public function getProxyPort(): ?string |
|
| 337 | + { |
|
| 338 | + return $this->proxyPort; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @return string|null |
|
| 343 | + */ |
|
| 344 | + public function getProxyAuth(): ?string |
|
| 345 | + { |
|
| 346 | + return $this->proxyAuth; |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | 349 | } |
@@ -17,192 +17,192 @@ |
||
| 17 | 17 | class Middleware |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Create curl handler. |
|
| 22 | - * |
|
| 23 | - * @param ?string $method |
|
| 24 | - * @param string $uri |
|
| 25 | - * @param array|HttpOptions $options |
|
| 26 | - * |
|
| 27 | - * @return \CurlHandle|false |
|
| 28 | - */ |
|
| 29 | - public static function create_curl_handler(?string $method, string $uri, array|HttpOptions $options = []): \CurlHandle|false |
|
| 30 | - { |
|
| 31 | - $handler = curl_init(); |
|
| 32 | - if (is_resource($handler) || !$handler) { |
|
| 33 | - return false; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - if (gettype($options) === 'array') { |
|
| 37 | - $options = new HttpOptions($options); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - if (count($options->getQuery()) > 0) { |
|
| 41 | - if (!str_contains($uri, '?')) { |
|
| 42 | - $uri .= '?'; |
|
| 43 | - } |
|
| 44 | - $uri .= $options->getQueryString(); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - curl_setopt($handler, CURLOPT_URL, $uri); |
|
| 48 | - |
|
| 49 | - self::set_curl_options($method, $handler, $options); |
|
| 50 | - |
|
| 51 | - return $handler; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Setup curl options based on the given method and our options. |
|
| 56 | - * |
|
| 57 | - * @param \CurlHandle $cHandler |
|
| 58 | - * @param ?string $method |
|
| 59 | - * @param HttpOptions $options |
|
| 60 | - * |
|
| 61 | - * @return void |
|
| 62 | - */ |
|
| 63 | - public static function set_curl_options(?string $method, \CurlHandle $cHandler, HttpOptions $options): void |
|
| 64 | - { |
|
| 65 | - curl_setopt($cHandler, CURLOPT_HEADER, true); |
|
| 66 | - curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'GET'); |
|
| 67 | - |
|
| 68 | - # Fetch the header |
|
| 69 | - $fetchedHeaders = []; |
|
| 70 | - foreach ($options->getHeaders() as $header => $value) { |
|
| 71 | - $fetchedHeaders[] = $header . ': ' . $value; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - # Set headers |
|
| 75 | - curl_setopt($cHandler, CURLOPT_HTTPHEADER, $fetchedHeaders ?? []); |
|
| 76 | - |
|
| 77 | - # Add body if we have one. |
|
| 78 | - if ($options->getBody()) { |
|
| 79 | - curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'POST'); |
|
| 80 | - curl_setopt($cHandler, CURLOPT_POSTFIELDS, $options->getBody()); |
|
| 81 | - curl_setopt($cHandler, CURLOPT_POST, true); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - # Check for a proxy |
|
| 85 | - if ($options->getProxy() != null) { |
|
| 86 | - curl_setopt($cHandler, CURLOPT_PROXY, $options->getProxy()->getHost()); |
|
| 87 | - curl_setopt($cHandler, CURLOPT_PROXYUSERPWD, $options->getProxy()->getAuth()); |
|
| 88 | - if ($options->getProxy()->type !== null) { |
|
| 89 | - curl_setopt($cHandler, CURLOPT_PROXYTYPE, $options->getProxy()->type); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - curl_setopt($cHandler, CURLOPT_RETURNTRANSFER, true); |
|
| 94 | - curl_setopt($cHandler, CURLOPT_FOLLOWLOCATION, true); |
|
| 95 | - |
|
| 96 | - # Add and override the custom curl options. |
|
| 97 | - foreach ($options->getCurlOptions() as $option => $value) { |
|
| 98 | - curl_setopt($cHandler, $option, $value); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - # if we have a timeout, set it. |
|
| 102 | - curl_setopt($cHandler, CURLOPT_TIMEOUT, $options->getTimeout()); |
|
| 103 | - |
|
| 104 | - # If self-signed certs are allowed, set it. |
|
| 105 | - if ((bool)getenv('HAS_SELF_SIGNED_CERT') === true) { |
|
| 106 | - curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, false); |
|
| 107 | - curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - (new Middleware())->handle_media($cHandler, $options); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Handle the media |
|
| 115 | - * |
|
| 116 | - * @param \CurlHandle $handler |
|
| 117 | - * @param HttpOptions $options |
|
| 118 | - * @return void |
|
| 119 | - */ |
|
| 120 | - private function handle_media(\CurlHandle $handler, HttpOptions $options): void |
|
| 121 | - { |
|
| 122 | - if (count($options->getMultipart()) > 0) { |
|
| 123 | - curl_setopt($handler, CURLOPT_POST, true); |
|
| 124 | - curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'POST'); |
|
| 125 | - |
|
| 126 | - $form_data = new FormData(); |
|
| 127 | - foreach ($options->getMultipart() as $key => $value) { |
|
| 128 | - $form_data->addFile($key, $value); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $headers = []; |
|
| 132 | - foreach ($options->getHeaders() as $header => $value) { |
|
| 133 | - if (Toolkit::insensitiveString($header, 'content-type')) continue; |
|
| 134 | - $headers[] = $header . ': ' . $value; |
|
| 135 | - } |
|
| 136 | - $headers[] = 'Content-Type: multipart/form-data'; |
|
| 137 | - |
|
| 138 | - curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); |
|
| 139 | - curl_setopt($handler, CURLOPT_POSTFIELDS, $form_data->getFiles()); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param mixed $socket |
|
| 145 | - * @param int $len |
|
| 146 | - * @return string|null |
|
| 147 | - * @throws ConnectionException |
|
| 148 | - */ |
|
| 149 | - public static function stream_read(mixed $socket, int $len): string|null |
|
| 150 | - { |
|
| 151 | - if (!is_resource($socket)) { |
|
| 152 | - throw new ConnectionException(sprintf( |
|
| 153 | - '%s is not a valid resource. Datatype: %s', $socket, gettype($socket) |
|
| 154 | - )); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $data = ''; |
|
| 158 | - while (($dataLen = strlen($data)) < $len) { |
|
| 159 | - $buff = fread($socket, $len - $dataLen); |
|
| 160 | - |
|
| 161 | - if ($buff === false) { |
|
| 162 | - return null; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if ($buff === '') { |
|
| 166 | - $metadata = stream_get_meta_data($socket); |
|
| 167 | - throw new ConnectionException( |
|
| 168 | - sprintf('Empty read; connection dead? Stream state: %s', json_encode($metadata)), |
|
| 169 | - CommonsContract::CLIENT_EMPTY_READ |
|
| 170 | - ); |
|
| 171 | - } |
|
| 172 | - $data .= $buff; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - return $data; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param mixed $socket |
|
| 180 | - * @param string $data |
|
| 181 | - * @return bool |
|
| 182 | - * @throws ConnectionException |
|
| 183 | - */ |
|
| 184 | - public static function stream_write(mixed $socket, string $data): bool |
|
| 185 | - { |
|
| 186 | - if (!is_resource($socket)) { |
|
| 187 | - throw new ConnectionException(sprintf( |
|
| 188 | - '%s is not a valid resource. Datatype: %s', $socket, gettype($socket) |
|
| 189 | - )); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - $written = fwrite($socket, $data); |
|
| 193 | - |
|
| 194 | - if ($written < strlen($data)) { |
|
| 195 | - throw new ConnectionException( |
|
| 196 | - sprintf('Could only write %s out of %s bytes.', $written, strlen($data)), |
|
| 197 | - CommonsContract::CLIENT_COULD_ONLY_WRITE_LESS |
|
| 198 | - ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return true; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Stream connect |
|
| 206 | - */ |
|
| 20 | + /** |
|
| 21 | + * Create curl handler. |
|
| 22 | + * |
|
| 23 | + * @param ?string $method |
|
| 24 | + * @param string $uri |
|
| 25 | + * @param array|HttpOptions $options |
|
| 26 | + * |
|
| 27 | + * @return \CurlHandle|false |
|
| 28 | + */ |
|
| 29 | + public static function create_curl_handler(?string $method, string $uri, array|HttpOptions $options = []): \CurlHandle|false |
|
| 30 | + { |
|
| 31 | + $handler = curl_init(); |
|
| 32 | + if (is_resource($handler) || !$handler) { |
|
| 33 | + return false; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + if (gettype($options) === 'array') { |
|
| 37 | + $options = new HttpOptions($options); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + if (count($options->getQuery()) > 0) { |
|
| 41 | + if (!str_contains($uri, '?')) { |
|
| 42 | + $uri .= '?'; |
|
| 43 | + } |
|
| 44 | + $uri .= $options->getQueryString(); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + curl_setopt($handler, CURLOPT_URL, $uri); |
|
| 48 | + |
|
| 49 | + self::set_curl_options($method, $handler, $options); |
|
| 50 | + |
|
| 51 | + return $handler; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Setup curl options based on the given method and our options. |
|
| 56 | + * |
|
| 57 | + * @param \CurlHandle $cHandler |
|
| 58 | + * @param ?string $method |
|
| 59 | + * @param HttpOptions $options |
|
| 60 | + * |
|
| 61 | + * @return void |
|
| 62 | + */ |
|
| 63 | + public static function set_curl_options(?string $method, \CurlHandle $cHandler, HttpOptions $options): void |
|
| 64 | + { |
|
| 65 | + curl_setopt($cHandler, CURLOPT_HEADER, true); |
|
| 66 | + curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'GET'); |
|
| 67 | + |
|
| 68 | + # Fetch the header |
|
| 69 | + $fetchedHeaders = []; |
|
| 70 | + foreach ($options->getHeaders() as $header => $value) { |
|
| 71 | + $fetchedHeaders[] = $header . ': ' . $value; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + # Set headers |
|
| 75 | + curl_setopt($cHandler, CURLOPT_HTTPHEADER, $fetchedHeaders ?? []); |
|
| 76 | + |
|
| 77 | + # Add body if we have one. |
|
| 78 | + if ($options->getBody()) { |
|
| 79 | + curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'POST'); |
|
| 80 | + curl_setopt($cHandler, CURLOPT_POSTFIELDS, $options->getBody()); |
|
| 81 | + curl_setopt($cHandler, CURLOPT_POST, true); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + # Check for a proxy |
|
| 85 | + if ($options->getProxy() != null) { |
|
| 86 | + curl_setopt($cHandler, CURLOPT_PROXY, $options->getProxy()->getHost()); |
|
| 87 | + curl_setopt($cHandler, CURLOPT_PROXYUSERPWD, $options->getProxy()->getAuth()); |
|
| 88 | + if ($options->getProxy()->type !== null) { |
|
| 89 | + curl_setopt($cHandler, CURLOPT_PROXYTYPE, $options->getProxy()->type); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + curl_setopt($cHandler, CURLOPT_RETURNTRANSFER, true); |
|
| 94 | + curl_setopt($cHandler, CURLOPT_FOLLOWLOCATION, true); |
|
| 95 | + |
|
| 96 | + # Add and override the custom curl options. |
|
| 97 | + foreach ($options->getCurlOptions() as $option => $value) { |
|
| 98 | + curl_setopt($cHandler, $option, $value); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + # if we have a timeout, set it. |
|
| 102 | + curl_setopt($cHandler, CURLOPT_TIMEOUT, $options->getTimeout()); |
|
| 103 | + |
|
| 104 | + # If self-signed certs are allowed, set it. |
|
| 105 | + if ((bool)getenv('HAS_SELF_SIGNED_CERT') === true) { |
|
| 106 | + curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, false); |
|
| 107 | + curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + (new Middleware())->handle_media($cHandler, $options); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Handle the media |
|
| 115 | + * |
|
| 116 | + * @param \CurlHandle $handler |
|
| 117 | + * @param HttpOptions $options |
|
| 118 | + * @return void |
|
| 119 | + */ |
|
| 120 | + private function handle_media(\CurlHandle $handler, HttpOptions $options): void |
|
| 121 | + { |
|
| 122 | + if (count($options->getMultipart()) > 0) { |
|
| 123 | + curl_setopt($handler, CURLOPT_POST, true); |
|
| 124 | + curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'POST'); |
|
| 125 | + |
|
| 126 | + $form_data = new FormData(); |
|
| 127 | + foreach ($options->getMultipart() as $key => $value) { |
|
| 128 | + $form_data->addFile($key, $value); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $headers = []; |
|
| 132 | + foreach ($options->getHeaders() as $header => $value) { |
|
| 133 | + if (Toolkit::insensitiveString($header, 'content-type')) continue; |
|
| 134 | + $headers[] = $header . ': ' . $value; |
|
| 135 | + } |
|
| 136 | + $headers[] = 'Content-Type: multipart/form-data'; |
|
| 137 | + |
|
| 138 | + curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); |
|
| 139 | + curl_setopt($handler, CURLOPT_POSTFIELDS, $form_data->getFiles()); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param mixed $socket |
|
| 145 | + * @param int $len |
|
| 146 | + * @return string|null |
|
| 147 | + * @throws ConnectionException |
|
| 148 | + */ |
|
| 149 | + public static function stream_read(mixed $socket, int $len): string|null |
|
| 150 | + { |
|
| 151 | + if (!is_resource($socket)) { |
|
| 152 | + throw new ConnectionException(sprintf( |
|
| 153 | + '%s is not a valid resource. Datatype: %s', $socket, gettype($socket) |
|
| 154 | + )); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $data = ''; |
|
| 158 | + while (($dataLen = strlen($data)) < $len) { |
|
| 159 | + $buff = fread($socket, $len - $dataLen); |
|
| 160 | + |
|
| 161 | + if ($buff === false) { |
|
| 162 | + return null; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if ($buff === '') { |
|
| 166 | + $metadata = stream_get_meta_data($socket); |
|
| 167 | + throw new ConnectionException( |
|
| 168 | + sprintf('Empty read; connection dead? Stream state: %s', json_encode($metadata)), |
|
| 169 | + CommonsContract::CLIENT_EMPTY_READ |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | + $data .= $buff; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + return $data; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param mixed $socket |
|
| 180 | + * @param string $data |
|
| 181 | + * @return bool |
|
| 182 | + * @throws ConnectionException |
|
| 183 | + */ |
|
| 184 | + public static function stream_write(mixed $socket, string $data): bool |
|
| 185 | + { |
|
| 186 | + if (!is_resource($socket)) { |
|
| 187 | + throw new ConnectionException(sprintf( |
|
| 188 | + '%s is not a valid resource. Datatype: %s', $socket, gettype($socket) |
|
| 189 | + )); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + $written = fwrite($socket, $data); |
|
| 193 | + |
|
| 194 | + if ($written < strlen($data)) { |
|
| 195 | + throw new ConnectionException( |
|
| 196 | + sprintf('Could only write %s out of %s bytes.', $written, strlen($data)), |
|
| 197 | + CommonsContract::CLIENT_COULD_ONLY_WRITE_LESS |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return true; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Stream connect |
|
| 206 | + */ |
|
| 207 | 207 | |
| 208 | 208 | } |
| 209 | 209 | \ No newline at end of file |
@@ -19,271 +19,271 @@ |
||
| 19 | 19 | trait WSClientTrait |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Validates whether server sent valid upgrade response |
|
| 24 | - * |
|
| 25 | - * @param WebSocketConfig $config |
|
| 26 | - * @param string $pathWithQuery |
|
| 27 | - * @param string $key |
|
| 28 | - * @throws ConnectionException |
|
| 29 | - */ |
|
| 30 | - private function validateResponse(WebSocketConfig $config, string $pathWithQuery, string $key): void |
|
| 31 | - { |
|
| 32 | - $response = stream_get_line($this->socket, self::DEFAULT_RESPONSE_HEADER, "\r\n\r\n"); |
|
| 33 | - if (!preg_match(self::SEC_WEBSOCKET_ACCEPT_PTTRN, $response, $matches)) { |
|
| 34 | - $address = $config->getScheme() . '://' . $config->getHost() . ':' . $config->getPort() . $pathWithQuery; |
|
| 35 | - throw new ConnectionException( |
|
| 36 | - "Connection to '{$address}' failed: Server sent invalid upgrade response:\n" |
|
| 37 | - . $response, CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE |
|
| 38 | - ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - $keyAccept = trim($matches[1]); |
|
| 42 | - $expectedResponse = base64_encode(pack('H*', sha1($key . self::SERVER_KEY_ACCEPT))); |
|
| 43 | - if ($keyAccept !== $expectedResponse) { |
|
| 44 | - throw new ConnectionException( |
|
| 45 | - 'Server sent bad upgrade response.', |
|
| 46 | - CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE |
|
| 47 | - ); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Gets host uri based on protocol |
|
| 53 | - * |
|
| 54 | - * @param WebSocketConfig $config |
|
| 55 | - * @return string |
|
| 56 | - * @throws BadUriException |
|
| 57 | - */ |
|
| 58 | - private function getHostUri(WebSocketConfig $config): string |
|
| 59 | - { |
|
| 60 | - if (in_array($config->getScheme(), ['ws', 'wss'], true) === false) { |
|
| 61 | - throw new BadUriException( |
|
| 62 | - "Url should have scheme ws or wss, not '{$config->getScheme()}' from URI '$this->socketUrl' .", |
|
| 63 | - CommonsContract::CLIENT_INCORRECT_SCHEME |
|
| 64 | - ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return ($config->getScheme() === 'wss' ? 'ssl' : 'tcp') . '://' . $config->getHost(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param string $data |
|
| 72 | - * @return float|int |
|
| 73 | - * @throws ConnectionException |
|
| 74 | - */ |
|
| 75 | - private function getPayloadLength(string $data): float|int |
|
| 76 | - { |
|
| 77 | - $payloadLength = (int)ord($data[1]) & self::MASK_127; // Bits 1-7 in byte 1 |
|
| 78 | - if ($payloadLength > self::MASK_125) { |
|
| 79 | - if ($payloadLength === self::MASK_126) { |
|
| 80 | - $data = $this->read(2); // 126: Payload is a 16-bit unsigned int |
|
| 81 | - } else { |
|
| 82 | - $data = $this->read(8); // 127: Payload is a 64-bit unsigned int |
|
| 83 | - } |
|
| 84 | - $payloadLength = bindec(Toolkit::sprintB($data)); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return $payloadLength; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $data |
|
| 92 | - * @param int $payloadLength |
|
| 93 | - * @return string |
|
| 94 | - * @throws ConnectionException |
|
| 95 | - */ |
|
| 96 | - private function getPayloadData(string $data, int $payloadLength): string |
|
| 97 | - { |
|
| 98 | - // Masking? |
|
| 99 | - $mask = (bool)(ord($data[1]) >> 7); // Bit 0 in byte 1 |
|
| 100 | - $payload = ''; |
|
| 101 | - $maskingKey = ''; |
|
| 102 | - |
|
| 103 | - // Get masking key. |
|
| 104 | - if ($mask) { |
|
| 105 | - $maskingKey = $this->read(4); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Get the actual payload, if any (might not be for e.g. close frames. |
|
| 109 | - if ($payloadLength > 0) { |
|
| 110 | - $data = $this->read($payloadLength); |
|
| 111 | - |
|
| 112 | - if ($mask) { |
|
| 113 | - // Unmask payload. |
|
| 114 | - for ($i = 0; $i < $payloadLength; $i++) { |
|
| 115 | - $payload .= ($data[$i] ^ $maskingKey[$i % 4]); |
|
| 116 | - } |
|
| 117 | - } else { |
|
| 118 | - $payload = $data; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $payload; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @return string|null |
|
| 127 | - * @throws \Exception |
|
| 128 | - */ |
|
| 129 | - protected function receiveFragment(): string|null |
|
| 130 | - { |
|
| 131 | - $data = $this->read(2); |
|
| 132 | - if (is_string($data) === false) { |
|
| 133 | - return null; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $final = (bool)(ord($data[0]) & 1 << 7); |
|
| 137 | - |
|
| 138 | - $opcodeInt = ord($data[0]) & 31; |
|
| 139 | - $opcodeInts = array_flip(self::$opcodes); |
|
| 140 | - if (!array_key_exists($opcodeInt, $opcodeInts)) { |
|
| 141 | - throw new ConnectionException( |
|
| 142 | - "Bad opcode in websocket frame: $opcodeInt", |
|
| 143 | - CommonsContract::CLIENT_BAD_OPCODE |
|
| 144 | - ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $opcode = $opcodeInts[$opcodeInt]; |
|
| 148 | - |
|
| 149 | - if ($opcode !== 'continuation') { |
|
| 150 | - $this->lastOpcode = $opcode; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $payloadLength = $this->getPayloadLength($data); |
|
| 154 | - $payload = $this->getPayloadData($data, $payloadLength); |
|
| 155 | - |
|
| 156 | - if ($opcode === CommonsContract::EVENT_TYPE_CLOSE) { |
|
| 157 | - if ($payloadLength >= 2) { |
|
| 158 | - $statusBin = $payload[0] . $payload[1]; |
|
| 159 | - $status = bindec(sprintf('%08b%08b', ord($payload[0]), ord($payload[1]))); |
|
| 160 | - $this->closeStatus = $status; |
|
| 161 | - $payload = substr($payload, 2); |
|
| 162 | - |
|
| 163 | - if (!$this->isClosing) { |
|
| 164 | - $this->send($statusBin . 'Close acknowledged: ' . $status, |
|
| 165 | - CommonsContract::EVENT_TYPE_CLOSE); // Respond. |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - if ($this->isClosing) { |
|
| 170 | - $this->isClosing = false; // A close response, all done. |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - fclose($this->socket); |
|
| 174 | - $this->isConnected = false; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (!$final) { |
|
| 178 | - $this->hugePayload .= $payload; |
|
| 179 | - |
|
| 180 | - return null; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ($this->hugePayload) { |
|
| 184 | - $payload = $this->hugePayload .= $payload; |
|
| 185 | - $this->hugePayload = null; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $payload; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @param $final |
|
| 193 | - * @param $payload |
|
| 194 | - * @param $opcode |
|
| 195 | - * @param $masked |
|
| 196 | - * @throws \Exception |
|
| 197 | - */ |
|
| 198 | - protected function sendFragment($final, $payload, $opcode, $masked): void |
|
| 199 | - { |
|
| 200 | - // Binary string for header. |
|
| 201 | - $frameHeadBin = ''; |
|
| 202 | - // Write FIN, final fragment bit. |
|
| 203 | - $frameHeadBin .= (bool)$final ? '1' : '0'; |
|
| 204 | - // RSV 1, 2, & 3 false and unused. |
|
| 205 | - $frameHeadBin .= '000'; |
|
| 206 | - // Opcode rest of the byte. |
|
| 207 | - $frameHeadBin .= sprintf('%04b', self::$opcodes[$opcode]); |
|
| 208 | - // Use masking? |
|
| 209 | - $frameHeadBin .= $masked ? '1' : '0'; |
|
| 210 | - |
|
| 211 | - // 7 bits of payload length... |
|
| 212 | - $payloadLen = strlen($payload); |
|
| 213 | - if ($payloadLen > self::MAX_BYTES_READ) { |
|
| 214 | - $frameHeadBin .= decbin(self::MASK_127); |
|
| 215 | - $frameHeadBin .= sprintf('%064b', $payloadLen); |
|
| 216 | - } else if ($payloadLen > self::MASK_125) { |
|
| 217 | - $frameHeadBin .= decbin(self::MASK_126); |
|
| 218 | - $frameHeadBin .= sprintf('%016b', $payloadLen); |
|
| 219 | - } else { |
|
| 220 | - $frameHeadBin .= sprintf('%07b', $payloadLen); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $frame = ''; |
|
| 224 | - |
|
| 225 | - // Write frame head to frame. |
|
| 226 | - foreach (str_split($frameHeadBin, 8) as $binstr) { |
|
| 227 | - $frame .= chr(bindec($binstr)); |
|
| 228 | - } |
|
| 229 | - // Handle masking |
|
| 230 | - if ($masked) { |
|
| 231 | - // generate a random mask: |
|
| 232 | - $mask = ''; |
|
| 233 | - for ($i = 0; $i < 4; $i++) { |
|
| 234 | - $mask .= chr(random_int(0, 255)); |
|
| 235 | - } |
|
| 236 | - $frame .= $mask; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // Append payload to frame: |
|
| 240 | - for ($i = 0; $i < $payloadLen; $i++) { |
|
| 241 | - $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $this->write($frame); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Sec-WebSocket-Key generator |
|
| 249 | - * |
|
| 250 | - * @return string the 16 character length key |
|
| 251 | - * @throws \Exception |
|
| 252 | - */ |
|
| 253 | - private function generateKey(): string |
|
| 254 | - { |
|
| 255 | - $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$&/()=[]{}0123456789'; |
|
| 256 | - $key = ''; |
|
| 257 | - $chLen = strlen($chars); |
|
| 258 | - for ($i = 0; $i < self::KEY_GEN_LENGTH; $i++) { |
|
| 259 | - $key .= $chars[random_int(0, $chLen - 1)]; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - return base64_encode($key); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @param int $len |
|
| 267 | - * @return string|null |
|
| 268 | - * @throws ConnectionException |
|
| 269 | - */ |
|
| 270 | - protected function read(int $len): string|null |
|
| 271 | - { |
|
| 272 | - if ($this->socket && $this->isConnected()) { |
|
| 273 | - return Middleware::stream_read($this->socket, $len); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return null; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @param string $data |
|
| 281 | - * @return void |
|
| 282 | - * @throws ConnectionException |
|
| 283 | - */ |
|
| 284 | - protected function write(string $data): void |
|
| 285 | - { |
|
| 286 | - Middleware::stream_write($this->socket, $data); |
|
| 287 | - } |
|
| 22 | + /** |
|
| 23 | + * Validates whether server sent valid upgrade response |
|
| 24 | + * |
|
| 25 | + * @param WebSocketConfig $config |
|
| 26 | + * @param string $pathWithQuery |
|
| 27 | + * @param string $key |
|
| 28 | + * @throws ConnectionException |
|
| 29 | + */ |
|
| 30 | + private function validateResponse(WebSocketConfig $config, string $pathWithQuery, string $key): void |
|
| 31 | + { |
|
| 32 | + $response = stream_get_line($this->socket, self::DEFAULT_RESPONSE_HEADER, "\r\n\r\n"); |
|
| 33 | + if (!preg_match(self::SEC_WEBSOCKET_ACCEPT_PTTRN, $response, $matches)) { |
|
| 34 | + $address = $config->getScheme() . '://' . $config->getHost() . ':' . $config->getPort() . $pathWithQuery; |
|
| 35 | + throw new ConnectionException( |
|
| 36 | + "Connection to '{$address}' failed: Server sent invalid upgrade response:\n" |
|
| 37 | + . $response, CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE |
|
| 38 | + ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + $keyAccept = trim($matches[1]); |
|
| 42 | + $expectedResponse = base64_encode(pack('H*', sha1($key . self::SERVER_KEY_ACCEPT))); |
|
| 43 | + if ($keyAccept !== $expectedResponse) { |
|
| 44 | + throw new ConnectionException( |
|
| 45 | + 'Server sent bad upgrade response.', |
|
| 46 | + CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Gets host uri based on protocol |
|
| 53 | + * |
|
| 54 | + * @param WebSocketConfig $config |
|
| 55 | + * @return string |
|
| 56 | + * @throws BadUriException |
|
| 57 | + */ |
|
| 58 | + private function getHostUri(WebSocketConfig $config): string |
|
| 59 | + { |
|
| 60 | + if (in_array($config->getScheme(), ['ws', 'wss'], true) === false) { |
|
| 61 | + throw new BadUriException( |
|
| 62 | + "Url should have scheme ws or wss, not '{$config->getScheme()}' from URI '$this->socketUrl' .", |
|
| 63 | + CommonsContract::CLIENT_INCORRECT_SCHEME |
|
| 64 | + ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return ($config->getScheme() === 'wss' ? 'ssl' : 'tcp') . '://' . $config->getHost(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param string $data |
|
| 72 | + * @return float|int |
|
| 73 | + * @throws ConnectionException |
|
| 74 | + */ |
|
| 75 | + private function getPayloadLength(string $data): float|int |
|
| 76 | + { |
|
| 77 | + $payloadLength = (int)ord($data[1]) & self::MASK_127; // Bits 1-7 in byte 1 |
|
| 78 | + if ($payloadLength > self::MASK_125) { |
|
| 79 | + if ($payloadLength === self::MASK_126) { |
|
| 80 | + $data = $this->read(2); // 126: Payload is a 16-bit unsigned int |
|
| 81 | + } else { |
|
| 82 | + $data = $this->read(8); // 127: Payload is a 64-bit unsigned int |
|
| 83 | + } |
|
| 84 | + $payloadLength = bindec(Toolkit::sprintB($data)); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return $payloadLength; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $data |
|
| 92 | + * @param int $payloadLength |
|
| 93 | + * @return string |
|
| 94 | + * @throws ConnectionException |
|
| 95 | + */ |
|
| 96 | + private function getPayloadData(string $data, int $payloadLength): string |
|
| 97 | + { |
|
| 98 | + // Masking? |
|
| 99 | + $mask = (bool)(ord($data[1]) >> 7); // Bit 0 in byte 1 |
|
| 100 | + $payload = ''; |
|
| 101 | + $maskingKey = ''; |
|
| 102 | + |
|
| 103 | + // Get masking key. |
|
| 104 | + if ($mask) { |
|
| 105 | + $maskingKey = $this->read(4); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Get the actual payload, if any (might not be for e.g. close frames. |
|
| 109 | + if ($payloadLength > 0) { |
|
| 110 | + $data = $this->read($payloadLength); |
|
| 111 | + |
|
| 112 | + if ($mask) { |
|
| 113 | + // Unmask payload. |
|
| 114 | + for ($i = 0; $i < $payloadLength; $i++) { |
|
| 115 | + $payload .= ($data[$i] ^ $maskingKey[$i % 4]); |
|
| 116 | + } |
|
| 117 | + } else { |
|
| 118 | + $payload = $data; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $payload; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @return string|null |
|
| 127 | + * @throws \Exception |
|
| 128 | + */ |
|
| 129 | + protected function receiveFragment(): string|null |
|
| 130 | + { |
|
| 131 | + $data = $this->read(2); |
|
| 132 | + if (is_string($data) === false) { |
|
| 133 | + return null; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $final = (bool)(ord($data[0]) & 1 << 7); |
|
| 137 | + |
|
| 138 | + $opcodeInt = ord($data[0]) & 31; |
|
| 139 | + $opcodeInts = array_flip(self::$opcodes); |
|
| 140 | + if (!array_key_exists($opcodeInt, $opcodeInts)) { |
|
| 141 | + throw new ConnectionException( |
|
| 142 | + "Bad opcode in websocket frame: $opcodeInt", |
|
| 143 | + CommonsContract::CLIENT_BAD_OPCODE |
|
| 144 | + ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $opcode = $opcodeInts[$opcodeInt]; |
|
| 148 | + |
|
| 149 | + if ($opcode !== 'continuation') { |
|
| 150 | + $this->lastOpcode = $opcode; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $payloadLength = $this->getPayloadLength($data); |
|
| 154 | + $payload = $this->getPayloadData($data, $payloadLength); |
|
| 155 | + |
|
| 156 | + if ($opcode === CommonsContract::EVENT_TYPE_CLOSE) { |
|
| 157 | + if ($payloadLength >= 2) { |
|
| 158 | + $statusBin = $payload[0] . $payload[1]; |
|
| 159 | + $status = bindec(sprintf('%08b%08b', ord($payload[0]), ord($payload[1]))); |
|
| 160 | + $this->closeStatus = $status; |
|
| 161 | + $payload = substr($payload, 2); |
|
| 162 | + |
|
| 163 | + if (!$this->isClosing) { |
|
| 164 | + $this->send($statusBin . 'Close acknowledged: ' . $status, |
|
| 165 | + CommonsContract::EVENT_TYPE_CLOSE); // Respond. |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + if ($this->isClosing) { |
|
| 170 | + $this->isClosing = false; // A close response, all done. |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + fclose($this->socket); |
|
| 174 | + $this->isConnected = false; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (!$final) { |
|
| 178 | + $this->hugePayload .= $payload; |
|
| 179 | + |
|
| 180 | + return null; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ($this->hugePayload) { |
|
| 184 | + $payload = $this->hugePayload .= $payload; |
|
| 185 | + $this->hugePayload = null; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $payload; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @param $final |
|
| 193 | + * @param $payload |
|
| 194 | + * @param $opcode |
|
| 195 | + * @param $masked |
|
| 196 | + * @throws \Exception |
|
| 197 | + */ |
|
| 198 | + protected function sendFragment($final, $payload, $opcode, $masked): void |
|
| 199 | + { |
|
| 200 | + // Binary string for header. |
|
| 201 | + $frameHeadBin = ''; |
|
| 202 | + // Write FIN, final fragment bit. |
|
| 203 | + $frameHeadBin .= (bool)$final ? '1' : '0'; |
|
| 204 | + // RSV 1, 2, & 3 false and unused. |
|
| 205 | + $frameHeadBin .= '000'; |
|
| 206 | + // Opcode rest of the byte. |
|
| 207 | + $frameHeadBin .= sprintf('%04b', self::$opcodes[$opcode]); |
|
| 208 | + // Use masking? |
|
| 209 | + $frameHeadBin .= $masked ? '1' : '0'; |
|
| 210 | + |
|
| 211 | + // 7 bits of payload length... |
|
| 212 | + $payloadLen = strlen($payload); |
|
| 213 | + if ($payloadLen > self::MAX_BYTES_READ) { |
|
| 214 | + $frameHeadBin .= decbin(self::MASK_127); |
|
| 215 | + $frameHeadBin .= sprintf('%064b', $payloadLen); |
|
| 216 | + } else if ($payloadLen > self::MASK_125) { |
|
| 217 | + $frameHeadBin .= decbin(self::MASK_126); |
|
| 218 | + $frameHeadBin .= sprintf('%016b', $payloadLen); |
|
| 219 | + } else { |
|
| 220 | + $frameHeadBin .= sprintf('%07b', $payloadLen); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $frame = ''; |
|
| 224 | + |
|
| 225 | + // Write frame head to frame. |
|
| 226 | + foreach (str_split($frameHeadBin, 8) as $binstr) { |
|
| 227 | + $frame .= chr(bindec($binstr)); |
|
| 228 | + } |
|
| 229 | + // Handle masking |
|
| 230 | + if ($masked) { |
|
| 231 | + // generate a random mask: |
|
| 232 | + $mask = ''; |
|
| 233 | + for ($i = 0; $i < 4; $i++) { |
|
| 234 | + $mask .= chr(random_int(0, 255)); |
|
| 235 | + } |
|
| 236 | + $frame .= $mask; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // Append payload to frame: |
|
| 240 | + for ($i = 0; $i < $payloadLen; $i++) { |
|
| 241 | + $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $this->write($frame); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Sec-WebSocket-Key generator |
|
| 249 | + * |
|
| 250 | + * @return string the 16 character length key |
|
| 251 | + * @throws \Exception |
|
| 252 | + */ |
|
| 253 | + private function generateKey(): string |
|
| 254 | + { |
|
| 255 | + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$&/()=[]{}0123456789'; |
|
| 256 | + $key = ''; |
|
| 257 | + $chLen = strlen($chars); |
|
| 258 | + for ($i = 0; $i < self::KEY_GEN_LENGTH; $i++) { |
|
| 259 | + $key .= $chars[random_int(0, $chLen - 1)]; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + return base64_encode($key); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @param int $len |
|
| 267 | + * @return string|null |
|
| 268 | + * @throws ConnectionException |
|
| 269 | + */ |
|
| 270 | + protected function read(int $len): string|null |
|
| 271 | + { |
|
| 272 | + if ($this->socket && $this->isConnected()) { |
|
| 273 | + return Middleware::stream_read($this->socket, $len); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + return null; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @param string $data |
|
| 281 | + * @return void |
|
| 282 | + * @throws ConnectionException |
|
| 283 | + */ |
|
| 284 | + protected function write(string $data): void |
|
| 285 | + { |
|
| 286 | + Middleware::stream_write($this->socket, $data); |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | 289 | } |
| 290 | 290 | \ No newline at end of file |
@@ -96,42 +96,42 @@ |
||
| 96 | 96 | return (bool)preg_match_all('/' . $value . '/i', $string); |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Millisecond sleep |
|
| 101 | - * |
|
| 102 | - * @param int $milliseconds The milliseconds |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - public static function sleep(int $milliseconds): void |
|
| 106 | - { |
|
| 107 | - usleep($milliseconds * 1000); |
|
| 108 | - } |
|
| 99 | + /** |
|
| 100 | + * Millisecond sleep |
|
| 101 | + * |
|
| 102 | + * @param int $milliseconds The milliseconds |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + public static function sleep(int $milliseconds): void |
|
| 106 | + { |
|
| 107 | + usleep($milliseconds * 1000); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get current time in milliseconds |
|
| 112 | - * |
|
| 113 | - * @return int |
|
| 114 | - */ |
|
| 115 | - public static function time(): int |
|
| 116 | - { |
|
| 117 | - return (int)(microtime(true) * 1000); |
|
| 118 | - } |
|
| 110 | + /** |
|
| 111 | + * Get current time in milliseconds |
|
| 112 | + * |
|
| 113 | + * @return int |
|
| 114 | + */ |
|
| 115 | + public static function time(): int |
|
| 116 | + { |
|
| 117 | + return (int)(microtime(true) * 1000); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Helper to convert a binary to a string of '0' and '1'. |
|
| 122 | - * |
|
| 123 | - * @param string $string |
|
| 124 | - * @return string |
|
| 125 | - */ |
|
| 126 | - public static function sprintB(string $string): string |
|
| 127 | - { |
|
| 128 | - $return = ''; |
|
| 129 | - $strLen = strlen($string); |
|
| 130 | - for ($i = 0; $i < $strLen; $i++) { |
|
| 131 | - $return .= sprintf('%08b', ord($string[$i])); |
|
| 132 | - } |
|
| 120 | + /** |
|
| 121 | + * Helper to convert a binary to a string of '0' and '1'. |
|
| 122 | + * |
|
| 123 | + * @param string $string |
|
| 124 | + * @return string |
|
| 125 | + */ |
|
| 126 | + public static function sprintB(string $string): string |
|
| 127 | + { |
|
| 128 | + $return = ''; |
|
| 129 | + $strLen = strlen($string); |
|
| 130 | + for ($i = 0; $i < $strLen; $i++) { |
|
| 131 | + $return .= sprintf('%08b', ord($string[$i])); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - return $return; |
|
| 135 | - } |
|
| 134 | + return $return; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | } |
| 138 | 138 | \ No newline at end of file |