FPRDataUtils.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebasePerformance/Sources/FPRDataUtils.h"
  15. #import "FirebasePerformance/Sources/Common/FPRConstants.h"
  16. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  17. #pragma mark - Public functions
  18. NSString *FPRReservableName(NSString *name) {
  19. NSString *reservableName =
  20. [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  21. if ([reservableName hasPrefix:kFPRInternalNamePrefix]) {
  22. FPRLogError(kFPRClientNameReserved, @"%@ prefix is reserved. Dropped %@.",
  23. kFPRInternalNamePrefix, reservableName);
  24. return nil;
  25. }
  26. if (reservableName.length == 0) {
  27. FPRLogError(kFPRClientNameLengthCheckFailed, @"Given name is empty.");
  28. return nil;
  29. }
  30. if (reservableName.length > kFPRMaxNameLength) {
  31. FPRLogError(kFPRClientNameLengthCheckFailed, @"%@ is greater than %d characters, dropping it.",
  32. reservableName, kFPRMaxNameLength);
  33. return nil;
  34. }
  35. return reservableName;
  36. }
  37. NSString *FPRReservableAttributeName(NSString *name) {
  38. NSString *reservableName =
  39. [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  40. static NSArray<NSString *> *reservedPrefix = nil;
  41. static NSPredicate *characterCheck = nil;
  42. static dispatch_once_t onceToken;
  43. dispatch_once(&onceToken, ^{
  44. reservedPrefix = @[ @"firebase_", @"google_", @"ga_" ];
  45. NSString *characterRegex = @"[A-Z0-9a-z_]*";
  46. characterCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", characterRegex];
  47. });
  48. __block BOOL containsReservedPrefix = NO;
  49. [reservedPrefix enumerateObjectsUsingBlock:^(NSString *prefix, NSUInteger idx, BOOL *stop) {
  50. if ([reservableName hasPrefix:prefix]) {
  51. FPRLogError(kFPRClientNameReserved, @"%@ prefix is reserved. Dropped %@.", prefix,
  52. reservableName);
  53. *stop = YES;
  54. containsReservedPrefix = YES;
  55. }
  56. }];
  57. if (containsReservedPrefix) {
  58. return nil;
  59. }
  60. if (reservableName.length == 0) {
  61. FPRLogError(kFPRClientNameLengthCheckFailed, @"Given name is empty.");
  62. return nil;
  63. }
  64. if ([characterCheck evaluateWithObject:reservableName] == NO) {
  65. FPRLogError(kFPRAttributeNameIllegalCharacters,
  66. @"Illegal characters used for attribute name, "
  67. "characters allowed are alphanumeric or underscore.");
  68. return nil;
  69. }
  70. if (reservableName.length > kFPRMaxAttributeNameLength) {
  71. FPRLogError(kFPRClientNameLengthCheckFailed, @"%@ is greater than %d characters, dropping it.",
  72. reservableName, kFPRMaxAttributeNameLength);
  73. return nil;
  74. }
  75. return reservableName;
  76. }
  77. NSString *FPRValidatedAttributeValue(NSString *value) {
  78. if (value.length == 0) {
  79. FPRLogError(kFPRClientNameLengthCheckFailed, @"Given value is empty.");
  80. return nil;
  81. }
  82. if (value.length > kFPRMaxAttributeValueLength) {
  83. FPRLogError(kFPRClientNameLengthCheckFailed, @"%@ is greater than %d characters, dropping it.",
  84. value, kFPRMaxAttributeValueLength);
  85. return nil;
  86. }
  87. return value;
  88. }
  89. NSString *FPRTruncatedURLString(NSString *URLString) {
  90. NSString *truncatedURLString = URLString;
  91. NSString *pathSeparator = @"/";
  92. if (truncatedURLString.length > kFPRMaxURLLength) {
  93. NSString *truncationCharacter =
  94. [truncatedURLString substringWithRange:NSMakeRange(kFPRMaxURLLength, 1)];
  95. truncatedURLString = [URLString substringToIndex:kFPRMaxURLLength];
  96. if (![pathSeparator isEqual:truncationCharacter]) {
  97. NSRange rangeOfTruncation = [truncatedURLString rangeOfString:pathSeparator
  98. options:NSBackwardsSearch];
  99. truncatedURLString = [URLString substringToIndex:rangeOfTruncation.location];
  100. }
  101. FPRLogWarning(kFPRClientNameTruncated, @"URL exceeds %d characters. Truncated url: %@",
  102. kFPRMaxURLLength, truncatedURLString);
  103. }
  104. return truncatedURLString;
  105. }