Merge pull request #7123 from deepak1556/asar_range_patch
protocol: respect range header when reading from asar
This commit is contained in:
commit
e189132176
4 changed files with 62 additions and 42 deletions
|
@ -44,6 +44,7 @@ URLRequestAsarJob::URLRequestAsarJob(
|
||||||
: net::URLRequestJob(request, network_delegate),
|
: net::URLRequestJob(request, network_delegate),
|
||||||
type_(TYPE_ERROR),
|
type_(TYPE_ERROR),
|
||||||
remaining_bytes_(0),
|
remaining_bytes_(0),
|
||||||
|
seek_offset_(0),
|
||||||
range_parse_result_(net::OK),
|
range_parse_result_(net::OK),
|
||||||
weak_ptr_factory_(this) {}
|
weak_ptr_factory_(this) {}
|
||||||
|
|
||||||
|
@ -100,8 +101,6 @@ void URLRequestAsarJob::InitializeFileJob(
|
||||||
|
|
||||||
void URLRequestAsarJob::Start() {
|
void URLRequestAsarJob::Start() {
|
||||||
if (type_ == TYPE_ASAR) {
|
if (type_ == TYPE_ASAR) {
|
||||||
remaining_bytes_ = static_cast<int64_t>(file_info_.size);
|
|
||||||
|
|
||||||
int flags = base::File::FLAG_OPEN |
|
int flags = base::File::FLAG_OPEN |
|
||||||
base::File::FLAG_READ |
|
base::File::FLAG_READ |
|
||||||
base::File::FLAG_ASYNC;
|
base::File::FLAG_ASYNC;
|
||||||
|
@ -274,8 +273,28 @@ void URLRequestAsarJob::DidOpen(int result) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t file_size, read_offset;
|
||||||
if (type_ == TYPE_ASAR) {
|
if (type_ == TYPE_ASAR) {
|
||||||
int rv = stream_->Seek(file_info_.offset,
|
file_size = file_info_.size;
|
||||||
|
read_offset = file_info_.offset;
|
||||||
|
} else {
|
||||||
|
file_size = meta_info_.file_size;
|
||||||
|
read_offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!byte_range_.ComputeBounds(file_size)) {
|
||||||
|
NotifyStartError(
|
||||||
|
net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
||||||
|
net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
remaining_bytes_ = byte_range_.last_byte_position() -
|
||||||
|
byte_range_.first_byte_position() + 1;
|
||||||
|
seek_offset_ = byte_range_.first_byte_position() + read_offset;
|
||||||
|
|
||||||
|
if (remaining_bytes_ > 0 && seek_offset_ != 0) {
|
||||||
|
int rv = stream_->Seek(seek_offset_,
|
||||||
base::Bind(&URLRequestAsarJob::DidSeek,
|
base::Bind(&URLRequestAsarJob::DidSeek,
|
||||||
weak_ptr_factory_.GetWeakPtr()));
|
weak_ptr_factory_.GetWeakPtr()));
|
||||||
if (rv != net::ERR_IO_PENDING) {
|
if (rv != net::ERR_IO_PENDING) {
|
||||||
|
@ -284,49 +303,19 @@ void URLRequestAsarJob::DidOpen(int result) {
|
||||||
DidSeek(-1);
|
DidSeek(-1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!byte_range_.ComputeBounds(meta_info_.file_size)) {
|
// We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
|
||||||
NotifyStartError(
|
// the value that would mean seek success. This way we skip the code
|
||||||
net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
// handling seek failure.
|
||||||
net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
DidSeek(seek_offset_);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
remaining_bytes_ = byte_range_.last_byte_position() -
|
|
||||||
byte_range_.first_byte_position() + 1;
|
|
||||||
|
|
||||||
if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) {
|
|
||||||
int rv = stream_->Seek(byte_range_.first_byte_position(),
|
|
||||||
base::Bind(&URLRequestAsarJob::DidSeek,
|
|
||||||
weak_ptr_factory_.GetWeakPtr()));
|
|
||||||
if (rv != net::ERR_IO_PENDING) {
|
|
||||||
// stream_->Seek() failed, so pass an intentionally erroneous value
|
|
||||||
// into DidSeek().
|
|
||||||
DidSeek(-1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
|
|
||||||
// the value that would mean seek success. This way we skip the code
|
|
||||||
// handling seek failure.
|
|
||||||
DidSeek(byte_range_.first_byte_position());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void URLRequestAsarJob::DidSeek(int64_t result) {
|
void URLRequestAsarJob::DidSeek(int64_t result) {
|
||||||
if (type_ == TYPE_ASAR) {
|
if (result != seek_offset_) {
|
||||||
if (result != static_cast<int64_t>(file_info_.offset)) {
|
NotifyStartError(
|
||||||
NotifyStartError(
|
net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
||||||
net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
||||||
net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (result != byte_range_.first_byte_position()) {
|
|
||||||
NotifyStartError(
|
|
||||||
net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
|
||||||
net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
set_expected_content_size(remaining_bytes_);
|
set_expected_content_size(remaining_bytes_);
|
||||||
NotifyHeadersComplete();
|
NotifyHeadersComplete();
|
||||||
|
|
|
@ -118,6 +118,7 @@ class URLRequestAsarJob : public net::URLRequestJob {
|
||||||
|
|
||||||
net::HttpByteRange byte_range_;
|
net::HttpByteRange byte_range_;
|
||||||
int64_t remaining_bytes_;
|
int64_t remaining_bytes_;
|
||||||
|
int64_t seek_offset_;
|
||||||
|
|
||||||
net::Error range_parse_result_;
|
net::Error range_parse_result_;
|
||||||
|
|
||||||
|
|
|
@ -848,6 +848,36 @@ describe('asar package', function () {
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('loads video tag in html', function (done) {
|
||||||
|
this.timeout(20000)
|
||||||
|
|
||||||
|
after(function () {
|
||||||
|
ipcMain.removeAllListeners('asar-video')
|
||||||
|
return closeWindow(w).then(function () { w = null })
|
||||||
|
})
|
||||||
|
|
||||||
|
var w = new BrowserWindow({
|
||||||
|
show: false,
|
||||||
|
width: 400,
|
||||||
|
height: 400
|
||||||
|
})
|
||||||
|
var p = path.resolve(fixtures, 'asar', 'video.asar', 'index.html')
|
||||||
|
var u = url.format({
|
||||||
|
protocol: 'file',
|
||||||
|
slashed: true,
|
||||||
|
pathname: p
|
||||||
|
})
|
||||||
|
w.loadURL(u)
|
||||||
|
ipcMain.on('asar-video', function (event, message, error) {
|
||||||
|
if (message === 'ended') {
|
||||||
|
assert(!error)
|
||||||
|
done()
|
||||||
|
} else if (message === 'error') {
|
||||||
|
done(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('original-fs module', function () {
|
describe('original-fs module', function () {
|
||||||
|
|
BIN
spec/fixtures/asar/video.asar
vendored
Normal file
BIN
spec/fixtures/asar/video.asar
vendored
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue