Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions test/jdk/com/sun/net/httpserver/bugs/B6431193.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,59 +40,48 @@

public class B6431193 {

static boolean error = false;
static boolean handlerIsDaemon = true;

public static void read (InputStream i) throws IOException {
while (i.read() != -1);
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()) {
is.readAllBytes();
// .. 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());
} finally {
handlerIsDaemon = Thread.currentThread().isDaemon();
}
}
}

InetAddress loopback = InetAddress.getLoopbackAddress();
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/apps", new MyHandler());
server.setExecutor(null);
server.start();

HttpServer server;
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();
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();
InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream();
read (is);
server.stop(0);
if (error) {
throw new RuntimeException ("error in test");
.scheme("http")
.loopback()
.port(port)
.path("/apps/foo")
.toURL();
try (InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream()) {
is.readAllBytes();
}

}
catch (Exception e) {
if (handlerIsDaemon) {
throw new RuntimeException("request was handled by a daemon thread");
}
} catch (Exception e) {
throw new AssertionError("Unexpected exception: " + e, e);
} finally {
server.stop(0);
}
}
}