Server with HttpsServer from com.sun.net.httpserver
See original GitHub issueIs your feature request related to a problem? Please describe.
One of the easiest setups of HttpServer (for demo purposes) is to use com.sun.net.httpserver.HttpServer
following Java’s documentation.
However, when it comes to HttpsServer, Java’s documentation lacks any guidance. Not to mention, it has an obscure way to setup it:
SSLContext sslContext = SSLContext.getInstance (....);
HttpsServer server = HttpsServer.create();
server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
public void configure (HttpsParameters params) {
// get the remote address if needed
InetSocketAddress remote = params.getClientAddress();
SSLContext c = getSSLContext();
// get the default parameters
SSLParameters sslparams = c.getDefaultSSLParameters();
if (remote.equals (...) ) {
// modify the default set for client x
}
params.setSSLParameters(sslparams);
}
});
This is code snippet from documentation. To be frank, this is mostly all there is to documentation.
However, some have went to great lengths in order to try to setup it. Here’s one of the more popular answers in Stackoverflow. I’ve yet to test it out fully. Even if does work, the solution raises few questions, like why do we need to SSLEngine engine = c.createSSLEngine();
(I’m aware of SSLEngine
in JSSE hierarchy).
Describe the solution you’d like
The solution would be to add another module, called something like server-with-sun-httpserver
and provide implementation with explanation how and why to setup it.
Describe alternatives you’ve considered Alternatives are simple Spring-boot or Jersey applications which are covered. However, for demo purposes, sometimes they are “too heavy”, thus there is no alternatives.
Additional context To build an unsecure HttpServer is easy:
//https://docs.oracle.com/en/java/javase/11/docs/api/jdk.httpserver/com/sun/net/httpserver/package-summary.html
public class UnSecureServer {
public static void main(String[] args) throws IOException {
var server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new MyHandler());
server.start();
}
private static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
try (var is = t.getRequestBody(); var os = t.getResponseBody()) {
read(is); // .. read the request body
var response = "This is the response";
t.sendResponseHeaders(200, response.length());
os.write(response.getBytes());
}
}
private void read(InputStream is) {
try (var bufferedReader = new BufferedReader(new InputStreamReader(is))) {
bufferedReader.lines().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And I’m currently working on making SecureServer
work, but thought that maybe you can share your insights and would like to add this to the current project.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Thank you for your nice words!
This page gets indexed by google, so if someone searches on these topics they could this page. You can if you want refer on your stackoverflow question to this page so the community can see a more detailed answer or code examples.
I added an answer to your stackoverflow question a more basic setup without the usage of SSLFactory.
This is what happens when I start a topic and really try to dig in 😄 Say the word if it’s bothering you. It’s just very rare to find somebody knowledgeable and who’s willing to actively share his findings.
Ok, that’s actually good to know. That means I can stop chasing this, as I was going to sacrifice another few days on this 😄
And to the rest. Thanks once again for extensive explanation. Maybe this could be placed somewhere as documentation? Thus in the future, you wouldn’t be bother again, with the same question. I think you can even post it into stackoverflow as an answer and I will accept it (link to question).