Skip to content
Open
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
46 changes: 46 additions & 0 deletions ext/mysqli/tests/mysqli_compression_packet_sizes.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Compressed protocol: packets on both sides of the compression threshold
--EXTENSIONS--
mysqli
--SKIPIF--
<?php
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php
require_once 'connect.inc';

$link = mysqli_init();
if (!my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, MYSQLI_CLIENT_COMPRESS)) {
printf("[001] Cannot connect: [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
}
var_dump($link->query("SHOW SESSION STATUS LIKE 'Compression'")->fetch_row()[1]);

foreach ([1, 230, 235, 236, 237, 238, 240, 600, 4000, 100000, 3000000] as $length) {
$value = substr(str_repeat('compressed protocol packet ', intdiv($length, 27) + 1), 0, $length);
$row = $link->query("SELECT '$value' AS v, LENGTH('$value') AS l")->fetch_assoc();
if ($row['v'] !== $value || (int) $row['l'] !== $length) {
printf("[002] Round trip failed for %d bytes\n", $length);
}
}

$stmt = $link->prepare("SELECT HEX(?)");
for ($i = 0; $i < 200; $i++) {
$value = random_bytes(1 + $i * 37);
$stmt->bind_param('s', $value);
$stmt->execute();
$stmt->bind_result($hex);
$stmt->fetch();
$stmt->free_result();
if ($hex !== strtoupper(bin2hex($value))) {
printf("[003] Round trip failed for %d bytes\n", strlen($value));
}
}
$stmt->close();

$link->close();
print "done!";
?>
--EXPECT--
string(2) "ON"
done!
3 changes: 2 additions & 1 deletion ext/mysqlnd/mysqlnd_protocol_frame_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ static ssize_t write_compressed_packet(
/* here we need to compress the data and then write it, first comes the compressed header */
size_t tmp_complen = to_be_sent;
size_t payload_size;
if (PASS == pfc->data->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), &tmp_complen,
if (to_be_sent >= MYSQLND_MIN_COMPRESS_LENGTH
&& PASS == pfc->data->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), &tmp_complen,
uncompressed_payload, to_be_sent))
{
int3store(compress_buf + MYSQLND_HEADER_SIZE, to_be_sent);
Expand Down
2 changes: 2 additions & 0 deletions ext/mysqlnd/mysqlnd_wireprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#define MYSQLND_HEADER_SIZE 4
#define COMPRESSED_HEADER_SIZE 3
/* Packets smaller than this are sent uncompressed: they barely shrink. */
#define MYSQLND_MIN_COMPRESS_LENGTH 512

#define MYSQLND_NULL_LENGTH (zend_ulong) ~0

Expand Down
Loading