forked from geziyor/geziyor
/
request.go
75 lines (60 loc) · 1.6 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
package client
import (
"io"
"net/http"
)
type ResourceType int
const (
ResourceTypeDocument ResourceType = 1 << iota
ResourceTypeStylesheet
ResourceTypeImage
ResourceTypeMedia
ResourceTypeFont
ResourceTypeScript
ResourceTypeTextTrack
ResourceTypeXHR
ResourceTypeFetch
ResourceTypeEventSource
ResourceTypeWebSocket
ResourceTypeManifest
ResourceTypeSignedExchange
ResourceTypePing
ResourceTypeCSPViolationReport
ResourceTypeOther
)
// Request is a small wrapper around *http.Request that contains Metadata and Rendering option
type Request struct {
*http.Request
// Meta contains arbitrary data.
// Use this Meta map to store contextual data between your requests
Meta map[string]interface{}
// If true, requests will be synchronized
Synchronized bool
// If true request will be opened in Chrome and
// fully rendered HTML DOM response will returned as response
Rendered bool
// Optional response body encoding. Leave empty for automatic detection.
// If you're having issues with auto detection, set this.
Encoding string
// Set this true to cancel requests. Should be used on middlewares.
Cancelled bool
// Resources that will be disabled
Disabled ResourceType
retryCounter int
}
// Cancel request
func (r *Request) Cancel() {
r.Cancelled = true
}
// NewRequest returns a new Request given a method, URL, and optional body.
func NewRequest(method, url string, body io.Reader) (*Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
request := Request{
Request: req,
Meta: make(map[string]interface{}),
}
return &request, nil
}