Search in sources :

Example 6 with HttpConnectCallback

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));
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with HttpConnectCallback

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());
    }
}
Also used : AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) Uri(android.net.Uri) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Aggregations

HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)7 TimeoutException (java.util.concurrent.TimeoutException)6 AsyncSSLException (com.koushikdutta.async.AsyncSSLException)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 ByteBufferList (com.koushikdutta.async.ByteBufferList)3 DataEmitter (com.koushikdutta.async.DataEmitter)3 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)3 DataCallback (com.koushikdutta.async.callback.DataCallback)3 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)3 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)3 Uri (android.net.Uri)2 Cancellable (com.koushikdutta.async.future.Cancellable)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Semaphore (java.util.concurrent.Semaphore)2 AsyncSocket (com.koushikdutta.async.AsyncSocket)1 ConnectCallback (com.koushikdutta.async.callback.ConnectCallback)1 Future (com.koushikdutta.async.future.Future)1 FutureCallback (com.koushikdutta.async.future.FutureCallback)1