-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for switching protocol responses
- Loading branch information
Showing
4 changed files
with
190 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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,111 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client\Connection; | ||
|
||
use Amp\CancellationToken; | ||
use Amp\Promise; | ||
use Amp\Socket\EncryptableSocket; | ||
use Amp\Socket\ResourceSocket; | ||
use Amp\Socket\SocketAddress; | ||
use Amp\Socket\TlsInfo; | ||
use Amp\Success; | ||
|
||
final class UpgradedSocket implements EncryptableSocket | ||
{ | ||
/** @var ResourceSocket */ | ||
private $socket; | ||
|
||
/** @var string|null */ | ||
private $buffer; | ||
|
||
/** | ||
* @param EncryptableSocket $socket | ||
* @param string $buffer Remaining buffer previously read from the socket. | ||
*/ | ||
public function __construct(EncryptableSocket $socket, string $buffer) | ||
{ | ||
$this->socket = $socket; | ||
$this->buffer = $buffer !== '' ? $buffer : null; | ||
} | ||
|
||
public function read(): Promise | ||
{ | ||
if ($this->buffer !== null) { | ||
$buffer = $this->buffer; | ||
$this->buffer = null; | ||
return new Success($buffer); | ||
} | ||
|
||
return $this->socket->read(); | ||
} | ||
|
||
public function close(): void | ||
{ | ||
$this->socket->close(); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->close(); | ||
} | ||
|
||
public function write(string $data): Promise | ||
{ | ||
return $this->socket->write($data); | ||
} | ||
|
||
public function end(string $finalData = ""): Promise | ||
{ | ||
return $this->socket->end($finalData); | ||
} | ||
|
||
public function reference(): void | ||
{ | ||
$this->socket->reference(); | ||
} | ||
|
||
public function unreference(): void | ||
{ | ||
$this->socket->unreference(); | ||
} | ||
|
||
public function isClosed(): bool | ||
{ | ||
return $this->socket->isClosed(); | ||
} | ||
|
||
public function getLocalAddress(): SocketAddress | ||
{ | ||
return $this->socket->getLocalAddress(); | ||
} | ||
|
||
public function getRemoteAddress(): SocketAddress | ||
{ | ||
return $this->socket->getRemoteAddress(); | ||
} | ||
|
||
public function getResource() | ||
{ | ||
return $this->socket->getResource(); | ||
} | ||
|
||
public function setupTls(?CancellationToken $token = null): Promise | ||
{ | ||
return $this->socket->setupTls($token); | ||
} | ||
|
||
public function shutdownTls(?CancellationToken $token = null): Promise | ||
{ | ||
return $this->socket->shutdownTls(); | ||
} | ||
|
||
public function getTlsState(): int | ||
{ | ||
return $this->socket->getTlsState(); | ||
} | ||
|
||
public function getTlsInfo(): ?TlsInfo | ||
{ | ||
return $this->socket->getTlsInfo(); | ||
} | ||
} |
This file contains 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 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