Skip to content

Commit

Permalink
fix issue #15 HttpsConfigurator.configure method not executed for htt…
Browse files Browse the repository at this point in the history
…ps connections
  • Loading branch information
robaho committed Jan 16, 2025
1 parent 2e7f580 commit 4d2c7b6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
48 changes: 48 additions & 0 deletions src/main/java/robaho/net/httpserver/SSLConfigurator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package robaho.net.httpserver;

import java.net.InetSocketAddress;

import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;

import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;

class SSLConfigurator {
static void configure(SSLSocket s,HttpsConfigurator cfg) {
s.setUseClientMode(false);
if(cfg==null) return;
InetSocketAddress remoteAddress = (InetSocketAddress)s.getRemoteSocketAddress();
Parameters params = new Parameters (cfg, remoteAddress);
cfg.configure(params);
SSLParameters sslParams = params.getSSLParameters();
if (sslParams != null) {
s.setSSLParameters(sslParams);
}
}
static class Parameters extends HttpsParameters {
InetSocketAddress addr;
HttpsConfigurator cfg;

Parameters (HttpsConfigurator cfg, InetSocketAddress addr) {
this.addr = addr;
this.cfg = cfg;
}
@Override
public InetSocketAddress getClientAddress () {
return addr;
}
@Override
public HttpsConfigurator getHttpsConfigurator() {
return cfg;
}
SSLParameters params;
@Override
public void setSSLParameters (SSLParameters p) {
params = p;
}
SSLParameters getSSLParameters () {
return params;
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/robaho/net/httpserver/ServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package robaho.net.httpserver;

import java.io.EOFException;
import static java.nio.charset.StandardCharsets.ISO_8859_1;

import java.io.IOException;
Expand Down Expand Up @@ -63,6 +62,7 @@
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;

import robaho.net.httpserver.http2.HTTP2Connection;
import robaho.net.httpserver.http2.HTTP2ErrorCode;
Expand Down Expand Up @@ -370,17 +370,17 @@ public void run() {
// not work, so upgrade to a SSLSocket after connection
SSLSocketFactory ssf = httpsConfig.getSSLContext().getSocketFactory();
SSLSocket sslSocket = (SSLSocket) ssf.createSocket(s, null, false);
SSLConfigurator.configure(sslSocket,httpsConfig);

sslSocket.setHandshakeApplicationProtocolSelector((_sslSocket, protocols) -> {
if (protocols.contains("h2") && ServerConfig.http2OverSSL()) {
return "h2";
} else {
return "http/1.1";
}
});
sslSocket.setUseClientMode(false);
// the following forces the SSL handshake to complete in order to determine the negotiated protocol
var session = sslSocket.getSession();

if ("h2".equals(sslSocket.getApplicationProtocol())) {
logger.log(Level.DEBUG, () -> "http2 connection "+sslSocket.toString());
http2 = true;
Expand Down

0 comments on commit 4d2c7b6

Please sign in to comment.