From fc4bc62f5ee29d107c069a089be30f6b7b985fa1 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Wed, 23 Nov 2016 15:01:03 +0000 Subject: [PATCH] Adds a setConfig option to hoxy By allowing the user to setConfig, they can customise the request validProtocols, removeHeaders and nonEntityMethods with custom values. --- src/main.js | 2 ++ src/request.js | 36 ++++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main.js b/src/main.js index cd7d0f5..21a2f47 100644 --- a/src/main.js +++ b/src/main.js @@ -4,10 +4,12 @@ */ import Proxy from './proxy' +import { setConfig } from './request' export default { Proxy: Proxy, createServer: function(opts) { return new Proxy(opts) }, + setConfig, } diff --git a/src/request.js b/src/request.js index 84f805b..970fabc 100644 --- a/src/request.js +++ b/src/request.js @@ -9,21 +9,25 @@ import url from 'url' import querystring from 'querystring' import assert from 'assert' -let validProtocols = { - 'http:': true, - 'https:': true, +let config = { + validProtocols: { + 'http:': true, + 'https:': true, + }, + removeHeaders: { + 'accept-encoding': true, // until proxy handles gzip + 'proxy-connection': true, + 'proxy-authorization': true, + }, + nonEntityMethods: { + GET: true, + HEAD: true, + TRACE: true, + }, } -let removeHeaders = { - 'accept-encoding': true, // until proxy handles gzip - 'proxy-connection': true, - 'proxy-authorization': true, -} - -let nonEntityMethods = { - GET: true, - HEAD: true, - TRACE: true, +export function setConfig(_config) { + config = _config; } /** @@ -44,7 +48,7 @@ export default class Request extends Body { } set protocol(protocol){ - if (!validProtocols.hasOwnProperty(protocol)) { + if (!config.validProtocols.hasOwnProperty(protocol)) { throw new Error('invalid protocol: ' + protocol) // TODO: test this } this._setRawDataItem('protocol', protocol) @@ -211,7 +215,7 @@ export default class Request extends Body { // TODO: emit debug log events for things that are changed. _finalize(){ - if (nonEntityMethods.hasOwnProperty(this.method)) { + if (config.nonEntityMethods.hasOwnProperty(this.method)) { this.string = '' // TODO: test } if (!this._source){ @@ -230,7 +234,7 @@ export default class Request extends Body { Object.keys(this.headers).forEach(name => { // TODO: test - if (removeHeaders.hasOwnProperty(name)) { + if (config.removeHeaders.hasOwnProperty(name)) { delete this.headers[name] } else if (this.headers[name] === undefined){ delete this.headers[name]