diff mbox

[FFmpeg-devel,1/2] lavf/tls_securetransport: return buffered data first from tls_read

Message ID 20171025181500.6261-1-ffmpeg@tmm1.net
State Accepted
Commit 9c8922acadb5187c274250d6cde653b7bad2559e
Headers show

Commit Message

Aman Gupta Oct. 25, 2017, 6:14 p.m. UTC
From: Aman Gupta <aman@tmm1.net>

This fixes a deadlock while reading a chunked https response, if
multiple_requests=1 is also set. Without an EOF to signal the end of
the last chunk, tls_read gets stuck forever trying to read more data
than is available. This occurs with the http protocol reproducibly,
because http.c always reads 4kb at a time, and the last chunk of an
http response is often much smaller.

After this commit, tls_read always returns any buffered plaintext
first before attempting to read more encrypted data off the
underlying tcp socket.
---
 libavformat/tls_securetransport.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/libavformat/tls_securetransport.c b/libavformat/tls_securetransport.c
index bc8a32069e..9ea588ae3a 100644
--- a/libavformat/tls_securetransport.c
+++ b/libavformat/tls_securetransport.c
@@ -352,8 +352,12 @@  static int map_ssl_error(OSStatus status, size_t processed)
 static int tls_read(URLContext *h, uint8_t *buf, int size)
 {
     TLSContext *c = h->priv_data;
-    size_t processed = 0;
-    int ret = SSLRead(c->ssl_context, buf, size, &processed);
+    size_t available = 0, processed = 0;
+    int ret;
+    SSLGetBufferedReadSize(c->ssl_context, &available);
+    if (available)
+        size = FFMIN(available, size);
+    ret = SSLRead(c->ssl_context, buf, size, &processed);
     ret = map_ssl_error(ret, processed);
     if (ret > 0)
         return ret;