-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
50 lines (41 loc) · 958 Bytes
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"errors"
"html"
"io"
"io/ioutil"
"log"
"net/http"
"regexp"
)
type URLInfo struct {
title string
err error
}
func ValidateURL(url string) URLInfo {
regex := regexp.MustCompile("<title>(.*)</title>")
log.Printf("Validating %s", url)
res, err := http.Get(url)
if err != nil {
log.Printf("Error %s", err.Error())
return URLInfo{title: "", err: err}
}
defer res.Body.Close()
log.Printf("Returned status code: %d", res.StatusCode)
if (res.StatusCode < 200) || (res.StatusCode > 299) {
return URLInfo{title: "", err: errors.New("URL Not found")}
}
limitedReader := io.LimitReader(res.Body, 8*1024)
page, err := ioutil.ReadAll(limitedReader)
if err != nil {
return URLInfo{title: "", err: err}
}
matches := regex.FindStringSubmatch(string(page))
var title string
title = url
if len(matches) > 1 {
title = matches[1]
}
title = html.UnescapeString(title)
return URLInfo{title: title, err: nil}
}