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