-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add multiplexed RPC stream transport message handling #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| class UserAgent | ||
| { | ||
| /** | ||
| * @var string|null | ||
| * @var null|string | ||
| */ | ||
| protected static $value; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/jet. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/jet | ||
| * @document https://github.com/friendsofhyperf/jet/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Jet\Transporter; | ||
|
|
||
| use Exception; | ||
| use FriendsOfHyperf\Jet\Exception\ConnectionException; | ||
| use FriendsOfHyperf\Jet\Exception\RecvFailedException; | ||
| use RuntimeException; | ||
|
|
||
| class MultiplexRpcTransporter extends StreamSocketTransporter | ||
| { | ||
| public const PING = 'ping'; | ||
|
|
||
| public const PONG = 'pong'; | ||
|
|
||
| public function receive() | ||
| { | ||
| stream_set_blocking($this->client, false); | ||
|
|
||
| while (true) { | ||
| $header = $this->readBytes(4); | ||
|
|
||
| $unpacked = unpack('Nlength', $header); | ||
| $length = $unpacked['length']; | ||
|
|
||
| if ($length < 4) { | ||
| throw new RecvFailedException(sprintf('Invalid package length: %d', $length)); | ||
| } | ||
| $body = $this->readBytes($length); | ||
| if (in_array($body, [self::PING, self::PONG], true)) { | ||
| continue; | ||
| } | ||
|
|
||
| return $header . $body; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| private function readBytes(int $length): string | ||
| { | ||
| $buffer = ''; | ||
|
|
||
| while (strlen($buffer) < $length) { | ||
| $read = [$this->client]; | ||
| $write = null; | ||
| $except = null; | ||
|
|
||
| $selected = stream_select($read, $write, $except, $this->timeout); | ||
| if ($selected === false) { | ||
| throw new RuntimeException('Failed to select stream.'); | ||
| } | ||
|
|
||
| if ($selected === 0) { | ||
| throw new RecvFailedException('Receive timeout.'); | ||
| } | ||
|
|
||
| foreach ($read as $stream) { | ||
| $chunk = fread($stream, $length - strlen($buffer)); | ||
|
|
||
| if ($chunk === false) { | ||
| throw new RecvFailedException('Receive failed.'); | ||
| } | ||
|
|
||
| if ($chunk === '' && feof($stream)) { | ||
| throw new ConnectionException('Connection was closed.'); | ||
| } | ||
|
|
||
| $buffer .= $chunk; | ||
| } | ||
| } | ||
|
|
||
| return $buffer; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a maximum frame-size guard before reading the body.
Line 31 reads a peer-declared
$lengthwith no upper bound. A malicious or buggy peer can advertise a huge size and force excessive memory/read pressure before failure. Please reject frames above a protocol-safe max.Suggested patch
class MultiplexRpcTransporter extends StreamSocketTransporter { public const PING = 'ping'; public const PONG = 'pong'; + private const MAX_FRAME_LENGTH = 8 * 1024 * 1024; // adjust to protocol limit @@ $unpacked = unpack('Nlength', $header); $length = $unpacked['length']; if ($length < 4) { throw new RecvFailedException(sprintf('Invalid package length: %d', $length)); } + if ($length > self::MAX_FRAME_LENGTH) { + throw new RecvFailedException(sprintf('Package too large: %d', $length)); + } $body = $this->readBytes($length);📝 Committable suggestion
🤖 Prompt for AI Agents