Simplify response type check

This commit is contained in:
Radhi Fadlillah 2018-05-20 22:40:03 +07:00
parent b9f2b4bd58
commit a915874208
2 changed files with 6 additions and 19 deletions

View file

@ -1128,14 +1128,14 @@ func FromURL(url *nurl.URL, timeout time.Duration) (Article, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
// If response is not HTML, stop process // Check content type. If not HTML, stop process
mimeType, err := getMimeType(resp.Body) contentType := resp.Header.Get("Content-type")
if err != nil { if contentType == "" {
return Article{}, err contentType = "application/octet-stream"
} }
if !strings.HasPrefix(mimeType, "text/html") { if !strings.HasPrefix(contentType, "text/html") {
return Article{}, fmt.Errorf("URL must be a text/html, found %s", mimeType) return Article{}, fmt.Errorf("URL must be a text/html, found %s", contentType)
} }
// Parse response body // Parse response body

View file

@ -3,8 +3,6 @@ package readability
import ( import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"io"
"net/http"
"os" "os"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -70,17 +68,6 @@ func removeSeparator(str string, separators ...string) string {
return strings.Join(finalWords, " ") return strings.Join(finalWords, " ")
} }
func getMimeType(resp io.Reader) (string, error) {
buffer := make([]byte, 512)
_, err := resp.Read(buffer)
if err != nil {
return "", err
}
mimeType := http.DetectContentType(buffer)
return mimeType, nil
}
func normalizeText(str string) string { func normalizeText(str string) string {
return strings.Join(strings.Fields(str), " ") return strings.Join(strings.Fields(str), " ")
} }