|
7 | 7 | #import "InstabugNetworkLoggerBridge.h"
|
8 | 8 | #import "Util/IBGNetworkLogger+CP.h"
|
9 | 9 |
|
10 |
| - |
11 | 10 | @implementation InstabugNtworkLoggerBridge
|
12 |
| -//@property NSMutableDictionary<NSString *, IBGURLRequestAsyncObfuscationCompletedHandler> *dictionary; |
13 | 11 |
|
14 | 12 |
|
| 13 | +- (instancetype)init { |
| 14 | + self = [super init]; |
| 15 | + if (self) { |
| 16 | + _requestObfuscationCompletionDictionary = [[NSMutableDictionary alloc] init]; |
| 17 | + _responseObfuscationCompletionDictionary = [[NSMutableDictionary alloc] init]; |
| 18 | + _requestFilteringCompletionDictionary = [[NSMutableDictionary alloc] init]; |
| 19 | + _responseFilteringCompletionDictionary = [[NSMutableDictionary alloc] init]; |
| 20 | + } |
| 21 | + return self; |
| 22 | +} |
15 | 23 |
|
16 | 24 | - (dispatch_queue_t)methodQueue {
|
17 | 25 | return dispatch_get_main_queue();
|
@@ -50,70 +58,136 @@ -(void)stopObserving {
|
50 | 58 | resolve(@(IBGNetworkLogger.isNativeNetworkInterceptionFeatureEnabled));
|
51 | 59 | }
|
52 | 60 |
|
53 |
| -//RCT_EXPORT_METHOD(registerNetworkLogsListener){ |
54 |
| -// |
55 |
| -// [IBGNetworkLogger setRequestAsyncObfuscationHandler:^(NSURLRequest *requestToBeObfuscated, IBGURLRequestAsyncObfuscationCompletedHandler completion) { |
56 |
| -// |
57 |
| -// NSString *tempId = [[[NSUUID alloc] init] UUIDString]; |
58 |
| -// self.dictionary[tempId] = completion; |
59 |
| -// |
60 |
| -// // Ensure the URL, HTTP body, and headers are in the correct format |
61 |
| -// NSString *urlString = requestToBeObfuscated.URL.absoluteString ?: @""; |
62 |
| -// NSString *bodyString = [[NSString alloc] initWithData:requestToBeObfuscated.HTTPBody encoding:NSUTF8StringEncoding] ?: @""; |
63 |
| -// NSDictionary *headerDict = requestToBeObfuscated.allHTTPHeaderFields ?: @{}; |
64 |
| -// |
65 |
| -// // Create the dictionary to send |
66 |
| -// NSDictionary *dict = @{ |
67 |
| -// @"tempId": tempId, |
68 |
| -// @"url": urlString, |
69 |
| -// @"requestBody": bodyString, |
70 |
| -// @"requestHeader": headerDict |
71 |
| -// }; |
72 |
| -// |
73 |
| -// // Send the event |
74 |
| -// [self sendEventWithName:@"IBGNetworkLoggerHandler" body:dict]; |
75 |
| -// |
76 |
| -// }]; |
77 |
| -//} |
| 61 | +RCT_EXPORT_METHOD(registerNetworkLogsListener) { |
| 62 | + |
| 63 | + [IBGNetworkLogger setRequestAsyncObfuscationHandler:^(NSURLRequest * _Nonnull requestToBeObfuscated, |
| 64 | + void (^ _Nonnull completion)(NSURLRequest * _Nonnull)) { |
| 65 | + NSString *tempId = [[[NSUUID alloc] init] UUIDString]; |
| 66 | + self.requestObfuscationCompletionDictionary[tempId] = completion; |
| 67 | + |
| 68 | + // Ensure the URL, HTTP body, and headers are in the correct format |
| 69 | + NSString *urlString = requestToBeObfuscated.URL.absoluteString ?: @""; |
| 70 | + NSString *bodyString = [[NSString alloc] initWithData:requestToBeObfuscated.HTTPBody encoding:NSUTF8StringEncoding] ?: @""; |
| 71 | + NSDictionary *headerDict = requestToBeObfuscated.allHTTPHeaderFields ?: @{}; |
| 72 | + |
| 73 | + // Create the dictionary to send |
| 74 | + NSDictionary *dict = @{ |
| 75 | + @"tempId": tempId, |
| 76 | + @"url": urlString, |
| 77 | + @"requestBody": bodyString, |
| 78 | + @"requestHeader": headerDict |
| 79 | + }; |
| 80 | + |
| 81 | + // Send the event |
| 82 | + [self sendEventWithName:@"IBGNetworkLoggerHandler" body:dict]; |
| 83 | + |
| 84 | + }]; |
| 85 | + |
| 86 | + |
| 87 | + [IBGNetworkLogger setResponseObfuscationHandler:^(NSData * _Nullable responseData, NSURLResponse * _Nonnull response, NetworkObfuscationCompletionBlock _Nonnull completion) { |
| 88 | + |
| 89 | + NSString *tempId = [[[NSUUID alloc] init] UUIDString]; |
| 90 | + self.responseObfuscationCompletionDictionary[tempId] = completion; |
| 91 | + |
| 92 | + |
| 93 | + // MARK: TODO: Convert Response To Dictionary & Pass it To React Native |
| 94 | + |
| 95 | + }]; |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + [IBGNetworkLogger setRequestFilteringHandler:^(NSURLRequest * _Nonnull request, void (^ _Nonnull completion)(BOOL)) { |
| 100 | + |
| 101 | + NSString *tempId = [[[NSUUID alloc] init] UUIDString]; |
| 102 | + self.requestFilteringCompletionDictionary[tempId] = completion; |
| 103 | + |
| 104 | + |
| 105 | + // MARK: TODO: Convert Request To Dictionary & Pass it To React Native |
| 106 | + |
| 107 | + }]; |
| 108 | + |
| 109 | + |
| 110 | + [IBGNetworkLogger setResponseFilteringHandler:^(NSURLResponse * _Nonnull request, void (^ _Nonnull completion)(BOOL)) { |
| 111 | + |
| 112 | + NSString *tempId = [[[NSUUID alloc] init] UUIDString]; |
| 113 | + self.responseFilteringCompletionDictionary[tempId] = completion; |
| 114 | + |
| 115 | + |
| 116 | + // MARK: TODO: Convert Request To Dictionary & Pass it To React Native |
| 117 | + |
| 118 | + }]; |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +} |
78 | 124 |
|
79 | 125 |
|
80 |
| -//RCT_EXPORT_METHOD(updateNetworkLogSnapshot:(NSString * _Nonnull)jsonString) { |
81 |
| -// // Properly initialize the NSMutableURLRequest |
82 |
| -// NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; |
83 |
| -// |
84 |
| -// // Convert jsonString to NSData |
85 |
| -// NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; |
86 |
| -// |
87 |
| -// // Parse the JSON into a dictionary |
88 |
| -// NSError *error = nil; |
89 |
| -// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; |
90 |
| -// |
91 |
| -// // Check for JSON parsing errors |
92 |
| -// if (error) { |
93 |
| -// NSLog(@"Failed to parse JSON: %@", error); |
| 126 | +RCT_EXPORT_METHOD(updateNetworkLogSnapshot:(NSString * _Nonnull)jsonString) { |
| 127 | + // Properly initialize the NSMutableURLRequest |
| 128 | + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; |
| 129 | + |
| 130 | + // Convert jsonString to NSData |
| 131 | + NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; |
| 132 | + |
| 133 | + // Parse the JSON into a dictionary |
| 134 | + NSError *error = nil; |
| 135 | + NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; |
| 136 | + |
| 137 | + // Check for JSON parsing errors |
| 138 | + if (error) { |
| 139 | + NSLog(@"Failed to parse JSON: %@", error); |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + // Set the URL, HTTP body, and headers |
| 144 | + request.URL = [NSURL URLWithString:dict[@"url"]]; |
| 145 | + request.HTTPBody = [dict[@"requestBody"] dataUsingEncoding:NSUTF8StringEncoding]; |
| 146 | + |
| 147 | + // Ensure requestHeader is a dictionary |
| 148 | + if ([dict[@"requestHeader"] isKindOfClass:[NSDictionary class]]) { |
| 149 | + request.allHTTPHeaderFields = dict[@"requestHeader"]; |
| 150 | + } else { |
| 151 | + NSLog(@"Invalid requestHeader format"); |
94 | 152 | // return;
|
95 |
| -// } |
96 |
| -// |
97 |
| -// // Set the URL, HTTP body, and headers |
98 |
| -// request.URL = [NSURL URLWithString:dict[@"url"]]; |
99 |
| -// request.HTTPBody = [dict[@"requestBody"] dataUsingEncoding:NSUTF8StringEncoding]; |
100 |
| -// |
101 |
| -// // Ensure requestHeader is a dictionary |
102 |
| -// if ([dict[@"requestHeader"] isKindOfClass:[NSDictionary class]]) { |
103 |
| -// request.allHTTPHeaderFields = dict[@"requestHeader"]; |
104 |
| -// } else { |
105 |
| -// NSLog(@"Invalid requestHeader format"); |
106 |
| -//// return; |
107 |
| -// } |
108 |
| -// |
109 |
| -// // Ensure self.completion is not nil before calling it |
110 |
| -// NSString *tempId = dict[@"tempId"]; |
111 |
| -// if ([tempId isKindOfClass:[NSString class]] && self.dictionary[tempId] != nil) { |
112 |
| -// ((IBGURLRequestObfuscationHandler)self.dictionary[tempId])(request); |
113 |
| -// } else { |
114 |
| -// NSLog(@"Not Available Completion"); |
115 |
| -// } |
116 |
| -//} |
| 153 | + } |
| 154 | + |
| 155 | + // Ensure self.completion is not nil before calling it |
| 156 | + NSString *tempId = dict[@"tempId"]; |
| 157 | + if ([tempId isKindOfClass:[NSString class]] && self.requestObfuscationCompletionDictionary[tempId] != nil) { |
| 158 | + ((IBGURLRequestAsyncObfuscationCompletedHandler)self.requestObfuscationCompletionDictionary[tempId])(request); |
| 159 | + } else { |
| 160 | + NSLog(@"Not Available Completion"); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + // MARK: Might need to moved this into another method. |
| 165 | + NSURLResponse *response; // Must be initialized from React Native Objects |
| 166 | + NSData *responseData; // Must be initialized from React Native Objects |
| 167 | + if ([tempId isKindOfClass:[NSString class]] && self.responseObfuscationCompletionDictionary[tempId] != nil) { |
| 168 | + |
| 169 | + ((NetworkObfuscationCompletionBlock)self.responseObfuscationCompletionDictionary[tempId])(responseData, response); |
| 170 | + } else { |
| 171 | + NSLog(@"Not Available Completion"); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + if ([tempId isKindOfClass:[NSString class]] && self.responseFilteringCompletionDictionary[tempId] != nil) { |
| 177 | + // ⬇️ YES == Response will be saved, NO == will be ignored |
| 178 | + ((IBGURLRequestResponseAsyncFilteringCompletedHandler)self.responseFilteringCompletionDictionary[tempId])(YES); |
| 179 | + } else { |
| 180 | + NSLog(@"Not Available Completion"); |
| 181 | + } |
| 182 | + |
| 183 | + if ([tempId isKindOfClass:[NSString class]] && self.requestFilteringCompletionDictionary[tempId] != nil) { |
| 184 | + // ⬇️ YES == Request will be saved, NO == will be ignored |
| 185 | + ((IBGURLRequestResponseAsyncFilteringCompletedHandler)self.requestFilteringCompletionDictionary[tempId])(YES); |
| 186 | + } else { |
| 187 | + NSLog(@"Not Available Completion"); |
| 188 | + } |
| 189 | + |
| 190 | +} |
117 | 191 |
|
118 | 192 | RCT_EXPORT_METHOD(setNetworkLoggingRequestFilterPredicateIOS: (BOOL)value){
|
119 | 193 |
|
|
0 commit comments