From cf079f6c43f6800a9b931088d77826e211512a75 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Fri, 25 Jan 2019 10:59:08 -0800 Subject: [PATCH] fix: registerStreamProtocol callback with large chunks (backport: 4-0-x) (#16540) --- atom/browser/net/url_request_stream_job.cc | 3 ++- spec/api-protocol-spec.js | 24 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/atom/browser/net/url_request_stream_job.cc b/atom/browser/net/url_request_stream_job.cc index aac30522964a1..8be31c146c435 100644 --- a/atom/browser/net/url_request_stream_job.cc +++ b/atom/browser/net/url_request_stream_job.cc @@ -139,6 +139,7 @@ void URLRequestStreamJob::StartAsync( } void URLRequestStreamJob::OnData(std::vector&& buffer) { // NOLINT + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); if (write_buffer_.empty()) { // Quick branch without copying. write_buffer_ = std::move(buffer); @@ -173,7 +174,7 @@ void URLRequestStreamJob::OnError(int error) { int URLRequestStreamJob::ReadRawData(net::IOBuffer* dest, int dest_size) { response_start_time_ = base::TimeTicks::Now(); - if (ended_) + if (ended_ && write_buffer_.empty()) return 0; // When write_buffer_ is empty, there is no data valable yet, we have to save diff --git a/spec/api-protocol-spec.js b/spec/api-protocol-spec.js index 73986af442fd8..f2d9587067c0f 100644 --- a/spec/api-protocol-spec.js +++ b/spec/api-protocol-spec.js @@ -602,6 +602,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', () => {