/** * Check the integrity of the package. * * @param string $buffer * @param TcpConnection $connection * @return int */ public static function input($buffer, TcpConnection $connection) { // Judge whether the package length exceeds the limit. if (strlen($buffer) >= TcpConnection::$maxPackageSize) { $connection->close(); return 0; } // Find the position of "\n". $pos = strpos($buffer, "\n"); // No "\n", packet length is unknown, continue to wait for the data so return 0. if ($pos === false) { return 0; } // Return the current package length. return $pos + 1; }
/** * Check the integrity of the package. * * @param string $recv_buffer * @param TcpConnection $connection * @return int */ public static function input($recv_buffer, TcpConnection $connection) { if (!strpos($recv_buffer, "\r\n\r\n")) { // Judge whether the package length exceeds the limit. if (strlen($recv_buffer) >= TcpConnection::$maxPackageSize) { $connection->close(); return 0; } return 0; } list($header, ) = explode("\r\n\r\n", $recv_buffer, 2); if (0 === strpos($recv_buffer, "POST")) { // find Content-Length $match = array(); if (preg_match("/\r\nContent-Length: ?(\\d+)/i", $header, $match)) { $content_length = $match[1]; return $content_length + strlen($header) + 4; } else { return 0; } } else { return strlen($header) + 4; } }