Skip to content

Commit

Permalink
support cname, path style and prefix path mode (aliyun#255) (aliyun#269)
Browse files Browse the repository at this point in the history
Co-authored-by: wushuai1415 <[email protected]>
  • Loading branch information
huiguangjun and wushuai1415 authored Jul 12, 2021
1 parent cbbce99 commit f46fe3a
Show file tree
Hide file tree
Showing 17 changed files with 1,384 additions and 84 deletions.
97 changes: 49 additions & 48 deletions AliyunOSSSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,76 +416,76 @@
D87183061FC56358000DD9EC /* AliyunOSSSDK */ = {
isa = PBXGroup;
children = (
D87183071FC56358000DD9EC /* OSSNetworking.m */,
D87183081FC56358000DD9EC /* OSSHttpdns.m */,
D87183091FC56358000DD9EC /* OSSModel.h */,
D871830A1FC56358000DD9EC /* OSSClient.m */,
D871830C1FC56358000DD9EC /* OSSCompat.h */,
D871830D1FC56358000DD9EC /* OSSLog.m */,
D871830E1FC56358000DD9EC /* OSSUtil.h */,
D871830F1FC56358000DD9EC /* OSSXMLDictionary.h */,
D87183101FC56358000DD9EC /* OSSClient.h */,
D87183111FC56358000DD9EC /* OSSModel.m */,
D87183121FC56358000DD9EC /* OSSHttpdns.h */,
D87183131FC56358000DD9EC /* OSSNetworking.h */,
D87183141FC56358000DD9EC /* OSSIPv6 */,
D871831B1FC56358000DD9EC /* OSSDefine.h */,
D871831C1FC56358000DD9EC /* OSSCompat.m */,
D871831D1FC56358000DD9EC /* OSSLog.h */,
D871831E1FC56358000DD9EC /* OSSTask */,
D871832D1FC56358000DD9EC /* OSSXMLDictionary.m */,
D871832E1FC56358000DD9EC /* OSSFileLog */,
D871833A1FC56358000DD9EC /* OSSUtil.m */,
D871833B1FC56358000DD9EC /* OSSService.h */,
D8C5E5401FCE915000613BA5 /* aos_crc64.c */,
D8C5E53F1FCE914F00613BA5 /* aos_crc64.h */,
D827B40921107169003A24F6 /* NSDate+OSS.h */,
D827B40A21107169003A24F6 /* NSDate+OSS.m */,
D8C5E54B1FCEB57600613BA5 /* NSMutableData+OSS_CRC.h */,
D8C5E54C1FCEB57600613BA5 /* NSMutableData+OSS_CRC.m */,
D829F3A71FD8CFBE00A8C2DC /* OSSInputStreamHelper.h */,
D829F3A81FD8CFBE00A8C2DC /* OSSInputStreamHelper.m */,
D842D5891FFC8C0500220913 /* OSSLog.swift */,
D85277E92015BBAD00D7B1DE /* OSSRequest.h */,
D85277EA2015BBAD00D7B1DE /* OSSRequest.m */,
D85277EF2015BD5A00D7B1DE /* OSSNetworkingRequestDelegate.h */,
D85277F02015BD5A00D7B1DE /* OSSNetworkingRequestDelegate.m */,
D827B40F21114E4B003A24F6 /* NSMutableDictionary+OSS.h */,
D827B41021114E4B003A24F6 /* NSMutableDictionary+OSS.m */,
D85277F62015CB3500D7B1DE /* OSSAllRequestNeededMessage.h */,
D85277F72015CB3500D7B1DE /* OSSAllRequestNeededMessage.m */,
D85278022015D58400D7B1DE /* OSSHttpResponseParser.h */,
D85278032015D58400D7B1DE /* OSSHttpResponseParser.m */,
D85278082015D59000D7B1DE /* OSSURLRequestRetryHandler.h */,
D85278092015D59000D7B1DE /* OSSURLRequestRetryHandler.m */,
D87183101FC56358000DD9EC /* OSSClient.h */,
D871830A1FC56358000DD9EC /* OSSClient.m */,
D871830C1FC56358000DD9EC /* OSSCompat.h */,
D871831C1FC56358000DD9EC /* OSSCompat.m */,
D852780E2015DF4900D7B1DE /* OSSConstants.h */,
D852780F2015DF4900D7B1DE /* OSSConstants.m */,
D80C88C7201AC1B400F379D8 /* OSSGetObjectACLRequest.h */,
D80C88C8201AC1B400F379D8 /* OSSGetObjectACLRequest.m */,
D80C88CD201AC21200F379D8 /* OSSResult.h */,
D80C88CE201AC21200F379D8 /* OSSResult.m */,
D80C88D3201AC33000F379D8 /* OSSGetObjectACLResult.h */,
D80C88D4201AC33000F379D8 /* OSSGetObjectACLResult.m */,
D871831B1FC56358000DD9EC /* OSSDefine.h */,
D80C88E5201B06CF00F379D8 /* OSSDeleteMultipleObjectsRequest.h */,
D80C88E6201B06CF00F379D8 /* OSSDeleteMultipleObjectsRequest.m */,
D80C88EB201B06E200F379D8 /* OSSDeleteMultipleObjectsResult.h */,
D80C88EC201B06E200F379D8 /* OSSDeleteMultipleObjectsResult.m */,
D871832E1FC56358000DD9EC /* OSSFileLog */,
D8C2951020F46A79008A08AA /* OSSGetBucketInfoRequest.h */,
D8C2951120F46A79008A08AA /* OSSGetBucketInfoRequest.m */,
D8C2951620F46A95008A08AA /* OSSGetBucketInfoResult.h */,
D8C2951720F46A95008A08AA /* OSSGetBucketInfoResult.m */,
D827B40921107169003A24F6 /* NSDate+OSS.h */,
D827B40A21107169003A24F6 /* NSDate+OSS.m */,
D827B40F21114E4B003A24F6 /* NSMutableDictionary+OSS.h */,
D827B41021114E4B003A24F6 /* NSMutableDictionary+OSS.m */,
D827B4152111A6E4003A24F6 /* OSSPutSymlinkRequest.h */,
D827B4162111A6E4003A24F6 /* OSSPutSymlinkRequest.m */,
D80C88C7201AC1B400F379D8 /* OSSGetObjectACLRequest.h */,
D80C88C8201AC1B400F379D8 /* OSSGetObjectACLRequest.m */,
D80C88D3201AC33000F379D8 /* OSSGetObjectACLResult.h */,
D80C88D4201AC33000F379D8 /* OSSGetObjectACLResult.m */,
D827B41B2111A6FA003A24F6 /* OSSGetSymlinkRequest.h */,
D827B41C2111A6FA003A24F6 /* OSSGetSymlinkRequest.m */,
D827B4332111ADF7003A24F6 /* OSSGetSymlinkResult.h */,
D827B4342111ADF7003A24F6 /* OSSGetSymlinkResult.m */,
D87183121FC56358000DD9EC /* OSSHttpdns.h */,
D87183081FC56358000DD9EC /* OSSHttpdns.m */,
D85278022015D58400D7B1DE /* OSSHttpResponseParser.h */,
D85278032015D58400D7B1DE /* OSSHttpResponseParser.m */,
D829F3A71FD8CFBE00A8C2DC /* OSSInputStreamHelper.h */,
D829F3A81FD8CFBE00A8C2DC /* OSSInputStreamHelper.m */,
D87183141FC56358000DD9EC /* OSSIPv6 */,
D871831D1FC56358000DD9EC /* OSSLog.h */,
D871830D1FC56358000DD9EC /* OSSLog.m */,
D842D5891FFC8C0500220913 /* OSSLog.swift */,
D87183091FC56358000DD9EC /* OSSModel.h */,
D87183111FC56358000DD9EC /* OSSModel.m */,
D87183131FC56358000DD9EC /* OSSNetworking.h */,
D87183071FC56358000DD9EC /* OSSNetworking.m */,
D85277EF2015BD5A00D7B1DE /* OSSNetworkingRequestDelegate.h */,
D85277F02015BD5A00D7B1DE /* OSSNetworkingRequestDelegate.m */,
D827B4152111A6E4003A24F6 /* OSSPutSymlinkRequest.h */,
D827B4162111A6E4003A24F6 /* OSSPutSymlinkRequest.m */,
D827B42D2111ADE9003A24F6 /* OSSPutSymlinkResult.h */,
D827B42E2111ADE9003A24F6 /* OSSPutSymlinkResult.m */,
D85277E92015BBAD00D7B1DE /* OSSRequest.h */,
D85277EA2015BBAD00D7B1DE /* OSSRequest.m */,
D827B4212111A71B003A24F6 /* OSSRestoreObjectRequest.h */,
D827B4222111A71B003A24F6 /* OSSRestoreObjectRequest.m */,
D827B4272111A853003A24F6 /* OSSRestoreObjectResult.h */,
D827B4282111A853003A24F6 /* OSSRestoreObjectResult.m */,
D827B42D2111ADE9003A24F6 /* OSSPutSymlinkResult.h */,
D827B42E2111ADE9003A24F6 /* OSSPutSymlinkResult.m */,
D827B4332111ADF7003A24F6 /* OSSGetSymlinkResult.h */,
D827B4342111ADF7003A24F6 /* OSSGetSymlinkResult.m */,
D80C88CD201AC21200F379D8 /* OSSResult.h */,
D80C88CE201AC21200F379D8 /* OSSResult.m */,
D871833B1FC56358000DD9EC /* OSSService.h */,
D871831E1FC56358000DD9EC /* OSSTask */,
D85278082015D59000D7B1DE /* OSSURLRequestRetryHandler.h */,
D85278092015D59000D7B1DE /* OSSURLRequestRetryHandler.m */,
D871830E1FC56358000DD9EC /* OSSUtil.h */,
D871833A1FC56358000DD9EC /* OSSUtil.m */,
D871830F1FC56358000DD9EC /* OSSXMLDictionary.h */,
D871832D1FC56358000DD9EC /* OSSXMLDictionary.m */,
);
path = AliyunOSSSDK;
sourceTree = "<group>";
Expand Down Expand Up @@ -756,6 +756,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down
64 changes: 60 additions & 4 deletions AliyunOSSSDK/OSSClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,13 @@ - (OSSTask *)invokeRequest:(OSSNetworkingRequestDelegate *)request requireAuthen
}

request.isHttpdnsEnable = self.clientConfiguration.isHttpdnsEnable;

request.isPathStyleAccessEnable = self.clientConfiguration.isPathStyleAccessEnable;
request.isCustomPathPrefixEnable = self.clientConfiguration.isCustomPathPrefixEnable;

return [_networking sendRequest:request];
}


#pragma implement restful apis

- (OSSTask *)getService:(OSSGetServiceRequest *)request {
Expand Down Expand Up @@ -1913,18 +1916,43 @@ - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
NSString * accessKey = [(NSString *)[splitResult objectAtIndex:0] substringFromIndex:4];
NSString * signature = [splitResult objectAtIndex:1];

BOOL isPathStyle = false;
NSURL * endpointURL = [NSURL URLWithString:self.endpoint];
NSString * host = endpointURL.host;
NSString * port = @"";
NSString * path = @"";
NSString * pathStylePath = @"";
if ([OSSUtil isOssOriginBucketHost:host]) {
host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
} else if ([OSSUtil isIncludeCnameExcludeList:self.clientConfiguration.cnameExcludeList host:host]) {
if (self.clientConfiguration.isPathStyleAccessEnable) {
isPathStyle = true;
} else {
host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
}
} else if ([[OSSIPv6Adapter getInstance] isIPv4Address:host] ||
[[OSSIPv6Adapter getInstance] isIPv6Address:host]) {
isPathStyle = true;
}
if (endpointURL.port) {
port = [NSString stringWithFormat:@":%@", endpointURL.port];
}
if (self.clientConfiguration.isCustomPathPrefixEnable) {
path = endpointURL.path;
}
if (isPathStyle) {
pathStylePath = [@"/" stringByAppendingString:bucketName];
}

[params oss_setObject:signature forKey:@"Signature"];
[params oss_setObject:accessKey forKey:@"OSSAccessKeyId"];
[params oss_setObject:expires forKey:@"Expires"];
NSString * stringURL = [NSString stringWithFormat:@"%@://%@/%@?%@",
NSString * stringURL = [NSString stringWithFormat:@"%@://%@%@%@%@/%@?%@",
endpointURL.scheme,
host,
port,
path,
pathStylePath,
[OSSUtil encodeURL:objectKey],
[OSSUtil populateQueryStringFromParameter:params]];
return [OSSTask taskWithResult:stringURL];
Expand All @@ -1944,22 +1972,50 @@ - (OSSTask *)presignPublicURLWithBucketName:(NSString *)bucketName
withParameters:(NSDictionary *)parameters {

return [[OSSTask taskWithResult:nil] continueWithBlock:^id(OSSTask *task) {
BOOL isPathStyle = false;
NSURL * endpointURL = [NSURL URLWithString:self.endpoint];
NSString * host = endpointURL.host;
NSString * port = @"";
NSString * path = @"";
NSString * pathStylePath = @"";
if ([OSSUtil isOssOriginBucketHost:host]) {
host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
} else if ([OSSUtil isIncludeCnameExcludeList:self.clientConfiguration.cnameExcludeList host:host]) {
if (self.clientConfiguration.isPathStyleAccessEnable) {
isPathStyle = true;
} else {
host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
}
} else if ([[OSSIPv6Adapter getInstance] isIPv4Address:host] ||
[[OSSIPv6Adapter getInstance] isIPv6Address:host]) {
isPathStyle = true;
}
if (endpointURL.port) {
port = [NSString stringWithFormat:@":%@", endpointURL.port];
}
if (self.clientConfiguration.isCustomPathPrefixEnable) {
path = endpointURL.path;
}
if (isPathStyle) {
pathStylePath = [@"/" stringByAppendingString:bucketName];
}
if ([parameters count] > 0) {
NSString * stringURL = [NSString stringWithFormat:@"%@://%@/%@?%@",
NSString * stringURL = [NSString stringWithFormat:@"%@://%@%@%@%@/%@?%@",
endpointURL.scheme,
host,
port,
path,
pathStylePath,
[OSSUtil encodeURL:objectKey],
[OSSUtil populateQueryStringFromParameter:parameters]];
return [OSSTask taskWithResult:stringURL];
} else {
NSString * stringURL = [NSString stringWithFormat:@"%@://%@/%@",
NSString * stringURL = [NSString stringWithFormat:@"%@://%@%@%@%@/%@",
endpointURL.scheme,
host,
port,
path,
pathStylePath,
[OSSUtil encodeURL:objectKey]];
return [OSSTask taskWithResult:stringURL];
}
Expand Down
10 changes: 10 additions & 0 deletions AliyunOSSSDK/OSSModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ Sets the session Id for background file transmission
*/
@property (nonatomic, copy) NSString * userAgentMark;

/**
Sets the flag of using Second Level Domain style to access the endpoint. By default it's false.
*/
@property (nonatomic, assign) BOOL isPathStyleAccessEnable;

/**
Sets the flag of using custom path prefix to access the endpoint. By default it's false.
*/
@property (nonatomic, assign) BOOL isCustomPathPrefixEnable;

/**
Sets CName excluded list.
*/
Expand Down
3 changes: 3 additions & 0 deletions AliyunOSSSDK/OSSModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ - (instancetype)init {
self.backgroundSesseionIdentifier = BACKGROUND_SESSION_IDENTIFIER;
self.timeoutIntervalForRequest = OSSDefaultTimeoutForRequestInSecond;
self.timeoutIntervalForResource = OSSDefaultTimeoutForResourceInSecond;
self.isPathStyleAccessEnable = NO;
self.isCustomPathPrefixEnable = NO;
self.cnameExcludeList = @[];
}
return self;
}
Expand Down
3 changes: 3 additions & 0 deletions AliyunOSSSDK/OSSNetworkingRequestDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
@property (nonatomic, assign) BOOL isBackgroundUploadFileTask;
@property (nonatomic, assign) BOOL isHttpdnsEnable;

@property (nonatomic, assign) BOOL isPathStyleAccessEnable;
@property (nonatomic, assign) BOOL isCustomPathPrefixEnable;
@property (nonatomic, copy) NSArray * cnameExcludeList;

@property (nonatomic, assign) uint32_t currentRetryCount;
@property (nonatomic, strong) NSError * error;
Expand Down
47 changes: 32 additions & 15 deletions AliyunOSSSDK/OSSNetworkingRequestDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,47 @@ - (OSSTask *)buildInternalHttpRequest {
#define URLENCODE(a) [OSSUtil encodeURL:(a)]
OSSLogDebug(@"start to build request")
// build base url string
NSString *bucketName = self.allNeededMessage.bucketName;
NSString *objectKey = self.allNeededMessage.objectKey;
NSString *urlString = self.allNeededMessage.endpoint;
NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:urlString];
NSString *headerHost = nil;
BOOL isPathStyle = NO;

NSURLComponents *temComs = [NSURLComponents new];
temComs.scheme = urlComponents.scheme;
temComs.host = urlComponents.host;
temComs.port = urlComponents.port;
if (self.isCustomPathPrefixEnable) {
temComs.path = urlComponents.path;
}

if ([self.allNeededMessage.bucketName oss_isNotEmpty]) {
OSSIPv6Adapter *ipAdapter = [OSSIPv6Adapter getInstance];
if ([OSSUtil isOssOriginBucketHost:temComs.host]) {
// eg. insert bucket to the begining of host.
temComs.host = [NSString stringWithFormat:@"%@.%@", self.allNeededMessage.bucketName, temComs.host];
headerHost = temComs.host;
if ([temComs.scheme.lowercaseString isEqualToString:@"http"] && self.isHttpdnsEnable) {
NSString *dnsResult = [OSSUtil getIpByHost: temComs.host];
temComs.host = dnsResult;
}
} else if ([ipAdapter isIPv4Address:temComs.host] || [ipAdapter isIPv6Address:temComs.host]) {
temComs.path = [NSString stringWithFormat:@"/%@",self.allNeededMessage.bucketName];
}
}

urlString = temComs.string;
OSSIPv6Adapter *ipAdapter = [OSSIPv6Adapter getInstance];
if ([OSSUtil isOssOriginBucketHost:temComs.host]) {
// eg. insert bucket to the begining of host.
temComs.host = [NSString stringWithFormat:@"%@.%@", self.allNeededMessage.bucketName, temComs.host];
headerHost = temComs.host;
if ([temComs.scheme.lowercaseString isEqualToString:@"http"] && self.isHttpdnsEnable) {
NSString *dnsResult = [OSSUtil getIpByHost: temComs.host];
temComs.host = dnsResult;
}
} else if (self.allNeededMessage.isHostInCnameExcludeList) {
if (self.isPathStyleAccessEnable) {
isPathStyle = YES;
} else {
temComs.host = [NSString stringWithFormat:@"%@.%@", self.allNeededMessage.bucketName, temComs.host];
}
} else if ([ipAdapter isIPv4Address:temComs.host] || [ipAdapter isIPv6Address:temComs.host]) {
isPathStyle = YES;
}
}

urlString = temComs.string;

if (isPathStyle) {
urlString = [NSString stringWithFormat:@"%@/%@", urlString, bucketName];
}
// join object name
if ([self.allNeededMessage.objectKey oss_isNotEmpty]) {
urlString = [urlString oss_stringByAppendingPathComponentForURL:URLENCODE(self.allNeededMessage.objectKey)];
Expand All @@ -123,6 +138,7 @@ - (OSSTask *)buildInternalHttpRequest {
urlString = [NSString stringWithFormat:@"%@?%@", urlString, queryString];
}
}

OSSLogDebug(@"built full url: %@", urlString)

// generate internal request For NSURLSession
Expand Down Expand Up @@ -166,4 +182,5 @@ - (OSSTask *)buildInternalHttpRequest {
#undef URLENCODE//(a)
return [OSSTask taskWithResult:nil];
}

@end
1 change: 1 addition & 0 deletions AliyunOSSSDK/OSSUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

@interface OSSUtil : NSObject

+ (BOOL)isIncludeCnameExcludeList:(NSArray *)cnameExcludeList host:(NSString *)host;
+ (NSString *)calBase64Sha1WithData:(NSString *)data withSecret:(NSString *)key;
+ (NSString *)calBase64WithData:(uint8_t *)data;
+ (NSString *)encodeURL:(NSString *)url;
Expand Down
9 changes: 9 additions & 0 deletions AliyunOSSSDK/OSSUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@

@implementation OSSUtil

+ (BOOL)isIncludeCnameExcludeList:(NSArray *)cnameExcludeList host:(NSString *)host {
for (NSString *cnameExclude in cnameExcludeList) {
if ([host hasSuffix:cnameExclude]) {
return YES;
}
}
return NO;
}

+ (NSString *)calBase64Sha1WithData:(NSString *)data withSecret:(NSString *)key {
NSData *secretData = [key dataUsingEncoding:NSUTF8StringEncoding];
NSData *clearTextData = [data dataUsingEncoding:NSUTF8StringEncoding];
Expand Down
Loading

0 comments on commit f46fe3a

Please sign in to comment.