Skip to content

Commit

Permalink
Merge pull request #6 from randomairborne/main
Browse files Browse the repository at this point in the history
Auto-Convert UTF-16 to UTF-8
  • Loading branch information
LordRalex authored Jul 25, 2022
2 parents 0c362bb + 544f1c9 commit 61c393a
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"bytes"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strings"
"unicode/utf16"
"unicode/utf8"
)

var contentTemplate *template.Template
Expand Down Expand Up @@ -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 {
Expand All @@ -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", "<br>", -1)

Expand All @@ -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
}

0 comments on commit 61c393a

Please sign in to comment.