From 544f1c956cd63e859828348a27ab0f2e585730fa Mon Sep 17 00:00:00 2001 From: valkyrie_pilot Date: Sun, 24 Jul 2022 19:24:03 -0600 Subject: [PATCH] Auto-Convert UTF-16 to UTF-8 --- main.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 70e25de..da5c657 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "html/template" "io" @@ -8,6 +9,8 @@ import ( "net/http" "os" "strings" + "unicode/utf16" + "unicode/utf8" ) var contentTemplate *template.Template @@ -40,7 +43,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { } defer req.Body.Close() - w.Header().Set("Content-Type", "text/html; charset=utf-8") if req.StatusCode != http.StatusOK { @@ -53,6 +55,15 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprintln(w, "An error occurred reading the contents of the response") return } + ctype_encoding := strings.Split(req.Header.Get("Content-Type"), ";%20") + if strings.ToLower(ctype_encoding[len(ctype_encoding)-1]) == "charset=utf-16" { + contents, err = DecodeUTF16(contents) + if err != nil { + log.Println(err) + fmt.Fprintln(w, "An error occurred decoding the response") + return + } + } escaped := template.HTMLEscapeString(string(contents)) corrected := strings.Replace(escaped, "\n", "
", -1) @@ -63,3 +74,26 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { return } } + +func DecodeUTF16(b []byte) ([]byte, error) { + + if len(b)%2 != 0 { + return make([]byte, 0), fmt.Errorf("Must have even length byte slice") + } + + u16s := make([]uint16, 1) + + ret := &bytes.Buffer{} + + b8buf := make([]byte, 4) + + lb := len(b) + for i := 0; i < lb; i += 2 { + u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8) + r := utf16.Decode(u16s) + n := utf8.EncodeRune(b8buf, r[0]) + ret.Write(b8buf[:n]) + } + + return ret.Bytes(), nil +} \ No newline at end of file