From 57f790f96d5ed1f1dad8179a110143e9d3df808b Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Tue, 16 Feb 2010 14:00:36 -0800 Subject: [PATCH] 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. related-to-bug: 2442307 --- media/libstagefright/HTTPDataSource.cpp | 15 +++++++++++++++ media/libstagefright/HTTPStream.cpp | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/media/libstagefright/HTTPDataSource.cpp b/media/libstagefright/HTTPDataSource.cpp index 4b630b999c6bd..bf020e92b6f58 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 + #include "include/stagefright_string.h" #include "include/HTTPStream.h" @@ -266,6 +270,8 @@ ssize_t HTTPDataSource::sendRangeRequest(size_t offset) { } 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 @@ ssize_t HTTPDataSource::readAt(off_t offset, void *data, size_t size) { 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 3711acacdde66..2c5da686b50ee 100644 --- a/media/libstagefright/HTTPStream.cpp +++ b/media/libstagefright/HTTPStream.cpp @@ -276,11 +276,11 @@ ssize_t HTTPStream::receive(void *data, size_t size) { } 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;