Note that there are some explanatory texts on larger screens.

plurals
  1. POIn PHP: OpenSSL Error messages: error: 1409F07F: SSL routines: SSL3_WRITE_PENDING: bad write retry
    primarykey
    data
    text
    <p>I'm trying to send a huge amount of data using SSL/TLS connection in PHP. It works pretty well if the data chunk isn't very big or if I don't use TLS, but what I need (near 2MiB), the <code>fwrite</code> function shows the warning:</p> <blockquote> <p>Warning: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error: 1409F07F: SSL routines: SSL3_WRITE_PENDING: bad write retry</p> </blockquote> <p>The relevant code I'm using to connect clients:</p> <pre><code>$cntxt = stream_context_create(array('ssl' =&gt; array('local_cert' =&gt; 'certificate.pem'))); $server = stream_socket_server('tls://127.0.0.1:8080', $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $cntxt); // Wait for client connection // $client = stream_socket_accept($server); // Use non-blocking socket to allow answering many clients at a time stream_set_blocking($client, 0); $clients[] = $client; </code></pre> <p>When sending data, it's append to a buffer and, from time to time, this function is called for each client and linked buffer:</p> <pre><code>function trySend($client, &amp;$buffer) { if (strlen($buffer)) { $len = fwrite($client, $buffer); $buffer = substr($buffer, $len); } } </code></pre> <p>As I said, my code works for small ammount of data or for normal (non-TLS) connections. I've searched for this error and found <a href="http://www.openssl.org/docs/ssl/SSL_write.html" rel="nofollow">http://www.openssl.org/docs/ssl/SSL_write.html</a>:</p> <blockquote> <p>SSL_write() will only return with success, when the complete contents of buf of length num has been written. This default behaviour can be changed with the SSL_MODE_ENABLE_PARTIAL_WRITE option of SSL_CTX_set_mode(3). When this flag is set, SSL_write() will also return with success, when a partial write has been successfully completed. In this case the SSL_write() operation is considered completed. The bytes are sent and a new SSL_write() operation with a new buffer (with the already sent bytes removed) must be started. A partial write is performed with the size of a message block, which is 16kB for SSLv3/TLSv1.</p> </blockquote> <p>But how can I do this in PHP?</p> <p>Any help appreciated :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload