从管道 HTTP 流中删除 headers
Remove headers from a piped HTTP stream
举一个我想要实现的简短示例,假设我们有一个 HTTP 服务器已经在处理给定的请求:
require('http').createServer(function(req, res) {
var payload = new Buffer('Hello World\n', 'utf8');
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': payload.length,
'Connection': 'Keep-Alive'
});
res.end(payload);
}).listen(8888);
现在,考虑接收请求的第二个 HTTP 服务器的存在,并且为了服务它需要调用第一个服务器。 (例如,当我们有一个需要调用给定 RESTful 端点的 Web 应用程序时的经典场景)。
var http = require('http');
http.createServer(function(req, res) {
var fwdrq = http.request({
hostname: 'localhost',
port: 8888,
path: '/',
method: 'GET'
});
fwdrq.on('response',function(response){
response.pipe(res);
});
fwdrq.on('error', function(error){
console.log(error);
});
fwdrq.end();
}).listen(9999);
现在,我喜欢将原始请求与第二台服务器完成的内部请求的响应进行管道传输的想法,这非常方便,而这正是我所需要的。但在我将响应发送回客户端之前,我想有机会从第一台服务器发送的响应中删除任何 hop-by-hop headers 。我肯定想要整个有效负载,并且我想要它的响应中的一些 headers,但肯定不是全部。
例如,我想避免发送 headers,例如 Proxy-Authenticate 或 Connection,或任何被视为 hop-by-hop headers 的人。另外,如果我希望我的第二台服务器以这种方式运行,我想考虑不发送回 keep alives 的可能性,等等。
我知道如何在传递响应之前添加 headers,但是一旦通过管道传输,我不知道如何从正在传输的响应中删除 headers。
不要误会我的意思,我知道我可以通过订阅事件然后自己构建响应来做到这一点,但我想知道如果我正在管道响应,这是否仍然可行。
有人知道如何完成这个技巧吗?
根据我使用 pipe 进行的测试,只有有效负载被传输,而不是 headers。
打开 Chrome 开发人员控制台(Firebug 在 Firefox 上,在桌面上打开 Fiddler)并查看从服务器返回的 HTTP 响应。来自上游服务器的 headers 不会传回最终用户。如果您在代理服务器上手动添加 headers,您会看到它们确实显示给最终用户。
var http = require('http');
http.createServer(function(req, res) {
var payload = new Buffer('Hello World\n', 'utf8');
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': payload.length,
'Connection': 'Keep-Alive',
'UpstreamHeader': 'Test'
});
res.end(payload);
}).listen(8888);
http.createServer(function(req, res) {
var fwdrq = http.request({
hostname: 'localhost',
port: 8888,
path: '/',
method: 'GET'
});
//Uncomment the lines below to add headers from the proxy server
/*res.writeHead(200, {
'ProxyHeader': 'Test'
});*/
fwdrq.on('response', function(response) {
response.pipe(res);
});
fwdrq.on('error', function(error) {
console.log(error);
});
fwdrq.end();
}).listen(80);
举一个我想要实现的简短示例,假设我们有一个 HTTP 服务器已经在处理给定的请求:
require('http').createServer(function(req, res) {
var payload = new Buffer('Hello World\n', 'utf8');
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': payload.length,
'Connection': 'Keep-Alive'
});
res.end(payload);
}).listen(8888);
现在,考虑接收请求的第二个 HTTP 服务器的存在,并且为了服务它需要调用第一个服务器。 (例如,当我们有一个需要调用给定 RESTful 端点的 Web 应用程序时的经典场景)。
var http = require('http');
http.createServer(function(req, res) {
var fwdrq = http.request({
hostname: 'localhost',
port: 8888,
path: '/',
method: 'GET'
});
fwdrq.on('response',function(response){
response.pipe(res);
});
fwdrq.on('error', function(error){
console.log(error);
});
fwdrq.end();
}).listen(9999);
现在,我喜欢将原始请求与第二台服务器完成的内部请求的响应进行管道传输的想法,这非常方便,而这正是我所需要的。但在我将响应发送回客户端之前,我想有机会从第一台服务器发送的响应中删除任何 hop-by-hop headers 。我肯定想要整个有效负载,并且我想要它的响应中的一些 headers,但肯定不是全部。
例如,我想避免发送 headers,例如 Proxy-Authenticate 或 Connection,或任何被视为 hop-by-hop headers 的人。另外,如果我希望我的第二台服务器以这种方式运行,我想考虑不发送回 keep alives 的可能性,等等。
我知道如何在传递响应之前添加 headers,但是一旦通过管道传输,我不知道如何从正在传输的响应中删除 headers。
不要误会我的意思,我知道我可以通过订阅事件然后自己构建响应来做到这一点,但我想知道如果我正在管道响应,这是否仍然可行。
有人知道如何完成这个技巧吗?
根据我使用 pipe 进行的测试,只有有效负载被传输,而不是 headers。
打开 Chrome 开发人员控制台(Firebug 在 Firefox 上,在桌面上打开 Fiddler)并查看从服务器返回的 HTTP 响应。来自上游服务器的 headers 不会传回最终用户。如果您在代理服务器上手动添加 headers,您会看到它们确实显示给最终用户。
var http = require('http');
http.createServer(function(req, res) {
var payload = new Buffer('Hello World\n', 'utf8');
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': payload.length,
'Connection': 'Keep-Alive',
'UpstreamHeader': 'Test'
});
res.end(payload);
}).listen(8888);
http.createServer(function(req, res) {
var fwdrq = http.request({
hostname: 'localhost',
port: 8888,
path: '/',
method: 'GET'
});
//Uncomment the lines below to add headers from the proxy server
/*res.writeHead(200, {
'ProxyHeader': 'Test'
});*/
fwdrq.on('response', function(response) {
response.pipe(res);
});
fwdrq.on('error', function(error) {
console.log(error);
});
fwdrq.end();
}).listen(80);