Merge "If we never triggered a range request but know the content length make sure to not read more data than there could be, otherwise we'd block indefinitely if the server doesn't close the connection."
diff --git a/media/libstagefright/HTTPDataSource.cpp b/media/libstagefright/HTTPDataSource.cpp
index 4b630b9..bf020e9 100644
--- a/media/libstagefright/HTTPDataSource.cpp
+++ b/media/libstagefright/HTTPDataSource.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+//#define LOG_NDEBUG 0
+#define LOG_TAG "HTTPDataSource"
+#include <utils/Log.h>
+
 #include "include/stagefright_string.h"
 #include "include/HTTPStream.h"
 
@@ -266,6 +270,8 @@
 }
 
 ssize_t HTTPDataSource::readAt(off_t offset, void *data, size_t size) {
+    LOGV("readAt %ld, size %d", offset, size);
+
     if (offset >= mBufferOffset
             && offset < (off_t)(mBufferOffset + mBufferLength)) {
         size_t num_bytes_available = mBufferLength - (offset - mBufferOffset);
@@ -298,6 +304,15 @@
 
     mBufferOffset = offset;
 
+    if (mContentLengthValid
+            && mBufferOffset + contentLength >= mContentLength) {
+        // If we never triggered a range request but know the content length,
+        // make sure to not read more data than there could be, otherwise
+        // we'd block indefinitely if the server doesn't close the connection.
+
+        contentLength = mContentLength - mBufferOffset;
+    }
+
     if (contentLength <= 0) {
         return contentLength;
     }
diff --git a/media/libstagefright/HTTPStream.cpp b/media/libstagefright/HTTPStream.cpp
index 3711aca..2c5da68 100644
--- a/media/libstagefright/HTTPStream.cpp
+++ b/media/libstagefright/HTTPStream.cpp
@@ -276,11 +276,11 @@
             }
 
             disconnect();
-            return total == 0 ? ERROR_IO : total;
+            return total == 0 ? (ssize_t)ERROR_IO : total;
         } else if (n == 0) {
             disconnect();
 
-            return total == 0 ? ERROR_CONNECTION_LOST : total;
+            return total == 0 ? (ssize_t)ERROR_CONNECTION_LOST : total;
         }
 
         total += (size_t)n;