@@ -76,69 +76,68 @@ -(void)stopObserving {
7676RCT_EXPORT_METHOD (registerNetworkLogsListener: (NetworkListenerType) listenerType) {
7777 switch (listenerType) {
7878 case NetworkListenerTypeFiltering:
79- NSLog (@" Andrew: Network logs filtering enabled" );
8079 [self setupRequestFilteringHandler ];
8180 break ;
8281
8382 case NetworkListenerTypeObfuscation:
84- NSLog (@" Andrew: Network logs obfuscation enabled" );
8583 [self setupRequestObfuscationHandler ];
8684 break ;
8785
8886 case NetworkListenerTypeBoth:
89- NSLog ( @" Andrew: Both filtering and obfuscation enabled " );
90- [self setupRequestFilteringAndObfuscationHandler ];
87+ // The obfuscation handler sends additional data to the JavaScript side. If filtering is applied, the request will be ignored; otherwise, it will be obfuscated and saved in the database.
88+ [self setupRequestObfuscationHandler ];
9189 break ;
9290
9391 default :
94- NSLog (@" Andrew: Unknown NetworkListenerType" );
92+ NSLog (@" Unknown NetworkListenerType" );
9593 break ;
9694 }
9795}
9896
9997
100- RCT_EXPORT_METHOD (updateNetworkLogSnapshot:(NSString * _Nonnull)jsonString) {
101- // Properly initialize the NSMutableURLRequest
102- NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] init ];
103-
104- // Convert jsonString to NSData
105- NSData *data = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
106-
107- // Parse the JSON into a dictionary
108- NSError *error = nil ;
109- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error];
110-
111- // Check for JSON parsing errors
112- if (error) {
113- NSLog (@" Failed to parse JSON: %@ " , error);
98+ RCT_EXPORT_METHOD (updateNetworkLogSnapshot:(NSString * _Nonnull)url
99+ callbackID:(NSString * _Nonnull)callbackID
100+ requestBody:(NSString * _Nullable)requestBody
101+ responseBody:(NSString * _Nullable)responseBody
102+ responseCode:(double )responseCode
103+ requestHeaders:(NSDictionary * _Nullable)requestHeaders
104+ responseHeaders:(NSDictionary * _Nullable)responseHeaders)
105+ {
106+ // Validate and construct the URL
107+ NSURL *requestURL = [NSURL URLWithString: url];
108+ if (!requestURL) {
109+ NSLog (@" Invalid URL: %@ " , url);
114110 return ;
115111 }
116112
117- // Set the URL, HTTP body, and headers
118- request.URL = [NSURL URLWithString: dict[@" url" ]];
119- request.HTTPBody = [dict[@" requestBody" ] dataUsingEncoding: NSUTF8StringEncoding];
113+ // Initialize the NSMutableURLRequest
114+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] initWithURL: requestURL];
115+
116+ // Set the HTTP body if provided
117+ if (requestBody && [requestBody isKindOfClass: [NSString class ]]) {
118+ request.HTTPBody = [requestBody dataUsingEncoding: NSUTF8StringEncoding];
119+ }
120120
121- // Ensure requestHeader is a dictionary
122- if ([dict[ @" requestHeader " ] isKindOfClass: [NSDictionary class ]]) {
123- request.allHTTPHeaderFields = dict[ @" requestHeader " ] ;
121+ // Ensure requestHeaders is a valid dictionary before setting it
122+ if (requestHeaders && [requestHeaders isKindOfClass: [NSDictionary class ]]) {
123+ request.allHTTPHeaderFields = requestHeaders ;
124124 } else {
125- NSLog (@" Invalid requestHeader format" );
126- // return;
125+ NSLog (@" Invalid requestHeaders format, expected NSDictionary." );
127126 }
128127
129- // Ensure self.completion is not nil before calling it
130- NSString *callbackID = dict[@" id" ];
131- if ([callbackID isKindOfClass: [NSString class ]] && self.requestObfuscationCompletionDictionary [callbackID] != nil ) {
132- ((IBGURLRequestAsyncObfuscationCompletedHandler)self.requestObfuscationCompletionDictionary [callbackID])(request);
128+ // Ensure callbackID is valid and the completion handler exists
129+ IBGURLRequestAsyncObfuscationCompletedHandler completionHandler = self.requestObfuscationCompletionDictionary [callbackID];
130+ if (callbackID && [callbackID isKindOfClass: [NSString class ]] && completionHandler) {
131+ // Call the completion handler with the constructed request
132+ completionHandler (request);
133133 } else {
134- NSLog (@" Not Available Completion " );
134+ NSLog (@" CallbackID not found or completion handler is unavailable for CallbackID: %@ " , callbackID );
135135 }
136-
137136}
138137
139138RCT_EXPORT_METHOD (setNetworkLoggingRequestFilterPredicateIOS: (NSString * _Nonnull) callbackID : (BOOL )value ){
140139
141- if ([callbackID isKindOfClass: [ NSString class ]] && self.requestFilteringCompletionDictionary [callbackID] != nil ) {
140+ if (self.requestFilteringCompletionDictionary [callbackID] != nil ) {
142141 // ⬇️ YES == Request will be saved, NO == will be ignored
143142 ((IBGURLRequestResponseAsyncFilteringCompletedHandler)self.requestFilteringCompletionDictionary [callbackID])(value);
144143 } else {
@@ -178,28 +177,8 @@ - (void)setupRequestObfuscationHandler {
178177 }];
179178}
180179
181- // Set up the obfuscation handler
182- - (void )setupRequestFilteringAndObfuscationHandler {
183-
184- NSString *callbackID = [[[NSUUID alloc ] init ] UUIDString ];
185-
186- [IBGNetworkLogger setCPRequestAsyncObfuscationHandler: ^(NSURLRequest * _Nonnull request, void (^ _Nonnull completion)(NSURLRequest * _Nonnull)) {
187- self.requestObfuscationCompletionDictionary [callbackID] = completion;
188- }];
189-
190- [IBGNetworkLogger setCPRequestAsyncObfuscationHandler: ^(NSURLRequest * _Nonnull request, void (^ _Nonnull completion)(NSURLRequest * _Nonnull)) {
191- self.requestObfuscationCompletionDictionary [callbackID] = completion;
192-
193- NSDictionary *dict = [self createNetworkRequestDictForRequest: request callbackID: callbackID];
194- if (hasListeners){
195- [self sendEventWithName: @" IBGNetworkLoggerHandler" body: dict];
196- }
197-
198- }];
199- }
200-
201180// Helper to create a dictionary from the request and callbackID
202- - (NSDictionary *)createNetworkRequestDictForRequest : (NSURLRequest *)request callbackID : (NSString *)callbackID {
181+ - (NSDictionary *)createNetworkRequestDictForRequest : (NSURLRequest *)request callbackID : (NSString *)callbackID {
203182 NSString *urlString = request.URL .absoluteString ?: @" " ;
204183 NSString *bodyString = [[NSString alloc ] initWithData: request.HTTPBody encoding: NSUTF8StringEncoding] ?: @" " ;
205184 NSDictionary *headerDict = request.allHTTPHeaderFields ?: @{};
0 commit comments