From f850b5f400b82d1b05b025369bd2c82bd979b264 Mon Sep 17 00:00:00 2001 From: matthew16550 Date: Fri, 12 Feb 2021 17:19:03 +1100 Subject: [PATCH] * Allow specifying the bind address that picoweb will listen on (e.g. -picoweb:8000:localhost). * Change picoweb server to print the actual port being listened on when port 0 was specified. --- src/net/sourceforge/plantuml/Option.java | 14 ++++++++------ src/net/sourceforge/plantuml/Run.java | 4 +--- .../plantuml/picoweb/PicoWebServer.java | 9 ++++++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/net/sourceforge/plantuml/Option.java b/src/net/sourceforge/plantuml/Option.java index ad463f7ee..3a50a8e86 100644 --- a/src/net/sourceforge/plantuml/Option.java +++ b/src/net/sourceforge/plantuml/Option.java @@ -79,6 +79,7 @@ public class Option { private boolean textProgressBar = false; private int nbThreads = 0; private int ftpPort = -1; + private String picowebBindAddress = null; private int picowebPort = -1; private boolean hideMetadata = false; private boolean checkMetadata = false; @@ -370,12 +371,9 @@ public class Option { this.ftpPort = Integer.parseInt(s.substring(x + 1)); } } else if (StringUtils.goLowerCase(s).startsWith("-picoweb")) { - final int x = s.indexOf(':'); - if (x == -1) { - this.picowebPort = 8080; - } else { - this.picowebPort = Integer.parseInt(s.substring(x + 1)); - } + final String[] parts = s.split(":"); + this.picowebPort = parts.length > 1 ? Integer.parseInt(parts[1]) : 8080; + this.picowebBindAddress = parts.length > 2 ? parts[2] : null; } else if (s.startsWith("-c")) { s = s.substring(2); config.add(StringUtils.eventuallyRemoveStartingAndEndingDoubleQuote(s)); @@ -403,6 +401,10 @@ public class Option { return ftpPort; } + public String getPicowebBindAddress() { + return picowebBindAddress; + } + public int getPicowebPort() { return picowebPort; } diff --git a/src/net/sourceforge/plantuml/Run.java b/src/net/sourceforge/plantuml/Run.java index a6465e758..dd9364b85 100644 --- a/src/net/sourceforge/plantuml/Run.java +++ b/src/net/sourceforge/plantuml/Run.java @@ -334,9 +334,7 @@ public class Run { } private static void goPicoweb(Option option) throws IOException { - final int picoWebport = option.getPicowebPort(); - System.err.println("webPort=" + picoWebport); - PicoWebServer.startServer(picoWebport); + PicoWebServer.startServer(option.getPicowebPort(), option.getPicowebBindAddress()); } public static void printFonts() { diff --git a/src/net/sourceforge/plantuml/picoweb/PicoWebServer.java b/src/net/sourceforge/plantuml/picoweb/PicoWebServer.java index afa1b1cf7..9ffe12b9f 100644 --- a/src/net/sourceforge/plantuml/picoweb/PicoWebServer.java +++ b/src/net/sourceforge/plantuml/picoweb/PicoWebServer.java @@ -42,6 +42,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; +import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; @@ -72,11 +73,13 @@ public class PicoWebServer implements Runnable { } public static void main(String[] args) throws IOException { - startServer(8080); + startServer(8080, null); } - public static void startServer(final int port) throws IOException { - final ServerSocket serverConnect = new ServerSocket(port); + public static void startServer(final int port, final String bindAddress) throws IOException { + final InetAddress bindAddress1 = bindAddress == null ? null : InetAddress.getByName(bindAddress); + final ServerSocket serverConnect = new ServerSocket(port, 50, bindAddress1); + System.err.println("webPort=" + serverConnect.getLocalPort()); while (true) { final PicoWebServer myServer = new PicoWebServer(serverConnect.accept()); final Thread thread = new Thread(myServer);