Skip to content
Closed
Show file tree
Hide file tree
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
28 changes: 12 additions & 16 deletions test/jdk/com/sun/net/httpserver/FileServerHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2025, 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 @@ -47,7 +47,6 @@ public FileServerHandler (String docroot) {
this.docroot = docroot;
}

int invocation = 1;
public void handle (HttpExchange t)
throws IOException
{
Expand Down Expand Up @@ -91,16 +90,16 @@ public void handle (HttpExchange t)
rmap.set ("Content-Type", "text/html");
t.sendResponseHeaders (200, 0);
String[] list = f.list();
OutputStream os = t.getResponseBody();
PrintStream p = new PrintStream (os);
p.println ("<h2>Directory listing for: " + path+ "</h2>");
p.println ("<ul>");
for (int i=0; i<list.length; i++) {
p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");
try (final OutputStream os = t.getResponseBody();
final PrintStream p = new PrintStream (os)) {
p.println("<h2>Directory listing for: " + path + "</h2>");
p.println("<ul>");
for (int i = 0; i < list.length; i++) {
p.println("<li><a href=\"" + list[i] + "\">" + list[i] + "</a></li>");
}
p.println("</ul><p><hr>");
p.flush();
}
p.println ("</ul><p><hr>");
p.flush();
p.close();
} else {
int clen;
if (fixedrequest != null) {
Expand All @@ -109,10 +108,9 @@ public void handle (HttpExchange t)
clen = 0;
}
t.sendResponseHeaders (200, clen);
OutputStream os = t.getResponseBody();
FileInputStream fis = new FileInputStream (f);
int count = 0;
try {
try (final OutputStream os = t.getResponseBody();
final FileInputStream fis = new FileInputStream (f)) {
byte[] buf = new byte [16 * 1024];
int len;
while ((len=fis.read (buf)) != -1) {
Expand All @@ -122,8 +120,6 @@ public void handle (HttpExchange t)
} catch (IOException e) {
e.printStackTrace();
}
fis.close();
os.close();
}
}

Expand Down
179 changes: 89 additions & 90 deletions test/jdk/com/sun/net/httpserver/Test12.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,12 @@
* questions.
*/

/*
* @test
* @bug 6270015
* @library /test/lib
* @build jdk.test.lib.Asserts
* jdk.test.lib.Utils
* jdk.test.lib.net.SimpleSSLContext
* jdk.test.lib.net.URIBuilder
* @run main/othervm Test12
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test12
* @summary Light weight HTTP server
*/

import com.sun.net.httpserver.*;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.io.*;
import java.net.*;
Expand All @@ -49,26 +38,32 @@
import static jdk.test.lib.Asserts.assertFileContentsEqual;
import static jdk.test.lib.Utils.createTempFileOfSize;

/* basic http/s connectivity test
* Tests:
* - same as Test1, but in parallel
/*
* @test
* @bug 6270015 8359477
* @summary Light weight HTTP server - basic http/s connectivity test, same as Test1,
* but in parallel
* @library /test/lib
* @build jdk.test.lib.Asserts
* jdk.test.lib.Utils
* jdk.test.lib.net.SimpleSSLContext
* jdk.test.lib.net.URIBuilder
* @run main/othervm Test12
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test12
*/

public class Test12 extends Test {

private static final String TEMP_FILE_PREFIX =
HttpServer.class.getPackageName() + '-' + Test12.class.getSimpleName() + '-';

static SSLContext ctx;

static boolean fail = false;

public static void main (String[] args) throws Exception {
HttpServer s1 = null;
HttpsServer s2 = null;
ExecutorService executor=null;
Path smallFilePath = createTempFileOfSize(TEMP_FILE_PREFIX, null, 23);
Path largeFilePath = createTempFileOfSize(TEMP_FILE_PREFIX, null, 2730088);
final ExecutorService executor = Executors.newCachedThreadPool();
try {
System.out.print ("Test12: ");
InetAddress loopback = InetAddress.getLoopbackAddress();
Expand All @@ -80,7 +75,6 @@ public static void main (String[] args) throws Exception {
HttpHandler h = new FileServerHandler(smallFilePath.getParent().toString());
HttpContext c1 = s1.createContext ("/", h);
HttpContext c2 = s2.createContext ("/", h);
executor = Executors.newCachedThreadPool();
s1.setExecutor (executor);
s2.setExecutor (executor);
ctx = new SimpleSSLContext().get();
Expand All @@ -90,7 +84,7 @@ public static void main (String[] args) throws Exception {

int port = s1.getAddress().getPort();
int httpsport = s2.getAddress().getPort();
Runner r[] = new Runner[8];
final Runner[] r = new Runner[8];
r[0] = new Runner (true, "http", port, smallFilePath);
r[1] = new Runner (true, "http", port, largeFilePath);
r[2] = new Runner (true, "https", httpsport, smallFilePath);
Expand All @@ -99,95 +93,100 @@ public static void main (String[] args) throws Exception {
r[5] = new Runner (false, "http", port, largeFilePath);
r[6] = new Runner (false, "https", httpsport, smallFilePath);
r[7] = new Runner (false, "https", httpsport, largeFilePath);
start (r);
join (r);
System.out.println ("OK");
// submit the tasks
final List<Future<Void>> futures = new ArrayList<>();
for (Runner runner : r) {
futures.add(executor.submit(runner));
}
// wait for the tasks' completion
for (Future<Void> f : futures) {
f.get();
}
System.out.println ("All " + futures.size() + " tasks completed successfully");
} finally {
if (s1 != null)
if (s1 != null) {
s1.stop(0);
if (s2 != null)
}
if (s2 != null) {
s2.stop(0);
if (executor != null)
executor.shutdown ();
}
boolean terminated = executor.isTerminated();
if (!terminated) {
executor.shutdown();
boolean interrupted = false;
while (!terminated) {
try {
terminated = executor.awaitTermination(1L, TimeUnit.DAYS);
} catch (InterruptedException e) {
if (!interrupted) {
executor.shutdownNow();
interrupted = true;
}
}
}
if (interrupted) {
Thread.currentThread().interrupt();
}
}
// it's OK to delete these files since the server side handlers
// serving these files have completed (guaranteed by the completion of Executor.close())
System.out.println("deleting " + smallFilePath);
Files.delete(smallFilePath);
System.out.println("deleting " + largeFilePath);
Files.delete(largeFilePath);
}
}

static void start (Runner[] x) {
for (int i=0; i<x.length; i++) {
x[i].start();
}
}

static void join (Runner[] x) {
for (int i=0; i<x.length; i++) {
try {
x[i].join();
} catch (InterruptedException e) {}
}
}


static class Runner extends Thread {
static class Runner implements Callable<Void> {

boolean fixedLen;
String protocol;
int port;
private final Path filePath;

Runner (boolean fixedLen, String protocol, int port, Path filePath) {
Runner(boolean fixedLen, String protocol, int port, Path filePath) {
this.fixedLen=fixedLen;
this.protocol=protocol;
this.port=port;
this.filePath = filePath;
}

public void run () {
try {
URL url = URIBuilder.newBuilder()
.scheme(protocol)
.loopback()
.port(port)
.path("/" + filePath.getFileName())
.toURL();
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
if (urlc instanceof HttpsURLConnection) {
HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
urlcs.setHostnameVerifier (new HostnameVerifier () {
public boolean verify (String s, SSLSession s1) {
return true;
}
});
urlcs.setSSLSocketFactory (ctx.getSocketFactory());
}
byte [] buf = new byte [4096];

if (fixedLen) {
urlc.setRequestProperty ("XFixed", "yes");
}
InputStream is = urlc.getInputStream();
File temp = File.createTempFile ("Test1", null);
temp.deleteOnExit();
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
int c, count = 0;
while ((c=is.read(buf)) != -1) {
count += c;
fout.write (buf, 0, c);
}
is.close();
fout.close();

if (count != filePath.toFile().length()) {
throw new RuntimeException ("wrong amount of data returned");
}
assertFileContentsEqual(filePath, temp.toPath());
temp.delete();
} catch (Exception e) {
e.printStackTrace();
fail = true;
@Override
public Void call() throws Exception {
final URL url = URIBuilder.newBuilder()
.scheme(protocol)
.loopback()
.port(port)
.path("/" + filePath.getFileName())
.toURL();
final HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
if (urlc instanceof HttpsURLConnection) {
HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
urlcs.setHostnameVerifier (new HostnameVerifier () {
public boolean verify (String s, SSLSession s1) {
return true;
}
});
urlcs.setSSLSocketFactory (ctx.getSocketFactory());
}
if (fixedLen) {
urlc.setRequestProperty ("XFixed", "yes");
}
final Path temp = Files.createTempFile(Path.of("."), "Test12", null);
final long numReceived;
try (InputStream is = urlc.getInputStream();
OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp.toFile()))) {
numReceived = is.transferTo(fout);
}
System.out.println("received " + numReceived + " response bytes for " + url);
final long expected = filePath.toFile().length();
if (numReceived != expected) {
throw new RuntimeException ("expected " + expected + " bytes, but received "
+ numReceived);
}
assertFileContentsEqual(filePath, temp);
Files.delete(temp);
return null;
}
}

}