Skip to content

Commit

Permalink
fix: Ignore searchDomain for localhost and IP
Browse files Browse the repository at this point in the history
Backport of 8cfb230
  • Loading branch information
Jan Krems committed Mar 29, 2018
1 parent 685d29a commit 7030693
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
25 changes: 21 additions & 4 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var Url, cleanProperty, isObject, merge, mergeSafe, reduce, ref, replacePathParams;
var IPv4, Url, buildHostname, canApplySearchDomain, cleanProperty, isObject, merge, mergeSafe, reduce, ref, replacePathParams;

ref = require('lodash'), merge = ref.merge, isObject = ref.isObject, reduce = ref.reduce;

Expand Down Expand Up @@ -91,6 +91,25 @@ this.parseDefaults = function(rawConfig, serviceName) {
};
};

IPv4 = /^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$/;

canApplySearchDomain = function(hostname) {
if (hostname === 'localhost') {
return false;
}
if (hostname[hostname.length - 1] === '.') {
return false;
}
return hostname.indexOf(':') === -1 && !IPv4.test(hostname);
};

buildHostname = function(hostname, searchDomain) {
if (searchDomain && canApplySearchDomain(hostname)) {
return hostname + "." + searchDomain + ".";
}
return hostname;
};

this.applyBaseUrl = function(baseUrl, options) {
var basePath, hostname, pathname, port, protocol, query, search, searchDomain, uri;
uri = options.uri;
Expand All @@ -106,9 +125,7 @@ this.applyBaseUrl = function(baseUrl, options) {
query = uri.query, search = uri.search;
searchDomain = options.searchDomain;
delete options.searchDomain;
if (searchDomain && hostname && hostname[hostname.length - 1] !== '.') {
hostname += "." + searchDomain + ".";
}
hostname = buildHostname(hostname, searchDomain);
uri = Url.format({
protocol: protocol,
hostname: hostname,
Expand Down
15 changes: 13 additions & 2 deletions src/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ cleanProperty = (obj, value, key) ->
delete defaults.endpointDefaults
{ defaults, endpointDefaults }

IPv4 = /^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$/
canApplySearchDomain = (hostname) ->
return false if hostname == 'localhost'
return false if hostname[hostname.length - 1] == '.'
# A hostname shouldn't contain ":" (IPv6 adddress) or be an IPv4 address
hostname.indexOf(':') == -1 && !IPv4.test(hostname)

buildHostname = (hostname, searchDomain) ->
if searchDomain && canApplySearchDomain(hostname)
return "#{hostname}.#{searchDomain}."
hostname

@applyBaseUrl = (baseUrl, options) ->
uri = options.uri
uri = Url.parse uri if 'string' == typeof uri
Expand All @@ -88,8 +100,7 @@ cleanProperty = (obj, value, key) ->

{searchDomain} = options
delete options.searchDomain # in case request starts looking at it
if searchDomain && hostname && hostname[hostname.length - 1] != '.'
hostname += ".#{searchDomain}."
hostname = buildHostname(hostname, searchDomain)

uri = Url.format {protocol, hostname, port, pathname, query, search}
options.uri = replacePathParams(uri, options.pathParams)
Expand Down
16 changes: 16 additions & 0 deletions test/option_mappers.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ describe 'option mappers', ->

client.foo.bar '123'

it 'never appends the searchDomain to localhost', (done) ->
client = new MyApi myApi: { baseUrl: 'http://localhost/v2', searchDomain: 'bar123' }
client.hub = fetch: (opts, cb) ->
assert.equal 'http://localhost/v2/foo/bars/123', opts.uri
done()

client.foo.bar '123'

it 'never appends the searchDomain to an IP address', (done) ->
client = new MyApi myApi: { baseUrl: 'http://127.0.0.1:3000/v2', searchDomain: 'bar123' }
client.hub = fetch: (opts, cb) ->
assert.equal 'http://127.0.0.1:3000/v2/foo/bars/123', opts.uri
done()

client.foo.bar '123'

describe 'onlyGlobal', ->
it 'does not disturb the endpoint function', (done) ->
myApi_onlyGlobal.hub = fetch: (opts, cb) ->
Expand Down

0 comments on commit 7030693

Please sign in to comment.