Fixed bug in the cgi.fun library. (0.40.7)
This commit is contained in:
parent
c68244d6f6
commit
3bcdc83e30
3 changed files with 33 additions and 37 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(fun VERSION 0.40.6 LANGUAGES C)
|
project(fun VERSION 0.40.7 LANGUAGES C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
|
||||||
|
|
@ -20,5 +20,6 @@ print("decod: " + cgi.url_decode(s))
|
||||||
|
|
||||||
/* Expected output:
|
/* Expected output:
|
||||||
raw: name=Fun+Lang&msg=Hello%2C+World%21
|
raw: name=Fun+Lang&msg=Hello%2C+World%21
|
||||||
decod: name=Fun Lang&msg=Hello%2C World%21
|
decod: name=Fun Lang&msg=Hello, World!
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -257,44 +257,39 @@ class CGI()
|
||||||
|
|
||||||
// Minimal url-decoder: '+' -> space; %XX for ASCII hex
|
// Minimal url-decoder: '+' -> space; %XX for ASCII hex
|
||||||
fun url_decode(this, s)
|
fun url_decode(this, s)
|
||||||
src = to_string(s)
|
_src = to_string(s)
|
||||||
out = []
|
_res_str = ""
|
||||||
i = 0
|
number _j = 0
|
||||||
n = len(src)
|
number _m = len(_src)
|
||||||
while (i < n)
|
while (_j < _m)
|
||||||
ch = substr(src, i, 1)
|
_c = substr(_src, _j, 1)
|
||||||
if (ch == "+")
|
if (_c == "+")
|
||||||
push(out, " ")
|
_res_str = _res_str + " "
|
||||||
i = i + 1
|
_j = _j + 1
|
||||||
else if (ch == "%" && i + 2 < n)
|
else if (_c == "%" && _j + 2 < _m)
|
||||||
h1 = substr(src, i + 1, 1)
|
_h1 = substr(_src, _j + 1, 1)
|
||||||
h2 = substr(src, i + 2, 1)
|
_h2 = substr(_src, _j + 2, 1)
|
||||||
// Convert two hex digits to a single character (uppercase/lowercase allowed)
|
_hex = "0123456789ABCDEF"
|
||||||
hexdigits = "0123456789ABCDEF"
|
_v1 = find(_hex, str_to_upper(_h1))
|
||||||
hexdigits_l = "0123456789abcdef"
|
_v2 = find(_hex, str_to_upper(_h2))
|
||||||
v1 = find(hexdigits, str_to_upper(h1))
|
if (_v1 >= 0 && _v2 >= 0)
|
||||||
v2 = find(hexdigits, str_to_upper(h2))
|
number _code = _v1 * 16 + _v2
|
||||||
if (v1 >= 0 && v2 >= 0)
|
// Using a more robust table with explicit characters
|
||||||
code = v1 * 16 + v2
|
_ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
||||||
// Build single-byte string from code using substr on a constant table
|
if (_code >= 32 && _code <= 126)
|
||||||
// Table of 256 bytes isn't available; approximate by mapping common ASCII 0x20..0x7E
|
_res_str = _res_str + substr(_ascii, _code - 32, 1)
|
||||||
ascii = "\x20!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
|
||||||
if (code >= 32 && code <= 126)
|
|
||||||
push(out, substr(ascii, code - 32, 1))
|
|
||||||
else
|
else
|
||||||
// Non-printable: keep percent sequence as-is
|
_res_str = _res_str + "%"
|
||||||
push(out, "%")
|
_res_str = _res_str + _h1
|
||||||
push(out, h1)
|
_res_str = _res_str + _h2
|
||||||
push(out, h2)
|
_j = _j + 3
|
||||||
i = i + 3
|
|
||||||
else
|
else
|
||||||
// Not hex, keep as-is
|
_res_str = _res_str + _c
|
||||||
push(out, ch)
|
_j = _j + 1
|
||||||
i = i + 1
|
|
||||||
else
|
else
|
||||||
push(out, ch)
|
_res_str = _res_str + _c
|
||||||
i = i + 1
|
_j = _j + 1
|
||||||
return join(out, "")
|
return _res_str
|
||||||
|
|
||||||
fun _merge_params(this, pairs)
|
fun _merge_params(this, pairs)
|
||||||
// pairs: array of [key, value] entries
|
// pairs: array of [key, value] entries
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue