Search in sources :

Example 36 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class FixedLengthInputStream method startHttpServer.

/**
     * Http Server
     */
HttpServer startHttpServer() throws IOException {
    if (debug) {
        Logger logger = Logger.getLogger("com.sun.net.httpserver");
        Handler outHandler = new StreamHandler(System.out, new SimpleFormatter());
        outHandler.setLevel(Level.FINEST);
        logger.setLevel(Level.FINEST);
        logger.addHandler(outHandler);
    }
    HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    httpServer.createContext("/flis/", new MyHandler(POST_SIZE));
    httpServer.start();
    return httpServer;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpHandler(com.sun.net.httpserver.HttpHandler)

Example 37 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class Deadlock method main.

public static void main(String[] args) throws Exception {
    Handler handler = new Handler();
    InetSocketAddress addr = new InetSocketAddress(0);
    HttpServer server = HttpServer.create(addr, 0);
    HttpContext ctx = server.createContext("/test", handler);
    BasicAuthenticator a = new BasicAuthenticator("foobar@test.realm") {

        @Override
        public boolean checkCredentials(String username, String pw) {
            return "fred".equals(username) && pw.charAt(0) == 'x';
        }
    };
    ctx.setAuthenticator(a);
    ExecutorService executor = Executors.newCachedThreadPool();
    server.setExecutor(executor);
    server.start();
    java.net.Authenticator.setDefault(new MyAuthenticator());
    System.out.print("Deadlock: ");
    for (int i = 0; i < 2; i++) {
        Runner t = new Runner(server, i);
        t.start();
        t.join();
    }
    server.stop(2);
    executor.shutdown();
    if (error) {
        throw new RuntimeException("test failed error");
    }
    if (count != 2) {
        throw new RuntimeException("test failed count = " + count);
    }
    System.out.println("OK");
}
Also used : BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpContext(com.sun.net.httpserver.HttpContext) ExecutorService(java.util.concurrent.ExecutorService) HttpHandler(com.sun.net.httpserver.HttpHandler)

Example 38 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class HttpOnly method test.

void test(String[] args) throws Exception {
    HttpServer server = startHttpServer();
    CookieHandler previousHandler = CookieHandler.getDefault();
    try {
        InetSocketAddress address = server.getAddress();
        URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress() + ":" + address.getPort() + URI_PATH);
        populateCookieStore(uri);
        doClient(uri);
    } finally {
        CookieHandler.setDefault(previousHandler);
        server.stop(0);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) URI(java.net.URI) CookieHandler(java.net.CookieHandler)

Example 39 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class InfiniteLoop method main.

public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/test/InfiniteLoop", new RespHandler());
    server.start();
    try {
        InetSocketAddress address = server.getAddress();
        URL url = new URL("http://localhost:" + address.getPort() + "/test/InfiniteLoop");
        final Phaser phaser = new Phaser(2);
        for (int i = 0; i < 10; i++) {
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            final InputStream is = uc.getInputStream();
            final Thread thread = new Thread() {

                public void run() {
                    try {
                        phaser.arriveAndAwaitAdvance();
                        while (is.read() != -1) Thread.sleep(50);
                    } catch (Exception x) {
                        x.printStackTrace();
                    }
                }
            };
            thread.start();
            phaser.arriveAndAwaitAdvance();
            is.close();
            System.out.println("returned from close");
            thread.join();
        }
    } finally {
        server.stop(0);
    }
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Phaser(java.util.concurrent.Phaser)

Example 40 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class ChunkedEncodingTest method test.

public static void test() {
    HttpServer server = null;
    try {
        serverDigest = MessageDigest.getInstance("MD5");
        clientDigest = MessageDigest.getInstance("MD5");
        server = startHttpServer();
        int port = server.getAddress().getPort();
        out.println("Server listening on port: " + port);
        client("http://localhost:" + port + "/chunked/");
        if (!MessageDigest.isEqual(clientMac, serverMac)) {
            throw new RuntimeException("Data received is NOT equal to the data sent");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (server != null)
            server.stop(0);
    }
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer)

Aggregations

HttpServer (com.sun.net.httpserver.HttpServer)48 InetSocketAddress (java.net.InetSocketAddress)28 HttpHandler (com.sun.net.httpserver.HttpHandler)12 IOException (java.io.IOException)10 File (java.io.File)7 Test (org.junit.Test)7 HttpContext (com.sun.net.httpserver.HttpContext)6 HttpExchange (com.sun.net.httpserver.HttpExchange)6 JarFile (java.util.jar.JarFile)6 MockHttpServer (org.elasticsearch.mocksocket.MockHttpServer)6 URL (java.net.URL)5 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)3 HttpsServer (com.sun.net.httpserver.HttpsServer)3 InputStream (java.io.InputStream)3 ExecutorService (java.util.concurrent.ExecutorService)3 CompilerError (com.redhat.ceylon.compiler.java.test.CompilerError)2 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2