Skip to content

Instantly share code, notes, and snippets.

@sshilko
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshilko/41f04ddaaf5f73f0bb91 to your computer and use it in GitHub Desktop.
Save sshilko/41f04ddaaf5f73f0bb91 to your computer and use it in GitHub Desktop.
PHP 5.5.23 breaks TLS (SSL) fread timeout
<?php
$url = 'tls://gateway.sandbox.push.apple.com:2195';
$streamContext = stream_context_create(array('ssl' => array(
'verify_peer' => 1,
'cafile' => './entrust_2048_ca.cer',
'local_cert' => './dev.pem'
)));
$connectTimeout = ini_get("default_socket_timeout");
$socket = stream_socket_client($url,
$error1,
$error2,
$connectTimeout,
STREAM_CLIENT_CONNECT,
$streamContext);
stream_set_blocking($socket, 0);
stream_set_write_buffer($socket, 0);
stream_set_read_buffer($socket, 0);
/**
* ApnsPHP_Message_Custom object
*/
$content = "YTozOntzOjc6Ik1FU1NBR0UiO086MjI6IkFwbnNQSFBfTWVzc2FnZV9DdXN0b20iOjEyOntzOjE3OiIAKgBfc0FjdGlvbkxvY0tleSI7TjtzOjExOiIAKgBfc0xvY0tleSI7TjtzOjEyOiIAKgBfYUxvY0FyZ3MiO047czoxNjoiACoAX3NMYXVuY2hJbWFnZSI7TjtzOjI2OiIAKgBfYkF1dG9BZGp1c3RMb25nUGF5bG9hZCI7YjoxO3M6MTc6IgAqAF9hRGV2aWNlVG9rZW5zIjthOjE6e2k6MDtzOjY0OiIzMzZhNmVmYWJlYmJjMjE0ZDkyYTFhOWUxYmEzYWRiMzExNzFmMTQ5M2JmMzgzN2UzZTBlNmU4ZTE2ODNmMDFiIjt9czo5OiIAKgBfc1RleHQiO3M6MTA6Ikx1bmE6IFRlc3QiO3M6MTA6IgAqAF9uQmFkZ2UiO2k6MTtzOjEwOiIAKgBfc1NvdW5kIjtzOjE1OiJhcG4tcmVjaWV2ZS53YXYiO3M6MjE6IgAqAF9hQ3VzdG9tUHJvcGVydGllcyI7YTozOntzOjM6InVpZCI7aToxNzQ0O3M6MzoiZGlkIjtzOjI0OiI1NTIzQUE1MUJDQzk2MkI2MTg4QjQ1NkQiO3M6MToidCI7czoxOiJ1Ijt9czoxNjoiACoAX25FeHBpcnlWYWx1ZSI7aTo2MDQ4MDA7czoyMToiACoAX21DdXN0b21JZGVudGlmaWVyIjtOO31zOjE5OiJCSU5BUllfTk9USUZJQ0FUSU9OIjtzOjE3MjoiAQAAAAFVLOTRACAzam76vrvCFNkqGp4bo62zEXHxSTvzg34+Dm6OFoPwGwB/eyJhcHMiOnsiYWxlcnQiOnsiYm9keSI6Ikx1bmE6IFRlc3QifSwiYmFkZ2UiOjEsInNvdW5kIjoiYXBuLXJlY2lldmUud2F2In0sInVpZCI6MTc0NCwiZGlkIjoiNTUyM0FBNTFCQ0M5NjJCNjE4OEI0NTZEIiwidCI6InUifSI7czo2OiJFUlJPUlMiO2E6MDp7fX0=";
$content = unserialize(base64_decode($content));
$binartData = $content['BINARY_NOTIFICATION'];
$expect = strlen($binartData);
echo 'Sending ' . $expect . ' bytes over ' . $url . ":\n";
$written = (int) fwrite($socket, $binartData);
if ($written != $expect) {
die('Error 1 writing to socket');
}
echo "Socket state:\n";
print_r(stream_get_meta_data($socket));
/**
* If you send a notification that is accepted by APNs, nothing is returned.
* If you send a notification that is malformed or otherwise unintelligible,
* APNs returns an error-response packet and closes the connection.
*
* Format of error-response packet
* 1+1+4 = 6 bytes
* https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
*/
$timeoutSet = stream_set_timeout($socket, 1, 0);
echo 'Timeout set: ' . (int) $timeoutSet . "\n";
$isError = fread($socket, 6);
/*
fread() reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met:
+ length bytes have been read
+ EOF (end of file) is reached
+ a packet becomes available or the socket timeout occurs (for network streams)
+ if the stream is read buffered and it does not represent a plain file,
at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made;
depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
*/
if ($isError) {
$isError = unpack('Ccommand/CstatusCode/Nidentifier', $isError);
}
echo "Has errors:\n";
var_dump($isError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment