Changeset 45 in code


Ignore:
Timestamp:
Nov 28, 2016, 12:49:22 PM (9 years ago)
Author:
asciimoo
Message:

Merge pull request #35 from dalf/url_encoding

Fetched URI matched the mortyurl.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r44 r45  
    1818        "github.com/valyala/fasthttp"
    1919        "golang.org/x/net/html"
    20         "golang.org/x/text/encoding/charmap"
     20        "golang.org/x/net/html/charset"
     21        "golang.org/x/text/encoding"
    2122)
    2223
     
    123124`
    124125
     126var HTML_META_CONTENT_TYPE string = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
     127
    125128func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) {
    126129
     
    230233        var responseBody []byte
    231234
    232         if len(contentInfo) == 2 && bytes.Contains(contentInfo[1], []byte("ISO-8859-2")) && bytes.Contains(contentInfo[0], []byte("text")) {
    233                 var err error
    234                 responseBody, err = charmap.ISO8859_2.NewDecoder().Bytes(resp.Body())
    235                 if err != nil {
    236                         // HTTP status code 503 : Service Unavailable
    237                         p.serveMainPage(ctx, 503, err)
    238                         return
     235        if len(contentInfo) == 2 && bytes.Contains(contentInfo[0], []byte("text")) {
     236                e, ename, _ := charset.DetermineEncoding(resp.Body(), string(contentType))
     237                if (e != encoding.Nop) && (!strings.EqualFold("utf-8", ename)) {
     238                        responseBody, err = e.NewDecoder().Bytes(resp.Body())
     239                        if err != nil {
     240                                // HTTP status code 503 : Service Unavailable
     241                                p.serveMainPage(ctx, 503, err)
     242                                return
     243                        }
     244                } else {
     245                        responseBody = resp.Body()
    239246                }
    240247        } else {
     
    319326        unsafeElements := make([][]byte, 0, 8)
    320327        state := STATE_DEFAULT
    321 
    322328        for {
    323329                token := decoder.Next()
     
    347353                                        for {
    348354                                                attrName, attrValue, moreAttr := decoder.TagAttr()
    349                                                 if !bytes.Equal(attrName, []byte("href")) {
    350                                                         continue
    351                                                 }
    352                                                 parsedURI, err := url.Parse(string(attrValue))
    353                                                 if err == nil {
    354                                                         rc.BaseURL = parsedURI
     355                                                if bytes.Equal(attrName, []byte("href")) {
     356                                                        parsedURI, err := url.Parse(string(attrValue))
     357                                                        if err == nil {
     358                                                                rc.BaseURL = parsedURI
     359                                                        }
    355360                                                }
    356361                                                if !moreAttr {
     
    383388                                }
    384389
     390                                if bytes.Equal(tag, []byte("meta")) {
     391                                        sanitizeMetaTag(rc, out, attrs)
     392                                        break
     393                                }
     394
    385395                                fmt.Fprintf(out, "<%s", tag)
    386396
    387397                                if hasAttrs {
    388                                         if bytes.Equal(tag, []byte("meta")) {
    389                                                 sanitizeMetaAttrs(rc, out, attrs)
    390                                         } else {
    391                                                 sanitizeAttrs(rc, out, attrs)
    392                                         }
     398                                        sanitizeAttrs(rc, out, attrs)
    393399                                }
    394400
     
    400406                                                state = STATE_IN_STYLE
    401407                                        }
     408                                }
     409
     410                                if bytes.Equal(tag, []byte("head")) {
     411                                        fmt.Fprintf(out, HTML_META_CONTENT_TYPE)
    402412                                }
    403413
     
    499509}
    500510
    501 func sanitizeMetaAttrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
     511func sanitizeMetaTag(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
    502512        var http_equiv []byte
    503513        var content []byte
     
    512522                        content = attrValue
    513523                }
    514         }
    515 
     524                if bytes.Equal(attrName, []byte("charset")) {
     525                        // exclude <meta charset="...">
     526                        return
     527                }
     528        }
     529
     530        if bytes.Equal(http_equiv, []byte("content-type")) {
     531                return
     532        }
     533
     534        out.Write([]byte("<meta"))
    516535        urlIndex := bytes.Index(bytes.ToLower(content), []byte("url="))
    517536        if bytes.Equal(http_equiv, []byte("refresh")) && urlIndex != -1 {
     
    530549                sanitizeAttrs(rc, out, attrs)
    531550        }
    532 
     551        out.Write([]byte(">"))
    533552}
    534553
Note: See TracChangeset for help on using the changeset viewer.