|
12 | 12 | #import "STNetTaskQueueLog.h" |
13 | 13 | #import <objc/runtime.h> |
14 | 14 |
|
15 | | -static uint8_t const STBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
16 | | -static NSString * STBase64String(NSString *string) |
17 | | -{ |
18 | | - NSMutableString *encodedString = [NSMutableString new]; |
19 | | - |
20 | | - NSData *data = [NSData dataWithBytes:string.UTF8String length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; |
21 | | - NSUInteger length = data.length; |
22 | | - uint8_t *bytes = (uint8_t *)data.bytes; |
23 | | - |
24 | | - for (NSUInteger i = 0; i < length; i += 3) { |
25 | | - uint8_t byte = bytes[i]; |
26 | | - int tableIndex = (byte & 0xFC) >> 2; |
27 | | - [encodedString appendFormat:@"%c", STBase64EncodingTable[tableIndex]]; |
28 | | - tableIndex = (byte & 0x03) << 4; |
29 | | - |
30 | | - if (i + 1 < length) { |
31 | | - byte = bytes[i + 1]; |
32 | | - tableIndex |= (byte & 0xF0) >> 4; |
33 | | - [encodedString appendFormat:@"%c", STBase64EncodingTable[tableIndex]]; |
34 | | - tableIndex = (byte & 0x0F) << 2; |
35 | | - |
36 | | - if (i + 2 < length) { |
37 | | - byte = bytes[i + 2]; |
38 | | - tableIndex |= (byte & 0xC0) >> 6; |
39 | | - [encodedString appendFormat:@"%c", STBase64EncodingTable[tableIndex]]; |
40 | | - |
41 | | - tableIndex = (byte & 0x3F); |
42 | | - [encodedString appendFormat:@"%c", STBase64EncodingTable[tableIndex]]; |
43 | | - } |
44 | | - else { |
45 | | - [encodedString appendFormat:@"%c=", STBase64EncodingTable[tableIndex]]; |
46 | | - } |
47 | | - } |
48 | | - else { |
49 | | - [encodedString appendFormat:@"%c=", STBase64EncodingTable[tableIndex]]; |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - return [NSString stringWithString:encodedString]; |
54 | | -} |
55 | | - |
56 | 15 | @interface STHTTPNetTask (STInternal) |
57 | 16 |
|
58 | 17 | @property (atomic, assign) NSInteger statusCode; |
@@ -133,7 +92,8 @@ - (void)start |
133 | 92 |
|
134 | 93 | if (_baseURL.user.length || _baseURL.password.length) { |
135 | 94 | NSString *credentials = [NSString stringWithFormat:@"%@:%@", _baseURL.user, _baseURL.password]; |
136 | | - [request setValue:[NSString stringWithFormat:@"Basic %@", STBase64String(credentials)] forHTTPHeaderField:@"Authorization"]; |
| 95 | + credentials = [[credentials dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:kNilOptions]; |
| 96 | + [request setValue:[NSString stringWithFormat:@"Basic %@", credentials] forHTTPHeaderField:@"Authorization"]; |
137 | 97 | } |
138 | 98 |
|
139 | 99 | switch (_task.method) { |
|
0 commit comments