Skip to content

Commit 8be75a6

Browse files
committed
Fix PHPMem client #70
1 parent 3fa9ec6 commit 8be75a6

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/Dashboards/Memcached/PHPMem.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function set(string $key, mixed $value, int $expiration = 0): bool {
7171
return $this->memcached->set($key, $value, $expiration);
7272
}
7373

74+
$key = preg_replace('/[\s\x00-\x1f]+/', '', $key);
7475
$value = is_scalar($value) ? (string) $value : serialize($value);
7576
$raw = $this->runCommand('set '.$key.' 0 '.$expiration.' '.strlen($value)."\r\n".$value);
7677

@@ -93,6 +94,7 @@ public function get(string $key): string|false {
9394
return is_scalar($value) ? (string) $value : serialize($value);
9495
}
9596

97+
$key = preg_replace('/[\s\x00-\x1f]+/', '', $key);
9698
$raw = $this->runCommand('get '.$key);
9799
$data = explode("\r\n", $raw);
98100

@@ -110,6 +112,8 @@ public function get(string $key): string|false {
110112
* @throws MemcachedException
111113
*/
112114
public function delete(string $key): bool {
115+
$key = preg_replace('/[\s\x00-\x1f]+/', '', $key);
116+
113117
return $this->runCommand('delete '.$key) === 'DELETED';
114118
}
115119

@@ -297,6 +301,8 @@ public function parseLine(string $line): array {
297301
* @throws MemcachedException
298302
*/
299303
public function getKeyMeta(string $key): array {
304+
$key = preg_replace('/[\s\x00-\x1f]+/', '', $key);
305+
300306
if (version_compare($this->version(), '1.5.19', '>=')) {
301307
$raw = $this->runCommand('me '.$key);
302308

@@ -410,16 +416,15 @@ public function disconnect(): void {
410416
* verbosity <level>\r\n
411417
* quit\r\n
412418
*
413-
* Note: \r\n is added automatically to the end,
414-
* and \r\n (as a plain string) is converted to a real end of the line.
419+
* Note: \r\n is added automatically to the end.
415420
*
416421
* @link https://github.com/memcached/memcached/blob/master/doc/protocol.txt
417422
*
418423
* @throws MemcachedException
419424
*/
420425
public function runCommand(string $command): string {
421426
$command_name = strtolower(strtok($command, ' '));
422-
$command = strtr($command, ['\r\n' => "\r\n"])."\r\n";
427+
$command .= "\r\n";
423428
$data = $this->streamConnection($command, $command_name);
424429

425430
return rtrim($data, "\r\n");

tests/Dashboards/MemcachedTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public static function commandDataProvider(): Iterator {
152152
*/
153153
#[DataProvider('commandDataProvider')]
154154
public function testRunCommand(string $expected, string $command): void {
155+
$command = strtr($command, ['\r\n' => "\r\n"]);
155156
$this->assertSame(strtr($expected, ['\r\n' => "\r\n"]), $this->memcached->runCommand($command));
156157
}
157158

0 commit comments

Comments
 (0)