|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const http2 = require('http2'); |
| 9 | +const { once } = require('events'); |
| 10 | + |
| 11 | +// Queued request bodies must not prevent responses on local streams. |
| 12 | +async function test(bodySize, { |
| 13 | + informational = 0, |
| 14 | + blocked = false, |
| 15 | + trailers = false, |
| 16 | +} = {}) { |
| 17 | + const server = http2.createServer({ |
| 18 | + settings: { initialWindowSize: blocked ? 0 : 65535 }, |
| 19 | + }); |
| 20 | + const sessions = new Set(); |
| 21 | + server.on('session', (session) => sessions.add(session)); |
| 22 | + server.on('stream', (stream, headers) => { |
| 23 | + stream.on('error', () => {}); |
| 24 | + if (headers[':path'] === '/reuse') { |
| 25 | + stream.additionalHeaders({ ':status': 103 }); |
| 26 | + stream.respond({ ':status': 200 }); |
| 27 | + stream.end(); |
| 28 | + } else { |
| 29 | + if (informational) stream.additionalHeaders({ ':status': informational }); |
| 30 | + stream.respond({ ':status': 413 }, { waitForTrailers: trailers }); |
| 31 | + if (trailers) { |
| 32 | + stream.on('wantTrailers', () => stream.sendTrailers({ 'x-trailer': 'x' })); |
| 33 | + } |
| 34 | + stream.end(); |
| 35 | + } |
| 36 | + }); |
| 37 | + server.listen(0); |
| 38 | + await once(server, 'listening'); |
| 39 | + |
| 40 | + const client = http2.connect(`http://localhost:${server.address().port}`, { |
| 41 | + maxSessionMemory: 1, |
| 42 | + }); |
| 43 | + await Promise.all([ |
| 44 | + once(client, 'remoteSettings'), |
| 45 | + once(client, 'localSettings'), |
| 46 | + ]); |
| 47 | + const req = client.request({ ':method': 'POST' }); |
| 48 | + const response = once(req, 'response'); |
| 49 | + const ended = once(req, 'end'); |
| 50 | + const closed = once(req, 'close'); |
| 51 | + req.on('headers', informational ? common.mustCall((headers) => { |
| 52 | + assert.strictEqual(headers[':status'], informational); |
| 53 | + }) : common.mustNotCall()); |
| 54 | + req.on('trailers', trailers ? common.mustCall((headers) => { |
| 55 | + assert.strictEqual(headers['x-trailer'], 'x'); |
| 56 | + }) : common.mustNotCall()); |
| 57 | + req.resume(); |
| 58 | + // Fill the write queue before any response headers can arrive. |
| 59 | + req.end(Buffer.alloc(bodySize)); |
| 60 | + const [[headers]] = await Promise.all([response, ended]); |
| 61 | + assert.strictEqual(headers[':status'], 413); |
| 62 | + req.destroy(); |
| 63 | + await closed; |
| 64 | + // The session must remain usable after canceling the upload. |
| 65 | + const reuse = client.request({ ':path': '/reuse' }); |
| 66 | + reuse.on('headers', common.mustCall()); |
| 67 | + const reusedResponse = once(reuse, 'response'); |
| 68 | + reuse.end(); |
| 69 | + const [reusedHeaders] = await reusedResponse; |
| 70 | + assert.strictEqual(reusedHeaders[':status'], 200); |
| 71 | + const reusedClose = once(reuse, 'close'); |
| 72 | + reuse.close(); |
| 73 | + await reusedClose; |
| 74 | + client.destroy(); |
| 75 | + for (const session of sessions) session.destroy(); |
| 76 | + server.close(); |
| 77 | +} |
| 78 | + |
| 79 | +(async () => { |
| 80 | + const bodySize = 4 * 1024 * 1024; |
| 81 | + await test(bodySize); |
| 82 | + await test(bodySize, { blocked: true }); |
| 83 | + await test(bodySize, { informational: 100, blocked: true }); |
| 84 | + await test(bodySize, { informational: 103, blocked: true }); |
| 85 | + await test(bodySize, { trailers: true, blocked: true }); |
| 86 | + await test(1); |
| 87 | +})().then(common.mustCall()); |
0 commit comments