Search in sources :

Example 21 with AsyncHttpServerResponse

use of com.koushikdutta.async.http.server.AsyncHttpServerResponse in project ion by koush.

the class HttpTests method testPut.

public void testPut() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.addAction("PUT", "/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                response.send(request.getMethod());
            }
        });
        httpServer.listen(Ion.getDefault(getContext()).getServer(), 5555);
        Future<String> ret = Ion.with(getContext()).load("PUT", "http://localhost:5555/").asString();
        assertEquals(ret.get(), "PUT");
    } finally {
        httpServer.stop();
    }
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse)

Example 22 with AsyncHttpServerResponse

use of com.koushikdutta.async.http.server.AsyncHttpServerResponse 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 23 with AsyncHttpServerResponse

use of com.koushikdutta.async.http.server.AsyncHttpServerResponse in project ion by koush.

the class StreamTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    AsyncHttpServer server = new AsyncHttpServer();
    port = server.listen(0).getLocalPort();
    server.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            ByteBuffer b = ByteBufferList.obtain(random.length);
            b.put(random);
            b.flip();
            ByteBufferList list = new ByteBufferList(b);
            Util.writeAll(response, list, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.end();
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) ByteBuffer(java.nio.ByteBuffer)

Aggregations

AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)23 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)23 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)21 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)20 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)6 AsyncServer (com.koushikdutta.async.AsyncServer)4 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)4 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)4 JsonObject (com.google.gson.JsonObject)3 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)3 Part (com.koushikdutta.async.http.body.Part)3 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)2 AsyncProxyServer (com.koushikdutta.async.http.server.AsyncProxyServer)2 HeadersResponse (com.koushikdutta.ion.HeadersResponse)2 File (java.io.File)2 KeyStore (java.security.KeyStore)2 Semaphore (java.util.concurrent.Semaphore)2 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)2 SSLContext (javax.net.ssl.SSLContext)2