diff --git a/application/core/Files/AsynchronousFileReader.php b/application/core/Files/AsynchronousFileReader.php index ef6380ff..a28079a0 100644 --- a/application/core/Files/AsynchronousFileReader.php +++ b/application/core/Files/AsynchronousFileReader.php @@ -40,71 +40,72 @@ class AsynchronousFileReader { public function appendData() { foreach($this->sockets as $key => &$socket) { /** @var SocketStructure $socket */ - $socket->streamBuffer .= fread($socket->socket, 4096); + do { + $line = fgets($socket->socket, 4096); + if (empty($socket->header) && $line == "\r\n") { + $socket->header = $this->parseHeader($socket->streamBuffer); + $socket->streamBuffer = ""; + $line = ""; + } + $socket->streamBuffer .= $line; - - //$socket->streamBuffer .= fgets($socket->socket, 4096); - //var_dump($socket->streamBuffer); - /* $meta = stream_get_meta_data($socket->socket); - while($meta["unread_bytes"] > 0){ - var_dump("test"); - $socket->streamBuffer .= fgets($socket->socket, 4096); + if (feof($socket->socket) || isset($socket->header["content-length"]) && strlen($socket->streamBuffer) >= $socket->header["content-length"]) { + //TODO special handling for chunked... + fclose($socket->socket); + unset($this->sockets[$key]); + $this->handleContent($socket); + continue 2; + }else{ $meta = stream_get_meta_data($socket->socket); - var_dump($meta); } - - var_dump($meta); - exit();*/ - if (feof($socket->socket) || time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) { - fclose($socket->socket); - unset($this->sockets[$key]); - $result = ""; - $error = 0; - if (time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) { - $error = self::TIMEOUT_ERROR; - } else if (substr($socket->streamBuffer, 9, 3) != "200") { - $error = self::RESPONSE_ERROR; - $result = $this->parseResult($socket->streamBuffer); - - } else if ($socket->streamBuffer == '') { - $error = self::NO_DATA_ERROR; - } else { - $result = $this->parseResult($socket->streamBuffer); - if ($result == self::INVALID_RESULT_ERROR) { - $error = self::INVALID_RESULT_ERROR; - } - } - - call_user_func($socket->function, $result, $error); - } + } while($meta["unread_bytes"] > 0); } } + /** + * Handles the Content + * @param $socket + */ + private function handleContent(SocketStructure $socket){ //TODO timeout handling + //if (feof($socket->socket) || time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) { + $result = ""; + $error = 0; + /*if (time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) { + $error = self::TIMEOUT_ERROR; + } else*/ + if ($socket->header["status"] != "200") { + $error = self::RESPONSE_ERROR; + $result = $this->parseResult2($socket); + + } else if ($socket->streamBuffer == '') { + $error = self::NO_DATA_ERROR; + } else { + $result = $this->parseResult2($socket); + if ($result == self::INVALID_RESULT_ERROR) { + $error = self::INVALID_RESULT_ERROR; + } + } + //var_dump($result); + call_user_func($socket->function, $result, $error); + //} + } + /** * Parse the Stream Result * - * @param $streamBuffer + * @param SocketStructure $socket + * @internal param $streamBuffer * @return string */ - private function parseResult($streamBuffer) { - $resultArray = explode("\r\n\r\n", $streamBuffer, 2); + private function parseResult2(SocketStructure $socket) { - switch(count($resultArray)) { - case 0: - return self::INVALID_RESULT_ERROR; - case 1: - return ''; - } - - $header = $this->parseHeader($resultArray[0]); - - if (isset($header["transfer-encoding"])) { - $result = $this->decode_chunked($resultArray[1]); + if (isset($socket->header["transfer-encoding"])) { + $result = $this->decode_chunked($socket->streamBuffer); } else { - $result = $resultArray[1]; + $result = $socket->streamBuffer; } - return $this->decompressData($header, $result); + return $this->decompressData($socket->header, $result); } /** @@ -160,6 +161,9 @@ class AsynchronousFileReader { } foreach($headers as $v) { + if ($v == "") { + break; + } $h = preg_split('/:\s*/', $v); $output[strtolower($h[0])] = $h[1]; } diff --git a/application/core/Files/SocketStructure.php b/application/core/Files/SocketStructure.php index 3cf30799..49a43d15 100644 --- a/application/core/Files/SocketStructure.php +++ b/application/core/Files/SocketStructure.php @@ -15,6 +15,7 @@ class SocketStructure { public $function; public $url; public $creationTime; + public $header; public function __construct($url, $socket, $function) { $this->url = $url; @@ -22,5 +23,6 @@ class SocketStructure { $this->function = $function; $this->creationTime = time(); $this->streamBuffer = ''; + $this->header = array(); } } \ No newline at end of file diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 506fd2fc..82c4fd37 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -12,6 +12,7 @@ use ManiaControl\ManiaExchange\ManiaExchangeList; use ManiaControl\ManiaExchange\ManiaExchangeManager; use ManiaControl\ManiaExchange\MXMapInfo; use ManiaControl\Players\Player; +use Maniaplanet\DedicatedServer\InvalidArgumentException; use Maniaplanet\DedicatedServer\Xmlrpc\Exception; /** @@ -561,8 +562,8 @@ class MapManager implements CallbackListener { } else { try { $this->maniaControl->client->writeFileFromString($mapFileName, $file); - } catch(Exception $e) { - if ($e->getMessage() == 'transport error - request too large!') { + } catch(InvalidArgumentException $e) { + if ($e->getMessage() == 'data are too big') { $this->maniaControl->chat->sendError("Map is too big for a remote save.", $login); return; }