Lua socket URL unescape and spaces

When a param in an URL is esacaped, spaces can be replaced by “%20” or “+”

In the Vera, when I use “url.unescape” to retrieve the original value, I get space when %20 is present but + is not replaced by a space.
What would be the good approach to solve this problem ?

I can use:

text = url.unescape(text:gsub("%+", "%%20")) but it supposes that + are initially replaced by %2b in the URL.

URI escaping is tricky because there are actually two very similar forms of it, used in slightly different contexts. One of them mandates that space be replaced by + and the other mandates that space be replaced by %20. (See here for a description of the differences: URL encoding - Wikipedia)

As luck would have it, both forms insist on + being encoded to %2D, so if you see a + in the encoded form it can only be a space. Your gsub approach should be valid in all cases. Presupposing that the upstream process that produced the string was conforming, which we know LuaUPnP to not be.

If it helps - I had problems in GCal3 with user input of urls.
In UI5, inputs with # or & in the string get truncated at the first occurrence. Mios said they’d fix it in UI7 but would not commit to fixing in UI5.

They also advised:
“We use encodeURIComponent() function and we also replace ’ with %27” and use the following encode function

     encodeString: function(str){
            try{
                var str = encodeURIComponent(str);
                // treat single quote
                str = str.replace("'","%27");
                return str;
            } catch(e) {
                Utils.logError('Error in Utils.encodeString(): ' + e);
            }
        },

I ended up putting this into my code to further parse url type strings

  newID = string.gsub(newID,'%&','%%26') -- encode any &
  newID = string.gsub(newID,'#','%%23')  -- encode any #
  newID = string.gsub(newID,'+','%%2B')  -- encode any +
  newID = string.gsub(newID,'@','%%40')  -- encode any @