|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// The free socket data guard in http.Agent only runs when the event loop |
| 4 | +// polls for I/O. If data reaches an idle socket's OS buffer and a request |
| 5 | +// takes that socket before the loop has polled, the guard never sees the data |
| 6 | +// and it would be parsed as the response to that request. The Agent must |
| 7 | +// check for pending data before handing a kept-alive socket to a request. |
| 8 | + |
| 9 | +const common = require('../common'); |
| 10 | +const assert = require('assert'); |
| 11 | +const http = require('http'); |
| 12 | + |
| 13 | +const poison = |
| 14 | + 'HTTP/1.1 200 OK\r\n' + |
| 15 | + 'X-Poisoned: true\r\n' + |
| 16 | + 'Connection: keep-alive\r\n' + |
| 17 | + 'Content-Length: 0\r\n' + |
| 18 | + '\r\n'; |
| 19 | + |
| 20 | +// Keep the event loop from polling until the bytes are in the client's |
| 21 | +// socket buffer, so libuv cannot deliver them to the data guard first. |
| 22 | +function waitForPendingRead(socket) { |
| 23 | + const deadline = Date.now() + common.platformTimeout(5000); |
| 24 | + while (!socket._handle.hasPendingRead()) { |
| 25 | + assert.ok(Date.now() < deadline, 'timed out waiting for pending data'); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function checkResponse(req, expectedBody, cb) { |
| 30 | + req.on('response', common.mustCall((res) => { |
| 31 | + let body = ''; |
| 32 | + res.setEncoding('utf8'); |
| 33 | + res.on('data', (chunk) => { body += chunk; }); |
| 34 | + res.on('end', common.mustCall(() => { |
| 35 | + assert.strictEqual(res.headers['x-poisoned'], undefined); |
| 36 | + assert.strictEqual(body, expectedBody); |
| 37 | + cb?.(res); |
| 38 | + })); |
| 39 | + })); |
| 40 | +} |
| 41 | + |
| 42 | +// A socket taken from the free pool. |
| 43 | +{ |
| 44 | + let serverSocket; |
| 45 | + const server = http.createServer(common.mustCall((req, res) => { |
| 46 | + serverSocket ||= req.socket; |
| 47 | + res.end(req.url); |
| 48 | + }, 2)); |
| 49 | + |
| 50 | + server.listen(0, common.mustCall(() => { |
| 51 | + const agent = new http.Agent({ keepAlive: true }); |
| 52 | + const options = { host: '127.0.0.1', port: server.address().port, agent }; |
| 53 | + const name = agent.getName(options); |
| 54 | + |
| 55 | + const request1 = http.request({ ...options, path: '/request1' }); |
| 56 | + request1.end(); |
| 57 | + checkResponse(request1, '/request1'); |
| 58 | + |
| 59 | + request1.on('close', common.mustCall(() => { |
| 60 | + process.nextTick(common.mustCall(() => { |
| 61 | + assert.strictEqual(agent.freeSockets[name]?.length, 1); |
| 62 | + const freeSocket = agent.freeSockets[name][0]; |
| 63 | + |
| 64 | + serverSocket.write(poison); |
| 65 | + waitForPendingRead(freeSocket); |
| 66 | + |
| 67 | + // Same tick: the loop has not polled since the data arrived. |
| 68 | + const request2 = http.request({ ...options, path: '/request2' }); |
| 69 | + request2.end(); |
| 70 | + |
| 71 | + assert.strictEqual(freeSocket.destroyed, true); |
| 72 | + assert.strictEqual(request2.reusedSocket, false); |
| 73 | + |
| 74 | + checkResponse(request2, '/request2', common.mustCall(() => { |
| 75 | + agent.destroy(); |
| 76 | + server.close(); |
| 77 | + })); |
| 78 | + })); |
| 79 | + })); |
| 80 | + })); |
| 81 | +} |
| 82 | + |
| 83 | +// A released socket handed directly to a queued request. |
| 84 | +{ |
| 85 | + let serverSocket; |
| 86 | + const server = http.createServer(common.mustCall((req, res) => { |
| 87 | + serverSocket ||= req.socket; |
| 88 | + res.end(req.url); |
| 89 | + }, 2)); |
| 90 | + |
| 91 | + server.listen(0, common.mustCall(() => { |
| 92 | + const agent = new http.Agent({ keepAlive: true, maxSockets: 1 }); |
| 93 | + const options = { host: '127.0.0.1', port: server.address().port, agent }; |
| 94 | + const name = agent.getName(options); |
| 95 | + |
| 96 | + const request1 = http.request({ ...options, path: '/request1' }); |
| 97 | + request1.end(); |
| 98 | + const request2 = http.request({ ...options, path: '/request2' }); |
| 99 | + request2.end(); |
| 100 | + assert.strictEqual(agent.requests[name]?.length, 1); |
| 101 | + |
| 102 | + let socket1; |
| 103 | + request1.on('socket', common.mustCall((socket) => { |
| 104 | + socket1 = socket; |
| 105 | + })); |
| 106 | + |
| 107 | + // The socket is released to the queued request on the next tick after |
| 108 | + // 'end', so the loop does not poll in between. |
| 109 | + checkResponse(request1, '/request1', common.mustCall(() => { |
| 110 | + serverSocket.write(poison); |
| 111 | + waitForPendingRead(socket1); |
| 112 | + })); |
| 113 | + |
| 114 | + checkResponse(request2, '/request2', common.mustCall(() => { |
| 115 | + assert.strictEqual(socket1.destroyed, true); |
| 116 | + assert.strictEqual(request2.reusedSocket, false); |
| 117 | + agent.destroy(); |
| 118 | + server.close(); |
| 119 | + })); |
| 120 | + })); |
| 121 | +} |
0 commit comments