-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
har_router.go
110 lines (103 loc) · 2.46 KB
/
har_router.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
package playwright
import (
"errors"
)
type harRouter struct {
localUtils *localUtilsImpl
harId string
notFoundAction HarNotFound
urlOrPredicate interface{}
err error
}
func (r *harRouter) addContextRoute(context BrowserContext) error {
if r.err != nil {
return r.err
}
err := context.Route(r.urlOrPredicate, func(route Route) {
err := r.handle(route)
if err != nil {
logger.Error("Error handling context route", "error", err)
}
})
if err != nil {
return err
}
return r.err
}
func (r *harRouter) addPageRoute(page Page) error {
if r.err != nil {
return r.err
}
err := page.Route(r.urlOrPredicate, func(route Route) {
err := r.handle(route)
if err != nil {
logger.Error("Error handling page route", "error", err)
}
})
if err != nil {
return err
}
return r.err
}
func (r *harRouter) dispose() {
go r.localUtils.HarClose(r.harId)
}
func (r *harRouter) handle(route Route) error {
if r.err != nil {
return r.err
}
request := route.Request()
postData, err := request.PostDataBuffer()
if err != nil {
return err
}
response, err := r.localUtils.HarLookup(harLookupOptions{
HarId: r.harId,
URL: request.URL(),
Method: request.Method(),
Headers: request.Headers(),
IsNavigationRequest: request.IsNavigationRequest(),
PostData: postData,
})
if err != nil {
return err
}
switch response.Action {
case "redirect":
if response.RedirectURL == nil {
return errors.New("redirect url is null")
}
return route.(*routeImpl).redirectedNavigationRequest(*response.RedirectURL)
case "fulfill":
if response.Body == nil {
return errors.New("fulfill body is null")
}
return route.Fulfill(RouteFulfillOptions{
Body: *response.Body,
Status: response.Status,
Headers: deserializeNameAndValueToMap(response.Headers),
})
case "error":
logger.Error("har action error", "error", *response.Message)
fallthrough
case "noentry":
}
if r.notFoundAction == *HarNotFoundAbort {
return route.Abort()
}
return route.Fallback()
}
func newHarRouter(localUtils *localUtilsImpl, file string, notFoundAction HarNotFound, urlOrPredicate interface{}) *harRouter {
harId, err := localUtils.HarOpen(file)
var url interface{} = "**/*"
if urlOrPredicate != nil {
url = urlOrPredicate
}
return &harRouter{
localUtils: localUtils,
harId: harId,
notFoundAction: notFoundAction,
urlOrPredicate: url,
err: err,
}
}