Changeset 13 in code for trunk


Ignore:
Timestamp:
Oct 27, 2016, 2:16:10 PM (9 years ago)
Author:
asciimoo
Message:

[fix] exclude dns-prefetch and preloaded scripts - fixes #4

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r12 r13  
    4747        []byte("align"),
    4848        []byte("alt"),
     49        []byte("as"),
    4950        []byte("autocomplete"),
    5051        []byte("charset"),
     
    329330                                }
    330331                                var attrs [][][]byte
    331                                 fmt.Fprintf(out, "<%s", tag)
    332332                                if hasAttrs {
    333333                                        for {
     
    338338                                                }
    339339                                        }
     340                                }
     341                                if bytes.Equal(tag, []byte("link")) {
     342                                        sanitizeLinkTag(rc, out, attrs)
     343                                        break
     344                                }
     345
     346                                fmt.Fprintf(out, "<%s", tag)
     347
     348                                if hasAttrs {
    340349                                        if bytes.Equal(tag, []byte("meta")) {
    341350                                                sanitizeMetaAttrs(rc, out, attrs)
     
    344353                                        }
    345354                                }
     355
    346356                                if token == html.SelfClosingTagToken {
    347357                                        fmt.Fprintf(out, " />")
     
    352362                                        }
    353363                                }
     364
    354365                                if bytes.Equal(tag, []byte("form")) {
    355366                                        var formURL *url.URL
     
    421432}
    422433
     434func sanitizeLinkTag(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
     435        exclude := false
     436        for _, attr := range attrs {
     437                attrName := attr[0]
     438                attrValue := attr[1]
     439                if bytes.Equal(attrName, []byte("rel")) {
     440                        if bytes.Equal(attrValue, []byte("dns-prefetch")) {
     441                                exclude = true
     442                                break
     443                        }
     444                }
     445                if bytes.Equal(attrName, []byte("as")) {
     446                        if bytes.Equal(attrValue, []byte("script")) {
     447                                exclude = true
     448                                break
     449                        }
     450                }
     451        }
     452
     453        if !exclude {
     454                out.Write([]byte("<link"))
     455                for _, attr := range attrs {
     456                        sanitizeAttr(rc, out, attr[0], attr[1])
     457                }
     458                out.Write([]byte(">"))
     459        }
     460}
     461
    423462func sanitizeMetaAttrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
    424463        var http_equiv []byte
Note: See TracChangeset for help on using the changeset viewer.