Skip to content

Commit

Permalink
Added server connector builder (#93)
Browse files Browse the repository at this point in the history
* Added ServerConnectorBuilder and refactored HttpsConnectorFactory

* Refactored HttpConnectorFactory

* Corrected build command

* Added parameter for Server

* Corrected http listener address

* Removed not necessary files

* Corrected import command

* Changed variable context

* Update src/main/java/winstone/ServerConnectorBuilder.java

Co-Authored-By: Jesse Glick <jglick@cloudbees.com>

* Update src/main/java/winstone/ServerConnectorBuilder.java

Co-Authored-By: Jesse Glick <jglick@cloudbees.com>

* Added keep alive timeout and fixed linting

* Update HttpsConnectorFactory.java

* Update .gitignore

Co-authored-by: Jesse Glick <jglick@cloudbees.com>
  • Loading branch information
olamy and jglick committed Mar 5, 2020
1 parent f57af5f commit 5e1a898
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target
.project
.settings
dependency-reduced-pom.xml
.idea/*
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main/java/winstone/ConnectorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ public interface ConnectorFactory {
* @param server
*/
boolean start(Map args, Server server) throws IOException;
}
}
39 changes: 13 additions & 26 deletions src/main/java/winstone/HttpConnectorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
*/
package winstone;

import org.eclipse.jetty.server.ForwardedRequestCustomizer;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import winstone.cmdline.Option;

import java.io.IOException;
Expand All @@ -28,31 +24,22 @@ public class HttpConnectorFactory implements ConnectorFactory {
public boolean start(Map args, Server server) throws IOException {
// Load resources
int listenPort = Option.HTTP_PORT.get(args);
String listenAddress = Option.HTTP_LISTEN_ADDRESS.get(args);
int keepAliveTimeout = Option.HTTP_KEEP_ALIVE_TIMEOUT.get(args);

if (listenPort < 0) {
return false;
} else {
ServerConnector connector = createConnector(server, args);
connector.setPort(listenPort);
connector.setHost(listenAddress);
connector.setIdleTimeout(keepAliveTimeout);

HttpConfiguration config = connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
config.addCustomizer(new ForwardedRequestCustomizer());
config.setRequestHeaderSize(Option.REQUEST_HEADER_SIZE.get(args));

server.addConnector(connector);
return true;
}
}
else {
ServerConnectorBuilder scb = new ServerConnectorBuilder()
.withServer(server)
.withAcceptors(Option.JETTY_ACCEPTORS.get(args))
.withSelectors(Option.JETTY_SELECTORS.get(args))
.withListenerPort(listenPort)
.withListenerAddress(Option.HTTP_LISTEN_ADDRESS.get(args))
.withRequestHeaderSize(Option.REQUEST_HEADER_SIZE.get(args))
.withKeepAliveTimeout(Option._KEEP_ALIVE_TIMEOUT.get(args));

This comment has been minimized.

Copy link
@olamy
server.addConnector(scb.build());
return true;

/**
* Gets a server socket - this is mostly for the purpose of allowing an
* override in the SSL connector.
*/
protected ServerConnector createConnector(Server server, Map args) {
return new ServerConnector(server, Option.JETTY_ACCEPTORS.get( args ), Option.JETTY_SELECTORS.get( args ));
}
}
}
}
36 changes: 11 additions & 25 deletions src/main/java/winstone/HttpsConnectorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
*/
package winstone;

import org.eclipse.jetty.server.ForwardedRequestCustomizer;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import winstone.cmdline.Option;

import java.io.IOException;
Expand All @@ -27,9 +22,8 @@
public class HttpsConnectorFactory extends AbstractSecuredConnectorFactory implements ConnectorFactory {

public boolean start(Map args, Server server) throws IOException {

int listenPort = Option.HTTPS_PORT.get(args);
String listenAddress = Option.HTTPS_LISTEN_ADDRESS.get(args);
int keepAliveTimeout = Option.HTTPS_KEEP_ALIVE_TIMEOUT.get(args);

if (listenPort<0) {
// not running HTTPS listener
Expand All @@ -38,25 +32,17 @@ public boolean start(Map args, Server server) throws IOException {

configureSsl(args, server);

ServerConnector connector = createConnector(server,args);
connector.setPort(listenPort);
connector.setHost(listenAddress);
connector.setIdleTimeout(keepAliveTimeout);

HttpConfiguration config = connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
config.addCustomizer(new ForwardedRequestCustomizer());
config.setRequestHeaderSize(Option.REQUEST_HEADER_SIZE.get(args));

server.addConnector(connector);

ServerConnectorBuilder scb = new ServerConnectorBuilder()
.withServer(server)
.withAcceptors(Option.JETTY_ACCEPTORS.get(args))
.withSelectors(Option.JETTY_SELECTORS.get(args))
.withListenerPort(listenPort)
.withListenerAddress(Option.HTTPS_LISTEN_ADDRESS.get(args))
.withRequestHeaderSize(Option.REQUEST_HEADER_SIZE.get(args))
.withKeepAliveTimeout(Option._KEEP_ALIVE_TIMEOUT.get(args))

This comment has been minimized.

Copy link
@olamy
.withSslContext(getSSLContext(args));
server.addConnector(scb.build());
return true;
}

private ServerConnector createConnector(Server server, Map args) {
SslContextFactory sslcf = getSSLContext(args);
return new ServerConnector(server,Option.JETTY_ACCEPTORS.get( args ), Option.JETTY_SELECTORS.get( args ),sslcf);
}



}
83 changes: 83 additions & 0 deletions src/main/java/winstone/ServerConnectorBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package winstone;

import org.eclipse.jetty.server.ForwardedRequestCustomizer;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;

class ServerConnectorBuilder {

private int listenerPort;
private int keepAliveTimeout;
private int acceptors;
private int selectors;
private int requestHeaderSize;
private String listenerAddress;
private Server server;
private SslContextFactory sslContextFactory;

public ServerConnectorBuilder withListenerPort(int listenerPort) {
this.listenerPort = listenerPort;
return this;
}

public ServerConnectorBuilder withKeepAliveTimeout(int keepAliveTimeout) {
this.keepAliveTimeout = keepAliveTimeout;
return this;
}

public ServerConnectorBuilder withListenerAddress(String listenerAddress) {
this.listenerAddress = listenerAddress;
return this;
}

public ServerConnectorBuilder withServer(Server server) {
this.server = server;
return this;
}

public ServerConnectorBuilder withAcceptors(int acceptors) {
this.acceptors = acceptors;
return this;
}

public ServerConnectorBuilder withSelectors(int selectors) {
this.selectors = selectors;
return this;
}

public ServerConnectorBuilder withSslContext(SslContextFactory sslContextFactory) {
this.sslContextFactory = sslContextFactory;
return this;
}

public ServerConnectorBuilder withRequestHeaderSize(int requestHeaderSize) {
this.requestHeaderSize = requestHeaderSize;
return this;
}

public ServerConnector build() {

ServerConnector sc;

if (sslContextFactory != null) {
sc = new ServerConnector(server, acceptors, selectors, sslContextFactory);
} else {
sc = new ServerConnector(server, acceptors, selectors);
}

sc.setPort(listenerPort);
sc.setHost(listenerAddress);
sc.setIdleTimeout(keepAliveTimeout);

HttpConfiguration hc = sc.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
hc.addCustomizer(new ForwardedRequestCustomizer());
hc.setRequestHeaderSize(requestHeaderSize);

return sc;

}

}

1 comment on commit 5e1a898

@Achever22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpConfiguration hc = sc.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
hc.addCustomizer(new ForwardedRequestCustomizer());
hc.setRequestHeaderSize(requestHeaderSize);

Please sign in to comment.