1
0
Fork 0
forked from fun/fun

Beautified some code in the cgi.fun library. (0.40.8)

This commit is contained in:
Johannes Findeisen 2026-04-27 01:55:01 +02:00
commit 9bba1e34ec
2 changed files with 30 additions and 31 deletions

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(fun VERSION 0.40.7 LANGUAGES C) project(fun VERSION 0.40.8 LANGUAGES C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)

View file

@ -255,41 +255,40 @@ class CGI()
resp = resp + "Connection: close\r\n\r\n" + b resp = resp + "Connection: close\r\n\r\n" + b
return resp return resp
// Minimal url-decoder: '+' -> space; %XX for ASCII hex
fun url_decode(this, s) fun url_decode(this, s)
_src = to_string(s) source_str = to_string(s)
_res_str = "" decoded_result = ""
number _j = 0 number current_pos = 0
number _m = len(_src) number source_len = len(source_str)
while (_j < _m) while (current_pos < source_len)
_c = substr(_src, _j, 1) current_char = substr(source_str, current_pos, 1)
if (_c == "+") if (current_char == "+")
_res_str = _res_str + " " decoded_result = decoded_result + " "
_j = _j + 1 current_pos = current_pos + 1
else if (_c == "%" && _j + 2 < _m) else if (current_char == "%" && current_pos + 2 < source_len)
_h1 = substr(_src, _j + 1, 1) hex_digit1 = substr(source_str, current_pos + 1, 1)
_h2 = substr(_src, _j + 2, 1) hex_digit2 = substr(source_str, current_pos + 2, 1)
_hex = "0123456789ABCDEF" hex_table = "0123456789ABCDEF"
_v1 = find(_hex, str_to_upper(_h1)) val1 = find(hex_table, str_to_upper(hex_digit1))
_v2 = find(_hex, str_to_upper(_h2)) val2 = find(hex_table, str_to_upper(hex_digit2))
if (_v1 >= 0 && _v2 >= 0) if (val1 >= 0 && val2 >= 0)
number _code = _v1 * 16 + _v2 number char_code = val1 * 16 + val2
// Using a more robust table with explicit characters // Using a more robust table with explicit characters
_ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ascii_table = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
if (_code >= 32 && _code <= 126) if (char_code >= 32 && char_code <= 126)
_res_str = _res_str + substr(_ascii, _code - 32, 1) decoded_result = decoded_result + substr(ascii_table, char_code - 32, 1)
else else
_res_str = _res_str + "%" decoded_result = decoded_result + "%"
_res_str = _res_str + _h1 decoded_result = decoded_result + hex_digit1
_res_str = _res_str + _h2 decoded_result = decoded_result + hex_digit2
_j = _j + 3 current_pos = current_pos + 3
else else
_res_str = _res_str + _c decoded_result = decoded_result + current_char
_j = _j + 1 current_pos = current_pos + 1
else else
_res_str = _res_str + _c decoded_result = decoded_result + current_char
_j = _j + 1 current_pos = current_pos + 1
return _res_str return decoded_result
fun _merge_params(this, pairs) fun _merge_params(this, pairs)
// pairs: array of [key, value] entries // pairs: array of [key, value] entries