Skip to content

Core\Http\Response::tls

$response->tls(): ?Core\Http\TlsInfo

Returns the details of the TLS connection a reply came over, or null when the reply did not come over TLS.

TLS is the encryption that https uses. When a reply arrives over TLS, tls() returns a Core\Http\TlsInfo. It has the TLS version, the cipher, the server's certificates, and isVerified(), which says whether the certificate was checked. A reply over plain http has no TLS connection, so tls() returns null. The return type is ?Core\Http\TlsInfo, so your program checks for null before it reads anything.

Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. A fixed reply does not use any connection, so tls() returns null in all of them. They show the check for null, a log line with the TLS version, and a program that sends a token only after a verified connection.

Return value

Returns ?Core\Http\TlsInfo. The TLS details. The result is null if the reply did not use TLS. That is the case for a plain http call, or for a call that a test served from its table of replies.

Examples

Checking for a tls connection

<?nvs
// Shows how to check whether a reply came over an encrypted TLS connection.
// `Core\Test::answerHttp` gives a fixed reply to this address, so the example runs
// without a network. A fixed reply does not use any connection.
Core\Test::answerHttp('https://api.example.com/health', 200, { body: 'ok' });
Core\Http\Response $response = Core\Http\Client::get('https://api.example.com/health');
// `tls()` returns a `Core\Http\TlsInfo` when the reply came over TLS, and `null`
// when it did not. This reply is a fixed reply, so this prints `no TLS`.
?Core\Http\TlsInfo $info = $response->tls();
if ($info == null) {
echo "no TLS\n";
} else {
echo 'TLS version: ', $info->version(), "\n";
}
Output
no TLS

A log line for each call

<?nvs
// Shows a log line for each request that names the TLS version of the connection.
class CallLog
{
// Returns one log line with the address, the status code and the TLS version.
// The version is `none` when the reply did not come over TLS.
public static function line(string $url, Core\Http\Response $response): string
{
?Core\Http\TlsInfo $info = $response->tls();
string $version = 'none';
if ($info != null) {
$version = $info->version();
}
return $url . ' ' . $response->status() . ' tls=' . $version;
}
}
// Two fixed replies, so the example runs without a network.
Core\Test::answerHttp('https://shop.example.com/cart', 200, {});
Core\Test::answerHttp('http://shop.example.com/old-page', 404, {});
// Fixed replies do not use a connection, so both lines end with `tls=none`.
// A reply from a real `https` server shows a version such as `TLSv1.3`.
array<string> $urls = ['https://shop.example.com/cart', 'http://shop.example.com/old-page'];
foreach ($urls as string $url) {
echo CallLog::line($url, Core\Http\Client::get($url)), "\n";
}
Output
https://shop.example.com/cart 200 tls=none
http://shop.example.com/old-page 404 tls=none

Send a token only over verified tls

<?nvs
// Shows a program that sends its access token only after a verified TLS connection.
class Api
{
// Returns `true` when the reply came over TLS and the server's certificate
// was checked. Returns `false` in every other case.
public static function isVerified(Core\Http\Response $response): bool
{
?Core\Http\TlsInfo $info = $response->tls();
if ($info == null) {
return false;
}
return $info->isVerified();
}
}
// A fixed reply, so the example runs without a network.
Core\Test::answerHttp('https://api.example.com/ping', 200, {});
Core\Http\Response $ping = Core\Http\Client::get('https://api.example.com/ping');
// A fixed reply does not use a connection, so `tls()` returns `null`.
// This prints `Not sending the token.`
if (Api::isVerified($ping)) {
echo "Sending the token.\n";
} else {
echo "Not sending the token.\n";
}
Output
Not sending the token.