revert missing tests + small fixes

- revert the 4 missing tests, e.g. proxy test from commit 20468f5
- add virtual host name `test.localhost` to embedded jetty server
  (JUnit Tests) since localhost and IP-Addresses are no longer
  supported by the proxy and use this address inside proxy `src`
- add `test-localhost` support for the docker tests. To support
  this the docker hostname need to be set to test.localhost by:
  `--hostname=test.localhost` (only for the docker tests)
- proxy: add file format support for PDF
- proxy: add error messages on "bad request" response
- proxy: remove dead code
- old proxy: add error messages on "bad request" response
- fix incorrect README link to docs
- add `HTTP_PROXY_READ_TIMEOUT` option -- close #240
This commit is contained in:
Florian
2023-05-04 00:52:00 +02:00
committed by PlantUML
parent ed49010303
commit e6566b58bd
14 changed files with 496 additions and 101 deletions

View File

@@ -29,12 +29,10 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;
import java.util.List;
import java.util.stream.Collectors;
import javax.imageio.IIOException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
@@ -82,25 +80,25 @@ public class ProxyServlet extends HttpServlet {
return false;
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String fmt = request.getParameter("fmt");
final String source = request.getParameter("src");
final String index = request.getParameter("idx");
if (forbiddenURL(source)) {
response.setStatus(400);
return;
}
final URL srcUrl;
// Check if the src URL is valid
final URL srcUrl;
try {
srcUrl = new URL(source);
} catch (MalformedURLException mue) {
mue.printStackTrace();
response.setStatus(400);
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "URL malformed.");
return;
}
// Check if URL is in a forbidden format (e.g. IP-Address)
if (forbiddenURL(source)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Forbidden URL format.");
return;
}
@@ -112,8 +110,7 @@ public class ProxyServlet extends HttpServlet {
BlockUml block = blocks.get(n);
Diagram diagram = block.getDiagram();
UmlSource umlSrc = diagram.getSource();
String uml = umlSrc.getPlainString();
//System.out.println("uml=" + uml);
String uml = umlSrc.getPlainString("\n");
// generate the response
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request);
@@ -128,7 +125,6 @@ public class ProxyServlet extends HttpServlet {
// Browser has closed the connection, so the HTTP OutputStream is closed
// Silently catch the exception to avoid annoying log
}
dr = null;
}
/**
@@ -141,25 +137,10 @@ public class ProxyServlet extends HttpServlet {
* @throws IOException if an input or output exception occurred
*/
private String getSource(final URL url) throws IOException {
String line;
BufferedReader rd;
StringBuilder sb;
try {
HttpURLConnection con = getConnection(url);
rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
sb = new StringBuilder();
while ((line = rd.readLine()) != null) {
sb.append(line + '\n');
}
rd.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
rd = null;
HttpURLConnection conn = getConnection(url);
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
return br.lines().collect(Collectors.joining("\n"));
}
return "";
}
/**
@@ -174,23 +155,16 @@ public class ProxyServlet extends HttpServlet {
if (format == null) {
return FileFormat.PNG;
}
if (format.equals("svg")) {
return FileFormat.SVG;
switch (format.toLowerCase()) {
case "png": return FileFormat.PNG;
case "svg": return FileFormat.SVG;
case "eps": return FileFormat.EPS;
case "epstext": return FileFormat.EPS_TEXT;
case "txt": return FileFormat.UTXT;
case "map": return FileFormat.UTXT;
case "pdf": return FileFormat.PDF;
default: return FileFormat.PNG;
}
if (format.equals("eps")) {
return FileFormat.EPS;
}
if (format.equals("epstext")) {
return FileFormat.EPS_TEXT;
}
if (format.equals("txt")) {
return FileFormat.UTXT;
}
if (format.equals("map")) {
return FileFormat.UTXT;
}
return FileFormat.PNG;
}
/**
@@ -203,47 +177,19 @@ public class ProxyServlet extends HttpServlet {
* @throws IOException if an input or output exception occurred
*/
private HttpURLConnection getConnection(final URL url) throws IOException {
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
//if (con instanceof HttpsURLConnection) {
// printHttpsCert((HttpsURLConnection) con);
//}
con.setRequestMethod("GET");
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
String token = System.getenv("HTTP_AUTHORIZATION");
if (token != null) {
con.setRequestProperty("Authorization", token);
conn.setRequestProperty("Authorization", token);
}
con.setReadTimeout(10000); // 10 seconds
con.connect();
return con;
}
/**
* Debug method used to dump the certificate info.
*
* @param con the https connection
*/
@SuppressWarnings("unused")
private void printHttpsCert(final HttpsURLConnection con) {
if (con != null) {
try {
System.out.println("Response Code : " + con.getResponseCode());
System.out.println("Cipher Suite : " + con.getCipherSuite());
System.out.println("\n");
Certificate[] certs = con.getServerCertificates();
for (Certificate cert : certs) {
System.out.println("Cert Type : " + cert.getType());
System.out.println("Cert Hash Code : " + cert.hashCode());
System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
System.out.println("\n");
}
} catch (SSLPeerUnverifiedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
final String timeoutString = System.getenv("HTTP_PROXY_READ_TIMEOUT");
int timeout = 10000; // 10 seconds as default
if (timeoutString != null && timeoutString.matches("^\\d+$")) {
timeout = Integer.parseInt(timeoutString);
}
conn.setReadTimeout(timeout);
conn.connect();
return conn;
}
}