Skip to content

Commit

Permalink
Allow access to bound address.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Sep 2, 2024
1 parent 84f80e6 commit cc22c26
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/main/java/com/sshtools/uhttpd/UHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1998,9 +1999,18 @@ public interface RootContext extends Context, Runnable {

void start();

Optional<Integer> httpsPort();
default Optional<Integer> httpsPort() {
return httpAddress().filter(a -> a instanceof InetSocketAddress).map(a -> ((InetSocketAddress)a).getPort());
}

default Optional<Integer> httpPort() {
return httpsAddress().filter(a -> a instanceof InetSocketAddress).map(a -> ((InetSocketAddress)a).getPort());

}

Optional<SocketAddress> httpAddress();

Optional<Integer> httpPort();
Optional<SocketAddress> httpsAddress();

/**
* Get the temporary directory
Expand Down Expand Up @@ -2242,7 +2252,11 @@ public enum Scheme {
HTTP, HTTPS
}

private final static Map<Integer, Status> 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"),
Expand All @@ -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<Status> ofCode(int code) {
return Optional.ofNullable(byCode.get(code));
}

public int getCode() {
return code;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -5630,17 +5651,17 @@ public Path tmpDir() {
}

@Override
public Optional<Integer> httpPort() {
public Optional<SocketAddress> 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<Integer> httpsPort() {
return sslServerSocket == null ? Optional.empty() : Optional.of(sslServerSocket.getLocalPort());
public Optional<SocketAddress> httpsAddress() {
return sslServerSocket == null ? Optional.empty() : Optional.of((sslServerSocket.getLocalSocketAddress()));
}

@Override
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cc22c26

Please sign in to comment.