fix: registerStreamProtocol callback with large chunks (#16532)

This commit is contained in:
Jeremy Apthorp 2019-01-25 10:57:26 -08:00 committed by GitHub
parent 8396a2d504
commit 63bf370cc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -141,6 +141,7 @@ void URLRequestStreamJob::StartAsync(
} }
void URLRequestStreamJob::OnData(std::vector<char>&& buffer) { // NOLINT void URLRequestStreamJob::OnData(std::vector<char>&& buffer) { // NOLINT
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (write_buffer_.empty()) { if (write_buffer_.empty()) {
// Quick branch without copying. // Quick branch without copying.
write_buffer_ = std::move(buffer); write_buffer_ = std::move(buffer);
@ -175,7 +176,7 @@ void URLRequestStreamJob::OnError(int error) {
int URLRequestStreamJob::ReadRawData(net::IOBuffer* dest, int dest_size) { int URLRequestStreamJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
response_start_time_ = base::TimeTicks::Now(); response_start_time_ = base::TimeTicks::Now();
if (ended_) if (ended_ && write_buffer_.empty())
return 0; return 0;
// When write_buffer_ is empty, there is no data valable yet, we have to save // When write_buffer_ is empty, there is no data valable yet, we have to save

View file

@ -654,6 +654,30 @@ describe('protocol module', () => {
}) })
}) })
}) })
it('can handle large responses', async () => {
const data = Buffer.alloc(128 * 1024)
const handler = (request, callback) => {
callback(getStream(data.length, data))
}
await new Promise((resolve, reject) => {
protocol.registerStreamProtocol(protocolName, handler, err => {
if (err) return reject(err)
resolve()
})
})
const r = await new Promise((resolve, reject) => {
$.ajax({
url: protocolName + '://fake-host',
cache: false,
success: resolve,
error: (xhr, errorType, error) => {
reject(error || new Error(`Request failed: ${xhr.status}`))
}
})
})
assert.strictEqual(r.length, data.length)
})
}) })
describe('protocol.isProtocolHandled', (done) => { describe('protocol.isProtocolHandled', (done) => {