diff --git a/src/main/java/com/sshtools/uhttpd/UHTTPD.java b/src/main/java/com/sshtools/uhttpd/UHTTPD.java index 6850733..8fd01a5 100644 --- a/src/main/java/com/sshtools/uhttpd/UHTTPD.java +++ b/src/main/java/com/sshtools/uhttpd/UHTTPD.java @@ -71,6 +71,7 @@ import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; @@ -1998,9 +1999,18 @@ public interface RootContext extends Context, Runnable { void start(); - Optional httpsPort(); + default Optional httpsPort() { + return httpAddress().filter(a -> a instanceof InetSocketAddress).map(a -> ((InetSocketAddress)a).getPort()); + } + + default Optional httpPort() { + return httpsAddress().filter(a -> a instanceof InetSocketAddress).map(a -> ((InetSocketAddress)a).getPort()); + + } + + Optional httpAddress(); - Optional httpPort(); + Optional httpsAddress(); /** * Get the temporary directory @@ -2242,7 +2252,11 @@ public enum Scheme { HTTP, HTTPS } + private final static Map byCode = new HashMap<>(); + public enum Status { + + CONTINUE(100, "Continue"), SWITCHING_PROTOCOLS(101, "Switching Protocols"), PROCESSING(102, "Processing"), OK(200, "OK"), CREATED(201, "Created"), ACCEPTED(202, "Accepted"), NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"), NO_CONTENT(204, "No Content"), @@ -2266,9 +2280,14 @@ public enum Status { private String text; Status(int code, String text) { + byCode.put(code, this); this.code = code; this.text = text; } + + public static Optional ofCode(int code) { + return Optional.ofNullable(byCode.get(code)); + } public int getCode() { return code; @@ -4072,8 +4091,10 @@ public void get(Transaction req) throws Exception { LOG.log(Level.DEBUG, "Locating resource for {0}", path); var fullPath = Paths.get(path).normalize().toString(); var url = base.map(c -> c.getResource(path)).orElseGet(() -> loader.orElse(ClasspathResources.class.getClassLoader()).getResource(fullPath)); - if (url == null) - throw new FileNotFoundException(fullPath); + if (url == null) { + /* TODO we want this to fall through really to the default notFound() */ +// throw new FileNotFoundException(fullPath); + } else { urlResource(url).get(req); } @@ -5630,17 +5651,17 @@ public Path tmpDir() { } @Override - public Optional httpPort() { + public Optional httpAddress() { try { - return serverSocketChannel == null ? Optional.empty() : Optional.of(((InetSocketAddress)serverSocketChannel.getLocalAddress()).getPort()); + return serverSocketChannel == null ? Optional.empty() : Optional.of((serverSocketChannel.getLocalAddress())); } catch (IOException e) { throw new UncheckedIOException(e); } } @Override - public Optional httpsPort() { - return sslServerSocket == null ? Optional.empty() : Optional.of(sslServerSocket.getLocalPort()); + public Optional httpsAddress() { + return sslServerSocket == null ? Optional.empty() : Optional.of((sslServerSocket.getLocalSocketAddress())); } @Override @@ -5703,7 +5724,8 @@ public void get(Transaction tx) throws Exception { tx.selector = Optional.of(c.getKey()); try { c.getValue().get(tx); - } catch (FileNotFoundException fnfe) { + } catch (FileNotFoundException | NoSuchFileException fnfe) { + fnfe.printStackTrace(); if (LOG.isLoggable(Level.DEBUG)) LOG.log(Level.DEBUG, "File not found. {0}", fnfe.getMessage()); tx.notFound();