Search in sources :

Example 1 with AsyncServerSocket

use of com.koushikdutta.async.AsyncServerSocket in project ion by koush.

the class Issues method testIssue312.

public void testIssue312() throws Exception {
    String b64 = "SFRUUC8xLjAgMzAyIEZvdW5kDQpTZXQtQ29va2ll\n" + "OlNFU1NJT049NUJBRDlERTEwQjY0NjgwNDsKTG9j\n" + "YXRpb246IGhvbWUuY2dpCkNvbnRlbnQtdHlwZTog\n" + "dGV4dC9odG1sCgo8aHRtbD48aGVhZD48bWV0YSBo\n" + "dHRwLWVxdWl2PSdyZWZyZXNoJyBjb250ZW50PScw\n" + "OyB1cmw9aG9tZS5jZ2knPjwvbWV0YT48L2hlYWQ+\n" + "PGJvZHk+PC9ib2R5PjwvaHRtbD4K";
    /*
        HTTP/1.0 302 Found
        Set-Cookie:SESSION=5BAD9DE10B646804;
        Location: home.cgi
        Content-type: text/html

        <html><head><meta http-equiv='refresh' content='0; url=home.cgi'></meta></head><body></body></html>
         */
    // the above is using newlines, and not CRLF.
    final byte[] responseData = Base64.decode(b64, 0);
    server = Ion.getDefault(getContext()).getServer().listen(null, 0, new ListenCallback() {

        @Override
        public void onAccepted(final AsyncSocket socket) {
            Util.writeAll(socket, responseData, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    socket.end();
                    server.stop();
                }
            });
        }

        @Override
        public void onListening(AsyncServerSocket socket) {
        }

        @Override
        public void onCompleted(Exception ex) {
        }
    });
    Ion.with(getContext()).load("http://localhost:" + server.getLocalPort()).followRedirect(false).asString().get();
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncSocket(com.koushikdutta.async.AsyncSocket) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ListenCallback(com.koushikdutta.async.callback.ListenCallback)

Example 2 with AsyncServerSocket

use of com.koushikdutta.async.AsyncServerSocket in project ion by koush.

the class HttpTests method testProxy.

public void testProxy() throws Exception {
    wasProxied = false;
    final AsyncServer proxyServer = new AsyncServer();
    try {
        AsyncProxyServer httpServer = new AsyncProxyServer(proxyServer) {

            @Override
            protected boolean onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                wasProxied = true;
                return super.onRequest(request, response);
            }
        };
        AsyncServerSocket s = httpServer.listen(proxyServer, 0);
        Future<String> ret = Ion.with(getContext()).load("http://www.clockworkmod.com").proxy("localhost", s.getLocalPort()).asString();
        String data;
        assertNotNull(data = ret.get(10000, TimeUnit.MILLISECONDS));
        assertTrue(data.contains("ClockworkMod"));
        assertTrue(wasProxied);
    } finally {
        proxyServer.stop();
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncServer(com.koushikdutta.async.AsyncServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) AsyncProxyServer(com.koushikdutta.async.http.server.AsyncProxyServer)

Example 3 with AsyncServerSocket

use of com.koushikdutta.async.AsyncServerSocket in project AndroidAsync by koush.

the class CacheTests method testMaxAgePrivate.

public void testMaxAgePrivate() throws Exception {
    AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault());
    ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10);
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.get("/uname/(.*)", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                response.getHeaders().set("Date", HttpDate.format(new Date()));
                response.getHeaders().set("Cache-Control", "private, max-age=10000");
                response.send(request.getMatcher().group(1));
            }
        });
        AsyncServerSocket socket = httpServer.listen(AsyncServer.getDefault(), 0);
        int port = socket.getLocalPort();
        // clear the old cache
        cache.clear();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        assertEquals(cache.getCacheHitCount(), 1);
        assertEquals(cache.getNetworkCount(), 1);
    } finally {
        AsyncServer.getDefault().stop();
        client.getMiddleware().remove(cache);
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) ResponseCacheMiddleware(com.koushikdutta.async.http.cache.ResponseCacheMiddleware) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) File(java.io.File) Date(java.util.Date) HttpDate(com.koushikdutta.async.http.HttpDate) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 4 with AsyncServerSocket

use of com.koushikdutta.async.AsyncServerSocket in project AndroidAsync by koush.

the class HttpClientTests method testProxy.

public void testProxy() throws Exception {
    wasProxied = false;
    final AsyncServer proxyServer = new AsyncServer();
    try {
        AsyncProxyServer httpServer = new AsyncProxyServer(proxyServer) {

            @Override
            protected boolean onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                wasProxied = true;
                return super.onRequest(request, response);
            }
        };
        AsyncServerSocket socket = httpServer.listen(proxyServer, 0);
        //            client.getSocketMiddleware().enableProxy("localhost", 5555);
        AsyncHttpGet get = new AsyncHttpGet("http://www.clockworkmod.com");
        get.enableProxy("localhost", socket.getLocalPort());
        Future<String> ret = client.executeString(get, null);
        String data;
        assertNotNull(data = ret.get(TIMEOUT, TimeUnit.MILLISECONDS));
        assertTrue(data.contains("ClockworkMod"));
        assertTrue(wasProxied);
    } finally {
        proxyServer.stop();
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncServer(com.koushikdutta.async.AsyncServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) AsyncProxyServer(com.koushikdutta.async.http.server.AsyncProxyServer)

Aggregations

AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)4 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)3 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)3 AsyncServer (com.koushikdutta.async.AsyncServer)2 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)2 AsyncProxyServer (com.koushikdutta.async.http.server.AsyncProxyServer)2 AsyncSocket (com.koushikdutta.async.AsyncSocket)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 ListenCallback (com.koushikdutta.async.callback.ListenCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 HttpDate (com.koushikdutta.async.http.HttpDate)1 ResponseCacheMiddleware (com.koushikdutta.async.http.cache.ResponseCacheMiddleware)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1 File (java.io.File)1 Date (java.util.Date)1