http.escape and http.unescape for windows
parent
12ecd0f3dc
commit
550e8cd0cb
|
@ -0,0 +1,9 @@
|
||||||
|
import http
|
||||||
|
|
||||||
|
fn test_escape_unescape() {
|
||||||
|
original := 'те ст: т\\%'
|
||||||
|
escaped := http.escape(original)
|
||||||
|
assert escaped == '%D1%82%D0%B5%20%D1%81%D1%82%3A%20%D1%82%5C%25'
|
||||||
|
unescaped := http.unescape(escaped)
|
||||||
|
assert unescaped == original
|
||||||
|
}
|
|
@ -18,10 +18,15 @@ import const (
|
||||||
INTERNET_DEFAULT_HTTP_PORT
|
INTERNET_DEFAULT_HTTP_PORT
|
||||||
INTERNET_DEFAULT_HTTPS_PORT
|
INTERNET_DEFAULT_HTTPS_PORT
|
||||||
INTERNET_SERVICE_HTTP
|
INTERNET_SERVICE_HTTP
|
||||||
|
INTERNET_MAX_URL_LENGTH
|
||||||
|
URL_ESCAPE_PERCENT
|
||||||
|
URL_ESCAPE_SEGMENT_ONLY
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BUF_MAX = 1024
|
BUF_MAX = 1024
|
||||||
|
URL_ESCAPE_AS_UTF8 = 0x00040000 // missing in mingw, require Windows 7
|
||||||
|
URL_ESCAPE_ASCII_URI_COMPONENT = 0x00080000 // missing in mingw, require Windows 8
|
||||||
)
|
)
|
||||||
|
|
||||||
pub fn (req &Request) do() Response {
|
pub fn (req &Request) do() Response {
|
||||||
|
@ -190,19 +195,17 @@ pub fn (req &Request) do() Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn escape(s string) string {
|
pub fn escape(s string) string {
|
||||||
# DWORD size=1;
|
mut buf := &u16(malloc(INTERNET_MAX_URL_LENGTH * 2)) // INTERNET_MAX_URL_LENGTH * sizeof(wchar_t)
|
||||||
# char *escaped = NULL;
|
mut nr_chars := INTERNET_MAX_URL_LENGTH
|
||||||
# char *empty_string = NULL;
|
res := C.UrlEscape(s.to_wide(), buf, &nr_chars, URL_ESCAPE_PERCENT | URL_ESCAPE_AS_UTF8 | URL_ESCAPE_ASCII_URI_COMPONENT)
|
||||||
# HRESULT res = UrlEscapeA(s.str, empty_string, &size, URL_ESCAPE_PERCENT | URL_ESCAPE_SEGMENT_ONLY);
|
return string_from_wide2(buf, nr_chars)
|
||||||
# if (res == E_POINTER)
|
}
|
||||||
{
|
|
||||||
# escaped = HeapAlloc(GetProcessHeap(), 0, size);
|
pub fn unescape(s string) string {
|
||||||
# if (!escaped)
|
mut buf := &u16(malloc(INTERNET_MAX_URL_LENGTH * 2))
|
||||||
# return s;
|
mut nr_chars := INTERNET_MAX_URL_LENGTH
|
||||||
# UrlEscapeA(s.str, escaped, &size, URL_ESCAPE_PERCENT | URL_ESCAPE_SEGMENT_ONLY);
|
res := C.UrlUnescape(s.to_wide(), &buf, &nr_chars, URL_ESCAPE_AS_UTF8 | URL_ESCAPE_ASCII_URI_COMPONENT)
|
||||||
# return tos2(escaped);
|
return string_from_wide2(buf, nr_chars)
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn C.InternetReadFile(voidptr, voidptr, int, intptr) bool
|
fn C.InternetReadFile(voidptr, voidptr, int, intptr) bool
|
||||||
|
|
Loading…
Reference in New Issue