How to know if a URL is decoded/encoded?

后端 未结 5 796
后悔当初
后悔当初 2021-02-05 04:37

I am using Javascript method decodeURIComponent to decode an encoded URL. Now I am having an issue, that sometimes the URL is get encoded twice during redirection b

相关标签:
5条回答
  • 2021-02-05 05:14

    There is really simple and fast and trusted way to know if a URL string is encoded or Not.

    For Example: decodeURIComponent(decodeURIComponent(encodeURIComponent("SWQgPSA0NjI%3D"))) <> "SWQgPSA0NjI=" ----> Not equal to orginal value then Already encoded

    just this code:

      var encValue = encodeURIComponent(Value);
        try {
             if (decodeURIComponent(decodeURIComponent(encValue)) === Value) {
              //not encodec yet...so return encoded of val
              return encValue;
             }
           } catch (err) {
               //not encodec yet...so return encoded of val
               return encValue;
           }
    
     return Value  //same value returned
    
                            
    
    0 讨论(0)
  • 2021-02-05 05:20

    you can keep decode until the string did not change:

    str = "xxxxxxx"
    dec_str = decode(str)
    while(dec_str != str)
         str = dec_str;
         dec_str = decode(str);
    
    0 讨论(0)
  • 2021-02-05 05:25
    function checkEncodeURI(str) {
     return /\%/i.test(str)
    }
    

    Test :

    let url1 = "https://quora.com/unanswered/I’m-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I’ve-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one"
    let url2 = 'https://www.quora.com/unanswered/I%E2%80%99m-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I%E2%80%99ve-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one'
    let a = checkEncodeURI(url1)
    console.log(a)
    let b = checkEncodeURI(url2)
    console.log(b)
    
    0 讨论(0)
  • 2021-02-05 05:27

    Repeatedly decoding until you find no % signs will work over 99% of the time. It'll work even better if you repeatedly call so long as a match for /%[0-9a-f]{2}/i can be found.

    However, if I were (for some bizarre reason) to name a file 100%achieved, that would cause a problem because %ac would be decoded to ¬, causing the decode to fail. Unfortunately there's no way to detect this case.

    Ideally you should know if something is encoded more than once, and optimally you shouldn't let it happen in the first place.

    0 讨论(0)
  • 2021-02-05 05:30

    There is a simple way to konw if a URL string is encoded.

    Take the initial string and compare it with the result of decoding it. If the result is the same, the string is not encoded; if the result is different then it is encoded.

    I had this issue with my urls and I used this functions:

    function isEncoded(uri) {
      uri = uri || '';
    
      return uri !== decodeURIComponent(uri);
    }
    

    So now I can use isEncoded as the discriminant in a while loop (or with recursion) to know if I need to keep calling decodeURIComponent on the string:

    function fullyDecodeURI(uri){
    
      while (isEncoded(uri)){
        uri = decodeURIComponent(uri);
      }
    
      return uri;
    }
    

    This solved the problem for me of decoding urls encoded multiple times. Hope this helps.

    0 讨论(0)
提交回复
热议问题