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

fix($resource): allow params in hostname (except for IPv6 addresses) #14906

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ function shallowClearAndCopy(src, dst) {
*/
angular.module('ngResource', ['ng']).
provider('$resource', function ResourceProvider() {
var PROTOCOL_AND_DOMAIN_REGEX = /^https?:\/\/[^/]*/;
var PROTOCOL_AND_IPV6_REGEX = /^https?:\/\/\[[^\]]*][^/]*/;

var provider = this;

Expand Down Expand Up @@ -541,7 +541,7 @@ angular.module('ngResource', ['ng']).
url = actionUrl || self.template,
val,
encodedVal,
protocolAndDomain = '';
protocolAndIpv6 = '';

var urlParams = self.urlParams = Object.create(null);
forEach(url.split(/\W/), function(param) {
Expand All @@ -556,8 +556,8 @@ angular.module('ngResource', ['ng']).
}
});
url = url.replace(/\\:/g, ':');
url = url.replace(PROTOCOL_AND_DOMAIN_REGEX, function(match) {
protocolAndDomain = match;
url = url.replace(PROTOCOL_AND_IPV6_REGEX, function(match) {
protocolAndIpv6 = match;
return '';
});

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


// set params - delegate param encoding to $http
Expand Down
34 changes: 29 additions & 5 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,35 @@ describe('basic usage', function() {
});

it('should support IPv6 URLs', function() {
var R = $resource('http://[2620:0:861:ed1a::1]/:ed1a/', {}, {}, {stripTrailingSlashes: false});
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/foo/').respond({});
$httpBackend.expect('GET', 'http://[2620:0:861:ed1a::1]/').respond({});
R.get({ed1a: 'foo'});
R.get({});
test('http://[2620:0:861:ed1a::1]', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]');
test('http://[2620:0:861:ed1a::1]/', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/');
test('http://[2620:0:861:ed1a::1]/:ed1a', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/foo');
test('http://[2620:0:861:ed1a::1]/:ed1a', {}, 'http://[2620:0:861:ed1a::1]/');
test('http://[2620:0:861:ed1a::1]/:ed1a/', {ed1a: 'foo'}, 'http://[2620:0:861:ed1a::1]/foo/');
test('http://[2620:0:861:ed1a::1]/:ed1a/', {}, 'http://[2620:0:861:ed1a::1]/');

// Helpers
function test(templateUrl, params, actualUrl) {
var R = $resource(templateUrl, null, null, {stripTrailingSlashes: false});
$httpBackend.expect('GET', actualUrl).respond(null);
R.get(params);
}
});

it('should support params in the `hostname` part of the URL', function() {
test('http://:hostname', {hostname: 'foo.com'}, 'http://foo.com');
test('http://:hostname/', {hostname: 'foo.com'}, 'http://foo.com/');
test('http://:l2Domain.:l1Domain', {l1Domain: 'com', l2Domain: 'bar'}, 'http://bar.com');
test('http://:l2Domain.:l1Domain/', {l1Domain: 'com', l2Domain: 'bar'}, 'http://bar.com/');
test('http://127.0.0.:octet', {octet: 42}, 'http://127.0.0.42');
test('http://127.0.0.:octet/', {octet: 42}, 'http://127.0.0.42/');

// Helpers
function test(templateUrl, params, actualUrl) {
var R = $resource(templateUrl, null, null, {stripTrailingSlashes: false});
$httpBackend.expect('GET', actualUrl).respond(null);
R.get(params);
}
});

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