From 8e0374e010c01e1c5d052cab183b17d3be55201d Mon Sep 17 00:00:00 2001 From: Darragh Clarke Date: Wed, 27 Mar 2024 14:00:10 +0000 Subject: [PATCH 1/3] use try-finally and try-with-resources as well as slight cleanup --- .../com/sun/net/httpserver/bugs/B6431193.java | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/test/jdk/com/sun/net/httpserver/bugs/B6431193.java b/test/jdk/com/sun/net/httpserver/bugs/B6431193.java index 9be9759054b9a..e3dfac972014d 100644 --- a/test/jdk/com/sun/net/httpserver/bugs/B6431193.java +++ b/test/jdk/com/sun/net/httpserver/bugs/B6431193.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,52 +47,45 @@ public static void read (InputStream i) throws IOException { i.close(); } - /** - * @param args - */ - public static void main(String[] args) { + public static void main(String[] args) throws IOException { class MyHandler implements HttpHandler { public void handle(HttpExchange t) throws IOException { - InputStream is = t.getRequestBody(); - read(is); - // .. read the request body + try (InputStream is = t.getRequestBody(); + OutputStream os = t.getResponseBody()) { + read(is); + // .. read the request body String response = "This is the response"; - t.sendResponseHeaders(200, response.length()); - OutputStream os = t.getResponseBody(); - os.write(response.getBytes()); - os.close(); - error = Thread.currentThread().isDaemon(); + t.sendResponseHeaders(200, response.length()); + os.write(response.getBytes()); + os.close(); + error = Thread.currentThread().isDaemon(); + } } } - - HttpServer server; + InetAddress loopback = InetAddress.getLoopbackAddress(); + HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 10); try { - InetAddress loopback = InetAddress.getLoopbackAddress(); - server = HttpServer.create(new InetSocketAddress(loopback, 0), 10); - server.createContext("/apps", new MyHandler()); server.setExecutor(null); // creates a default executor - server.start(); + server.start(); int port = server.getAddress().getPort(); - String s = "http://localhost:"+port+"/apps/foo"; URL url = URIBuilder.newBuilder() - .scheme("http") - .loopback() - .port(port) - .path("/apps/foo") - .toURL(); + .scheme("http") + .loopback() + .port(port) + .path("/apps/foo") + .toURL(); InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream(); - read (is); - server.stop(0); + read(is); if (error) { - throw new RuntimeException ("error in test"); + throw new RuntimeException("error in test"); } - - } - catch (Exception e) { + } catch (Exception e) { throw new AssertionError("Unexpected exception: " + e, e); + } finally { + server.stop(0); } } } From 1294dbc0f4fa32d749fe9884983b19d9e59b309c Mon Sep 17 00:00:00 2001 From: Darragh Clarke Date: Tue, 2 Apr 2024 12:50:02 +0100 Subject: [PATCH 2/3] implemented feedback --- .../com/sun/net/httpserver/bugs/B6431193.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/jdk/com/sun/net/httpserver/bugs/B6431193.java b/test/jdk/com/sun/net/httpserver/bugs/B6431193.java index e3dfac972014d..4d3b31806398a 100644 --- a/test/jdk/com/sun/net/httpserver/bugs/B6431193.java +++ b/test/jdk/com/sun/net/httpserver/bugs/B6431193.java @@ -40,25 +40,20 @@ public class B6431193 { - static boolean error = false; - - public static void read (InputStream i) throws IOException { - while (i.read() != -1); - i.close(); - } + static boolean handlerIsDaemon = false; public static void main(String[] args) throws IOException { class MyHandler implements HttpHandler { public void handle(HttpExchange t) throws IOException { try (InputStream is = t.getRequestBody(); OutputStream os = t.getResponseBody()) { - read(is); + is.readAllBytes(); // .. read the request body String response = "This is the response"; t.sendResponseHeaders(200, response.length()); os.write(response.getBytes()); os.close(); - error = Thread.currentThread().isDaemon(); + handlerIsDaemon = Thread.currentThread().isDaemon(); } } } @@ -78,9 +73,10 @@ public void handle(HttpExchange t) throws IOException { .path("/apps/foo") .toURL(); InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream(); - read(is); - if (error) { - throw new RuntimeException("error in test"); + is.readAllBytes(); + is.close(); + if (handlerIsDaemon) { + throw new RuntimeException("request was handled by a daemon thread"); } } catch (Exception e) { throw new AssertionError("Unexpected exception: " + e, e); From c540dd58e1410e0b5469b2c06ebc43515eeaafa2 Mon Sep 17 00:00:00 2001 From: Darragh Clarke Date: Mon, 8 Apr 2024 16:14:41 +0100 Subject: [PATCH 3/3] implementing feedback --- .../com/sun/net/httpserver/bugs/B6431193.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/jdk/com/sun/net/httpserver/bugs/B6431193.java b/test/jdk/com/sun/net/httpserver/bugs/B6431193.java index 4d3b31806398a..b439801485836 100644 --- a/test/jdk/com/sun/net/httpserver/bugs/B6431193.java +++ b/test/jdk/com/sun/net/httpserver/bugs/B6431193.java @@ -40,7 +40,7 @@ public class B6431193 { - static boolean handlerIsDaemon = false; + static boolean handlerIsDaemon = true; public static void main(String[] args) throws IOException { class MyHandler implements HttpHandler { @@ -52,7 +52,7 @@ public void handle(HttpExchange t) throws IOException { String response = "This is the response"; t.sendResponseHeaders(200, response.length()); os.write(response.getBytes()); - os.close(); + } finally { handlerIsDaemon = Thread.currentThread().isDaemon(); } } @@ -60,11 +60,11 @@ public void handle(HttpExchange t) throws IOException { InetAddress loopback = InetAddress.getLoopbackAddress(); HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 10); + server.createContext("/apps", new MyHandler()); + server.setExecutor(null); + server.start(); + try { - server.createContext("/apps", new MyHandler()); - server.setExecutor(null); - // creates a default executor - server.start(); int port = server.getAddress().getPort(); URL url = URIBuilder.newBuilder() .scheme("http") @@ -72,9 +72,9 @@ public void handle(HttpExchange t) throws IOException { .port(port) .path("/apps/foo") .toURL(); - InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream(); - is.readAllBytes(); - is.close(); + try (InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream()) { + is.readAllBytes(); + } if (handlerIsDaemon) { throw new RuntimeException("request was handled by a daemon thread"); }