Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 752b1e6

Browse files
committed
fix($resource): allow params in hostname (except for IPv6 addresses)
Support for IPv6 addresses (in b643f0d) was too aggressive and broke support for params in the `hostname` part of a URL. This commit restores support for params in the `hostname`, as long as it is not an IPv6 address. Fixes #14542 Closes #14906
1 parent 1102c84 commit 752b1e6

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/ngResource/resource.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ function shallowClearAndCopy(src, dst) {
430430
*/
431431
angular.module('ngResource', ['ng']).
432432
provider('$resource', function ResourceProvider() {
433-
var PROTOCOL_AND_DOMAIN_REGEX = /^https?:\/\/[^/]*/;
433+
var PROTOCOL_AND_IPV6_REGEX = /^https?:\/\/\[[^\]]*][^/]*/;
434434

435435
var provider = this;
436436

@@ -541,7 +541,7 @@ angular.module('ngResource', ['ng']).
541541
url = actionUrl || self.template,
542542
val,
543543
encodedVal,
544-
protocolAndDomain = '';
544+
protocolAndIpv6 = '';
545545

546546
var urlParams = self.urlParams = Object.create(null);
547547
forEach(url.split(/\W/), function(param) {
@@ -556,8 +556,8 @@ angular.module('ngResource', ['ng']).
556556
}
557557
});
558558
url = url.replace(/\\:/g, ':');
559-
url = url.replace(PROTOCOL_AND_DOMAIN_REGEX, function(match) {
560-
protocolAndDomain = match;
559+
url = url.replace(PROTOCOL_AND_IPV6_REGEX, function(match) {
560+
protocolAndIpv6 = match;
561561
return '';
562562
});
563563

@@ -594,7 +594,7 @@ angular.module('ngResource', ['ng']).
594594
// E.g. `http://url.com/id./format?q=x` becomes `http://url.com/id.format?q=x`
595595
url = url.replace(/\/\.(?=\w+($|\?))/, '.');
596596
// replace escaped `/\.` with `/.`
597-
config.url = protocolAndDomain + url.replace(/\/\\\./, '/.');
597+
config.url = protocolAndIpv6 + url.replace(/\/\\\./, '/.');
598598

599599

600600
// set params - delegate param encoding to $http

test/ngResource/resourceSpec.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,35 @@ describe('basic usage', function() {
321321
});
322322

323323
it('should support IPv6 URLs', function() {
324-
var R = $resource('http://[2620:0:861:ed1a::1]/:ed1a/', {}, {}, {stripTrailingSlashes: false});
325-
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/foo/').respond({});
326-
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/').respond({});
327-
R.get({ed1a: 'foo'});
328-
R.get({});
324+
test('http://[2620:0:861:ed1a::1]', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]');
325+
test('http://[2620:0:861:ed1a::1]/', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/');
326+
test('http://[2620:0:861:ed1a::1]/:ed1a', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/foo');
327+
test('http://[2620:0:861:ed1a::1]/:ed1a', {}, 'http://[2620:0:861:ed1a::1]/');
328+
test('http://[2620:0:861:ed1a::1]/:ed1a/', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/foo/');
329+
test('http://[2620:0:861:ed1a::1]/:ed1a/', {}, 'http://[2620:0:861:ed1a::1]/');
330+
331+
// Helpers
332+
function test(templateUrl, params, actualUrl) {
333+
var R = $resource(templateUrl, null, null, {stripTrailingSlashes: false});
334+
$httpBackend.expect('GET', actualUrl).respond(null);
335+
R.get(params);
336+
}
337+
});
338+
339+
it('should support params in the `hostname` part of the URL', function() {
340+
test('http://:hostname', {hostname: 'foo.com'}, 'http://foo.com');
341+
test('http://:hostname/', {hostname: 'foo.com'}, 'http://foo.com/');
342+
test('http://:l2Domain.:l1Domain', {l1Domain: 'com', l2Domain: 'bar'}, 'http://bar.com');
343+
test('http://:l2Domain.:l1Domain/', {l1Domain: 'com', l2Domain: 'bar'}, 'http://bar.com/');
344+
test('http://127.0.0.:octet', {octet: 42}, 'http://127.0.0.42');
345+
test('http://127.0.0.:octet/', {octet: 42}, 'http://127.0.0.42/');
346+
347+
// Helpers
348+
function test(templateUrl, params, actualUrl) {
349+
var R = $resource(templateUrl, null, null, {stripTrailingSlashes: false});
350+
$httpBackend.expect('GET', actualUrl).respond(null);
351+
R.get(params);
352+
}
329353
});
330354

331355
it('should support overriding provider default trailing-slash stripping configuration', function() {

0 commit comments

Comments
 (0)