Skip to content

Commit

Permalink
fix: remove content-length header
Browse files Browse the repository at this point in the history
This would cause some forwarding to hang
  • Loading branch information
remy committed Aug 25, 2016
1 parent 022d5c5 commit ddf59f1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const replace = require('../replace-vars');
// reads config that defines
module.exports = ruleSource => {
const debug = require('debug')('broker:' + (process.env.BROKER_TYPE || 'filter'));
debug('loading new rules');

let rules = [];
const config = require('../config');
Expand All @@ -21,10 +20,13 @@ module.exports = ruleSource => {
}
}


if (!Array.isArray(rules)) {
throw new Error(`unsupported type (${typeof rules}) for filter rules`);
}

debug('loading %s new rules', rules.length);

// array of entries with
const tests = rules.map(entry => {
const keys = [];
Expand Down
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const app = module.exports = {

function main({ port, client } = {}) {
// note: the config is loaded in the main function to allow us to mock in tests
if (process.env.TAP) {
delete require.cache[require.resolve('./config.js')];
}
const config = require('./config');
if (client === undefined) {
client = !!config.brokerUrl;
Expand All @@ -17,9 +20,13 @@ function main({ port, client } = {}) {
const method = client ? 'client' : 'server';
process.env.BROKER_TYPE = method;

const debug = require('debug')(`broker:${method}`);

debug(config.accept);

let filters = {};
if (config.accept) {
require('debug')(`broker:${method}`)('loading rules from %s', config.accept);
debug('loading rules from %s', config.accept);
filters = require(path.resolve(process.cwd(), config.accept));
}

Expand Down
12 changes: 8 additions & 4 deletions lib/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function responseHandler(filterRules) {
if (error) {
debug('blocked %s %s', method, url);
return emit({
status: 400,
status: 401,
body: error.message,
});
}
Expand Down Expand Up @@ -88,9 +88,13 @@ function responseHandler(filterRules) {
}
}

// TEST remove x-forwarded headers
delete headers['x-forwarded-for'];
delete headers['x-forwarded-proto'];
// remove headers that we don't want to relay
// (because they corrupt the request)
[
'x-forwarded-for',
'x-forwarded-proto',
'content-length',
].map(_ => delete headers[_]);

request({
url: result,
Expand Down
Empty file removed test/fixtures/server/.keep
Empty file.
9 changes: 4 additions & 5 deletions test/functional/client-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const request = require('request');
const app = require('../../lib');
const root = __dirname;

const { port, localPort: servicePort, resetConfig } = require('../utils')(tap);
const { port, localPort: servicePort } = require('../utils')(tap);

test('internal sends request through client', t => {

Expand All @@ -22,18 +22,17 @@ test('internal sends request through client', t => {
process.env.BROKER_URL = `http://localhost:${serverPort}`;
process.env.BROKER_ID = '12345';
process.env.BROKER_TYPE = 'client';
const localPort = port();
const clientPort = port();
// invalidate the config require
resetConfig();
const client = app.main({ port: localPort });
const client = app.main({ port: clientPort });

// wait for the client to successfully connect to the server and identify itself
server.io.once('connection', socket => {
socket.once('identify', () => {
t.plan(2);

t.test('client can forward requests FROM internal service', t => {
const url = `http://localhost:${localPort}/service/test1`;
const url = `http://localhost:${clientPort}/service/test1`;
request({ url, method: 'get', json: true }, (err, res) => {
t.equal(res.statusCode, 200, '200 statusCode');
t.equal(res.body, 'test1', 'body correct');
Expand Down
4 changes: 1 addition & 3 deletions test/functional/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ test('simple end to end proxying', t => {
process.env.BROKER_URL = `http://localhost:${serverPort}`;
process.env.BROKER_ID = '12345';
process.env.BROKER_TYPE = 'client';
// invalidate the config require
delete require.cache[require.resolve(__dirname + '/../../lib/config.js')];
const client = app.main({ port: port() });

// wait for the client to successfully connect to the server and identify itself
Expand All @@ -53,7 +51,7 @@ test('simple end to end proxying', t => {
const url = `http://localhost:${serverPort}/broker/${id}/magic-path/x/random.json`;
request({ url, 'method': 'post', json: true }, (err, res, body) => {
t.equal(res.statusCode, 401, '401 statusCode');
t.match(body, /blocked/, '"blocked" body: ' + body);
t.equal(body, 'blocked', '"blocked" body: ' + body);
t.end();
});
});
Expand Down

0 comments on commit ddf59f1

Please sign in to comment.