-
-
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.
Use internal class for upgraded stream writing
- Loading branch information
Showing
4 changed files
with
70 additions
and
32 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
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,45 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client\Connection\Internal; | ||
|
||
use Amp\ByteStream\ClosedException; | ||
use Amp\ByteStream\OutputStream; | ||
use Amp\ByteStream\StreamException; | ||
use Amp\Http\Client\HttpException; | ||
use Amp\Promise; | ||
use function Amp\call; | ||
|
||
class Http2UpgradeOutputStream implements OutputStream | ||
{ | ||
private $write; | ||
|
||
public function __construct(callable $write) | ||
{ | ||
$this->write = $write; | ||
} | ||
|
||
public function write(string $data): Promise | ||
{ | ||
return $this->send($data, false); | ||
} | ||
|
||
public function end(string $finalData = ""): Promise | ||
{ | ||
return $this->send($finalData, true); | ||
} | ||
|
||
private function send(string $data, bool $final): Promise | ||
{ | ||
return call(function () use ($data, $final): \Generator { | ||
if ($this->write === null) { | ||
throw new ClosedException('The socket is no longer writable'); | ||
} | ||
|
||
try { | ||
return yield call($this->write, $data, $final); | ||
} catch (HttpException $exception) { | ||
throw new StreamException('An error occurred while writing to the tunnelled socket', 0, $exception); | ||
} | ||
}); | ||
} | ||
} |
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