|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Regression test for https://github.com/nodejs/node/issues/66525. |
| 4 | +// A compat API response to a request without a body must end with |
| 5 | +// END_STREAM, even if the connection flow-control window is exhausted |
| 6 | +// when the empty END_STREAM DATA frame is submitted. Previously the |
| 7 | +// stream was reset with RST_STREAM(NO_ERROR) and the queued END_STREAM |
| 8 | +// frame was dropped. |
| 9 | + |
| 10 | +const common = require('../common'); |
| 11 | +if (!common.hasCrypto) |
| 12 | + common.skip('missing crypto'); |
| 13 | + |
| 14 | +const assert = require('assert'); |
| 15 | +const http2 = require('http2'); |
| 16 | +const net = require('net'); |
| 17 | + |
| 18 | +const kData = 0; |
| 19 | +const kHeaders = 1; |
| 20 | +const kRstStream = 3; |
| 21 | +const kSettings = 4; |
| 22 | +const kWindowUpdate = 8; |
| 23 | +const kFlagEndStream = 0x1; |
| 24 | +const kFlagAck = 0x1; |
| 25 | +const kFlagsEndStreamEndHeaders = 0x5; |
| 26 | + |
| 27 | +function frame(type, flags, id, payload = Buffer.alloc(0)) { |
| 28 | + const header = Buffer.alloc(9); |
| 29 | + header.writeUIntBE(payload.length, 0, 3); |
| 30 | + header[3] = type; |
| 31 | + header[4] = flags; |
| 32 | + header.writeUInt32BE(id, 5); |
| 33 | + return Buffer.concat([header, payload]); |
| 34 | +} |
| 35 | + |
| 36 | +// Minimal HPACK encoding of a GET request for `path`: indexed :method GET, |
| 37 | +// indexed :scheme http, and literal :path and :authority without Huffman. |
| 38 | +function requestHeaders(path) { |
| 39 | + const literal = (index, value) => |
| 40 | + Buffer.concat([Buffer.from([index, value.length]), Buffer.from(value)]); |
| 41 | + return Buffer.concat([ |
| 42 | + Buffer.from([0x82, 0x86]), |
| 43 | + literal(0x04, path), |
| 44 | + literal(0x01, 'localhost'), |
| 45 | + ]); |
| 46 | +} |
| 47 | + |
| 48 | +let client; |
| 49 | +let endStreamReceived = false; |
| 50 | +let serverStreamClosed = false; |
| 51 | + |
| 52 | +function maybeFinish() { |
| 53 | + if (endStreamReceived && serverStreamClosed) { |
| 54 | + client.destroy(); |
| 55 | + server.close(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +const server = http2.createServer(); |
| 60 | + |
| 61 | +server.on('stream', common.mustCall((stream, headers) => { |
| 62 | + if (headers[':path'] === '/small') { |
| 63 | + // The stream must still close on its own once END_STREAM has been sent. |
| 64 | + stream.on('close', common.mustCall(() => { |
| 65 | + serverStreamClosed = true; |
| 66 | + maybeFinish(); |
| 67 | + })); |
| 68 | + } |
| 69 | +}, 2)); |
| 70 | + |
| 71 | +const responses = {}; |
| 72 | +server.on('request', common.mustCall((req, res) => { |
| 73 | + responses[req.url] = res; |
| 74 | + if (!responses['/small'] || !responses['/big']) return; |
| 75 | + // After /small's body has been written, let /big exhaust the connection |
| 76 | + // window before /small's END_STREAM frame is submitted. |
| 77 | + responses['/small'].stream.once('wantTrailers', () => { |
| 78 | + responses['/big'].end('y'.repeat(1_000_000)); |
| 79 | + }); |
| 80 | + responses['/small'].end('x'.repeat(1000)); |
| 81 | +}, 2)); |
| 82 | + |
| 83 | +server.listen(0, common.mustCall(() => { |
| 84 | + // A raw client that does not send WINDOW_UPDATE until later, so the |
| 85 | + // connection window stays exhausted once /big has drained it. |
| 86 | + client = net.connect(server.address().port, common.mustCall(() => { |
| 87 | + client.write(Buffer.concat([ |
| 88 | + Buffer.from('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'), |
| 89 | + frame(kSettings, 0, 0), |
| 90 | + frame(kHeaders, kFlagsEndStreamEndHeaders, 1, requestHeaders('/big')), |
| 91 | + frame(kHeaders, kFlagsEndStreamEndHeaders, 3, requestHeaders('/small')), |
| 92 | + ])); |
| 93 | + })); |
| 94 | + |
| 95 | + let buffered = Buffer.alloc(0); |
| 96 | + client.on('data', common.mustCallAtLeast((chunk) => { |
| 97 | + buffered = Buffer.concat([buffered, chunk]); |
| 98 | + while (buffered.length >= 9 && |
| 99 | + buffered.length >= 9 + buffered.readUIntBE(0, 3)) { |
| 100 | + const length = buffered.readUIntBE(0, 3); |
| 101 | + const type = buffered[3]; |
| 102 | + const flags = buffered[4]; |
| 103 | + const id = buffered.readUInt32BE(5) & 0x7fffffff; |
| 104 | + buffered = buffered.subarray(9 + length); |
| 105 | + |
| 106 | + if (type === kSettings && !(flags & kFlagAck)) |
| 107 | + client.write(frame(kSettings, kFlagAck, 0)); |
| 108 | + if (id !== 3) continue; |
| 109 | + |
| 110 | + assert.ok(type !== kRstStream || endStreamReceived, |
| 111 | + 'stream /small was reset before END_STREAM was sent'); |
| 112 | + if ((type === kData || type === kHeaders) && (flags & kFlagEndStream)) { |
| 113 | + endStreamReceived = true; |
| 114 | + maybeFinish(); |
| 115 | + } |
| 116 | + } |
| 117 | + })); |
| 118 | + |
| 119 | + // Reopen the windows later. A correct server sends /small's END_STREAM then. |
| 120 | + setTimeout(common.mustCall(() => { |
| 121 | + const increment = Buffer.alloc(4); |
| 122 | + increment.writeUInt32BE(10_000_000); |
| 123 | + client.write(Buffer.concat([ |
| 124 | + frame(kWindowUpdate, 0, 0, increment), |
| 125 | + frame(kWindowUpdate, 0, 1, increment), |
| 126 | + ])); |
| 127 | + }), common.platformTimeout(300)); |
| 128 | +})); |
0 commit comments