diff --git a/src/js/node/_http_server.ts b/src/js/node/_http_server.ts index edd958d037..b2d6c2aab2 100644 --- a/src/js/node/_http_server.ts +++ b/src/js/node/_http_server.ts @@ -1408,6 +1408,12 @@ const NodeHTTPServerSocket = class Socket extends Duplex { return this; } + setEncoding(_encoding) { + const err = new Error("Changing the socket encoding is not allowed per RFC7230 Section 3."); + err.code = "ERR_HTTP_SOCKET_ENCODING"; + throw err; + } + unref() { return this; } diff --git a/test/js/node/parallel/test-http-socket-encoding-error.js b/test/js/node/parallel/test-http-socket-encoding-error.js new file mode 100644 index 0000000000..66893e604c --- /dev/null +++ b/test/js/node/parallel/test-http-socket-encoding-error.js @@ -0,0 +1,26 @@ +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +const server = http.createServer().listen(0, connectToServer); + +server.on('connection', common.mustCall((socket) => { + assert.throws( + () => { + socket.setEncoding(''); + }, + { + code: 'ERR_HTTP_SOCKET_ENCODING', + name: 'Error', + message: 'Changing the socket encoding is not allowed per RFC7230 Section 3.' + } + ); + + socket.end(); +})); + +function connectToServer() { + const client = new http.Agent().createConnection(this.address().port, () => { + client.end(); + }).on('end', () => server.close()); +}