question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server with HttpsServer from com.sun.net.httpserver

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Hakky54commented, Jan 26, 2021

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.

1reaction
MrR0807commented, Jan 26, 2021

You come back with some quite interesting questions

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.

None of these two classes could give me the flexibility of configuring ssl with an SSLContext, SSLServerSocketFactory, KeyManager or TrustManager. I have dig into different kind of documentation and also analysed the source code of spring and came into conclusion that it is not possible with Spring Boot & Tomcat. Maybe I have missed something but I am 99% sure that it is not possible.

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

HttpsServer (Java HTTP Server ) - Oracle Help Center
This class is an extension of HttpServer which provides support for HTTPS. A HttpsServer must have an associated HttpsConfigurator object which is used...
Read more >
com.sun.net.httpserver.HttpsServer java code examples
This class is an extension of HttpServer which provides support for HTTPS. A HttpsServer must have an associated HttpsConfigurator object which is used...
Read more >
Simple Java HTTPS server - ssl - Stack Overflow
For this setup I am assuming you already have the keystore and truststore in place. The rest endpoint: import com.sun.net.httpserver.
Read more >
com.sun.net.httpserver.HttpsServer Java Examples
This page shows Java code examples of com.sun.net.httpserver.HttpsServer.
Read more >
Minimal HTTP Server by using com.sun.net.httpserver ...
Minimal HTTP Server by using com.sun.net.httpserver.HttpServer : Web Server « Network Protocol « Java.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found