Skip to content

Commit

Permalink
fixup! Logger: if the body is MsgPack encoded then it should not show…
Browse files Browse the repository at this point in the history
… nil but

Base64 encoded string instead.
  • Loading branch information
ricardopereira committed Oct 29, 2020
1 parent a285202 commit 4b4d417
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Source/ARTHttp.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,14 @@ - (void)dealloc {
}

- (NSObject<ARTCancellable> *)executeRequest:(NSMutableURLRequest *)request completion:(void (^)(NSHTTPURLResponse *_Nullable, NSData *_Nullable, NSError *_Nullable))callback {
NSString *requestBodyStr = [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding];
if (!requestBodyStr) {
requestBodyStr = [request.HTTPBody base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
}
[self.logger debug:@"--> %@ %@\n Body: %@\n Headers: %@", request.HTTPMethod, request.URL.absoluteString, requestBodyStr, request.allHTTPHeaderFields];
[self.logger debug:@"--> %@ %@\n Body: %@\n Headers: %@", request.HTTPMethod, request.URL.absoluteString, [self debugDescriptionOfBodyWithData:request.HTTPBody], request.allHTTPHeaderFields];

return [_urlSession get:request completion:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (error) {
[self.logger error:@"<-- %@ %@: error %@", request.HTTPMethod, request.URL.absoluteString, error];
} else {
NSString *responseDataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!responseDataStr) {
responseDataStr = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
}
[self.logger debug:@"<-- %@ %@: statusCode %ld\n Data: %@\n Headers: %@\n", request.HTTPMethod, request.URL.absoluteString, (long)httpResponse.statusCode, responseDataStr, httpResponse.allHeaderFields];
[self.logger debug:@"<-- %@ %@: statusCode %ld\n Data: %@\n Headers: %@\n", request.HTTPMethod, request.URL.absoluteString, (long)httpResponse.statusCode, [self debugDescriptionOfBodyWithData:data], httpResponse.allHeaderFields];
NSString *headerErrorMessage = httpResponse.allHeaderFields[ARTHttpHeaderFieldErrorMessageKey];
if (headerErrorMessage && ![headerErrorMessage isEqualToString:@""]) {
[self.logger warn:@"%@", headerErrorMessage];
Expand All @@ -72,4 +64,15 @@ - (void)dealloc {
}];
}

- (NSString *)debugDescriptionOfBodyWithData:(NSData *)data {
if (self.logger.logLevel <= ARTLogLevelDebug) {
NSString *requestBodyStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!requestBodyStr) {
requestBodyStr = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
}
return requestBodyStr;
}
return nil;
}

@end

0 comments on commit 4b4d417

Please sign in to comment.