Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: registerStreamProtocol callback with large chunks (backport: 4-0-x) #16540

Merged
merged 2 commits into from Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion atom/browser/net/url_request_stream_job.cc
Expand Up @@ -139,6 +139,7 @@ void URLRequestStreamJob::StartAsync(
}

void URLRequestStreamJob::OnData(std::vector<char>&& buffer) { // NOLINT
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (write_buffer_.empty()) {
// Quick branch without copying.
write_buffer_ = std::move(buffer);
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions spec/api-protocol-spec.js
Expand Up @@ -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', () => {
Expand Down