public function __construct() { parent::__construct(function (Stream $stream) { $headers = (yield $stream->readLine("\r\n\r\n", Stream::MODE_TRIM_TOKEN, 64 * KiB)); return Request::parse($headers); }); }
/** * @memcheck */ public function testFactory() { $body = "...body content..."; $req = Request::factory([Request::METHOD => "GET", Request::URI => URI::parse("/iddqd?cheat=1"), Request::VERSION => "1.1", Request::HEADERS => ["Host" => "example.com", "User-Agent" => ["nobody"], "Cookie" => ["one=1", "two=2"]], Request::BODY => $body]); $expected = implode("\r\n", ["GET /iddqd?cheat=1 HTTP/1.1", "Host: example.com", "User-Agent: nobody", "Cookie: one=1,two=2", "", $body]); $this->assertEquals($expected, strval($req)); }
<?php use ION\HTTP\Request; ION::promise(function () { $request = Request::factory([Request::METHOD => "GET", Request::URI => ION\URI::parse("http://example.com/"), Request::VERSION => "1.1", Request::HEADERS => ["User-Agent" => "ION HTTP Client example", "X-Client" => "PHP"]]); $respose = \ION\HTTP::request($request, \ION\Stream::socket("example.com:80")); $parser = new \ION\HTTP\WebSocketParser(); if ($frame = $parser($chunk)) { } $request->onBody($parser)->then(function (\ION\HTTP\WebSocket\Frame $frame) { }); while (!$parser->isFinished()) { $frame = (yield $request->onBody($parser)); } $parser = new \ION\HTTP\MultiPartParser("iddqd", 1000); $request->onBody($parser)->then(function (\ION\HTTP\MultiPart\Part $part) { }); while (!$parser->isFinished()) { $part = (yield $request->onBody($parser)); } $parser = new \ION\HTTP\ChunkedParser(); $request->onBody($parser)->then(function (string $chunk) { }); while (!$parser->isFinished()) { $chunk = (yield $request->onBody($parser)); } $response = (yield \ION\HTTP::request($request)); /* @var \ION\HTTP\Response $response */ echo "Code: " . $response->getStatusCode() . " (" . $response->getReasonPhrase() . ")\n\n"; echo "Headers: \n\n"; var_dump($response->getHeaders());
/** * @param Request $request * @param \ION\Stream $socket * * @return Deferred */ public static function request(Request $request, Stream $socket) : Deferred { return Response::parse((yield $socket->write($request->build())->readLine("\r\n\r\n", Stream::MODE_TRIM_TOKEN, 8192)->timeout(30))); }