Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AndroidAsync/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.koushikdutta.async"
android:versionCode="114"
android:versionName="1.1.4" >
android:versionCode="115"
android:versionName="1.1.5" >

<uses-sdk
android:minSdkVersion="8"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.koushikdutta.async;

import android.util.Log;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;

public class AsyncDatagramSocket extends AsyncNetworkSocket {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.koushikdutta.async;

import android.util.Log;

import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.callback.WritableCallback;
Expand Down Expand Up @@ -350,4 +351,8 @@ public InetSocketAddress getRemoteAddress() {
public int getLocalPort() {
return mChannel.getLocalPort();
}

public Object getSocket() {
return getChannel().getSocket();
}
}
16 changes: 13 additions & 3 deletions AndroidAsync/src/com/koushikdutta/async/AsyncSSLSocketWrapper.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package com.koushikdutta.async;

import android.os.Build;

import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.callback.WritableCallback;
import com.koushikdutta.async.wrapper.AsyncSocketWrapper;

import org.apache.http.conn.ssl.StrictHostnameVerifier;

import javax.net.ssl.*;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLEngineResult.Status;
import java.nio.ByteBuffer;
import java.security.KeyStore;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLEngineResult.Status;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class AsyncSSLSocketWrapper implements AsyncSocketWrapper, AsyncSSLSocket {
AsyncSocket mSocket;
BufferedDataEmitter mEmitter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ public String readString() {
builder.append(new String(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
reclaim(bb);
}
remaining = 0;
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ public void close() throws IOException {
}

public abstract int getLocalPort();
public abstract Object getSocket();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.koushikdutta.async;

import android.util.Log;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.DatagramChannel;
Expand Down Expand Up @@ -86,4 +83,9 @@ public long read(ByteBuffer[] byteBuffers) throws IOException {
public long read(ByteBuffer[] byteBuffers, int i, int i2) throws IOException {
return mChannel.read(byteBuffers, i, i2);
}

@Override
public Object getSocket() {
return mChannel.socket();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public long read(ByteBuffer[] byteBuffers, int i, int i2) throws IOException {
assert false;
throw new IOException(msg);
}

@Override
public Object getSocket() {
return mChannel.socket();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public long read(ByteBuffer[] byteBuffers) throws IOException {
public long read(ByteBuffer[] byteBuffers, int i, int i2) throws IOException {
return mChannel.read(byteBuffers, i, i2);
}

@Override
public Object getSocket() {
return mChannel.socket();
}
}
9 changes: 4 additions & 5 deletions AndroidAsync/src/com/koushikdutta/async/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ public static void writeAll(final DataSink sink, final ByteBufferList bb, final
sink.setWriteableCallback(wc = new WritableCallback() {
@Override
public void onWriteable() {
if (bb.remaining() > 0)
sink.write(bb);
sink.write(bb);
if (bb.remaining() == 0 && callback != null)
callback.onCompleted(null);
}
Expand All @@ -183,13 +182,13 @@ public static void writeAll(DataSink sink, byte[] bytes, CompletedCallback callb
writeAll(sink, bbl, callback);
}

public static AsyncSocket getWrappedSocket(AsyncSocket socket, Class wrappedClass) {
public static <T extends AsyncSocket> T getWrappedSocket(AsyncSocket socket, Class<T> wrappedClass) {
if (wrappedClass.isInstance(socket))
return socket;
return (T)socket;
while (socket instanceof AsyncSocketWrapper) {
socket = ((AsyncSocketWrapper)socket).getSocket();
if (wrappedClass.isInstance(socket))
return socket;
return (T)socket;
}
return null;
}
Expand Down
1 change: 1 addition & 0 deletions AndroidAsync/src/com/koushikdutta/async/future/Future.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface Future<T> extends Cancellable, java.util.concurrent.Future<T> {
* @return
*/
public Future<T> setCallback(FutureCallback<T> callback);
public <C extends TransformFuture<?, T>> C then(C callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ public SimpleFuture<T> setCallback(FutureCallback<T> callback) {
return this;
}

@Override
public <C extends TransformFuture<?, T>> C then(C callback) {
callback.setParent(this);
setCallback(callback);
return callback;
}

@Override
public SimpleFuture<T> setParent(Cancellable parent) {
super.setParent(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {

public Future<WebSocket> websocket(String uri, String protocol, final WebSocketConnectCallback callback) {
assert callback != null;
final AsyncHttpGet get = new AsyncHttpGet(uri);
final AsyncHttpGet get = new AsyncHttpGet(uri.replace("ws://", "http://").replace("wss://", "https://"));
return websocket(get, protocol, callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getMethod() {

@Override
public String toString() {
String path = AsyncHttpRequest.this.getUri().getPath();
String path = AsyncHttpRequest.this.getUri().getRawPath();
if (path.length() == 0)
path = "/";
String query = AsyncHttpRequest.this.getUri().getRawQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public AsyncServer getServer() {
}

public static class CacheData implements Parcelable {
DiskLruCache.Snapshot snapshot;
CacheResponse candidate;
long contentLength;
ResponseHeaders cachedResponseHeaders;
Expand Down Expand Up @@ -278,7 +279,7 @@ public Cancellable getSocket(final GetSocketData data) {
}

String key = uriToKey(data.request.getUri());
DiskLruCache.Snapshot snapshot;
DiskLruCache.Snapshot snapshot = null;
Entry entry;
try {
snapshot = cache.get(key);
Expand All @@ -299,7 +300,7 @@ public Cancellable getSocket(final GetSocketData data) {
snapshot.close();
return null;
}

CacheResponse candidate = entry.isHttps() ? new EntrySecureCacheResponse(entry, snapshot) : new EntryCacheResponse(entry, snapshot);

Map<String, List<String>> responseHeadersMap;
Expand All @@ -310,6 +311,7 @@ public Cancellable getSocket(final GetSocketData data) {
}
catch (Exception e) {
networkCount++;
snapshot.close();
return null;
}
if (responseHeadersMap == null || cachedResponseBody == null) {
Expand All @@ -319,6 +321,7 @@ public Cancellable getSocket(final GetSocketData data) {
catch (Exception e) {
}
networkCount++;
snapshot.close();
return null;
}

Expand Down Expand Up @@ -351,6 +354,7 @@ public void run() {
else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
data.request.logi("Response may be served from conditional cache");
CacheData cacheData = new CacheData();
cacheData.snapshot = snapshot;
cacheData.contentLength = contentLength;
cacheData.cachedResponseHeaders = cachedResponseHeaders;
cacheData.candidate = candidate;
Expand All @@ -367,6 +371,7 @@ else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
catch (Exception e) {
}
networkCount++;
snapshot.close();
return null;
}
}
Expand Down Expand Up @@ -567,9 +572,10 @@ public void onBodyDecoder(OnBodyData data) {
bodySpewer.spew();
return;
}

// did not validate, so fall through and cache the response
data.state.remove("cache-data");
cacheData.snapshot.close();
}

if (!caching)
Expand Down Expand Up @@ -623,17 +629,24 @@ public void onBodyDecoder(OnBodyData data) {

@Override
public void onRequestComplete(OnRequestCompleteData data) {
BodyCacher cacher = data.state.getParcelable("body-cacher");
if (cacher == null)
return;
CacheData cacheData = data.state.getParcelable("cache-data");
if (cacheData != null && cacheData.snapshot != null)
cacheData.snapshot.close();

try {
if (data.exception != null)
cacher.abort();
else
cacher.commit();
}
catch (Exception e) {
CachedSocket cachedSocket = Util.getWrappedSocket(data.socket, CachedSocket.class);
if (cachedSocket != null)
((SnapshotCacheResponse)cachedSocket.cacheResponse).getSnapshot().close();

BodyCacher cacher = data.state.getParcelable("body-cacher");
if (cacher != null) {
try {
if (data.exception != null)
cacher.abort();
else
cacher.commit();
}
catch (Exception e) {
}
}
}

Expand Down Expand Up @@ -901,11 +914,20 @@ private static InputStream newBodyInputStream(final DiskLruCache.Snapshot snapsh
};
}

static class EntryCacheResponse extends CacheResponse {
static interface SnapshotCacheResponse {
public DiskLruCache.Snapshot getSnapshot();
}

static class EntryCacheResponse extends CacheResponse implements SnapshotCacheResponse {
private final Entry entry;
private final DiskLruCache.Snapshot snapshot;
private final InputStream in;

@Override
public DiskLruCache.Snapshot getSnapshot() {
return snapshot;
}

public EntryCacheResponse(Entry entry, DiskLruCache.Snapshot snapshot) {
this.entry = entry;
this.snapshot = snapshot;
Expand All @@ -921,11 +943,17 @@ public EntryCacheResponse(Entry entry, DiskLruCache.Snapshot snapshot) {
}
}

static class EntrySecureCacheResponse extends SecureCacheResponse {
static class EntrySecureCacheResponse extends SecureCacheResponse implements SnapshotCacheResponse {
private final Entry entry;
private final DiskLruCache.Snapshot snapshot;
private final InputStream in;

@Override
public DiskLruCache.Snapshot getSnapshot() {
return snapshot;
}


public EntrySecureCacheResponse(Entry entry, DiskLruCache.Snapshot snapshot) {
this.entry = entry;
this.snapshot = snapshot;
Expand Down
Loading