I need to safely transmit strings that may contain non-breaking spaces (NBSP, Unicode U+00A0) through security filters like Cloudflare's. Additionally, I'm looking for a method to potentially reduce the payload size for efficiency. The challenge is to ensure these strings not losing their original structure.
Here is the client:
var encoded = window.btoa(encodeURIComponent("SELECT a FROM b"));
//U0VMRUNUJUMyJUEwYSVDMiVBMEZST00lQzIlQTBi
While in reality the "SELECT a FROM b" is "SELECTNBSF
aNBSF
FROMNBSF
b".
And the server side code reproduce this string: SELECT%C2%A0a%C2%A0FROM%C2%A0b
while the final should string should the same "SELECT a FROM b".
var valueBytes = Convert.FromBase64String(value);
return Encoding.UTF8.GetString(valueBytes);
How can I modify my server-side code to correctly decode the percent-encoded NBSP (and potentially other characters) back into their original form? Is there a more efficient way to reduce the payload size? (this is less important)
Solved with Uri.UnescapeDataString
:
var valueBytes = Convert.FromBase64String(value);
var decodedString = Encoding.UTF8.GetString(valueBytes);
return Uri.UnescapeDataString(decodedString);