Openresty How to decode a URL

Source: Internet
Author: User

URL encoding is an encoding used by HTTP to transmit various parameters at a single URL clock. This encoding will encode special characters (such as ' = ', ' & ', ' + ') in the form of '%<xx> ', where <xx> is the hexadecimal representation of the character. In addition, it converts spaces to "+". For example, it encodes the string "A+b=c" as "A%2bb+%3d+c". Finally, it joins each pair of parameter names and their values with "=" and connects each pair of results Name=value with "&". For example, for values:

Name = "A1"; query = "A+b = C"; Q= "Yes or no"

will be encoded as:

"Name=a1&query=a%2bb+%3d+c&q=yes+or+no"

Now, to decode this URL, you need to save each value in the encoding, and its name as a key, in a table. Once the function completes the basic code:

function unescape (s) s = string.gsub (S, "+", "") s = string.gsub (s), "percent (%x%x)", function (h) return string. char (Tonumber (H, +)) end) return S end

Use Gmatch to decode the name=value. Because both the name and the value cannot contain "&" and "=", you can use the pattern "[^&=]+" to match them:

cgi = {}function decode (s) for name, value in String.gmatch (S, "([^&=]+] = ([^&=]+)") Do name = Unescape ( Name) value = unescape (value) cgi[name] = value EndEnd

Excerpt from chapter 20th of the second edition of LUA programming-20.5 replacing -20.5.1URL encoding

Openresty How to decode a URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.