Repository navigation
Expand file tree
/
Copy pathrequest.v
More file actions
146 lines (131 loc) · 3.82 KB
/
Copy pathrequest.v
File metadata and controls
146 lines (131 loc) · 3.82 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
module cdv
import x.json2 as json
pub struct RequestInfo {
pub:
request_id string @[json: 'requestId']
loader_id string @[json: 'loaderId']
document_url string @[json: 'documentURL']
timestamp f64
wall_time f64 @[json: 'wallTime']
initiator json.Any
redirect_has_extra_info bool @[json: 'redirectHasExtraInfo']
redirect_response ?json.Any @[json: 'redirectResponse']
typ ?string @[json: 'type']
frame_id ?string @[json: 'frameId']
has_user_gesture ?bool @[json: 'hasUserGesture']
}
pub struct Request {
pub:
url string
method string
headers json.Any
url_fragment ?string @[json: 'urlFragment']
has_post_data_ ?bool @[json: 'hasPostData']
post_data_entries ?json.Any @[json: 'postDataEntries']
mixed_content_type ?string @[json: 'mixedContentType']
initial_priority string @[json: 'initialPriority']
referrer_policy string @[json: 'referrerPolicy']
is_link_preload ?bool @[json: 'isLinkPreload']
trust_token_params ?json.Any @[json: 'trustTokenParams']
is_same_site ?bool @[json: 'isSameSite']
pub mut:
info &RequestInfo = unsafe { nil } @[json: '-']
page &Page = unsafe { nil } @[json: '-']
has_post_data bool @[json: '-']
index int @[json: '-']
}
pub type EventRequest = fn (mut req Request) !bool
pub type EventRequestRef = fn (mut req Request, ref voidptr) !bool
pub struct DataRequest {
pub:
cb EventRequest = unsafe { nil }
cb_ref EventRequestRef = unsafe { nil }
pub mut:
ref voidptr
request ?Request
index int
}
fn (mut page Page) build_on_request(mut data DataRequest) {
page.on('request', fn (mut msg Message, mut data DataRequest) !bool {
mut req := msg.get_request()!
req.index = data.index
data.index++
if !isnil(data.cb) {
return data.cb(mut req)!
}
return data.cb_ref(mut req, data.ref)!
}, ref: data)
}
pub fn (mut page Page) on_request(cb EventRequest) {
mut data := &DataRequest{
cb: cb
}
page.build_on_request(mut data)
}
pub fn (mut page Page) on_request_ref(cb EventRequestRef, ref voidptr) {
mut data := &DataRequest{
cb_ref: cb
ref: ref
}
page.build_on_request(mut data)
}
pub fn (mut req Request) get_post_data() json.Any {
req_id := req.info.request_id
post_data := req.page.send_or_noop('Network.getRequestPostData',
params: {
'requestId': req_id
}
).result['postData'] or { json.Any{} }
return post_data
}
pub fn (req Request) done() bool {
return cdv_msg_done
}
pub fn (req Request) next() bool {
return cdv_msg_next
}
pub fn (mut req Request) redirect_response() ?Response {
mut info := req.info
if resp := info.redirect_response {
mut res := json.decode[Response](resp.json_str()) or { req.page.noop(err) }
res.info = &ResponseInfo{
request_id: info.request_id
loader_id: info.loader_id
timestamp: info.timestamp
typ: info.typ
frame_id: info.frame_id
}
res.page = req.page
return res
}
return none
}
pub fn (mut page Page) wait_for_request(cb EventRequest, opts ParamTimeout) ?Request {
mut data := &DataRequest{
cb: cb
}
page.on_request_ref(fn (mut req Request, mut data DataRequest) !bool {
is_done := data.cb(mut req)!
if is_done {
data.request = req
}
return is_done
}, data)
page.wait_for(opts.timeout)
return data.request
}
pub fn (mut page Page) wait_for_request_ref(cb EventRequestRef, ref voidptr, opts ParamTimeout) ?Request {
mut data := &DataRequest{
cb_ref: cb
ref: ref
}
page.on_request_ref(fn (mut req Request, mut data DataRequest) !bool {
is_done := data.cb_ref(mut req, data.ref)!
if is_done {
data.request = req
}
return is_done
}, data)
page.wait_for(opts.timeout)
return data.request
}