1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "NuHTTPDataSource"
#include <utils/Log.h>
#include "include/NuHTTPDataSource.h"
#include <cutils/properties.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/MediaDebug.h>
#include <media/stagefright/MediaErrors.h>
namespace android {
static bool ParseSingleUnsignedLong(
const char *from, unsigned long *x) {
char *end;
*x = strtoul(from, &end, 10);
if (end == from || *end != '\0') {
return false;
}
return true;
}
static bool ParseURL(
const char *url, String8 *host, unsigned *port,
String8 *path, bool *https) {
host->setTo("");
*port = 0;
path->setTo("");
size_t hostStart;
if (!strncasecmp("http://", url, 7)) {
hostStart = 7;
*https = false;
} else if (!strncasecmp("https://", url, 8)) {
hostStart = 8;
*https = true;
} else {
return false;
}
const char *slashPos = strchr(&url[hostStart], '/');
if (slashPos == NULL) {
host->setTo(&url[hostStart]);
path->setTo("/");
} else {
host->setTo(&url[hostStart], slashPos - &url[hostStart]);
path->setTo(slashPos);
}
const char *colonPos = strchr(host->string(), ':');
if (colonPos != NULL) {
unsigned long x;
if (!ParseSingleUnsignedLong(colonPos + 1, &x) || x >= 65536) {
return false;
}
*port = x;
size_t colonOffset = colonPos - host->string();
String8 tmp(host->string(), colonOffset);
*host = tmp;
} else {
*port = (*https) ? 443 : 80;
}
return true;
}
NuHTTPDataSource::NuHTTPDataSource(uint32_t flags)
: mFlags(flags),
mState(DISCONNECTED),
mPort(0),
mHTTPS(false),
mOffset(0),
mContentLength(0),
mContentLengthValid(false),
mHasChunkedTransferEncoding(false),
mChunkDataBytesLeft(0),
mDecryptHandle(NULL),
mDrmManagerClient(NULL) {
}
NuHTTPDataSource::~NuHTTPDataSource() {
if (mDecryptHandle != NULL) {
// To release mDecryptHandle
CHECK(mDrmManagerClient);
mDrmManagerClient->closeDecryptSession(mDecryptHandle);
mDecryptHandle = NULL;
}
if (mDrmManagerClient != NULL) {
delete mDrmManagerClient;
mDrmManagerClient = NULL;
}
}
status_t NuHTTPDataSource::connect(
const char *uri,
const KeyedVector<String8, String8> *overrides,
off64_t offset) {
String8 headers;
MakeFullHeaders(overrides, &headers);
return connect(uri, headers, offset);
}
status_t NuHTTPDataSource::connect(
const char *uri,
const String8 &headers,
off64_t offset) {
String8 host, path;
unsigned port;
mUri = uri;
mContentType = String8("application/octet-stream");
bool https;
if (!ParseURL(uri, &host, &port, &path, &https)) {
return ERROR_MALFORMED;
}
return connect(host, port, path, https, headers, offset);
}
static bool IsRedirectStatusCode(int httpStatus) {
return httpStatus == 301 || httpStatus == 302
|| httpStatus == 303 || httpStatus == 307;
}
status_t NuHTTPDataSource::connect(
const char *host, unsigned port, const char *path,
bool https,
const String8 &headers,
off64_t offset) {
if (!(mFlags & kFlagIncognito)) {
LOGI("connect to %s:%u%s @%lld", host, port, path, offset);
} else {
LOGI("connect to <URL suppressed> @%lld", offset);
}
bool needsToReconnect = true;
if (mState == CONNECTED && host == mHost && port == mPort
&& https == mHTTPS && offset == mOffset) {
if (mContentLengthValid && mOffset == mContentLength) {
LOGI("Didn't have to reconnect, old one's still good.");
needsToReconnect = false;
}
}
mHost = host;
mPort = port;
mPath = path;
mHTTPS = https;
mHeaders = headers;
status_t err = OK;
mState = CONNECTING;
if (needsToReconnect) {
mHTTP.disconnect();
err = mHTTP.connect(host, port, https);
}
if (err != OK) {
mState = DISCONNECTED;
} else if (mState != CONNECTING) {
err = UNKNOWN_ERROR;
} else {
mState = CONNECTED;
mOffset = offset;
mContentLength = 0;
mContentLengthValid = false;
String8 request("GET ");
request.append(mPath);
request.append(" HTTP/1.1\r\n");
request.append("Host: ");
request.append(mHost);
if (mPort != 80) {
request.append(StringPrintf(":%u", mPort).c_str());
}
request.append("\r\n");
if (offset != 0) {
char rangeHeader[128];
sprintf(rangeHeader, "Range: bytes=%lld-\r\n", offset);
request.append(rangeHeader);
}
request.append(mHeaders);
request.append("\r\n");
int httpStatus;
if ((err = mHTTP.send(request.string(), request.size())) != OK
|| (err = mHTTP.receive_header(&httpStatus)) != OK) {
mHTTP.disconnect();
mState = DISCONNECTED;
return err;
}
if (IsRedirectStatusCode(httpStatus)) {
AString value;
CHECK(mHTTP.find_header_value("Location", &value));
mState = DISCONNECTED;
mHTTP.disconnect();
return connect(value.c_str(), headers, offset);
}
if (httpStatus < 200 || httpStatus >= 300) {
mState = DISCONNECTED;
mHTTP.disconnect();
return ERROR_IO;
}
mHasChunkedTransferEncoding = false;
{
AString value;
if (mHTTP.find_header_value("Transfer-Encoding", &value)) {
// We don't currently support any transfer encodings but
// chunked.
if (!strcasecmp(value.c_str(), "chunked")) {
LOGI("Chunked transfer encoding applied.");
mHasChunkedTransferEncoding = true;
mChunkDataBytesLeft = 0;
} else {
mState = DISCONNECTED;
mHTTP.disconnect();
LOGE("We don't support '%s' transfer encoding.", value.c_str());
return ERROR_UNSUPPORTED;
}
}
}
{
AString value;
if (mHTTP.find_header_value("Content-Type", &value)) {
mContentType = String8(value.c_str());
} else {
mContentType = String8("application/octet-stream");
}
}
applyTimeoutResponse();
if (offset == 0) {
AString value;
unsigned long x;
if (mHTTP.find_header_value(AString("Content-Length"), &value)
&& ParseSingleUnsignedLong(value.c_str(), &x)) {
mContentLength = (off64_t)x;
mContentLengthValid = true;
} else {
LOGW("Server did not give us the content length!");
}
} else {
if (httpStatus != 206 /* Partial Content */) {
// We requested a range but the server didn't support that.
LOGE("We requested a range but the server didn't "
"support that.");
return ERROR_UNSUPPORTED;
}
AString value;
unsigned long x;
if (mHTTP.find_header_value(AString("Content-Range"), &value)) {
const char *slashPos = strchr(value.c_str(), '/');
if (slashPos != NULL
&& ParseSingleUnsignedLong(slashPos + 1, &x)) {
mContentLength = x;
mContentLengthValid = true;
}
}
}
}
return err;
}
void NuHTTPDataSource::disconnect() {
if (mState == CONNECTING || mState == CONNECTED) {
mHTTP.disconnect();
}
mState = DISCONNECTED;
}
status_t NuHTTPDataSource::initCheck() const {
return mState == CONNECTED ? OK : NO_INIT;
}
ssize_t NuHTTPDataSource::internalRead(void *data, size_t size) {
if (!mHasChunkedTransferEncoding) {
return mHTTP.receive(data, size);
}
if (mChunkDataBytesLeft < 0) {
return 0;
} else if (mChunkDataBytesLeft == 0) {
char line[1024];
status_t err = mHTTP.receive_line(line, sizeof(line));
if (err != OK) {
return err;
}
LOGV("line = '%s'", line);
char *end;
unsigned long n = strtoul(line, &end, 16);
if (end == line || (*end != ';' && *end != '\0')) {
LOGE("malformed HTTP chunk '%s'", line);
return ERROR_MALFORMED;
}
mChunkDataBytesLeft = n;
LOGV("chunk data size = %lu", n);
if (mChunkDataBytesLeft == 0) {
mChunkDataBytesLeft = -1;
return 0;
}
// fall through
}
if (size > (size_t)mChunkDataBytesLeft) {
size = mChunkDataBytesLeft;
}
ssize_t n = mHTTP.receive(data, size);
if (n < 0) {
return n;
}
mChunkDataBytesLeft -= (size_t)n;
if (mChunkDataBytesLeft == 0) {
char line[1024];
status_t err = mHTTP.receive_line(line, sizeof(line));
if (err != OK) {
return err;
}
if (line[0] != '\0') {
LOGE("missing HTTP chunk terminator.");
return ERROR_MALFORMED;
}
}
return n;
}
ssize_t NuHTTPDataSource::readAt(off64_t offset, void *data, size_t size) {
LOGV("readAt offset %ld, size %d", offset, size);
Mutex::Autolock autoLock(mLock);
if (offset != mOffset) {
String8 host = mHost;
String8 path = mPath;
String8 headers = mHeaders;
status_t err = connect(host, mPort, path, mHTTPS, headers, offset);
if (err != OK) {
return err;
}
}
if (mContentLengthValid) {
size_t avail =
(offset >= mContentLength) ? 0 : mContentLength - offset;
if (size > avail) {
size = avail;
}
}
size_t numBytesRead = 0;
while (numBytesRead < size) {
int64_t startTimeUs = ALooper::GetNowUs();
ssize_t n =
internalRead((uint8_t *)data + numBytesRead, size - numBytesRead);
if (n < 0) {
if (numBytesRead == 0 || mContentLengthValid) {
return n;
}
// If there was an error we want to at least return the data
// we've already successfully read. The next call to read will
// then return the error.
n = 0;
}
int64_t delayUs = ALooper::GetNowUs() - startTimeUs;
addBandwidthMeasurement(n, delayUs);
numBytesRead += (size_t)n;
if (n == 0) {
if (mContentLengthValid) {
// We know the content length and made sure not to read beyond
// it and yet the server closed the connection on us.
return ERROR_IO;
}
break;
}
}
mOffset += numBytesRead;
return numBytesRead;
}
status_t NuHTTPDataSource::getSize(off64_t *size) {
*size = 0;
if (mState != CONNECTED) {
return ERROR_IO;
}
if (mContentLengthValid) {
*size = mContentLength;
return OK;
}
return ERROR_UNSUPPORTED;
}
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() {
AString 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);
}
}
sp<DecryptHandle> NuHTTPDataSource::DrmInitialization() {
if (mDrmManagerClient == NULL) {
mDrmManagerClient = new DrmManagerClient();
}
if (mDrmManagerClient == NULL) {
return NULL;
}
if (mDecryptHandle == NULL) {
/* Note if redirect occurs, mUri is the redirect uri instead of the
* original one
*/
mDecryptHandle = mDrmManagerClient->openDecryptSession(mUri);
}
if (mDecryptHandle == NULL) {
delete mDrmManagerClient;
mDrmManagerClient = NULL;
}
return mDecryptHandle;
}
void NuHTTPDataSource::getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {
handle = mDecryptHandle;
*client = mDrmManagerClient;
}
String8 NuHTTPDataSource::getUri() {
return mUri;
}
String8 NuHTTPDataSource::getMIMEType() const {
return mContentType;
}
} // namespace android
|