forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork_agent.cc
160 lines (140 loc) · 4.76 KB
/
network_agent.cc
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "network_agent.h"
#include "network_inspector.h"
namespace node {
namespace inspector {
namespace protocol {
std::unique_ptr<Network::Request> createRequest(
const String& url,
const String& method,
std::unique_ptr<Network::Headers> headers) {
return Network::Request::create()
.setUrl(url)
.setMethod(method)
.setHeaders(std::move(headers))
.build();
}
std::unique_ptr<Network::Response> createResponse(
const String& url,
int status,
const String& statusText,
std::unique_ptr<Network::Headers> headers) {
return Network::Response::create()
.setUrl(url)
.setStatus(status)
.setStatusText(statusText)
.setHeaders(std::move(headers))
.build();
}
NetworkAgent::NetworkAgent(NetworkInspector* inspector,
v8_inspector::V8Inspector* v8_inspector)
: inspector_(inspector), v8_inspector_(v8_inspector) {
event_notifier_map_["requestWillBeSent"] = &NetworkAgent::requestWillBeSent;
event_notifier_map_["responseReceived"] = &NetworkAgent::responseReceived;
event_notifier_map_["loadingFailed"] = &NetworkAgent::loadingFailed;
event_notifier_map_["loadingFinished"] = &NetworkAgent::loadingFinished;
}
void NetworkAgent::emitNotification(
const String& event, std::unique_ptr<protocol::DictionaryValue> params) {
if (!inspector_->IsEnabled()) return;
auto it = event_notifier_map_.find(event);
if (it != event_notifier_map_.end()) {
(this->*(it->second))(std::move(params));
}
}
void NetworkAgent::Wire(UberDispatcher* dispatcher) {
frontend_ = std::make_unique<Network::Frontend>(dispatcher->channel());
Network::Dispatcher::wire(dispatcher, this);
}
DispatchResponse NetworkAgent::enable() {
inspector_->Enable();
return DispatchResponse::Success();
}
DispatchResponse NetworkAgent::disable() {
inspector_->Disable();
return DispatchResponse::Success();
}
void NetworkAgent::requestWillBeSent(
std::unique_ptr<protocol::DictionaryValue> params) {
String request_id;
params->getString("requestId", &request_id);
double timestamp;
params->getDouble("timestamp", ×tamp);
double wall_time;
params->getDouble("wallTime", &wall_time);
auto request = params->getObject("request");
String url;
request->getString("url", &url);
String method;
request->getString("method", &method);
std::unique_ptr<Network::Initiator> initiator =
Network::Initiator::create()
.setType(Network::Initiator::TypeEnum::Script)
.setStack(
v8_inspector_->captureStackTrace(true)->buildInspectorObject(0))
.build();
ErrorSupport errors;
errors.Push();
errors.SetName("headers");
auto headers =
Network::Headers::fromValue(request->getObject("headers"), &errors);
if (!errors.Errors().empty()) {
headers = std::make_unique<Network::Headers>(DictionaryValue::create());
}
frontend_->requestWillBeSent(request_id,
createRequest(url, method, std::move(headers)),
std::move(initiator),
timestamp,
wall_time);
}
void NetworkAgent::responseReceived(
std::unique_ptr<protocol::DictionaryValue> params) {
String request_id;
params->getString("requestId", &request_id);
double timestamp;
params->getDouble("timestamp", ×tamp);
String type;
params->getString("type", &type);
auto response = params->getObject("response");
String url;
response->getString("url", &url);
int status;
response->getInteger("status", &status);
String statusText;
response->getString("statusText", &statusText);
ErrorSupport errors;
errors.Push();
errors.SetName("headers");
auto headers =
Network::Headers::fromValue(response->getObject("headers"), &errors);
if (!errors.Errors().empty()) {
headers = std::make_unique<Network::Headers>(DictionaryValue::create());
}
frontend_->responseReceived(
request_id,
timestamp,
type,
createResponse(url, status, statusText, std::move(headers)));
}
void NetworkAgent::loadingFailed(
std::unique_ptr<protocol::DictionaryValue> params) {
String request_id;
params->getString("requestId", &request_id);
double timestamp;
params->getDouble("timestamp", ×tamp);
String type;
params->getString("type", &type);
String error_text;
params->getString("errorText", &error_text);
frontend_->loadingFailed(request_id, timestamp, type, error_text);
}
void NetworkAgent::loadingFinished(
std::unique_ptr<protocol::DictionaryValue> params) {
String request_id;
params->getString("requestId", &request_id);
double timestamp;
params->getDouble("timestamp", ×tamp);
frontend_->loadingFinished(request_id, timestamp);
}
} // namespace protocol
} // namespace inspector
} // namespace node