Skip to content

Commit 750c35d

Browse files
committed
Use base64 encoding util from iOS SDK.
1 parent c7615bc commit 750c35d

File tree

1 file changed

+2
-42
lines changed

1 file changed

+2
-42
lines changed

STNetTaskQueue/STHTTPNetTaskQueueHandler.m

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,6 @@
1212
#import "STNetTaskQueueLog.h"
1313
#import <objc/runtime.h>
1414

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-
5615
@interface STHTTPNetTask (STInternal)
5716

5817
@property (atomic, assign) NSInteger statusCode;
@@ -133,7 +92,8 @@ - (void)start
13392

13493
if (_baseURL.user.length || _baseURL.password.length) {
13594
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"];
13797
}
13898

13999
switch (_task.method) {

0 commit comments

Comments
 (0)