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

Commit 45f0939

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
1 parent 1102c84 commit 45f0939

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-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

+45-5
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,51 @@ 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+
var keepSlashes = {stripTrailingSlashes: false};
325+
326+
var R1 = $resource('http://[2620:0:861:ed1a::1]', null, null, keepSlashes);
327+
var R2 = $resource('http://[2620:0:861:ed1a::1]/', null, null, keepSlashes);
328+
var R3 = $resource('http://[2620:0:861:ed1a::1]/:ed1a', null, null, keepSlashes);
329+
var R4 = $resource('http://[2620:0:861:ed1a::1]/:ed1a/', null, null, keepSlashes);
330+
331+
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]').respond(null); // R1
332+
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/').respond(null); // R2
333+
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/foo').respond(null); // R3
334+
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/').respond(null); // R3
335+
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/foo/').respond(null); // R4
336+
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/').respond(null); // R4
337+
338+
R1.get({ed1a: 'foo'});
339+
R2.get({ed1a: 'foo'});
340+
R3.get({ed1a: 'foo'});
341+
R3.get({});
342+
R4.get({ed1a: 'foo'});
343+
R4.get({});
344+
});
345+
346+
it('should support params in the `hostname` part of the URL', function() {
347+
var keepSlashes = {stripTrailingSlashes: false};
348+
349+
var R1 = $resource('http://:hostname', null, null, keepSlashes);
350+
var R2 = $resource('http://:hostname/', null, null, keepSlashes);
351+
var R3 = $resource('http://:l2Domain.:l1Domain', null, null, keepSlashes);
352+
var R4 = $resource('http://:l2Domain.:l1Domain/', null, null, keepSlashes);
353+
var R5 = $resource('http://127.0.0.:octet', null, null, keepSlashes);
354+
var R6 = $resource('http://127.0.0.:octet/', null, null, keepSlashes);
355+
356+
$httpBackend.expect('GET', 'http://foo.com').respond(null); // R1
357+
$httpBackend.expect('GET', 'http://foo.com/').respond(null); // R2
358+
$httpBackend.expect('GET', 'http://bar.com').respond(null); // R3
359+
$httpBackend.expect('GET', 'http://bar.com/').respond(null); // R4
360+
$httpBackend.expect('GET', 'http://127.0.0.42').respond(null); // R5
361+
$httpBackend.expect('GET', 'http://127.0.0.42/').respond(null); // R6
362+
363+
R1.get({hostname: 'foo.com'});
364+
R2.get({hostname: 'foo.com'});
365+
R3.get({l1Domain: 'com', l2Domain: 'bar'});
366+
R4.get({l1Domain: 'com', l2Domain: 'bar'});
367+
R5.get({octet: 42});
368+
R6.get({octet: 42});
329369
});
330370

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

0 commit comments

Comments
 (0)