-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.v
144 lines (130 loc) · 4.2 KB
/
response.v
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
module cdv
import x.json2 as json
import encoding.base64
pub struct ResponseInfo {
pub:
request_id string @[json: 'requestId']
loader_id string @[json: 'loaderId']
timestamp f64
has_extra_info bool @[json: 'hasExtraInfo']
typ ?string @[json: 'type']
frame_id ?string @[json: 'frameId']
}
pub struct Response {
pub:
url string
status int
status_text string @[json: 'statusText']
headers json.Any
mime_type string @[json: 'mimeType']
charset string
request_headers ?json.Any @[json: 'requestHeaders']
connection_reused bool @[json: 'connectionReused']
connection_id f64 @[json: 'connectionId']
remote_ip_address ?string @[json: 'remoteIPAddress']
remote_port ?int @[json: 'remotePort']
from_disk_cache ?bool @[json: 'fromDiskCache']
from_service_worker ?bool @[json: 'fromServiceWorker']
from_prefetch_cache ?bool @[json: 'fromPrefetchCache']
from_early_hints ?bool @[json: 'fromEarlyHints']
service_worker_router_info ?json.Any @[json: 'serviceWorkerRouterInfo']
encoded_data_length f64 @[json: 'encodedDataLength']
timing ?json.Any
service_worker_response_source ?string @[json: 'serviceWorkerResponseSource']
response_time ?f64 @[json: 'responseTime']
cache_storage_cache_name ?string @[json: 'cacheStorageCacheName']
protocol ?string @[json: 'protocol']
alternate_protocol_usage ?string @[json: 'alternateProtocolUsage']
security_details ?json.Any @[json: 'securityDetails']
security_state string @[json: 'securityState']
pub mut:
info &ResponseInfo = unsafe { nil } @[json: '-']
page &Page = unsafe { nil } @[json: '-']
index int @[json: '-']
}
pub type EventResponse = fn (mut res Response) !bool
pub type EventResponseRef = fn (mut res Response, ref voidptr) !bool
pub struct DataResponse {
pub:
cb EventResponse = unsafe { nil }
cb_ref EventResponseRef = unsafe { nil }
pub mut:
ref voidptr
response ?Response
index int
}
fn (mut page Page) build_on_response(mut data DataResponse) {
page.on('response', fn (mut msg Message, mut data DataResponse) !bool {
mut res := msg.get_response()!
res.index = data.index
data.index++
if !isnil(data.cb) {
return data.cb(mut res)!
}
return data.cb_ref(mut res, data.ref)!
}, ref: data)
}
pub fn (mut page Page) on_response(cb EventResponse) {
mut data := &DataResponse{
cb: cb
}
page.build_on_response(mut data)
}
pub fn (mut page Page) on_response_ref(cb EventResponseRef, ref voidptr) {
mut data := &DataResponse{
cb_ref: cb
ref: ref
}
page.build_on_response(mut data)
}
pub fn (mut res Response) get_body() json.Any {
req_id := res.info.request_id
res_body := res.page.send_or_noop('Network.getResponseBody',
params: {
'requestId': req_id
}
).result
if body := res_body['body'] {
is_encoded := res_body['base64Encoded'] or { false }.bool()
if is_encoded {
return json.Any(base64.decode_str(body.str()))
}
return body
}
return json.Any{}
}
pub fn (res Response) done() bool {
return cdv_msg_done
}
pub fn (res Response) next() bool {
return cdv_msg_next
}
pub fn (mut page Page) wait_for_response(cb EventResponse, opts ParamTimeout) ?Response {
mut data := &DataResponse{
cb: cb
}
page.on_response_ref(fn (mut res Response, mut data DataResponse) !bool {
is_done := data.cb(mut res)!
if is_done {
data.response = res
}
return is_done
}, data)
page.wait_for(opts.timeout)
return data.response
}
pub fn (mut page Page) wait_for_response_ref(cb EventResponseRef, ref voidptr, opts ParamTimeout) ?Response {
mut data := &DataResponse{
cb_ref: cb
ref: ref
}
page.on_response_ref(fn (mut res Response, mut data DataResponse) !bool {
is_done := data.cb_ref(mut res, data.ref)!
if is_done {
data.response = res
}
return is_done
}, data)
page.wait_for(opts.timeout)
return data.response
}