Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@
'test/cctest/test_quic_tokens.cc',
],
'node_cctest_inspector_sources': [
'test/cctest/inspector/test_network_requests_buffer.cc',
'test/cctest/inspector/test_node_protocol.cc',
'test/cctest/test_inspector_socket.cc',
'test/cctest/test_inspector_socket_server.cc',
Expand Down
68 changes: 42 additions & 26 deletions src/inspector/network_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ using v8::Object;
using v8::Uint8Array;
using v8::Value;

constexpr size_t kDefaultMaxTotalBufferSize = 100 * 1024 * 1024; // 100MB

// Get a protocol string property from the object.
Maybe<protocol::String> ObjectGetProtocolString(v8::Local<v8::Context> context,
Local<Object> object,
Expand Down Expand Up @@ -246,7 +248,8 @@ NetworkAgent::NetworkAgent(
: inspector_(inspector),
v8_inspector_(v8_inspector),
env_(env),
network_resource_manager_(std::move(network_resource_manager)) {
network_resource_manager_(std::move(network_resource_manager)),
requests_(kDefaultMaxTotalBufferSize) {
event_notifier_map_["requestWillBeSent"] = &NetworkAgent::requestWillBeSent;
event_notifier_map_["responseReceived"] = &NetworkAgent::responseReceived;
event_notifier_map_["loadingFailed"] = &NetworkAgent::loadingFailed;
Expand Down Expand Up @@ -329,8 +332,15 @@ void NetworkAgent::Wire(protocol::UberDispatcher* dispatcher) {
protocol::Network::Dispatcher::wire(dispatcher, this);
}

protocol::DispatchResponse NetworkAgent::enable() {
protocol::DispatchResponse NetworkAgent::enable(
std::optional<int> in_maxTotalBufferSize,
std::optional<int> in_maxResourceBufferSize) {
inspector_->Enable();
requests_ = RequestsBuffer(
in_maxTotalBufferSize.value_or(kDefaultMaxTotalBufferSize));
if (in_maxResourceBufferSize) {
max_resource_buffer_size_ = *in_maxResourceBufferSize;
}
return protocol::DispatchResponse::Success();
}

Expand All @@ -341,7 +351,7 @@ protocol::DispatchResponse NetworkAgent::disable() {

protocol::DispatchResponse NetworkAgent::getRequestPostData(
const protocol::String& in_requestId, protocol::String* out_postData) {
auto request_entry = requests_.find(in_requestId);
auto request_entry = requests_.cfind(in_requestId);
if (request_entry == requests_.end()) {
// Request not found, ignore it.
return protocol::DispatchResponse::InvalidParams("Request not found");
Expand All @@ -362,7 +372,7 @@ protocol::DispatchResponse NetworkAgent::getRequestPostData(

// Concat response bodies.
protocol::Binary buf =
protocol::Binary::concat(request_entry->second.request_data_blobs);
protocol::Binary::concat(request_entry->second.request_data_blobs());
*out_postData = protocol::StringUtil::fromUTF8(buf.data(), buf.size());
return protocol::DispatchResponse::Success();
}
Expand All @@ -371,7 +381,7 @@ protocol::DispatchResponse NetworkAgent::getResponseBody(
const protocol::String& in_requestId,
protocol::String* out_body,
bool* out_base64Encoded) {
auto request_entry = requests_.find(in_requestId);
auto request_entry = requests_.cfind(in_requestId);
if (request_entry == requests_.end()) {
// Request not found, ignore it.
return protocol::DispatchResponse::InvalidParams("Request not found");
Expand All @@ -391,7 +401,7 @@ protocol::DispatchResponse NetworkAgent::getResponseBody(

// Concat response bodies.
protocol::Binary buf =
protocol::Binary::concat(request_entry->second.response_data_blobs);
protocol::Binary::concat(request_entry->second.response_data_blobs());
if (request_entry->second.response_charset == Charset::kBinary) {
// If the response is binary, we return base64 encoded data.
*out_body = buf.toBase64();
Expand All @@ -410,22 +420,26 @@ protocol::DispatchResponse NetworkAgent::getResponseBody(

protocol::DispatchResponse NetworkAgent::streamResourceContent(
const protocol::String& in_requestId, protocol::Binary* out_bufferedData) {
auto it = requests_.find(in_requestId);
if (it == requests_.end()) {
// Request not found, ignore it.
return protocol::DispatchResponse::InvalidParams("Request not found");
}
auto& request_entry = it->second;
bool is_response_finished = false;
{
auto it = requests_.find(in_requestId);
if (it == requests_.end()) {
// Request not found, ignore it.
return protocol::DispatchResponse::InvalidParams("Request not found");
}
auto& request_entry = it->second;

request_entry.is_streaming = true;
request_entry.is_streaming = true;

// Concat response bodies.
*out_bufferedData =
protocol::Binary::concat(request_entry.response_data_blobs);
// Clear buffered data.
request_entry.response_data_blobs.clear();
// Concat response bodies.
*out_bufferedData =
protocol::Binary::concat(request_entry.response_data_blobs());
// Clear buffered data.
request_entry.clear_response_data_blobs();
is_response_finished = request_entry.is_response_finished;
}

if (request_entry.is_response_finished) {
if (is_response_finished) {
// If the request is finished, remove the entry.
requests_.erase(in_requestId);
}
Expand Down Expand Up @@ -500,9 +514,11 @@ void NetworkAgent::requestWillBeSent(v8::Local<v8::Context> context,
}

auto request_charset = charset == "utf-8" ? Charset::kUTF8 : Charset::kBinary;
requests_.emplace(
request_id,
RequestEntry(timestamp, request_charset, request->getHasPostData()));
requests_.emplace(request_id,
RequestEntry(timestamp,
request_charset,
request->getHasPostData(),
max_resource_buffer_size_));
frontend_->requestWillBeSent(request_id,
std::move(request),
std::move(initiator),
Expand Down Expand Up @@ -580,7 +596,7 @@ void NetworkAgent::loadingFinished(v8::Local<v8::Context> context,

frontend_->loadingFinished(request_id, timestamp);

auto request_entry = requests_.find(request_id);
auto request_entry = requests_.cfind(request_id);
if (request_entry == requests_.end()) {
// No entry found. Ignore it.
return;
Expand All @@ -590,7 +606,7 @@ void NetworkAgent::loadingFinished(v8::Local<v8::Context> context,
// Streaming finished, remove the entry.
requests_.erase(request_id);
} else {
request_entry->second.is_response_finished = true;
requests_.find(request_id)->second.is_response_finished = true;
}
}

Expand Down Expand Up @@ -631,7 +647,7 @@ void NetworkAgent::dataSent(v8::Local<v8::Context> context,
}
Local<Uint8Array> data = data_obj.As<Uint8Array>();
auto data_bin = protocol::Binary::fromUint8Array(data);
request_entry->second.request_data_blobs.push_back(data_bin);
request_entry->second.push_request_data_blob(data_bin);
}

void NetworkAgent::dataReceived(v8::Local<v8::Context> context,
Expand Down Expand Up @@ -673,7 +689,7 @@ void NetworkAgent::dataReceived(v8::Local<v8::Context> context,
frontend_->dataReceived(
request_id, timestamp, data_length, encoded_data_length, data_bin);
} else {
request_entry.response_data_blobs.push_back(data_bin);
request_entry.push_response_data_blob(data_bin);
}
}

Expand Down
37 changes: 10 additions & 27 deletions src/inspector/network_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "env.h"
#include "io_agent.h"
#include "network_requests_buffer.h"
#include "network_resource_manager.h"
#include "node/inspector/protocol/Network.h"

Expand All @@ -15,31 +16,6 @@ namespace inspector {

class NetworkInspector;

// Supported charsets for devtools frontend on request/response data.
// If the charset is kUTF8, the data is expected to be text and can be
// formatted on the frontend.
enum class Charset {
kUTF8,
kBinary,
};

struct RequestEntry {
double timestamp;
bool is_request_finished = false;
bool is_response_finished = false;
bool is_streaming = false;
Charset request_charset;
std::vector<protocol::Binary> request_data_blobs;
Charset response_charset;
std::vector<protocol::Binary> response_data_blobs;

RequestEntry(double timestamp, Charset request_charset, bool has_request_body)
: timestamp(timestamp),
is_request_finished(!has_request_body),
request_charset(request_charset),
response_charset(Charset::kBinary) {}
};

class NetworkAgent : public protocol::Network::Backend {
public:
explicit NetworkAgent(
Expand All @@ -50,7 +26,9 @@ class NetworkAgent : public protocol::Network::Backend {

void Wire(protocol::UberDispatcher* dispatcher);

protocol::DispatchResponse enable() override;
protocol::DispatchResponse enable(
std::optional<int> in_maxTotalBufferSize,
std::optional<int> in_maxResourceBufferSize) override;

protocol::DispatchResponse disable() override;

Expand Down Expand Up @@ -104,12 +82,17 @@ class NetworkAgent : public protocol::Network::Backend {
NetworkInspector* inspector_;
v8_inspector::V8Inspector* v8_inspector_;
std::shared_ptr<protocol::Network::Frontend> frontend_;

using EventNotifier = void (NetworkAgent::*)(v8::Local<v8::Context> context,
v8::Local<v8::Object>);
std::unordered_map<protocol::String, EventNotifier> event_notifier_map_;
std::map<protocol::String, RequestEntry> requests_;
Environment* env_;
std::shared_ptr<NetworkResourceManager> network_resource_manager_;

// Per-resource buffer size in bytes to use when preserving network payloads
// (XHRs, etc).
size_t max_resource_buffer_size_ = 5 * 1024 * 1024; // 5MB
RequestsBuffer requests_;
};

} // namespace inspector
Expand Down
Loading
Loading