Skip to content

Commit

Permalink
Handle pipe replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Jun 18, 2024
1 parent 551b6b2 commit 6fe45ac
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var http = process.env.NXT_UNIT_INIT ? require('unit-http') : require('http'),
common = require('../common');
var http = process.env.NXT_UNIT_INIT ? require('unit-http') : require('http'),
common = require('../common');

/*!
* Array of passes.
Expand All @@ -26,7 +26,7 @@ module.exports = {
* @api private
*/

checkMethodAndHeader : function checkMethodAndHeader(req, socket) {
checkMethodAndHeader: function checkMethodAndHeader(req, socket) {
if (req.method !== 'GET' || !req.headers.upgrade) {
socket.destroy();
return true;
Expand All @@ -48,16 +48,16 @@ module.exports = {
* @api private
*/

XHeaders : function XHeaders(req, socket, options) {
if(!options.xfwd) return;
XHeaders: function XHeaders(req, socket, options) {
if (!options.xfwd) return;

var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : common.getPort(req),
for: req.connection.remoteAddress || req.socket.remoteAddress,
port: common.getPort(req),
proto: 'ws'
};

['for', 'port', 'proto'].forEach(function(header) {
['for', 'port', 'proto'].forEach(function (header) {
req.headers['x-forwarded-' + header] =
(req.headers['x-forwarded-' + header] || '') +
(req.headers['x-forwarded-' + header] ? ',' : '') +
Expand All @@ -75,9 +75,9 @@ module.exports = {
*
* @api private
*/
stream : function stream(req, socket, options, head, server, clb) {
stream: function stream(req, socket, options, head, server, clb) {

var createHttpHeader = function(line, headers) {
var createHttpHeader = function (line, headers) {
return Object.keys(headers).reduce(function (head, key) {
var value = headers[key];

Expand All @@ -91,7 +91,7 @@ module.exports = {
}
return head;
}, [line])
.join('\r\n') + '\r\n\r\n';
.join('\r\n') + '\r\n\r\n';
}

common.setupSocket(socket);
Expand All @@ -116,7 +116,7 @@ module.exports = {
}
});

proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
proxyReq.on('upgrade', function (proxyRes, proxySocket, proxyHead) {
proxySocket.on('error', onOutgoingError);

// Allow us to listen when the websocket has completed
Expand All @@ -141,7 +141,40 @@ module.exports = {
//
socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));

proxySocket.pipe(socket).pipe(proxySocket);
// UNIT DOESNOT SUPPORT THISSSS!!!!!!!!!!!!!!!!!
// proxySocket.pipe(socket).pipe(proxySocket);

// BEGIN EQUIVALENT
// Manually handle data events
proxySocket.on('data', (chunk) => {
socket.write(chunk);
});

socket.on('data', (chunk) => {
proxySocket.write(chunk);
});

// Handle end events to close the sockets properly
proxySocket.on('end', () => {
socket.end();
});

socket.on('end', () => {
proxySocket.end();
});

// Handle error events to clean up
proxySocket.on('error', (err) => {
console.error('Proxy socket error:', err);
socket.end();
});

socket.on('error', (err) => {
console.error('Socket error:', err);
proxySocket.end();
});
// END EQUIVALENT


server.emit('open', proxySocket);
server.emit('proxySocket', proxySocket); //DEPRECATED.
Expand Down

0 comments on commit 6fe45ac

Please sign in to comment.