|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// A server may send the final DATA, trailers with END_STREAM and |
| 4 | +// RST_STREAM(NO_ERROR) together while the request is still open (RFC 9113 |
| 5 | +// section 8.1), as gRPC servers do. A client that has paused partway through |
| 6 | +// the response must still be able to read the rest, and likewise for |
| 7 | +// RST_STREAM(CANCEL), which should only affect the request. |
| 8 | +// This closely reproduces https://github.com/nodejs/node/issues/66693 |
| 9 | +// which requires some raw frame writes to match the reset behaviour. |
| 10 | + |
| 11 | +const common = require('../common'); |
| 12 | +if (!common.hasCrypto) |
| 13 | + common.skip('missing crypto'); |
| 14 | + |
| 15 | +const assert = require('assert'); |
| 16 | +const http2 = require('http2'); |
| 17 | +const net = require('net'); |
| 18 | +const { once } = require('events'); |
| 19 | +const { setImmediate: setImmediateAsync } = require('timers/promises'); |
| 20 | +const h2test = require('../common/http2'); |
| 21 | + |
| 22 | +const first = Buffer.from('first chunk'); |
| 23 | +const last = Buffer.from('last chunk'); |
| 24 | +// HPACK: indexed :status 200. |
| 25 | +const responseHeaders = Buffer.from([0x88]); |
| 26 | +// HPACK: literal without indexing, grpc-status: 0. |
| 27 | +const responseTrailers = Buffer.concat([ |
| 28 | + Buffer.from([0x00, 11]), Buffer.from('grpc-status'), |
| 29 | + Buffer.from([1]), Buffer.from('0'), |
| 30 | +]); |
| 31 | +const streamId = 1; |
| 32 | +const startRequest = 'start of request'; |
| 33 | +const continueRequest = 'more of request'; |
| 34 | + |
| 35 | +async function startServer(code) { |
| 36 | + const server = net.createServer(common.mustCall((socket) => { |
| 37 | + // Client request -> response frames flow for the server: |
| 38 | + const replies = [ |
| 39 | + [startRequest, [ |
| 40 | + new h2test.HeadersFrame(streamId, responseHeaders), |
| 41 | + new h2test.DataFrame(streamId, first), |
| 42 | + ]], |
| 43 | + [continueRequest, [ |
| 44 | + new h2test.DataFrame(streamId, last), |
| 45 | + new h2test.HeadersFrame(streamId, responseTrailers, 0, true), |
| 46 | + new h2test.RstStreamFrame(streamId, code), |
| 47 | + ]], |
| 48 | + ]; |
| 49 | + |
| 50 | + let received = Buffer.alloc(0); |
| 51 | + socket.on('error', () => {}); |
| 52 | + socket.on('data', (chunk) => { |
| 53 | + received = Buffer.concat([received, chunk]); |
| 54 | + while (replies.length > 0 && received.includes(replies[0][0])) { |
| 55 | + const [, frames] = replies.shift(); |
| 56 | + socket.write(Buffer.concat(frames.map((frame) => frame.data))); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // Write settings to start the session: |
| 61 | + socket.write(new h2test.SettingsFrame().data); |
| 62 | + socket.write(new h2test.SettingsFrame(true).data); |
| 63 | + })); |
| 64 | + server.listen(0); |
| 65 | + await once(server, 'listening'); |
| 66 | + return server; |
| 67 | +} |
| 68 | + |
| 69 | +(async () => { |
| 70 | + for (const code of [ |
| 71 | + http2.constants.NGHTTP2_NO_ERROR, |
| 72 | + http2.constants.NGHTTP2_CANCEL, |
| 73 | + ]) { |
| 74 | + const server = await startServer(code); |
| 75 | + const session = http2.connect(`http://localhost:${server.address().port}`); |
| 76 | + const request = session.request({ ':method': 'POST' }); |
| 77 | + request.on('error', common.mustNotCall()); |
| 78 | + request.on('response', common.mustCall()); |
| 79 | + |
| 80 | + // Read the first response chunk, then stop reading. |
| 81 | + request.write(startRequest); |
| 82 | + const [chunk] = await once(request, 'data'); |
| 83 | + assert.deepStrictEqual(chunk, first); |
| 84 | + request.pause(); |
| 85 | + |
| 86 | + // Trigger the last response chunk + trailers + RST_STREAM. |
| 87 | + request.write(continueRequest); |
| 88 | + const [trailers] = await once(request, 'trailers'); |
| 89 | + assert.strictEqual(trailers['grpc-status'], '0'); |
| 90 | + |
| 91 | + while (!request.closed) |
| 92 | + await setImmediateAsync(); |
| 93 | + assert.strictEqual(request.rstCode, code); |
| 94 | + assert.strictEqual(request.destroyed, false); |
| 95 | + |
| 96 | + // Check that we can still read the response after the RST: |
| 97 | + const chunks = []; |
| 98 | + request.on('data', (chunk) => chunks.push(chunk)); |
| 99 | + request.on('end', common.mustCall()); |
| 100 | + request.resume(); |
| 101 | + await once(request, 'close'); |
| 102 | + assert.deepStrictEqual(Buffer.concat(chunks), last); |
| 103 | + |
| 104 | + session.destroy(); |
| 105 | + server.close(); |
| 106 | + } |
| 107 | +})().then(common.mustCall()); |
0 commit comments