Merge "Restore feature parity with the old HTTPDataSource as far as support for extra headers (cookies?) and socket-timeout are concerned." into kraken
This commit is contained in:
committed by
Android (Google) Code Review
commit
f5eafe40cd
@ -1152,7 +1152,7 @@ status_t AwesomePlayer::finishSetDataSource_l() {
|
||||
mConnectingDataSource = new NuHTTPDataSource;
|
||||
|
||||
mLock.unlock();
|
||||
status_t err = mConnectingDataSource->connect(mUri/*, mUriHeaders */);
|
||||
status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders);
|
||||
mLock.lock();
|
||||
|
||||
if (err != OK) {
|
||||
|
@ -109,7 +109,7 @@ sp<DataSource> DataSource::CreateFromURI(
|
||||
source = new FileSource(uri + 7);
|
||||
} else if (!strncasecmp("http://", uri, 7)) {
|
||||
sp<NuHTTPDataSource> httpSource = new NuHTTPDataSource;
|
||||
if (httpSource->connect(uri /* , headers */) != OK) {
|
||||
if (httpSource->connect(uri, headers) != OK) {
|
||||
return NULL;
|
||||
}
|
||||
source = new NuCachedSource2(httpSource);
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
#include "include/NuHTTPDataSource.h"
|
||||
|
||||
#include <cutils/properties.h>
|
||||
#include <media/stagefright/MediaDebug.h>
|
||||
#include <media/stagefright/MediaErrors.h>
|
||||
|
||||
@ -72,18 +73,33 @@ NuHTTPDataSource::NuHTTPDataSource()
|
||||
NuHTTPDataSource::~NuHTTPDataSource() {
|
||||
}
|
||||
|
||||
status_t NuHTTPDataSource::connect(const char *uri, off_t offset) {
|
||||
status_t NuHTTPDataSource::connect(
|
||||
const char *uri,
|
||||
const KeyedVector<String8, String8> *overrides,
|
||||
off_t offset) {
|
||||
String8 headers;
|
||||
MakeFullHeaders(overrides, &headers);
|
||||
|
||||
return connect(uri, headers, offset);
|
||||
}
|
||||
|
||||
status_t NuHTTPDataSource::connect(
|
||||
const char *uri,
|
||||
const String8 &headers,
|
||||
off_t offset) {
|
||||
String8 host, path;
|
||||
unsigned port;
|
||||
if (!ParseURL(uri, &host, &port, &path)) {
|
||||
return ERROR_MALFORMED;
|
||||
}
|
||||
|
||||
return connect(host, port, path, offset);
|
||||
return connect(host, port, path, headers, offset);
|
||||
}
|
||||
|
||||
status_t NuHTTPDataSource::connect(
|
||||
const char *host, unsigned port, const char *path, off_t offset) {
|
||||
const char *host, unsigned port, const char *path,
|
||||
const String8 &headers,
|
||||
off_t offset) {
|
||||
LOGI("connect to %s:%u%s @%ld", host, port, path, offset);
|
||||
|
||||
bool needsToReconnect = true;
|
||||
@ -99,6 +115,7 @@ status_t NuHTTPDataSource::connect(
|
||||
mHost = host;
|
||||
mPort = port;
|
||||
mPath = path;
|
||||
mHeaders = headers;
|
||||
|
||||
status_t err = OK;
|
||||
|
||||
@ -133,6 +150,7 @@ status_t NuHTTPDataSource::connect(
|
||||
request.append(rangeHeader);
|
||||
}
|
||||
|
||||
request.append(mHeaders);
|
||||
request.append("\r\n");
|
||||
|
||||
int httpStatus;
|
||||
@ -151,10 +169,17 @@ status_t NuHTTPDataSource::connect(
|
||||
|
||||
mHTTP.disconnect();
|
||||
|
||||
return connect(value.c_str());
|
||||
return connect(value.c_str(), headers, offset);
|
||||
}
|
||||
|
||||
CHECK(httpStatus >= 200 && httpStatus < 300);
|
||||
if (httpStatus < 200 || httpStatus >= 300) {
|
||||
mState = DISCONNECTED;
|
||||
mHTTP.disconnect();
|
||||
|
||||
return ERROR_IO;
|
||||
}
|
||||
|
||||
applyTimeoutResponse();
|
||||
|
||||
if (offset == 0) {
|
||||
string value;
|
||||
@ -200,7 +225,8 @@ ssize_t NuHTTPDataSource::readAt(off_t offset, void *data, size_t size) {
|
||||
if (offset != mOffset) {
|
||||
String8 host = mHost;
|
||||
String8 path = mPath;
|
||||
status_t err = connect(host, mPort, path, offset);
|
||||
String8 headers = mHeaders;
|
||||
status_t err = connect(host, mPort, path, headers, offset);
|
||||
|
||||
if (err != OK) {
|
||||
return err;
|
||||
@ -262,4 +288,51 @@ uint32_t NuHTTPDataSource::flags() {
|
||||
return kWantsPrefetching;
|
||||
}
|
||||
|
||||
// static
|
||||
void NuHTTPDataSource::MakeFullHeaders(
|
||||
const KeyedVector<String8, String8> *overrides, String8 *headers) {
|
||||
headers->setTo("");
|
||||
|
||||
headers->append("User-Agent: stagefright/1.1 (Linux;Android ");
|
||||
|
||||
#if (PROPERTY_VALUE_MAX < 8)
|
||||
#error "PROPERTY_VALUE_MAX must be at least 8"
|
||||
#endif
|
||||
|
||||
char value[PROPERTY_VALUE_MAX];
|
||||
property_get("ro.build.version.release", value, "Unknown");
|
||||
headers->append(value);
|
||||
headers->append(")\r\n");
|
||||
|
||||
if (overrides == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < overrides->size(); ++i) {
|
||||
String8 line;
|
||||
line.append(overrides->keyAt(i));
|
||||
line.append(": ");
|
||||
line.append(overrides->valueAt(i));
|
||||
line.append("\r\n");
|
||||
|
||||
headers->append(line);
|
||||
}
|
||||
}
|
||||
|
||||
void NuHTTPDataSource::applyTimeoutResponse() {
|
||||
string timeout;
|
||||
if (mHTTP.find_header_value("X-SocketTimeout", &timeout)) {
|
||||
const char *s = timeout.c_str();
|
||||
char *end;
|
||||
long tmp = strtol(s, &end, 10);
|
||||
if (end == s || *end != '\0') {
|
||||
LOGW("Illegal X-SocketTimeout value given.");
|
||||
return;
|
||||
}
|
||||
|
||||
LOGI("overriding default timeout, new timeout is %ld seconds", tmp);
|
||||
mHTTP.setReceiveTimeout(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
|
@ -13,10 +13,9 @@ namespace android {
|
||||
struct NuHTTPDataSource : public DataSource {
|
||||
NuHTTPDataSource();
|
||||
|
||||
status_t connect(const char *uri, off_t offset = 0);
|
||||
|
||||
status_t connect(
|
||||
const char *host, unsigned port, const char *path,
|
||||
const char *uri,
|
||||
const KeyedVector<String8, String8> *headers = NULL,
|
||||
off_t offset = 0);
|
||||
|
||||
void disconnect();
|
||||
@ -44,12 +43,27 @@ private:
|
||||
String8 mHost;
|
||||
unsigned mPort;
|
||||
String8 mPath;
|
||||
String8 mHeaders;
|
||||
|
||||
HTTPStream mHTTP;
|
||||
off_t mOffset;
|
||||
off_t mContentLength;
|
||||
bool mContentLengthValid;
|
||||
|
||||
status_t connect(
|
||||
const char *uri, const String8 &headers, off_t offset);
|
||||
|
||||
status_t connect(
|
||||
const char *host, unsigned port, const char *path,
|
||||
const String8 &headers,
|
||||
off_t offset);
|
||||
|
||||
void applyTimeoutResponse();
|
||||
|
||||
static void MakeFullHeaders(
|
||||
const KeyedVector<String8, String8> *overrides,
|
||||
String8 *headers);
|
||||
|
||||
NuHTTPDataSource(const NuHTTPDataSource &);
|
||||
NuHTTPDataSource &operator=(const NuHTTPDataSource &);
|
||||
};
|
||||
|
Reference in New Issue
Block a user