diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2015-07-18 19:41:18 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2015-07-18 19:41:18 +0200 |
commit | 13ec57cb1aeb58271cb0cde6b0ca4cd71fdfb177 (patch) | |
tree | eaa7b2fe84c3275a3ec924284513ac753e07daac /href.go | |
parent | 8b1d30985c12c0e0f94ad46bb941cf4a8c5c99a8 (diff) |
Check content type
Diffstat (limited to 'href.go')
-rw-r--r-- | href.go | 19 |
1 files changed, 16 insertions, 3 deletions
@@ -1,13 +1,20 @@ package main import ( + "errors" "net/http" "strings" - "errors" "golang.org/x/net/html" ) +var ( + notHTML = errors.New("not HTML content") + tooBig = errors.New("cotent too big") +) + +const MB = 1024 * 1024 + func findTitle(n *html.Node) (s string) { if n.Type == html.ElementNode && n.Data == "title" { for c := n.FirstChild; c != nil; c = c.NextSibling { @@ -29,8 +36,14 @@ func FetchTitle(url string) (string, error) { return "", err } defer resp.Body.Close() - if resp.ContentLength > 8*1024*1024 { - return "", errors.New("content too big") + + ct := resp.Header.Get("Content-Type") + if !strings.HasPrefix(ct, "text/html") { + return "", notHTML + } + + if resp.ContentLength > 8*MB { + return "", tooBig } doc, err := html.Parse(resp.Body) |