Index: /trunk/morty.go
===================================================================
--- /trunk/morty.go	(revision 43)
+++ /trunk/morty.go	(revision 44)
@@ -18,6 +18,5 @@
 	"github.com/valyala/fasthttp"
 	"golang.org/x/net/html"
-	"golang.org/x/net/html/charset"
-	"golang.org/x/text/encoding"
+	"golang.org/x/text/encoding/charmap"
 )
 
@@ -124,6 +123,4 @@
 `
 
-var HTML_META_CONTENT_TYPE string = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
-
 func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) {
 
@@ -167,16 +164,9 @@
 	req.SetConnectionClose()
 
-	reqQuery := parsedURI.Query()
-	ctx.QueryArgs().VisitAll(func(key, value []byte) {
-		reqQuery.Add(string(key), string(value))
-	})
-
-	parsedURI.RawQuery = reqQuery.Encode()
-
-	uriStr := parsedURI.String()
-
-	log.Println("getting", uriStr)
-
-	req.SetRequestURI(uriStr)
+	requestURIStr := string(requestURI)
+
+	log.Println("getting", requestURIStr)
+
+	req.SetRequestURI(requestURIStr)
 	req.Header.SetUserAgentBytes([]byte("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"))
 
@@ -217,5 +207,5 @@
 			}
 		}
-		error_message := fmt.Sprintf("invalid response: %d", resp.StatusCode())
+		error_message := fmt.Sprintf("invalid response: %d (%s)", resp.StatusCode(), requestURIStr)
 		p.serveMainPage(ctx, resp.StatusCode(), errors.New(error_message))
 		return
@@ -240,15 +230,11 @@
 	var responseBody []byte
 
-	if len(contentInfo) == 2 && bytes.Contains(contentInfo[0], []byte("text")) {
-		e, ename, _ := charset.DetermineEncoding(resp.Body(), string(contentType))
-		if (e != encoding.Nop) && (!strings.EqualFold("utf-8", ename)) {
-			responseBody, err = e.NewDecoder().Bytes(resp.Body())
-			if err != nil {
-				// HTTP status code 503 : Service Unavailable
-				p.serveMainPage(ctx, 503, err)
-				return
-			}
-		} else {
-			responseBody = resp.Body()
+	if len(contentInfo) == 2 && bytes.Contains(contentInfo[1], []byte("ISO-8859-2")) && bytes.Contains(contentInfo[0], []byte("text")) {
+		var err error
+		responseBody, err = charmap.ISO8859_2.NewDecoder().Bytes(resp.Body())
+		if err != nil {
+			// HTTP status code 503 : Service Unavailable
+			p.serveMainPage(ctx, 503, err)
+			return
 		}
 	} else {
@@ -333,4 +319,5 @@
 	unsafeElements := make([][]byte, 0, 8)
 	state := STATE_DEFAULT
+
 	for {
 		token := decoder.Next()
@@ -360,9 +347,10 @@
 					for {
 						attrName, attrValue, moreAttr := decoder.TagAttr()
-						if bytes.Equal(attrName, []byte("href")) {
-							parsedURI, err := url.Parse(string(attrValue))
-							if err == nil {
-								rc.BaseURL = parsedURI
-							}
+						if !bytes.Equal(attrName, []byte("href")) {
+							continue
+						}
+						parsedURI, err := url.Parse(string(attrValue))
+						if err == nil {
+							rc.BaseURL = parsedURI
 						}
 						if !moreAttr {
@@ -395,13 +383,12 @@
 				}
 
-				if bytes.Equal(tag, []byte("meta")) {
-					sanitizeMetaTag(rc, out, attrs)
-					break
-				}
-
 				fmt.Fprintf(out, "<%s", tag)
 
 				if hasAttrs {
-					sanitizeAttrs(rc, out, attrs)
+					if bytes.Equal(tag, []byte("meta")) {
+						sanitizeMetaAttrs(rc, out, attrs)
+					} else {
+						sanitizeAttrs(rc, out, attrs)
+					}
 				}
 
@@ -413,8 +400,4 @@
 						state = STATE_IN_STYLE
 					}
-				}
-
-				if bytes.Equal(tag, []byte("head")) {
-					fmt.Fprintf(out, HTML_META_CONTENT_TYPE)
 				}
 
@@ -516,5 +499,5 @@
 }
 
-func sanitizeMetaTag(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
+func sanitizeMetaAttrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
 	var http_equiv []byte
 	var content []byte
@@ -529,15 +512,6 @@
 			content = attrValue
 		}
-		if bytes.Equal(attrName, []byte("charset")) {
-			// exclude <meta charset="...">
-			return
-		}
-	}
-
-	if bytes.Equal(http_equiv, []byte("content-type")) {
-		return
-	}
-
-	out.Write([]byte("<meta"))
+	}
+
 	urlIndex := bytes.Index(bytes.ToLower(content), []byte("url="))
 	if bytes.Equal(http_equiv, []byte("refresh")) && urlIndex != -1 {
@@ -556,5 +530,5 @@
 		sanitizeAttrs(rc, out, attrs)
 	}
-	out.Write([]byte(">"))
+
 }
 
@@ -613,4 +587,5 @@
 		return fmt.Sprintf("./?mortyurl=%s", url.QueryEscape(uri)), nil
 	}
+
 	return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s", hash(uri, rc.Key), url.QueryEscape(uri)), nil
 }
