How to know if a URL is decoded/encoded?

后端 未结 5 795
后悔当初
后悔当初 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: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.

提交回复
热议问题