[Kimchi-devel] [PATCH] add a base64 safe url encode and decode to js utils

Zhou Zheng Sheng zhshzhou at linux.vnet.ibm.com
Mon Jul 21 07:57:30 UTC 2014



Hi,

You'd better mention the base64 Wikipedia.

http://en.wikipedia.org/wiki/Base64#Implementations_and_history

The "URL applications" section gave some suggestions for the URL encode.
It seems your patch follows the suggestions. You can make this link a
reference in the commit message.

on 2014/07/21 15:25, shaohef at linux.vnet.ibm.com wrote:
> From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
> 
> Most webs encode/decode url by base64 to escape the special characters.
> 
> base64 encode/decode is similar to base64 safe url encode/decode.
> 
> The difference is the alphabet.
> The base64 safe url alphabet uses '-' instead of '+' and '_' instead of '/'.
> 
> after this patch:
> In backend, we can encode an url:
>>>> base64.urlsafe_b64encode("/vnc_auto.html?port=64667&path=?token=opensuse12&encrypt=1")
> 'L3ZuY19hdXRvLmh0bWw_cG9ydD02NDY2NyZwYXRoPT90b2tlbj1vcGVuc3VzZTEyJmVuY3J5cHQ9MQ=='
> and in UI, we can decode it:
> kimchi.urlSafeB64Decode("L3ZuY19hdXRvLmh0bWw_cG9ydD02NDY2NyZwYXRoPT90b2tlbj1vcGVuc3VzZTEyJmVuY3J5cHQ9MQ==")
> "/vnc_auto.html?port=64667&path=?token=opensuse12&encrypt=1"
> 
> or
> 
> In UI, we can encode an url:
> kimchi.urlSafeB64Encode("/vnc_auto.html?port=64667&path=?token=opensuse12&encrypt=1")
> "L3ZuY19hdXRvLmh0bWw_cG9ydD02NDY2NyZwYXRoPT90b2tlbj1vcGVuc3VzZTEyJmVuY3J5cHQ9MQ=="
> and in backend, we can decode it:
>>>> base64.urlsafe_b64decode("L3ZuY19hdXRvLmh0bWw_cG9ydD02NDY2NyZwYXRoPT90b2tlbj1vcGVuc3VzZTEyJmVuY3J5cHQ9MQ==")
> '/vnc_auto.html?port=64667&path=?token=opensuse12&encrypt=1'
> 
> Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
> ---
>  ui/js/src/kimchi.utils.js | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/ui/js/src/kimchi.utils.js b/ui/js/src/kimchi.utils.js
> index c7103f8..480b9b5 100644
> --- a/ui/js/src/kimchi.utils.js
> +++ b/ui/js/src/kimchi.utils.js
> @@ -183,3 +183,11 @@ kimchi.escapeStr = function(str) {
> 
>      return str;
>  };
> +
> +kimchi.urlSafeB64Decode = function(str) {
> +    return atob(str.replace(/-/g, '+').replace(/_/g, '/'));
> +}
> +
> +kimchi.urlSafeB64Encode = function(str) {
> +    return btoa(str).replace(/\+/g, '-').replace(/\//g, '_');
> +}
> 


-- 
Zhou Zheng Sheng / 周征晟
E-mail: zhshzhou at linux.vnet.ibm.com
Telephone: 86-10-82454397




More information about the Kimchi-devel mailing list