use of com.koushikdutta.async.http.callback.HttpConnectCallback in project AndroidAsync by koush.
the class HttpClientTests method testClockworkMod.
public void testClockworkMod() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Md5 md5 = Md5.createInstance();
client.execute("http://www.clockworkmod.com", new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
// make sure gzip decoding works, as that is generally what github sends.
Assert.assertEquals("gzip", response.headers().get("Content-Encoding"));
response.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
md5.update(bb);
}
});
response.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
semaphore.release();
}
});
}
});
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
use of com.koushikdutta.async.http.callback.HttpConnectCallback in project AndroidAsync by koush.
the class AsyncProxyServer method onRequest.
@Override
protected void onRequest(HttpServerRequestCallback callback, AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
super.onRequest(callback, request, response);
if (callback != null)
return;
try {
Uri uri;
try {
uri = Uri.parse(request.getPath());
if (uri.getScheme() == null)
throw new Exception("no host or full uri provided");
} catch (Exception e) {
String host = request.getHeaders().get("Host");
int port = 80;
if (host != null) {
String[] splits = host.split(":", 2);
if (splits.length == 2) {
host = splits[0];
port = Integer.parseInt(splits[1]);
}
}
uri = Uri.parse("http://" + host + ":" + port + request.getPath());
}
proxyClient.execute(new AsyncHttpRequest(uri, request.getMethod(), request.getHeaders()), new HttpConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, AsyncHttpResponse remoteResponse) {
if (ex != null) {
response.code(500);
response.send(ex.getMessage());
return;
}
response.proxy(remoteResponse);
}
});
} catch (Exception e) {
response.code(500);
response.send(e.getMessage());
}
}
Aggregations