Skip to content

Commit 9dff7a4

Browse files
committed
add port rollback
1 parent b28cb89 commit 9dff7a4

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

library/src/main/java/com/qiniu/android/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "7.0.5.1";
5+
public static final String VERSION = "7.0.6";
66

77
public static final String UTF_8 = "utf-8";
88
}

library/src/main/java/com/qiniu/android/http/HttpManager.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ public void postData(String url, byte[] data, Header[] headers, ProgressHandler
111111
}
112112

113113
private void postEntity(String url, final HttpEntity entity, Header[] headers,
114-
ProgressHandler progressHandler, CompletionHandler completionHandler, final boolean forceIp) {
114+
final ProgressHandler progressHandler, final CompletionHandler completionHandler, final boolean forceIp) {
115115
final CompletionHandler wrapper = wrap(completionHandler);
116116
final Header[] h = reporter.appendStatHeaders(headers);
117117

118118
if (converter != null){
119119
url = converter.convert(url);
120120
}
121121

122-
final AsyncHttpResponseHandler originHandler = new ResponseHandler(url, wrapper, progressHandler);
122+
ResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler);
123123
if(backUpIp == null || converter != null){
124-
client.post(null, url, h, entity, null, originHandler);
124+
client.post(null, url, h, entity, null, handler);
125125
return;
126126
}
127127
final String url2 = url;
@@ -130,13 +130,13 @@ private void postEntity(String url, final HttpEntity entity, Header[] headers,
130130
t.execute(new Runnable() {
131131
@Override
132132
public void run() {
133-
URI uri = URI.create(url2);
133+
final URI uri = URI.create(url2);
134134
String ip = Dns.getAddress(uri.getHost());
135135
if (ip == null || ip.equals("") || forceIp) {
136136
ip = backUpIp;
137137
}
138138

139-
Header[] h2 = new Header[h.length + 1];
139+
final Header[] h2 = new Header[h.length + 1];
140140
System.arraycopy(h, 0, h2, 0, h.length);
141141

142142
String newUrl = null;
@@ -146,7 +146,25 @@ public void run() {
146146
throw new AssertionError(e);
147147
}
148148
h2[h.length] = new BasicHeader("Host", uri.getHost());
149-
client.post(null, newUrl, h2, entity, null, originHandler);
149+
final String ip2 = ip;
150+
ResponseHandler handler2 = new ResponseHandler(url2, wrap(new CompletionHandler() {
151+
@Override
152+
public void complete(ResponseInfo info, JSONObject response) {
153+
if (uri.getPort() == 80 || info.statusCode != ResponseInfo.CannotConnectToHost){
154+
completionHandler.complete(info, response);
155+
return;
156+
}
157+
String newUrl80 = null;
158+
try {
159+
newUrl80 = new URI(uri.getScheme(), null, ip2, 80, uri.getPath(), uri.getQuery(), null).toString();
160+
} catch (URISyntaxException e) {
161+
throw new AssertionError(e);
162+
}
163+
ResponseHandler handler3 = new ResponseHandler(newUrl80, completionHandler, progressHandler);
164+
client.post(null, newUrl80, h2, entity, null, handler3);
165+
}
166+
}), progressHandler);
167+
client.post(null, newUrl, h2, entity, null, handler2);
150168
}
151169
});
152170
}

library/src/main/java/com/qiniu/android/http/ResponseInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public boolean isQiniu() {
122122
}
123123

124124
public String toString() {
125-
return String.format(Locale.ENGLISH, "{ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, ip:%s, port:%d, duration:%f s, error:%s}",
125+
return String.format(Locale.ENGLISH, "{ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, ip:%s, port:%d, duration:%f s, error:%s}",
126126
super.toString(), statusCode, reqId, xlog, xvia, host, ip, port, duration, error);
127127
}
128128
}

library/src/main/java/com/qiniu/android/storage/Configuration.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public final class Configuration {
7171
private Configuration(Builder builder){
7272
upHost = builder.upHost;
7373
upHostBackup = builder.upHostBackup;
74-
upIp = builder.upIp;
75-
upPort = builder.upPort;
74+
upIp = getIp(builder);
75+
upPort = getPort(builder);
7676

7777
chunkSize = builder.chunkSize;
7878
putThreshold = builder.putThreshold;
@@ -90,6 +90,20 @@ private Configuration(Builder builder){
9090
urlConverter = builder.urlConverter;
9191
}
9292

93+
private static int getPort(Builder builder){
94+
if (builder.urlConverter != null){
95+
return 80;
96+
}
97+
return builder.upPort;
98+
}
99+
100+
private static String getIp(Builder builder){
101+
if (builder.urlConverter != null){
102+
return null;
103+
}
104+
return builder.upIp;
105+
}
106+
93107
private KeyGenerator getKeyGen(KeyGenerator keyGen) {
94108
if(keyGen == null) {
95109
keyGen = new KeyGenerator() {
@@ -182,7 +196,6 @@ public Builder retryMax(int times){
182196

183197
public Builder urlConverter(UrlConverter converter){
184198
this.urlConverter = converter;
185-
this.upIp = null;
186199
return this;
187200
}
188201

0 commit comments

Comments
 (0)