Skip to content

Commit

Permalink
Merge pull request #223 from pushuhengyang/master
Browse files Browse the repository at this point in the history
bucketname和endpoint  过滤规则修改
  • Loading branch information
sdlcdhc authored Dec 23, 2019
2 parents 31b64f7 + fcafcdb commit 510a5b2
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 21 deletions.
2 changes: 1 addition & 1 deletion AliyunOSSSDK/OSSDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#elif TARGET_OS_OSX
#define OSSUAPrefix @"aliyun-sdk-mac"
#endif
#define OSSSDKVersion @"2.10.7"
#define OSSSDKVersion @"2.10.8"

#define OSSListBucketResultXMLTOKEN @"ListBucketResult"
#define OSSNameXMLTOKEN @"Name"
Expand Down
36 changes: 20 additions & 16 deletions AliyunOSSSDK/OSSNetworkingRequestDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,27 @@ - (OSSTask *)buildInternalHttpRequest {
NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:urlString];
NSString *headerHost = nil;

if ([self.allNeededMessage.bucketName oss_isNotEmpty]) {
OSSIPv6Adapter *ipAdapter = [OSSIPv6Adapter getInstance];
if ([OSSUtil isOssOriginBucketHost:urlComponents.host]) {
// eg. insert bucket to the begining of host.
urlComponents.host = [NSString stringWithFormat:@"%@.%@", self.allNeededMessage.bucketName, urlComponents.host];
headerHost = urlComponents.host;

if ([urlComponents.scheme.lowercaseString isEqualToString:@"http"] && self.isHttpdnsEnable) {
NSString *dnsResult = [OSSUtil getIpByHost: urlComponents.host];
urlComponents.host = dnsResult;
}
} else if ([ipAdapter isIPv4Address:urlComponents.host] || [ipAdapter isIPv6Address:urlComponents.host]) {
urlComponents.path = [NSString stringWithFormat:@"/%@%@",self.allNeededMessage.bucketName, urlComponents.path];
}
}
NSURLComponents *temComs = [NSURLComponents new];
temComs.scheme = urlComponents.scheme;
temComs.host = urlComponents.host;
temComs.port = urlComponents.port;

urlString = urlComponents.string;
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;

// join object name
if ([self.allNeededMessage.objectKey oss_isNotEmpty]) {
Expand Down
2 changes: 1 addition & 1 deletion AliyunOSSSDK/OSSUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ + (BOOL)validateBucketName:(NSString *)bucketName {
static NSRegularExpression *regEx;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regEx = [[NSRegularExpression alloc] initWithPattern:@"^[a-z0-9][a-z0-9_\\-]{2,62}$" options:NSRegularExpressionCaseInsensitive error:nil];
regEx = [[NSRegularExpression alloc] initWithPattern:@"^[a-z0-9][a-z0-9\\-]{1,61}[a-z0-9]$" options:NSRegularExpressionCaseInsensitive error:nil];
});
NSUInteger regExMatches = [regEx numberOfMatchesInString:bucketName options:0 range:NSMakeRange(0, [bucketName length])];
return regExMatches != 0;
Expand Down
2 changes: 1 addition & 1 deletion AliyunOSSiOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |s|

s.name = "AliyunOSSiOS"

s.version = "2.10.7"
s.version = "2.10.8"

s.summary = "An iOS SDK for Aliyun Object Storage Service"

Expand Down
74 changes: 74 additions & 0 deletions AliyunOSSiOSTests/OSSUtilsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,78 @@ - (void)testPerformanceExample {
}];
}

- (void)testBucketName{
///^[a-z0-9][a-z0-9\\-]{1,61}[a-z0-9]$"

BOOL result1 = [OSSUtil validateBucketName:@"123-456abc"];
XCTAssertTrue(result1);

BOOL result2 = [OSSUtil validateBucketName:@"123-456abc*"];
XCTAssertFalse(result2);

BOOL result3 = [OSSUtil validateBucketName:@"-123-456abc"];
XCTAssertFalse(result3);

BOOL result4 = [OSSUtil validateBucketName:@"123\\456abc"];
XCTAssertFalse(result4);

BOOL result5 = [OSSUtil validateBucketName:@"abc123"];
XCTAssertTrue(result5);

BOOL result6 = [OSSUtil validateBucketName:@"abc_123"];
XCTAssertFalse(result6);

BOOL result7 = [OSSUtil validateBucketName:@"a"];
XCTAssertFalse(result7);

BOOL result8 = [OSSUtil validateBucketName:@"abcdefghig-abcdefghig-abcdefghig-abcdefghig-abcdefghig-abcdefghig"];
XCTAssertFalse(result8);

}


- (void)testEndpoint{
NSString *bucketName = @"test-image";
NSString *result1 = [self getResultEndpoint:@"http://123.test:8989/path?ooob" andBucketName:bucketName];
XCTAssertTrue([result1 isEqualToString:@"http://123.test:8989"]);


NSString *result2 = [self getResultEndpoint:@"http://192.168.0.1:8081" andBucketName:bucketName];
XCTAssertTrue([result2 isEqualToString:@"http://192.168.0.1:8081/test-image"]);

NSString *result3 = [self getResultEndpoint:@"http://192.168.0.1" andBucketName:bucketName];
XCTAssertTrue([result3 isEqualToString:@"http://192.168.0.1/test-image"]);

NSString *result4 = [self getResultEndpoint:@"http://oss-cn-region.aliyuncs.com" andBucketName:bucketName];
XCTAssertTrue([result4 isEqualToString:@"http://test-image.oss-cn-region.aliyuncs.com"]);

}

- (NSString *)getResultEndpoint:(NSString *)endpoint andBucketName:(NSString *)name{
NSURLComponents *urlComponents = [[NSURLComponents alloc]initWithString:endpoint];

NSURLComponents *temComs = [NSURLComponents new];
temComs.scheme = urlComponents.scheme;
temComs.host = urlComponents.host;
temComs.port = urlComponents.port;

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



@end
6 changes: 6 additions & 0 deletions CHANGLOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pod dependency: pod 'AliyunOSSiOS'

Update Logs:

2019/12/23
- release 2.10.8
1.modify the filter rules about bucketname.
2.modify the filter rules about endpoint.


2018/09/13
- release 2.10.7
1.refactor code of building canonical url for request,Keep its logic consistent with android sdk.
Expand Down
4 changes: 4 additions & 0 deletions OSSSwiftDemo/OSSSwiftDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0F1C7D7023AC9425000B4E56 /* OSSUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F1C7D6F23AC9425000B4E56 /* OSSUtilsTests.swift */; };
D8350C7B1FFF66B400D8DF46 /* test.xml in Resources */ = {isa = PBXBuildFile; fileRef = D8350C6D1FFF66B400D8DF46 /* test.xml */; };
D8350C7C1FFF66B400D8DF46 /* wangwang.zip in Resources */ = {isa = PBXBuildFile; fileRef = D8350C6E1FFF66B400D8DF46 /* wangwang.zip */; };
D8350C801FFF66B400D8DF46 /* OSSSwiftGlobalDefines.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8350C761FFF66B400D8DF46 /* OSSSwiftGlobalDefines.swift */; };
Expand Down Expand Up @@ -67,6 +68,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0F1C7D6F23AC9425000B4E56 /* OSSUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSSUtilsTests.swift; sourceTree = "<group>"; };
D8350C6D1FFF66B400D8DF46 /* test.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = test.xml; sourceTree = "<group>"; };
D8350C6E1FFF66B400D8DF46 /* wangwang.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = wangwang.zip; sourceTree = "<group>"; };
D8350C701FFF66B400D8DF46 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -222,6 +224,7 @@
D8F27EAF200C4CD9005A2C39 /* OSSCnameTests.swift */,
D852773F2010B21A00D7B1DE /* SequentialMultipartUploadTests.swift */,
D83CEFF5201F1BFD00F917E1 /* TriggerCallBackTests.swift */,
0F1C7D6F23AC9425000B4E56 /* OSSUtilsTests.swift */,
);
path = OSSSwiftDemoTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -349,6 +352,7 @@
D8E64170200A31530023DF48 /* OSSProviderTests.swift in Sources */,
D8E641642009C0EB0023DF48 /* OSSExceptionalTests.swift in Sources */,
D8D4DE112008BCE9000B9EBB /* OSSCheckCRC64Tests.swift in Sources */,
0F1C7D7023AC9425000B4E56 /* OSSUtilsTests.swift in Sources */,
D83CEFF6201F1BFD00F917E1 /* TriggerCallBackTests.swift in Sources */,
D85277402010B21A00D7B1DE /* SequentialMultipartUploadTests.swift in Sources */,
D8E6416C2009D93F0023DF48 /* OSSLogTests.swift in Sources */,
Expand Down
96 changes: 96 additions & 0 deletions OSSSwiftDemo/OSSSwiftDemoTests/OSSUtilsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// OSSUtilsTests.swift
// OSSSwiftDemoTests
//
// Created by 剑子 on 2019/12/20.
// Copyright © 2019 aliyun. All rights reserved.
//

import XCTest

import AliyunOSSiOS
import AliyunOSSSwiftSDK


class OSSUtilsTests: XCTestCase {

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.

// In UI tests it is usually best to stop immediately when a failure occurs.

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}


func testBucketName(){
///^[a-z0-9][a-z0-9\\-]{1,61}[a-z0-9]$"

let result1 = OSSUtil.validateBucketName("123-456abc")
XCTAssertTrue(result1)

let result2 = OSSUtil.validateBucketName("123-456abc-")
XCTAssertFalse(result2)

let result3 = OSSUtil.validateBucketName("-123-456abc")
XCTAssertFalse(result3)

let str4 = String("123\\456abc")
let result4 = OSSUtil.validateBucketName(str4)
XCTAssertFalse(result4)

let result5 = OSSUtil.validateBucketName("abc123")
XCTAssertTrue(result5)

let result6 = OSSUtil.validateBucketName("abc_123")
XCTAssertFalse(result6)

let result7 = OSSUtil.validateBucketName("a")
XCTAssertFalse(result7)

let str8 = String("abcdefghig-abcdefghig-abcdefghig-abcdefghig-abcdefghig-abcdefghig")
let result8 = OSSUtil.validateBucketName(str8)
XCTAssertFalse(result8)

}

func testEndpoint(){
let bucketName = "test-image"

let result1 = getResultEndpoint(endpoint: "http://123.test:8989/path?ooob")
XCTAssertTrue((result1 == "http://123.test:8989"))

let result2 = getResultEndpoint(endpoint: "http://192.168.0.1:8081")
XCTAssertTrue((result2 == "http://192.168.0.1:8081/\(bucketName)"))

let result3 = getResultEndpoint(endpoint: "http://192.168.0.1")
XCTAssertTrue((result3 == "http://192.168.0.1/\(bucketName)"))

let result4 = getResultEndpoint(endpoint: "http://oss-cn-region.aliyuncs.com")
XCTAssertTrue((result4 == "http://\(bucketName).oss-cn-region.aliyuncs.com"))
}

func getResultEndpoint(endpoint : String) -> String {
let bucketName = "test-image"
let urlComs = URLComponents.init(string: endpoint)
var temComs = URLComponents.init()
temComs.scheme = urlComs?.scheme
temComs.host = urlComs?.host
temComs.port = urlComs?.port

if (bucketName as NSString).oss_isNotEmpty() {
let ipAdapter = OSSIPv6Adapter.getInstance()
if OSSUtil.isOssOriginBucketHost(temComs.host!) {
temComs.host = bucketName + "." + temComs.host!
if (temComs.scheme?.lowercased() == "http") {
let dnsResult = OSSUtil.getIpByHost(temComs.host!)
temComs.host = dnsResult
}
}else if(ipAdapter!.isIPv4Address(temComs.host!) || ipAdapter!.isIPv6Address(temComs.host!) ){
temComs.path = "/\(bucketName)"
}
}

return temComs.string!
}
}
2 changes: 0 additions & 2 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ SDK提供进阶的上传、下载功能、断点续传,以及文件管理、Bu
![list1](https://github.com/aliyun/aliyun-oss-ios-sdk/blob/master/Images/list1.png)
## License
* Apache License 2.0.
Expand Down
1 change: 1 addition & 0 deletions Supporting Files/AliyunOSSiOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ FOUNDATION_EXPORT const unsigned char AliyunOSSiOSVersionString[];
#import "OSSService.h"
#import "OSSCompat.h"
#import "OSSXMLDictionary.h"
#import "OSSIPv6Adapter.h"

0 comments on commit 510a5b2

Please sign in to comment.