Skip to content

Commit 472b43d

Browse files
committed
Merge pull request #101 from longbai/use_ip_retry_host
use ip for upload backup
2 parents 95c976a + 1aefc20 commit 472b43d

File tree

9 files changed

+119
-18
lines changed

9 files changed

+119
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Estat/Xstat 等性能报告
1111
* post上传进度粒度更细(小影提交)
1212
* 统一android和objective C的错误码
13+
* 使用IP 作为 域名解析失败时上传方案
1314

1415
## 7.0.2 (2015-01-22)
1516

library/src/androidTest/java/com/qiniu/android/FormUploadTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.test.suitebuilder.annotation.SmallTest;
66
import android.util.Log;
77

8+
import com.qiniu.android.common.Config;
89
import com.qiniu.android.http.ResponseInfo;
910
import com.qiniu.android.storage.UpCompletionHandler;
1011
import com.qiniu.android.storage.UploadManager;
@@ -251,4 +252,34 @@ public void testNoComplete() {
251252
Assert.assertEquals("no UpCompletionHandler", error.getMessage());
252253
}
253254

255+
@SmallTest
256+
public void testIpBack() throws Throwable {
257+
Config.defaultUpHost = "upwelcome.qiniu.com";
258+
final String expectKey = "你好;\"\r\n\r\n\r\n";
259+
Map<String, String> params = new HashMap<String, String>();
260+
params.put("x:foo", "fooval");
261+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
262+
263+
uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
264+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
265+
Log.i("qiniutest", k + rinfo);
266+
key = k;
267+
info = rinfo;
268+
resp = response;
269+
signal.countDown();
270+
}
271+
}, opt);
272+
273+
274+
try {
275+
signal.await(60, TimeUnit.SECONDS); // wait for callback
276+
} catch (InterruptedException e) {
277+
e.printStackTrace();
278+
}
279+
Assert.assertEquals(expectKey, key);
280+
Assert.assertTrue(info.isOK());
281+
Assert.assertNotNull(info.reqId);
282+
Assert.assertNotNull(resp);
283+
}
284+
254285
}

library/src/androidTest/java/com/qiniu/android/HttpTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import junit.framework.Assert;
1212

13+
import org.apache.http.Header;
14+
import org.apache.http.message.BasicHeader;
1315
import org.json.JSONObject;
1416

1517
import java.util.concurrent.CountDownLatch;
@@ -159,4 +161,24 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
159161
Assert.assertEquals(ResponseInfo.CannotConnectToHost, info.statusCode);
160162
}
161163

164+
@SmallTest
165+
public void testPostIP() throws Throwable {
166+
Header[] x = {new BasicHeader("Host", "www.qiniu.com")};
167+
httpManager.postData("http://183.136.139.12/", "hello".getBytes(), x, null, new CompletionHandler() {
168+
@Override
169+
public void complete(ResponseInfo rinfo, JSONObject response) {
170+
Log.d("qiniutest", rinfo.toString());
171+
info = rinfo;
172+
signal.countDown();
173+
}
174+
});
175+
176+
try {
177+
signal.await(60, TimeUnit.SECONDS); // wait for callback
178+
} catch (InterruptedException e) {
179+
e.printStackTrace();
180+
}
181+
Assert.assertNotNull(info.reqId);
182+
Assert.assertEquals(200, info.statusCode);
183+
}
162184
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ public final class Config {
1414
*/
1515
public static final String UP_HOST_BACKUP = "up.qiniu.com";
1616

17+
/**
18+
* 备用上传服务器,当默认服务器网络链接失败时使用
19+
*/
20+
public static final String UP_IP_BACKUP = "183.136.139.16";
21+
22+
/**
23+
* 默认上传服务器
24+
*/
25+
public static String defaultUpHost = UP_HOST;
26+
27+
1728
/**
1829
* 断点上传时的分片大小(可根据网络情况适当调整)
1930
*/

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

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
import com.qiniu.android.common.Config;
77

88
import org.apache.http.Header;
9+
import org.apache.http.HttpEntity;
10+
import org.apache.http.message.BasicHeader;
911
import org.json.JSONObject;
1012

1113
import java.io.ByteArrayInputStream;
1214
import java.io.FileNotFoundException;
1315
import java.io.IOException;
16+
import java.net.URI;
17+
import java.net.URISyntaxException;
18+
import java.net.URL;
1419
import java.util.Map;
1520
import java.util.Random;
1621

@@ -23,12 +28,18 @@ public final class HttpManager {
2328
private static final String userAgent = getUserAgent();
2429
private AsyncHttpClient client;
2530
private IReport reporter;
31+
private String backUpIp;
2632

2733
public HttpManager(Proxy proxy) {
2834
this(proxy, null);
2935
}
3036

3137
public HttpManager(Proxy proxy, IReport reporter) {
38+
this(proxy, reporter, null);
39+
}
40+
41+
public HttpManager(Proxy proxy, IReport reporter, String backUpIp) {
42+
this.backUpIp = backUpIp;
3243
client = new AsyncHttpClient();
3344
client.setConnectTimeout(Config.CONNECT_TIMEOUT);
3445
client.setResponseTimeout(Config.RESPONSE_TIMEOUT);
@@ -85,20 +96,48 @@ private static String getUserAgent() {
8596
*/
8697
public void postData(String url, byte[] data, int offset, int size, Header[] headers,
8798
ProgressHandler progressHandler, final CompletionHandler completionHandler) {
88-
89-
CompletionHandler wrapper = wrap(completionHandler);
90-
Header[] h = reporter.appendStatHeaders(headers);
91-
AsyncHttpResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler);
9299
ByteArrayEntity entity = new ByteArrayEntity(data, offset, size, progressHandler);
93-
94-
client.post(null, url, h, entity, RequestParams.APPLICATION_OCTET_STREAM, handler);
100+
postEntity(url, entity, headers, progressHandler, completionHandler);
95101
}
96102

97103
public void postData(String url, byte[] data, Header[] headers,
98104
ProgressHandler progressHandler, CompletionHandler completionHandler) {
99105
postData(url, data, 0, data.length, headers, progressHandler, completionHandler);
100106
}
101107

108+
private void postEntity(final String url, final HttpEntity entity, Header[] headers,
109+
ProgressHandler progressHandler, CompletionHandler completionHandler) {
110+
final CompletionHandler wrapper = wrap(completionHandler);
111+
final Header[] h = reporter.appendStatHeaders(headers);
112+
final AsyncHttpResponseHandler originHandler = new ResponseHandler(url, wrapper, progressHandler);
113+
if(backUpIp == null){
114+
client.post(null, url, h, entity, null, originHandler);
115+
return;
116+
}
117+
118+
client.post(null, url, h, entity, null, new ResponseHandler(url, new CompletionHandler() {
119+
@Override
120+
public void complete(ResponseInfo info, JSONObject response) {
121+
if (info.statusCode != ResponseInfo.UnknownHost){
122+
wrapper.complete(info, response);
123+
return;
124+
}
125+
Header[] h2 = new Header[h.length + 1];
126+
System.arraycopy(h, 0, h2, 0, h.length);
127+
128+
URI uri = URI.create(url);
129+
String newUrl = null;
130+
try {
131+
newUrl = new URI(uri.getScheme(), null, backUpIp, uri.getPort(), uri.getPath(), uri.getQuery(), null).toString();
132+
} catch (URISyntaxException e) {
133+
throw new AssertionError(e);
134+
}
135+
h2[h.length] = new BasicHeader("Host", uri.getHost());
136+
client.post(null, newUrl, h2, entity, null, originHandler);
137+
}
138+
}, progressHandler));
139+
}
140+
102141
/**
103142
* 以POST方式发送multipart/form-data格式数据
104143
*
@@ -130,11 +169,9 @@ public void multipartPost(String url, PostArgs args, ProgressHandler progressHan
130169
}
131170
}
132171

133-
CompletionHandler wrapper = wrap(completionHandler);
134-
AsyncHttpResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler);
135172
ByteArrayEntity entity = mbuilder.build(progressHandler);
136173
Header[] h = reporter.appendStatHeaders(new Header[0]);
137-
client.post(null, url, h, entity, null, handler);
174+
postEntity(url, entity, h, progressHandler, completionHandler);
138175
}
139176

140177
private CompletionHandler wrap(final CompletionHandler completionHandler) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class ResponseHandler extends AsyncHttpResponseHandler {
4646
/**
4747
* 服务器IP
4848
*/
49-
private String ip;
49+
private String ip = null;
5050

5151
public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler) {
5252
super(Looper.getMainLooper());
@@ -62,11 +62,10 @@ public ResponseHandler(String url, CompletionHandler completionHandler, Progress
6262
this.progressHandler = progressHandler;
6363
}
6464

65-
private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers, byte[] responseBody, String host, double duration,
65+
private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers, byte[] responseBody, String host, String ip, double duration,
6666
Throwable error) {
6767
String reqId = null;
6868
String xlog = null;
69-
String ip = null;
7069
String xvia = null;
7170
if (headers != null) {
7271
for (Header h : headers) {
@@ -146,15 +145,15 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
146145
} catch (Exception e) {
147146
exception = e;
148147
}
149-
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, duration, exception);
148+
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, ip, duration, exception);
150149
Log.i("upload----success", info.toString());
151150
completionHandler.complete(info, obj);
152151
}
153152

154153
@Override
155154
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
156155
double duration = (System.currentTimeMillis() - reqStartTime) / 1000.0;
157-
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, duration, error);
156+
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, ip, duration, error);
158157
Log.i("upload----failed", info.toString());
159158
completionHandler.complete(info, null);
160159
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ public void complete(ResponseInfo info, JSONObject response) {
120120
completionHandler.complete(key, info, response);
121121
}
122122
};
123-
String host = Config.UP_HOST;
123+
String host = Config.defaultUpHost;
124124
if (info.needSwitchServer()) {
125125
host = Config.UP_HOST_BACKUP;
126126
}
127127
httpManager.multipartPost("http://" + host, args, progress, retried);
128128
}
129129
};
130130

131-
httpManager.multipartPost("http://" + Config.UP_HOST, args, progress, completion);
131+
httpManager.multipartPost("http://" + Config.defaultUpHost, args, progress, completion);
132132
}
133133
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void run() {
7878
e.printStackTrace();
7979
completionHandler.complete(key, ResponseInfo.fileError(e), null);
8080
}
81-
nextTask(offset, 0, Config.UP_HOST);
81+
nextTask(offset, 0, Config.defaultUpHost);
8282
}
8383

8484
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public UploadManager(Recorder recorder, KeyGenerator keyGen) {
3535
*/
3636
public UploadManager(Recorder recorder, KeyGenerator keyGen, Proxy proxy) {
3737
this.recorder = recorder;
38-
this.httpManager = new HttpManager(proxy, new StatReport());
38+
this.httpManager = new HttpManager(proxy, new StatReport(), Config.UP_IP_BACKUP);
3939
this.keyGen = keyGen;
4040
}
4141

0 commit comments

Comments
 (0)