Skip to content

Commit

Permalink
Merge pull request #205 from pushuhengyang/master
Browse files Browse the repository at this point in the history
修改bucketname和endpoint过滤规则
  • Loading branch information
sdlcdhc authored Dec 24, 2019
2 parents 6a5d106 + 66d9b3b commit eb4fbc9
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 12 deletions.
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Github地址:https://github.com/aliyun/aliyun-oss-android-sdk

更新日志:

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

2018/11/27
- release 2.9.2
1.add time interval for retry.
Expand Down
2 changes: 1 addition & 1 deletion oss-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
minSdkVersion 14
targetSdkVersion 25
versionCode 40
versionName "2.9.2"
versionName "2.9.3"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import android.text.TextUtils;

import com.alibaba.sdk.android.oss.common.LogThreadPoolManager;
import com.alibaba.sdk.android.oss.common.OSSHeaders;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.utils.BinaryUtil;
import com.alibaba.sdk.android.oss.common.utils.DateUtil;
import com.alibaba.sdk.android.oss.common.utils.HttpUtil;
import com.alibaba.sdk.android.oss.common.utils.HttpdnsMini;
import com.alibaba.sdk.android.oss.common.utils.IOUtils;
import com.alibaba.sdk.android.oss.common.utils.OSSUtils;
import com.alibaba.sdk.android.oss.common.utils.VersionInfoUtils;
Expand All @@ -17,9 +19,11 @@


import java.io.File;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.net.URI;

/**
* Created by jingdan on 2017/8/25.
Expand Down Expand Up @@ -290,6 +294,103 @@ public void testBase64() throws Exception{
assertEquals(srcFileBase64Md5,base64String);
}

public void testBucketName() {
///^[a-z0-9][a-z0-9\\-]{1,61}[a-z0-9]$"
Boolean result1 = OSSUtils.validateBucketName("123-456abc");
assertTrue(result1);

Boolean result2 = OSSUtils.validateBucketName("123-456abc-");
assertFalse(result2);

Boolean result3 = OSSUtils.validateBucketName("-123-456abc");
assertFalse(result3);

Boolean result4 = OSSUtils.validateBucketName("123\\456abc");
assertFalse(result4);

Boolean result5 = OSSUtils.validateBucketName("abc123");
assertTrue(result5);

Boolean result6 = OSSUtils.validateBucketName("abc_123");
assertFalse(result6);

Boolean result7 = OSSUtils.validateBucketName("a");
assertFalse(result7);

Boolean result8 = OSSUtils.validateBucketName("abcdefghig-abcdefghig-abcdefghig-abcdefghig-abcdefghig-abcdefghig");
assertFalse(result8);

}

public void testEndPoint() throws Exception{
String bucketName = "test-image";

String result1 = getResultEndPoint( "http://123.test:8989/path?ooob",bucketName);
assertEquals(result1 , "http://123.test:8989");

String result2 = getResultEndPoint( "http://192.168.0.1:8081",bucketName);
assertEquals(result2 , "http://192.168.0.1:8081/"+bucketName);

String result3 = getResultEndPoint( "http://192.168.0.1",bucketName);
assertEquals(result3 , "http://192.168.0.1/"+bucketName);

String result4 = getResultEndPoint( "http://oss-cn-region.aliyuncs.com",bucketName);
assertEquals(result4 , "http://"+bucketName+".oss-cn-region.aliyuncs.com");
}

private String getResultEndPoint(String urlString,String bucketName) throws Exception{

URI endpoint = new URI( urlString);

String scheme = endpoint.getScheme();
String originHost = endpoint.getHost();
String portString = null;

int port = endpoint.getPort();
if (port != -1) {
portString = String.valueOf(port);
}

if (TextUtils.isEmpty(originHost)){
String url = endpoint.toString();
OSSLog.logDebug("endpoint url : " + url);
}

OSSLog.logDebug(" scheme : " + scheme);
OSSLog.logDebug(" originHost : " + originHost);
OSSLog.logDebug(" port : " + portString);

String baseURL = scheme + "://" + originHost;
if(!TextUtils.isEmpty(portString)){
baseURL += (":" + portString);
}

if (!TextUtils.isEmpty(bucketName)) {
if (OSSUtils.isOssOriginHost(originHost)) {
// official endpoint
originHost = bucketName + "." + originHost;
String urlHost = null;

if (scheme == "http") {
urlHost = HttpdnsMini.getInstance().getIpByHostAsync(originHost);
} else {
OSSLog.logDebug("[buildCannonicalURL], disable httpdns");
}

if (!TextUtils.isEmpty(urlHost)) {
baseURL = scheme + "://" + urlHost;
} else {
baseURL = scheme + "://" + originHost;
}
}else if (OSSUtils.isValidateIP(originHost)) {
// ip address
baseURL += ("/" + bucketName);
}
}
return baseURL;
}



public static String toBase64String(byte[] binaryData){
return new String(Base64.encodeBase64(binaryData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
public final class OSSConstants {

public static final String SDK_VERSION = "2.9.2";
public static final String SDK_VERSION = "2.9.3";
public static final String DEFAULT_OSS_ENDPOINT = "http://oss-cn-hangzhou.aliyuncs.com";

public static final String DEFAULT_CHARSET_NAME = "utf-8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public static boolean validateBucketName(String bucketName) {
if (bucketName == null) {
return false;
}
final String BUCKETNAME_REGX = "^[a-z0-9][a-z0-9_\\-]{2,62}$";
final String BUCKETNAME_REGX = "^[a-z0-9][a-z0-9\\-]{1,61}[a-z0-9]$";
return bucketName.matches(BUCKETNAME_REGX);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,17 @@ public String buildCanonicalURL() throws Exception{
if (TextUtils.isEmpty(originHost)){
String url = endpoint.toString();
OSSLog.logDebug("endpoint url : " + url);
originHost = url.substring((scheme + "://").length(),url.length());
// originHost = url.substring((scheme + "://").length(),url.length());
}

OSSLog.logDebug(" scheme : " + scheme);
OSSLog.logDebug(" originHost : " + originHost);
OSSLog.logDebug(" port : " + portString);

String baseURL = endpoint.toString();
String baseURL = scheme + "://" + originHost;
if(!TextUtils.isEmpty(portString)){
baseURL += (":" + portString);
}

if (!TextUtils.isEmpty(bucketName)) {
if (OSSUtils.isOssOriginHost(originHost)) {
Expand All @@ -340,13 +343,8 @@ public String buildCanonicalURL() throws Exception{
}
}else if (OSSUtils.isValidateIP(originHost)) {
// ip address
baseURL = endpoint.toString() + "/" + bucketName;
} else {
// cname时不做任何处理
baseURL = endpoint.toString();
baseURL += ("/" + bucketName);
}
} else {
baseURL = endpoint.toString();
}

/*
Expand Down
2 changes: 1 addition & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
project.name=aliyun-oss-sdk-android
project.groupId=com.aliyun.dpa
project.artifactId=oss-android-sdk
project.version=2.9.2
project.version=2.9.3
project.packaging=aar
project.siteUrl=https://github.com/aliyun/aliyun-oss-android-sdk
project.gitUrl=https://github.com/aliyun/aliyun-oss-android-sdk.git
Expand Down

0 comments on commit eb4fbc9

Please sign in to comment.