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

Commit 9e30baa

Browse files
committedFeb 18, 2011
resources should not over-encode chars in url path
- added encodeUriSegment that properly encodes only those chars that URI RFC requires us to encode - modified Resource to use encodeUriSegment
1 parent a070ff5 commit 9e30baa

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed
 

‎src/Angular.js

+17
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,23 @@ function toKeyValue(obj) {
853853
return parts.length ? parts.join('&') : '';
854854
}
855855

856+
857+
/**
858+
* we need our custom mehtod because encodeURIComponent is too agressive and doesn't follow
859+
* http://www.ietf.org/rfc/rfc2396.txt with regards to the character set (pchar) allowed in path
860+
* segments
861+
*/
862+
function encodeUriSegment(val) {
863+
return encodeURIComponent(val).
864+
replace(/%40/gi, '@').
865+
replace(/%3A/gi, ':').
866+
replace(/%26/gi, '&').
867+
replace(/%3D/gi, '=').
868+
replace(/%2B/gi, '+').
869+
replace(/%24/g, '$').
870+
replace(/%2C/gi, ',');
871+
}
872+
856873
/**
857874
* @workInProgress
858875
* @ngdoc directive

‎src/Resource.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ function Route(template, defaults) {
1313

1414
Route.prototype = {
1515
url: function(params) {
16-
var path = [];
17-
var self = this;
18-
var url = this.template;
16+
var self = this,
17+
url = this.template,
18+
encodedVal;
19+
1920
params = params || {};
2021
forEach(this.urlParams, function(_, urlParam){
21-
var value = params[urlParam] || self.defaults[urlParam] || "";
22-
url = url.replace(new RegExp(":" + urlParam + "(\\W)"), encodeURIComponent(value) + "$1");
22+
encodedVal = encodeUriSegment(params[urlParam] || self.defaults[urlParam] || "")
23+
url = url.replace(new RegExp(":" + urlParam + "(\\W)"), encodedVal + "$1");
2324
});
2425
url = url.replace(/\/?#$/, '');
2526
var query = [];
2627
forEachSorted(params, function(value, key){
2728
if (!self.urlParams[key]) {
28-
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
29+
query.push(encodeUriSegment(key) + '=' + encodeUriSegment(value));
2930
}
3031
});
3132
url = url.replace(/\/*$/, '');

‎test/AngularSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ describe('angular', function(){
139139
});
140140

141141

142+
describe('encodeUriSegment', function() {
143+
it('should correctly encode uri segment and not encode chars defined as pchar set in rfc2396',
144+
function() {
145+
//don't encode alphanum
146+
expect(encodeUriSegment('asdf1234asdf')).
147+
toEqual('asdf1234asdf');
148+
149+
//don't encode unreserved'
150+
expect(encodeUriSegment("-_.!~*'() -_.!~*'()")).
151+
toEqual("-_.!~*'()%20-_.!~*'()");
152+
153+
//don't encode the rest of pchar'
154+
expect(encodeUriSegment(':@&=+$, :@&=+$,')).
155+
toEqual(':@&=+$,%20:@&=+$,');
156+
157+
//encode '/', ';' and ' ''
158+
expect(encodeUriSegment('/; /;')).
159+
toEqual('%2F%3B%20%2F%3B');
160+
});
161+
});
162+
163+
142164
describe ('rngScript', function() {
143165
it('should match angular.js', function() {
144166
expect('angular.js'.match(rngScript)).not.toBeNull();

‎test/ResourceSpec.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@ describe("resource", function() {
4343
it('should correctly encode url params', function(){
4444
var R = resource.route('/Path/:a');
4545
xhr.expectGET('/Path/foo%231').respond({});
46-
xhr.expectGET('/Path/doh!%40foo?bar=baz%231').respond({});
46+
xhr.expectGET('/Path/doh!@foo?bar=baz%231').respond({});
4747
R.get({a: 'foo#1'});
4848
R.get({a: 'doh!@foo', bar: 'baz#1'});
4949
});
5050

51+
it('should not encode @ in url params', function() {
52+
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc2396.txt
53+
//with regards to the character set (pchar) allowed in path segments
54+
//so we need this test to make sure that we don't over-encode the params and break stuff like
55+
//buzz api which uses @self
56+
57+
var R = resource.route('/Path/:a');
58+
xhr.expectGET('/Path/doh@foo?bar=baz@1').respond({});
59+
R.get({a: 'doh@foo', bar: 'baz@1'});
60+
})
61+
5162
it("should build resource with default param", function(){
5263
xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'});
5364
var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05});

0 commit comments

Comments
 (0)
This repository has been archived.