Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#import <React/RCTUtils.h>
#import <react/networking/NetworkReporter.h>

#include <mutex>

using namespace facebook::react;

#ifdef REACT_NATIVE_DEBUGGER_ENABLED
Expand Down Expand Up @@ -72,6 +74,8 @@ Headers convertNSDictionaryToHeaders(const NSDictionary<NSString *, NSString *>

// Dictionary to buffer incremental response bodies (CDP debugging active only)
static const NSMutableDictionary<NSString *, NSMutableString *> *responseBuffers = nil;
// Reporters can call from different threads, so every access to responseBuffers holds this
static std::mutex responseBuffersMutex;

#endif

Expand Down Expand Up @@ -135,16 +139,17 @@ + (void)reportResponseEnd:(NSString *)requestId encodedDataLength:(int)encodedDa

#ifdef REACT_NATIVE_DEBUGGER_ENABLED
// Debug build: Check for buffered response body and flush to NetworkReporter
if (responseBuffers != nullptr) {
NSMutableString *buffer = responseBuffers[requestId];
if (buffer != nullptr) {
if (buffer.length > 0) {
NetworkReporter::getInstance().storeResponseBody(
requestId.UTF8String, RCTStringViewFromNSString(buffer), false);
}
NSMutableString *buffer = nil;
{
std::lock_guard<std::mutex> lock(responseBuffersMutex);
if (responseBuffers != nullptr) {
buffer = responseBuffers[requestId];
[responseBuffers removeObjectForKey:requestId];
}
}
if (buffer.length > 0) {
NetworkReporter::getInstance().storeResponseBody(requestId.UTF8String, RCTStringViewFromNSString(buffer), false);
}
#endif
}

Expand All @@ -154,6 +159,7 @@ + (void)reportRequestFailed:(NSString *)requestId cancelled:(BOOL)cancelled

#ifdef REACT_NATIVE_DEBUGGER_ENABLED
// Debug build: Clear buffer for request
std::lock_guard<std::mutex> lock(responseBuffersMutex);
if (responseBuffers != nullptr) {
[responseBuffers removeObjectForKey:requestId];
}
Expand Down Expand Up @@ -196,6 +202,7 @@ + (void)maybeStoreResponseBodyIncremental:(NSString *)requestId data:(NSString *
return;
}

std::lock_guard<std::mutex> lock(responseBuffersMutex);
if (responseBuffers == nullptr) {
responseBuffers = [NSMutableDictionary dictionary];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <react/networking/NetworkReporter.h>

#include <cstddef>
#include <mutex>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: should this go inside the REACT_NATIVE_DEBUGGER_ENABLED block with <unordered_map>?

#include <string>
#ifdef REACT_NATIVE_DEBUGGER_ENABLED
#include <unordered_map>
Expand Down Expand Up @@ -55,6 +56,8 @@ std::string limitRequestBodySize(std::string requestBody) {

// Dictionary to buffer incremental response bodies (CDP debugging active only)
static std::unordered_map<std::string, std::string> responseBuffers;
// Requests report from different threads, so every access holds this
static std::mutex responseBuffersMutex;

#endif

Expand Down Expand Up @@ -126,11 +129,17 @@ static std::unordered_map<std::string, std::string> responseBuffers;

#ifdef REACT_NATIVE_DEBUGGER_ENABLED
// Debug build: Check for buffered response body and flush to NetworkReporter
auto it = responseBuffers.find(requestIdStr);
if (it != responseBuffers.end() && !it->second.empty()) {
NetworkReporter::getInstance().storeResponseBody(
requestIdStr, it->second, false);
responseBuffers.erase(it);
std::string body;
{
std::lock_guard<std::mutex> lock(responseBuffersMutex);
auto it = responseBuffers.find(requestIdStr);
if (it != responseBuffers.end()) {
body = std::move(it->second);
responseBuffers.erase(it);
}
}
if (!body.empty()) {
NetworkReporter::getInstance().storeResponseBody(requestIdStr, body, false);
}
#endif
}
Expand Down Expand Up @@ -173,8 +182,9 @@ JInspectorNetworkReporter::maybeStoreResponseBodyIncrementalImpl(
}

auto requestIdStr = requestId->toStdString();
auto& buffer = responseBuffers[requestIdStr];
buffer += data->toStdString();
auto dataStr = data->toStdString();
std::lock_guard<std::mutex> lock(responseBuffersMutex);
responseBuffers[requestIdStr] += dataStr;
#endif
}

Expand Down
Loading