test-platform/server/httputils.py

52 lines
1.0 KiB
Python
Raw Normal View History

2024-05-28 17:47:08 +03:00
html_escaping = {
"<": "&lt;",
">": "&gt;",
"\"": "&quot;"
}
2024-05-25 19:18:26 +03:00
def decode_url(url):
i = 0
end = len(url)
decode_buffer = ''
char_buffer = bytearray()
while i < end:
if url[i] == '%':
try:
char_buffer.append(int(f'0x{url[i+1:i+3]}', 16))
decode_buffer += char_buffer.decode("UTF-8")
del char_buffer[:]
i += 3
except:
i += 3
2024-05-28 17:47:08 +03:00
elif url[i] == "+":
decode_buffer += " "
i += 1
2024-05-25 19:18:26 +03:00
else:
decode_buffer += url[i]
i += 1
return decode_buffer
def parse_query(query):
query_items = [i for i in query.split("&") if i]
query_dict = {}
for i in query_items:
k, v = i.split("=")
query_dict[decode_url(k)] = decode_url(v)
return query_dict
2024-05-28 17:47:08 +03:00
def escape_sql_string(s):
return s.replace("'", "''")
def escape_html(s):
for k, v in html_escaping.items():
s = s.replace(k, v)
return s