There is a plain socket server listening on port 12345;
ServerSocket s = new ServerSocket(12345);
What I want to know is that it is possible that:
- If the client send a
httprequest, the server handle the request directly, - If the client send a
httpsrequest, the server change client socket to SSLSocket?
Thanks
Yes, it is. On the server side, the following works:
SSLSocketFactory.createSocket(Socket, ...)will by default convert the existingSocketinto a client-modeSSLSocket. Since the handshake only starts when you start reading/writing with the I/O streams, it’s still time to change the mode usingsetUseClientMode(false).Regarding the rest of the question:
Again, yes, it’s possible. It’s sometimes referred to as “port unification” and it’s implemented in Grizzly and thus Glassfish.
It works because both HTTP and TLS (upon which HTTPS works) are protocols where the client is expected to talk first. Therefore, the server can detect whether what the client initially sends is a TLS
ClientHellomessage (in which case it should try to proceed with the TLS handshake) or a plain HTTP request (e.g.GET / HTTP/1.1…).I suspect port unification is “easier” to do using
SSLEngine, otherwise, it might be hard to implement a read-ahead on a plain socket, which you would still be able to convert viaSSLSocketFactory.createSocket(Socket, ...).Note that this is still rather unusual, though.