1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-12-22 16:58:54 +00:00

[FEATURE] First HTTPS support

This commit is contained in:
Maxime Sinclair 2013-11-29 15:58:23 +01:00
parent e8920c230d
commit b9d71300df
2 changed files with 85 additions and 29 deletions

View File

@ -23,26 +23,27 @@
*/ */
package net.sourceforge.plantuml.servlet; package net.sourceforge.plantuml.servlet;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import HTTPClient.CookieModule;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ModuleException;
import HTTPClient.ParseException;
import net.sourceforge.plantuml.FileFormat; import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption; import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader; import net.sourceforge.plantuml.SourceStringReader;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
/* /*
* Proxy servlet of the webapp. * Proxy servlet of the webapp.
* This servlet retrieves the diagram source of a web resource (web html page) * This servlet retrieves the diagram source of a web resource (web html page)
@ -58,33 +59,44 @@ public class ProxyServlet extends HttpServlet {
final String source = request.getParameter("src"); final String source = request.getParameter("src");
final String index = request.getParameter("idx"); final String index = request.getParameter("idx");
final URL srcUrl;
// TODO Check if the src URL is valid // Check if the src URL is valid
try {
srcUrl = new URL(source);
} catch (MalformedURLException mue) {
mue.printStackTrace();
return;
}
// generate the response // generate the response
SourceStringReader reader = new SourceStringReader(getSource(source)); String diagmarkup = getSource(srcUrl);
System.out.println("getSource=>" + diagmarkup);
SourceStringReader reader = new SourceStringReader(diagmarkup);
int n = index == null ? 0 : Integer.parseInt(index); int n = index == null ? 0 : Integer.parseInt(index);
reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat(), false)); reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat(), false));
} }
private String getSource(String uri) throws IOException { private String getSource(URL url) throws IOException {
CookieModule.setCookiePolicyHandler(null); String line;
BufferedReader rd;
final Pattern p = Pattern.compile("http://[^/]+(/?.*)"); StringBuilder sb;
final Matcher m = p.matcher(uri);
if (m.find() == false) {
throw new IOException(uri);
}
final URL url = new URL(uri);
final HTTPConnection httpConnection = new HTTPConnection(url);
try { try {
final HTTPResponse resp = httpConnection.Get(m.group(1)); HttpURLConnection con = getConnection(url);
return resp.getText(); rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
} catch (ModuleException e) { sb = new StringBuilder();
throw new IOException(e.toString());
} catch (ParseException e) { while ((line = rd.readLine()) != null) {
throw new IOException(e.toString()); sb.append(line + '\n');
} }
rd.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally{
rd = null;
}
return "";
} }
private FileFormat getOutputFormat() { private FileFormat getOutputFormat() {
@ -100,4 +112,48 @@ public class ProxyServlet extends HttpServlet {
return FileFormat.PNG; return FileFormat.PNG;
} }
private HttpURLConnection getConnection(URL url) throws IOException {
if (url.getProtocol().startsWith("https")) {
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds
// printHttpsCert(con);
con.connect();
return con;
} else {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(10000); // 10 seconds
con.connect();
return con;
}
}
/**
* Debug method used to dump the certificate info
* @param con the https connection
*/
private void printHttpsCert(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();
}
}
}
} }

View File

@ -60,6 +60,6 @@ public class TestProxy extends WebappTestCase {
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy?src=invalidURL"); WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy?src=invalidURL");
WebResponse response = conversation.getResource(request); WebResponse response = conversation.getResource(request);
// Analyze response, it must be HTTP error 500 // Analyze response, it must be HTTP error 500
assertEquals("Response HTTP status is not 500", response.getResponseCode(), 500); assertEquals("Bad HTTP status received", 500, response.getResponseCode());
} }
} }