FIRCLSURLBuilder.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Google
  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 "Crashlytics/Shared/FIRCLSNetworking/FIRCLSURLBuilder.h"
  15. #import "Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
  16. @interface FIRCLSURLBuilder ()
  17. @property(nonatomic) NSMutableString *URLString;
  18. @property(nonatomic) NSUInteger queryParams;
  19. - (NSString *)escapeString:(NSString *)string;
  20. @end
  21. @implementation FIRCLSURLBuilder
  22. + (instancetype)URLWithBase:(NSString *)base {
  23. FIRCLSURLBuilder *url = [[FIRCLSURLBuilder alloc] init];
  24. [url appendComponent:base];
  25. return url;
  26. }
  27. - (instancetype)init {
  28. self = [super init];
  29. if (!self) {
  30. return nil;
  31. }
  32. _URLString = [[NSMutableString alloc] init];
  33. _queryParams = 0;
  34. return self;
  35. }
  36. - (NSString *)escapeString:(NSString *)string {
  37. #if TARGET_OS_WATCH
  38. // TODO: Question - Why does watchOS use a different encoding from the other platforms and the
  39. // Android SDK?
  40. // This broken the unit test on watchOS: https://github.com/firebase/firebase-ios-sdk/pull/10511
  41. return
  42. [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet
  43. URLPathAllowedCharacterSet]];
  44. #else
  45. return
  46. [string stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet
  47. .URLQueryAllowedCharacterSet];
  48. #endif
  49. }
  50. - (void)appendComponent:(NSString *)component {
  51. if (component.length == 0) {
  52. FIRCLSErrorLog(@"URLBuilder parameter component must not be empty");
  53. return;
  54. }
  55. [self.URLString appendString:component];
  56. }
  57. - (void)escapeAndAppendComponent:(NSString *)component {
  58. [self appendComponent:[self escapeString:component]];
  59. }
  60. - (void)appendValue:(id)value forQueryParam:(NSString *)param {
  61. if (!value) {
  62. return;
  63. }
  64. if (self.queryParams == 0) {
  65. [self appendComponent:@"?"];
  66. } else {
  67. [self appendComponent:@"&"];
  68. }
  69. self.queryParams += 1;
  70. [self appendComponent:param];
  71. [self appendComponent:@"="];
  72. if ([value isKindOfClass:NSString.class]) {
  73. [self escapeAndAppendComponent:value];
  74. } else {
  75. [self escapeAndAppendComponent:[value description]];
  76. }
  77. }
  78. - (NSURL *)URL {
  79. return [NSURL URLWithString:self.URLString];
  80. }
  81. @end