Skip to content

Commit

Permalink
Merge pull request #207 from longbai/proxy
Browse files Browse the repository at this point in the history
add proxy
  • Loading branch information
longbai authored Jul 14, 2016
2 parents 0d8e6ee + 81bcae5 commit 15a4918
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 53 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#Changelog

## 7.2.1 (2016-07-14)

### 修正
* 当出现异常服务端返回为空时会造成NPE

### 增加
* proxy 支持

## 7.2.0 (2016-04-29)

### 修改
Expand Down
26 changes: 12 additions & 14 deletions library/library.iml
Original file line number Diff line number Diff line change
Expand Up @@ -48,57 +48,55 @@
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/debugAndroidTest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-22.jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-23.jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/ivy.xml" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
Expand Down
27 changes: 26 additions & 1 deletion library/src/androidTest/java/com/qiniu/android/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.qiniu.android.http.Client;
import com.qiniu.android.http.CompletionHandler;
import com.qiniu.android.http.ProxyConfiguration;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.utils.StringMap;

Expand Down Expand Up @@ -196,7 +197,31 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
} catch (InterruptedException e) {
e.printStackTrace();
}
Assert.assertNotNull(info.reqId);
Assert.assertTrue(!"".equals(info.reqId));
Assert.assertEquals(200, info.statusCode);
}

@SmallTest
public void testProxy() throws Throwable {
StringMap x = new StringMap();
ProxyConfiguration p = new ProxyConfiguration("115.231.183.168", 80);
Client c = new Client(p, 10, 30, null, null);
c.asyncPost("http://upproxy1.qiniu.com", "hello".getBytes(),
x, null, new CompletionHandler() {
@Override
public void complete(ResponseInfo rinfo, JSONObject response) {
Log.d("qiniutest", rinfo.toString());
info = rinfo;
signal.countDown();
}
}, null);

try {
signal.await(60, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
Assert.assertTrue(!"".equals(info.reqId));
Assert.assertEquals(400, info.statusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* Created by bailong on 14/10/12.
*/
public final class TestConfig {
public static final String token = "anEC5u_72gw1kZPSy3Dsq1lo_DPXyvuPDaj4ePkN:zmaikrTu1lgLb8DTvKQbuFZ5ai0=:eyJzY29wZSI6ImFuZHJvaWRzZGsiLCJyZXR1cm5Cb2R5Ijoie1wiaGFzaFwiOlwiJChldGFnKVwiLFwia2V5XCI6XCIkKGtleSlcIixcImZuYW1lXCI6XCIgJChmbmFtZSkgXCIsXCJmc2l6ZVwiOlwiJChmc2l6ZSlcIixcIm1pbWVUeXBlXCI6XCIkKG1pbWVUeXBlKVwiLFwieDphXCI6XCIkKHg6YSlcIn0iLCJkZWFkbGluZSI6MTQ2NjIyMjcwMX0=";
public static final String token = "QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:Acr2g-mjKFQtGJaVpyLzMVSZ8Bk=:eyJzY29wZSI6ImFuZHJvaWRzZGsiLCJyZXR1cm5ib2R5Ijoie1wiaGFzaFwiOlwiJChldGFnKVwiLFwia2V5XCI6XCIkKGtleSlcIixcImZuYW1lXCI6XCIgJChmbmFtZSkgXCIsXCJmc2l6ZVwiOlwiJChmc2l6ZSlcIixcIm1pbWVUeXBlXCI6XCIkKG1pbWVUeXBlKVwiLFwieDphXCI6XCIkKHg6YSlcIn0iLCJkZWFkbGluZSI6NDI5NDk2NzI5NX0=";

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


public final class Constants {
public static final String VERSION = "7.2.0";
public static final String VERSION = "7.2.1";

public static final String UTF_8 = "utf-8";
}
11 changes: 9 additions & 2 deletions library/src/main/java/com/qiniu/android/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Authenticator;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Credentials;
import okhttp3.Dns;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
Expand All @@ -29,6 +31,8 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;

/**
* Created by bailong on 15/11/12.
Expand All @@ -45,12 +49,15 @@ public Client() {
this(null, 10, 30, null, null);
}

public Client(Proxy proxy, int connectTimeout, int responseTimeout, UrlConverter converter, final DnsManager dns) {
public Client(ProxyConfiguration proxy, int connectTimeout, int responseTimeout, UrlConverter converter, final DnsManager dns) {
this.converter = converter;
OkHttpClient.Builder builder = new OkHttpClient.Builder();

if (proxy != null) {
builder.proxy(proxy.toSystemProxy());
builder.proxy(proxy.proxy());
if (proxy.user != null && proxy.password != null){
builder.proxyAuthenticator(proxy.authenticator());
}
}
if (dns != null) {
builder.dns(new Dns() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Created by bailong on 16/1/8.
*/
public class CountingRequestBody extends RequestBody {
public final class CountingRequestBody extends RequestBody {

private static final int SEGMENT_SIZE = 2048; // okio.Segment.SIZE

Expand Down
29 changes: 0 additions & 29 deletions library/src/main/java/com/qiniu/android/http/Proxy.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.qiniu.android.http;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;

import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.Route;

/**
* http 代理
*/
public final class ProxyConfiguration {

public final String hostAddress;
public final int port;
public final String user;
public final String password;
public final Proxy.Type type;

/**
* @param hostAddress 服务器域名或IP,比如proxy.com, 192.168.1.1
* @param port 端口
* @param user 用户名,无则填null
* @param password 用户密码,无则填null
*/
public ProxyConfiguration(String hostAddress, int port, String user, String password, java.net.Proxy.Type type) {
this.hostAddress = hostAddress;
this.port = port;
this.user = user;
this.password = password;
this.type = type;
}

public ProxyConfiguration(String hostAddress, int port) {
this(hostAddress, port, null, null, Proxy.Type.HTTP);
}
Proxy proxy(){
return new Proxy(type, new InetSocketAddress(hostAddress, port));
}

Authenticator authenticator(){
return new Authenticator() {
@Override
public okhttp3.Request authenticate(Route route, okhttp3.Response response) throws IOException {
String credential = Credentials.basic(user, password);
return response.request().newBuilder().
header("Proxy-Authorization", credential).
header("Proxy-Connection", "Keep-Alive").build();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.qiniu.android.dns.NetworkInfo;
import com.qiniu.android.dns.local.AndroidDnsServer;
import com.qiniu.android.dns.local.Resolver;
import com.qiniu.android.http.Proxy;
import com.qiniu.android.http.ProxyConfiguration;
import com.qiniu.android.http.UrlConverter;

import java.io.File;
Expand All @@ -35,7 +35,7 @@ public final class Configuration {
public final Recorder recorder;
public final KeyGenerator keyGen;

public final Proxy proxy;
public final ProxyConfiguration proxy;

/**
* 断点上传时的分片大小(可根据网络情况适当调整)
Expand Down Expand Up @@ -119,7 +119,7 @@ public static class Builder {

private Recorder recorder = null;
private KeyGenerator keyGen = null;
private Proxy proxy = null;
private ProxyConfiguration proxy = null;

private int chunkSize = 256 * 1024;
private int putThreshold = 512 * 1024;
Expand Down Expand Up @@ -160,7 +160,7 @@ public Builder recorder(Recorder recorder, KeyGenerator keyGen) {
return this;
}

public Builder proxy(Proxy proxy) {
public Builder proxy(ProxyConfiguration proxy) {
this.proxy = proxy;
return this;
}
Expand Down

0 comments on commit 15a4918

Please sign in to comment.