Skip to content

Commit

Permalink
upd:getObjectUrl的qUrlParamList encode处理
Browse files Browse the repository at this point in the history
  • Loading branch information
livehigh committed Dec 1, 2021
1 parent dbaa639 commit 2deb189
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
20 changes: 10 additions & 10 deletions demo-album/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ function getObjectKeys(obj, forKey) {
/**
* obj转为string
* @param {Object} obj 需要转的对象,必须
* @param {Boolean} stayCase 保留原始大小写,默认false,非必须
* @param {Boolean} lowerCaseKey key是否转为小写,默认false,非必须
* @return {String} data 返回字符串
*/
var obj2str = function obj2str(obj, stayCase) {
var obj2str = function obj2str(obj, lowerCaseKey) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] === undefined || obj[key] === null ? '' : '' + obj[key];
key = stayCase ? camSafeUrlEncode(key) : camSafeUrlEncode(key).toLowerCase();
key = lowerCaseKey ? camSafeUrlEncode(key).toLowerCase() : camSafeUrlEncode(key);
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ var getAuth = function getAuth(opt) {
var signKey = CryptoJS.HmacSHA1(qKeyTime, SecretKey).toString();

// 步骤二:构成 FormatString
var formatString = [method, pathname, util.obj2str(queryParams), util.obj2str(headers), ''].join('\n');
var formatString = [method, pathname, util.obj2str(queryParams, true), util.obj2str(headers, true), ''].join('\n');

// 步骤三:计算 StringToSign
var stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(), ''].join('\n');
Expand Down Expand Up @@ -8248,7 +8248,7 @@ function getObjectUrl(params, callback) {

var queryParamsStr = '';
if (params.Query) {
queryParamsStr += util.obj2str(params.Query, true);
queryParamsStr += util.obj2str(params.Query);
}
if (params.QueryString) {
queryParamsStr += (queryParamsStr ? '&' : '') + params.QueryString;
Expand Down Expand Up @@ -8280,10 +8280,10 @@ function getObjectUrl(params, callback) {
return;
}

// 兼容万象url需要encode两次
// 兼容万象url qUrlParamList需要再encode一次
var replaceUrlParamList = function replaceUrlParamList(url) {
var urlParams = url.match(/q-url-param-list.*?(?=&)/g)[0];
var encodedParams = 'q-url-param-list=' + encodeURIComponent(encodeURIComponent(urlParams.replace(/q-url-param-list=/, '').toLowerCase())).toLowerCase();
var encodedParams = 'q-url-param-list=' + encodeURIComponent(urlParams.replace(/q-url-param-list=/, '')).toLowerCase();
var reg = new RegExp(urlParams, 'g');
var replacedUrl = url.replace(reg, encodedParams);
return replacedUrl;
Expand Down Expand Up @@ -8952,14 +8952,14 @@ function getObjectKeys(obj, forKey) {
});
};

var obj2str = function obj2str(obj) {
var obj2str = function obj2str(obj, lowerCaseKey) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] === undefined || obj[key] === null ? '' : '' + obj[key];
key = camSafeUrlEncode(key).toLowerCase();
key = lowerCaseKey ? camSafeUrlEncode(key).toLowerCase() : camSafeUrlEncode(key);
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val);
}
Expand Down Expand Up @@ -9032,7 +9032,7 @@ var request = function request(params, callback) {
});
});
} else {
var qsStr = params.qs && obj2str(params.qs) || '';
var qsStr = params.qs && obj2str(params.qs, true) || '';
if (qsStr) {
url += (url.indexOf('?') > -1 ? '&' : '?') + qsStr;
}
Expand Down
2 changes: 1 addition & 1 deletion demo-album/lib/cos-wx-sdk-v5.min.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions demo/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ function getObjectKeys(obj, forKey) {
/**
* obj转为string
* @param {Object} obj 需要转的对象,必须
* @param {Boolean} stayCase 保留原始大小写,默认false,非必须
* @param {Boolean} lowerCaseKey key是否转为小写,默认false,非必须
* @return {String} data 返回字符串
*/
var obj2str = function obj2str(obj, stayCase) {
var obj2str = function obj2str(obj, lowerCaseKey) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] === undefined || obj[key] === null ? '' : '' + obj[key];
key = stayCase ? camSafeUrlEncode(key) : camSafeUrlEncode(key).toLowerCase();
key = lowerCaseKey ? camSafeUrlEncode(key).toLowerCase() : camSafeUrlEncode(key);
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ var getAuth = function getAuth(opt) {
var signKey = CryptoJS.HmacSHA1(qKeyTime, SecretKey).toString();

// 步骤二:构成 FormatString
var formatString = [method, pathname, util.obj2str(queryParams), util.obj2str(headers), ''].join('\n');
var formatString = [method, pathname, util.obj2str(queryParams, true), util.obj2str(headers, true), ''].join('\n');

// 步骤三:计算 StringToSign
var stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(), ''].join('\n');
Expand Down Expand Up @@ -8248,7 +8248,7 @@ function getObjectUrl(params, callback) {

var queryParamsStr = '';
if (params.Query) {
queryParamsStr += util.obj2str(params.Query, true);
queryParamsStr += util.obj2str(params.Query);
}
if (params.QueryString) {
queryParamsStr += (queryParamsStr ? '&' : '') + params.QueryString;
Expand Down Expand Up @@ -8280,10 +8280,10 @@ function getObjectUrl(params, callback) {
return;
}

// 兼容万象url需要encode两次
// 兼容万象url qUrlParamList需要再encode一次
var replaceUrlParamList = function replaceUrlParamList(url) {
var urlParams = url.match(/q-url-param-list.*?(?=&)/g)[0];
var encodedParams = 'q-url-param-list=' + encodeURIComponent(encodeURIComponent(urlParams.replace(/q-url-param-list=/, '').toLowerCase())).toLowerCase();
var encodedParams = 'q-url-param-list=' + encodeURIComponent(urlParams.replace(/q-url-param-list=/, '')).toLowerCase();
var reg = new RegExp(urlParams, 'g');
var replacedUrl = url.replace(reg, encodedParams);
return replacedUrl;
Expand Down Expand Up @@ -8952,14 +8952,14 @@ function getObjectKeys(obj, forKey) {
});
};

var obj2str = function obj2str(obj) {
var obj2str = function obj2str(obj, lowerCaseKey) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] === undefined || obj[key] === null ? '' : '' + obj[key];
key = camSafeUrlEncode(key).toLowerCase();
key = lowerCaseKey ? camSafeUrlEncode(key).toLowerCase() : camSafeUrlEncode(key);
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val);
}
Expand Down Expand Up @@ -9032,7 +9032,7 @@ var request = function request(params, callback) {
});
});
} else {
var qsStr = params.qs && obj2str(params.qs) || '';
var qsStr = params.qs && obj2str(params.qs, true) || '';
if (qsStr) {
url += (url.indexOf('?') > -1 ? '&' : '?') + qsStr;
}
Expand Down
2 changes: 1 addition & 1 deletion demo/lib/cos-wx-sdk-v5.min.js

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
function camSafeUrlEncode(str) {
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}

function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
var obj2str = function (obj, lowerCaseKey) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = lowerCaseKey? camSafeUrlEncode(key).toLowerCase() : camSafeUrlEncode(key);
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

var request = function (params, callback) {
Expand Down Expand Up @@ -110,7 +110,7 @@ var request = function (params, callback) {
});
});
} else {
var qsStr = params.qs && obj2str(params.qs) || '';
var qsStr = params.qs && obj2str(params.qs, true) || '';
if (qsStr) {
url += (url.indexOf('?') > -1 ? '&' : '?') + qsStr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2936,7 +2936,7 @@ function getObjectUrl(params, callback) {

var queryParamsStr = '';
if(params.Query){
queryParamsStr += util.obj2str(params.Query, true);
queryParamsStr += util.obj2str(params.Query);
}
if(params.QueryString){
queryParamsStr += (queryParamsStr ? '&' : '') + params.QueryString;
Expand Down Expand Up @@ -2968,10 +2968,10 @@ function getObjectUrl(params, callback) {
return;
}

// 兼容万象url需要encode两次
// 兼容万象url qUrlParamList需要再encode一次
var replaceUrlParamList = function(url) {
var urlParams = url.match(/q-url-param-list.*?(?=&)/g)[0];
var encodedParams = 'q-url-param-list=' + encodeURIComponent(encodeURIComponent(urlParams.replace(/q-url-param-list=/, '').toLowerCase())).toLowerCase();
var encodedParams = 'q-url-param-list=' + encodeURIComponent(urlParams.replace(/q-url-param-list=/, '')).toLowerCase();
var reg = new RegExp(urlParams, 'g');
var replacedUrl = url.replace(reg, encodedParams);
return replacedUrl;
Expand Down
8 changes: 4 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ function getObjectKeys(obj, forKey) {
/**
* obj转为string
* @param {Object} obj 需要转的对象,必须
* @param {Boolean} stayCase 保留原始大小写,默认false,非必须
* @param {Boolean} lowerCaseKey key是否转为小写,默认false,非必须
* @return {String} data 返回字符串
*/
var obj2str = function (obj, stayCase) {
var obj2str = function (obj, lowerCaseKey) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = stayCase? camSafeUrlEncode(key) : camSafeUrlEncode(key).toLowerCase();
key = lowerCaseKey? camSafeUrlEncode(key).toLowerCase() : camSafeUrlEncode(key);
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ var getAuth = function (opt) {
var signKey = CryptoJS.HmacSHA1(qKeyTime, SecretKey).toString();

// 步骤二:构成 FormatString
var formatString = [method, pathname, util.obj2str(queryParams), util.obj2str(headers), ''].join('\n');
var formatString = [method, pathname, util.obj2str(queryParams, true), util.obj2str(headers, true), ''].join('\n');

// 步骤三:计算 StringToSign
var stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(), ''].join('\n');
Expand Down

0 comments on commit 2deb189

Please sign in to comment.