forked from horde/Http
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Create the PSR-18 PeclHttp client from original Request/Response…
… classes.
- Loading branch information
Showing
4 changed files
with
253 additions
and
9 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,42 @@ | ||
<?php | ||
namespace Horde\Http\Request; | ||
use \Psr\Http\Message\RequestInterface as Psr7Request; | ||
use \Psr\Http\Message\StreamInterface as Psr7Stream; | ||
/** | ||
* Convert a PSR-7 request message to a pecl/Http native message | ||
* Split off from the PeclHttp Client implementation | ||
*/ | ||
trait Psr7ToPeclHttp | ||
{ | ||
/** | ||
* Convert to native format | ||
* | ||
* @param Psr7Request $request The PSR request to convert | ||
* | ||
* @return \http\Client\Request | ||
*/ | ||
private function convertPsr7RequestToPeclHttp(Psr7Request $request): \http\Client\Request | ||
{ | ||
$extHttpReqBody = new \http\Message\Body(); | ||
// Mind: Do we need to support special formEncoding? The PSR request should return the correct content anyway. | ||
/* if (is_array($data)) { | ||
$body->addForm($data); | ||
} else { | ||
$body->append($data); | ||
}*/ | ||
// TODO: getResource and write reasonable buffer sizes to limit memory footprint | ||
$extHttpReqBody->append((string) $request->getBody()); | ||
// The extHttp headers format is incompatible with PSR getHeaders format. | ||
$extHttpReqHeaders = []; | ||
foreach ($request->getHeaders() as $name => $values) { | ||
$extHttpReqHeaders[$name] = $request->getHeaderLine($name); | ||
} | ||
$extHttpRequest = new \http\Client\Request( | ||
$request->getMethod(), | ||
(string) $request->getUri(), | ||
$extHttpReqHeaders, | ||
$extHttpReqBody | ||
); | ||
return $extHttpRequest; | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
namespace Horde\Http\Response; | ||
use \Psr\Http\Message\ResponseFactoryInterface; | ||
use \Psr\Http\Message\ResponseInterface; | ||
use \Psr\Http\Message\StreamFactoryInterface; | ||
use \Psr\Http\Message\StreamInterface; | ||
use http\Client\Response; | ||
|
||
/** | ||
* Convert a pecl/Http native message to a PSR7 response | ||
* Split off from the PeclHttp Client implementation | ||
*/ | ||
trait PeclHttpToPsr7 | ||
{ | ||
/** | ||
* Convert to PSR-7 format | ||
* | ||
* @param http\Client\Response $response The httpClient response | ||
* | ||
* @return ResponseInterface The PSR-7 equivalent | ||
*/ | ||
private function convertPeclHttpResponseToPsr7(Response $httpResponse | ||
): ResponseInterface | ||
{ | ||
try { | ||
$info = $httpResponse->getTransferInfo(); | ||
} catch (\http\Exception $e) { | ||
throw new ClientException($e); | ||
} | ||
try { | ||
$uri = $info->effective_url; | ||
} catch (\http\Exception\RuntimeException $e) { | ||
// TODO | ||
} | ||
$httpVersion = $httpResponse->getHttpVersion(); | ||
$responseCode = $info->response_code; | ||
$headers = $httpResponse->getHeaders(); | ||
$bodyResource = $httpResponse->getBody()->getResource(); // We can use body->getResource | ||
$psr7Stream = $this->streamFactory->createStreamFromResource($bodyResource); | ||
$psr7Response = $this->responseFactory->createResponse($responseCode); | ||
$psr7Response = $psr7Response->withProtocolVersion($httpVersion)->withBody($psr7Stream); | ||
foreach ($headers as $name => $value) { | ||
$psr7Response = $psr7Response->withHeader($name, $value); | ||
} | ||
return $psr7Response; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Horde\Http; | ||
use \Psr\Http\Message\StreamInterface; | ||
use InvalidArgumentException; | ||
/** | ||
* Static utilities for PSR-7 Stream objects | ||
*/ | ||
class StreamUtils | ||
{ | ||
/** | ||
* | ||
*/ | ||
const MB16 = 16777216; | ||
|
||
public static function copyStreamToResource(StreamInterface $stream, $resource, int $buffer = self::MB16) | ||
{ | ||
if (!is_resource($resource)) { | ||
throw new InvalidArgumentException('Second Parameter $resource must be resource'); | ||
} | ||
while (!$stream->eof()) { | ||
fwrite($resource, $stream->read($buffer)); | ||
} | ||
return $resource; | ||
} | ||
} |