Search in sources :

Example 11 with AsyncHttpServer

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

the class ExceptionTests method setUp.

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

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            response.getHeaders().set("Content-Length", "10");
            Util.writeAll(response, "five!".getBytes(), new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    // close the socket prematurely
                    response.getSocket().close();
                }
            });
        }
    });
    server.get("/chunked", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            Util.writeAll(response, "five!".getBytes(), new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    // close the socket prematurely
                    response.getSocket().close();
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) 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 12 with AsyncHttpServer

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

the class Issues method testIssue714.

public void testIssue714() throws Exception {
    AsyncHttpServer server = new AsyncHttpServer();
    server.post("/test", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            response.send(request.getHeaders().get("Authorization"));
        }
    });
    int port = server.listen(Ion.getDefault(getContext()).getServer(), 0).getLocalPort();
    String auth = "";
    for (int i = 0; i < 2048; i++) {
        auth += (char) ('0' + (i % 10));
    }
    System.out.println(auth);
    assertEquals(auth, Ion.with(getContext()).load("http://localhost:" + port + "/test").setHeader("Authorization", auth).setStringBody("testtest").asString().get());
}
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 13 with AsyncHttpServer

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

the class ProgressTests method testUpload.

public void testUpload() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.listen(Ion.getDefault(getContext()).getServer(), 5000);
        httpServer.post("/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                MultipartFormDataBody multipartFormDataBody = (MultipartFormDataBody) request.getBody();
                multipartFormDataBody.setMultipartCallback(new MultipartFormDataBody.MultipartCallback() {

                    @Override
                    public void onPart(Part part) {
                    }
                });
                multipartFormDataBody.setEndCallback(new CompletedCallback() {

                    @Override
                    public void onCompleted(Exception ex) {
                        response.send("Got parts!");
                    }
                });
            }
        });
        final Semaphore semaphore = new Semaphore(0);
        Ion.with(getContext()).load("http://localhost:5000/").uploadProgress(new ProgressCallback() {

            @Override
            public void onProgress(long downloaded, long total) {
                semaphore.release();
            }
        }).setMultipartParameter("foo", "bar").asString().get();
        assertTrue(semaphore.tryAcquire());
    } finally {
        Ion.getDefault(getContext()).getServer().stop();
    }
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) Part(com.koushikdutta.async.http.body.Part) ProgressCallback(com.koushikdutta.ion.ProgressCallback) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) Semaphore(java.util.concurrent.Semaphore) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 14 with AsyncHttpServer

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

the class SelfSignedCertificateTests method testKeys.

public void testKeys() throws Exception {
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    kmf.init(ks, "storepass".toCharArray());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore ts = KeyStore.getInstance(KeyStore.getDefaultType());
    ts.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    tmf.init(ts);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    AsyncHttpServer httpServer = new AsyncHttpServer();
    httpServer.listenSecure(8888, sslContext);
    httpServer.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            response.send("hello");
        }
    });
    Thread.sleep(1000);
    Ion ion = Ion.getInstance(getContext(), "CustomSSL");
    ion.getHttpClient().getSSLSocketMiddleware().setSSLContext(sslContext);
    ion.getHttpClient().getSSLSocketMiddleware().setTrustManagers(tmf.getTrustManagers());
    ion.build(getContext()).load("https://localhost:8888/").asString().get();
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) Ion(com.koushikdutta.ion.Ion) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 15 with AsyncHttpServer

use of com.koushikdutta.async.http.server.AsyncHttpServer in project cw-omnibus by commonsguy.

the class WebServerService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    ConnectivityManager mgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ni = mgr.getActiveNetworkInfo();
    if (ni == null || ni.getType() == ConnectivityManager.TYPE_MOBILE) {
        EventBus.getDefault().post(new ServerStartRejectedEvent());
        stopSelf();
    } else {
        rootPath = "/" + new BigInteger(20, rng).toString(24).toUpperCase();
        server = new AsyncHttpServer();
        server.get("/.*", new AssetRequestCallback());
        server.listen(4999);
        raiseReadyEvent();
        foregroundify();
        timeoutFuture = timer.schedule(onTimeout, MAX_IDLE_TIME_SECONDS, TimeUnit.SECONDS);
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) BigInteger(java.math.BigInteger)

Aggregations

AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)29 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)22 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)20 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)20 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)7 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)4 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)3 Part (com.koushikdutta.async.http.body.Part)3 Semaphore (java.util.concurrent.Semaphore)3 ConnectivityManager (android.net.ConnectivityManager)2 NetworkInfo (android.net.NetworkInfo)2 Handlebars (com.github.jknack.handlebars.Handlebars)2 JsonObject (com.google.gson.JsonObject)2 AsyncServer (com.koushikdutta.async.AsyncServer)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)2 WebSocket (com.koushikdutta.async.http.WebSocket)2 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)2 HeadersResponse (com.koushikdutta.ion.HeadersResponse)2 File (java.io.File)2