/** * @memcheck */ public function testEncrypt() { $listener = new Listener(ION_TEST_SERVER_HOST); $server_ssl = Crypto::server(Crypto::METHOD_TLSv12)->passPhrase('unittest')->localCert(ION_RESOURCES . '/cacert.pem', ION_RESOURCES . '/cakey.pem')->allowSelfSigned(); $client_ssl = Crypto::client(Crypto::METHOD_TLSv12)->allowSelfSigned(); $listener->encrypt($server_ssl); $listener->whenAccepted()->then(function (Stream $connect) { $this->data["connect"] = $this->describe($connect); $this->data["incoming"] = (yield $connect->readLine("\r\n")); $connect->write("welcome\r\n"); (yield $connect->flush()); $this->stop(); })->onFail(function (\Throwable $error) { $this->data["server.error"] = $this->describe($error); $this->stop(); }); $this->promise(function () use($client_ssl) { $socket = Stream::socket(ION_TEST_SERVER_HOST, $client_ssl); $socket->write("hello\r\n"); $this->data["outgoing"] = (yield $socket->readLine("\r\n")); }, false); $this->loop(); $this->assertEquals(["connect" => ["object" => "ION\\Stream"], "incoming" => "hello", "outgoing" => "welcome"], $this->data); }
/** * @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))); }
<?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());
/** * @memcheck */ public function testEncrypted() { $this->promise(function () { $socket = Stream::socket("example.com:443", Crypto::client()); $socket->write(implode("\r\n", ["GET / HTTP/1.1", "Host: example.com", "Connection: close", "Accept: text/html", "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36", "Accept-Language: en-US,en;q=0.8,ru;q=0.6"]) . "\r\n\r\n"); $this->data["response"] = (yield $socket->readAll()); }); $this->loop(1); $this->assertTrue(isset($this->data["response"])); $this->assertEquals("HTTP/1.1 200 OK", strstr($this->data["response"], "\r\n", true)); }