/
request.go
133 lines (113 loc) · 3.17 KB
/
request.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package crawler
import (
"database/sql"
"errors"
"log"
"net/http"
"net/url"
"time"
"github.com/ReanGD/go-web-search/database"
"github.com/ReanGD/go-web-search/proxy"
"github.com/ReanGD/go-web-search/werrors"
"github.com/uber-go/zap"
)
type request struct {
hostMng *hostsManager
client *http.Client
meta *proxy.Meta
urls map[string]sql.NullInt64
logger zap.Logger
}
func (r *request) get(u *url.URL) (int64, error) {
urlStr := u.String()
r.urls = make(map[string]sql.NullInt64)
hostID, robotOk := r.hostMng.CheckURL(u)
r.meta = proxy.NewMeta(hostID, urlStr, nil)
if !hostID.Valid {
r.meta.SetState(database.StateExternal)
}
if !robotOk {
r.meta.SetState(database.StateDisabledByRobotsTxt)
log.Printf("INFO: URL %s blocked by robot.txt", urlStr)
return 0, nil
}
startTime := time.Now()
request := &http.Request{
Method: "GET",
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: map[string][]string{
"User-Agent": {"Mozilla/5.0 (compatible; GoWebSearch/0.1)"},
"Accept": {"text/html;q=0.9,*/*;q=0.1"},
"Accept-Encoding": {"gzip;q=0.9,identity;q=0.5,*;q=0.1"},
"Accept-Language": {"ru-RU,ru;q=0.9,en-US;q=0.2,en;q=0.1"},
"Accept-Charset": {"utf-8;q=0.9,windows-1251;q=0.8,koi8-r;q=0.7,*;q=0.1"},
},
Body: nil,
Host: u.Host,
}
response, err := r.client.Do(request)
if err != nil {
r.meta.SetState(database.StateConnectError)
return 0, err
}
if r.meta.GetState() != database.StateSuccess {
// here or early - logging!!!
return 0, nil
}
// hostID, robotOk := r.hostMng.CheckURL(u)
// if !robotOk {
// r.meta.SetState(database.StateDisabledByRobotsTxt)
// return fmt.Errorf("INFO: URL %s blocked by robot.txt", NormalizeURL(©URL))
// }
loggerURL := r.logger.With(zap.String("url", r.meta.GetURL()))
RequestDurationMs := int64(time.Since(startTime) / time.Millisecond)
loggerURL.Debug(DbgRequestDuration, zap.Int64("duration", RequestDurationMs))
parser := newResponseParser(loggerURL, r.hostMng, r.meta)
err = parser.Run(response)
if err == nil {
r.urls = parser.URLs
} else {
werrors.LogError(loggerURL, err)
err = nil
}
return parser.BodyDurationMs, err
}
// Send - load and parse the urlStr
// urlStr - valid URL
func (r *request) Process(u *url.URL) (*proxy.PageData, int64) {
duration, err := r.get(u)
if err != nil {
log.Printf("ERROR: Get URL %s, message: %s", u.String(), err)
}
return proxy.NewPageData(r.meta, r.urls), duration
}
// Init - init request structure
func (r *request) Init(logger zap.Logger) {
r.client = new(http.Client)
r.logger = logger
r.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
if len(via) == 0 {
return nil
}
r.meta.SetState(database.StateDublicate)
r.meta.SetStatusCode(301)
hostID, _ := r.hostMng.CheckURL(req.URL)
copyURL := *req.URL
r.meta = proxy.NewMeta(hostID, NormalizeURL(©URL), r.meta)
if !hostID.Valid {
r.meta.SetState(database.StateExternal)
}
for attr, val := range via[0].Header {
if _, ok := req.Header[attr]; !ok {
req.Header[attr] = val
}
}
return nil
}
}