Search in sources :

Example 1 with ForwardedRequestCustomizer

use of org.eclipse.jetty.server.ForwardedRequestCustomizer in project camel by apache.

the class JettyHttpComponent9 method createConnectorJettyInternal.

protected AbstractConnector createConnectorJettyInternal(Server server, JettyHttpEndpoint endpoint, SslContextFactory sslcf) {
    try {
        String host = endpoint.getHttpUri().getHost();
        int porto = endpoint.getPort();
        org.eclipse.jetty.server.HttpConfiguration httpConfig = new org.eclipse.jetty.server.HttpConfiguration();
        httpConfig.setSendServerVersion(endpoint.isSendServerVersion());
        httpConfig.setSendDateHeader(endpoint.isSendDateHeader());
        httpConfig.setSendDateHeader(endpoint.isSendDateHeader());
        if (requestBufferSize != null) {
        // Does not work
        //httpConfig.setRequestBufferSize(requestBufferSize);
        }
        if (requestHeaderSize != null) {
            httpConfig.setRequestHeaderSize(requestHeaderSize);
        }
        if (responseBufferSize != null) {
            httpConfig.setOutputBufferSize(responseBufferSize);
        }
        if (responseHeaderSize != null) {
            httpConfig.setResponseHeaderSize(responseHeaderSize);
        }
        if (useXForwardedForHeader) {
            httpConfig.addCustomizer(new ForwardedRequestCustomizer());
        }
        HttpConnectionFactory httpFactory = new org.eclipse.jetty.server.HttpConnectionFactory(httpConfig);
        ArrayList<ConnectionFactory> connectionFactories = new ArrayList<ConnectionFactory>();
        ServerConnector result = new org.eclipse.jetty.server.ServerConnector(server);
        if (sslcf != null) {
            httpConfig.addCustomizer(new org.eclipse.jetty.server.SecureRequestCustomizer());
            SslConnectionFactory scf = new org.eclipse.jetty.server.SslConnectionFactory(sslcf, "HTTP/1.1");
            connectionFactories.add(scf);
            // The protocol name can be "SSL" or "SSL-HTTP/1.1" depending on the version of Jetty
            result.setDefaultProtocol(scf.getProtocol());
        }
        connectionFactories.add(httpFactory);
        result.setConnectionFactories(connectionFactories);
        result.setPort(porto);
        if (host != null) {
            result.setHost(host);
        }
        if (getSslSocketConnectorProperties() != null && "https".equals(endpoint.getProtocol())) {
            // must copy the map otherwise it will be deleted
            Map<String, Object> properties = new HashMap<String, Object>(getSslSocketConnectorProperties());
            IntrospectionSupport.setProperties(sslcf, properties);
            if (properties.size() > 0) {
                throw new IllegalArgumentException("There are " + properties.size() + " parameters that couldn't be set on the SocketConnector." + " Check the uri if the parameters are spelt correctly and that they are properties of the SelectChannelConnector." + " Unknown parameters=[" + properties + "]");
            }
        }
        return result;
    } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    }
}
Also used : HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) JettyHttpEndpoint(org.apache.camel.component.jetty.JettyHttpEndpoint) URISyntaxException(java.net.URISyntaxException) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory)

Example 2 with ForwardedRequestCustomizer

use of org.eclipse.jetty.server.ForwardedRequestCustomizer in project winstone by jenkinsci.

the class HttpConnectorFactory method start.

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);
        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;
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer)

Example 3 with ForwardedRequestCustomizer

use of org.eclipse.jetty.server.ForwardedRequestCustomizer in project winstone by jenkinsci.

the class HttpsConnectorFactory method start.

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
        return false;
    }
    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);
    return true;
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer)

Example 4 with ForwardedRequestCustomizer

use of org.eclipse.jetty.server.ForwardedRequestCustomizer in project dropwizard by dropwizard.

the class HttpConnectorFactory method buildHttpConfiguration.

protected HttpConfiguration buildHttpConfiguration() {
    final HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setHeaderCacheSize((int) headerCacheSize.toBytes());
    httpConfig.setOutputBufferSize((int) outputBufferSize.toBytes());
    httpConfig.setRequestHeaderSize((int) maxRequestHeaderSize.toBytes());
    httpConfig.setResponseHeaderSize((int) maxResponseHeaderSize.toBytes());
    httpConfig.setSendDateHeader(useDateHeader);
    httpConfig.setSendServerVersion(useServerHeader);
    httpConfig.setMinResponseDataRate(minResponseDataPerSecond.toBytes());
    httpConfig.setMinRequestDataRate(minRequestDataPerSecond.toBytes());
    httpConfig.setRequestCookieCompliance(requestCookieCompliance);
    httpConfig.setResponseCookieCompliance(responseCookieCompliance);
    if (useForwardedHeaders) {
        httpConfig.addCustomizer(new ForwardedRequestCustomizer());
    }
    return httpConfig;
}
Also used : HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer)

Example 5 with ForwardedRequestCustomizer

use of org.eclipse.jetty.server.ForwardedRequestCustomizer in project gerrit by GerritCodeReview.

the class JettyServer method listen.

private Connector[] listen(Server server, Config cfg) {
    // OpenID and certain web-based single-sign-on products can cause
    // some very long headers, especially in the Referer header. We
    // need to use a larger default header size to ensure we have
    // the space required.
    // 
    final int requestHeaderSize = cfg.getInt("httpd", "requestheadersize", 16386);
    final URI[] listenUrls = listenURLs(cfg);
    final boolean reuseAddress = cfg.getBoolean("httpd", "reuseaddress", true);
    final int acceptors = cfg.getInt("httpd", "acceptorThreads", 2);
    final AuthType authType = cfg.getEnum("auth", null, "type", AuthType.OPENID);
    reverseProxy = isReverseProxied(listenUrls);
    final Connector[] connectors = new Connector[listenUrls.length];
    for (int idx = 0; idx < listenUrls.length; idx++) {
        final URI u = listenUrls[idx];
        final int defaultPort;
        final ServerConnector c;
        HttpConfiguration config = defaultConfig(requestHeaderSize);
        if (AuthType.CLIENT_SSL_CERT_LDAP.equals(authType) && !"https".equals(u.getScheme())) {
            throw new IllegalArgumentException("Protocol '" + u.getScheme() + "' " + " not supported in httpd.listenurl '" + u + "' when auth.type = '" + AuthType.CLIENT_SSL_CERT_LDAP.name() + "'; only 'https' is supported");
        }
        if ("http".equals(u.getScheme())) {
            defaultPort = 80;
            c = newServerConnector(server, acceptors, config);
        } else if ("https".equals(u.getScheme())) {
            SslContextFactory.Server ssl = new SslContextFactory.Server();
            final Path keystore = getFile(cfg, "sslkeystore", "etc/keystore");
            String password = cfg.getString("httpd", null, "sslkeypassword");
            if (password == null) {
                password = "gerrit";
            }
            ssl.setKeyStorePath(keystore.toAbsolutePath().toString());
            ssl.setTrustStorePath(keystore.toAbsolutePath().toString());
            ssl.setKeyStorePassword(password);
            ssl.setTrustStorePassword(password);
            if (AuthType.CLIENT_SSL_CERT_LDAP.equals(authType)) {
                ssl.setNeedClientAuth(true);
                Path crl = getFile(cfg, "sslCrl", "etc/crl.pem");
                if (Files.exists(crl)) {
                    ssl.setCrlPath(crl.toAbsolutePath().toString());
                    ssl.setValidatePeerCerts(true);
                }
            }
            defaultPort = 443;
            config.addCustomizer(new SecureRequestCustomizer());
            c = new ServerConnector(server, null, null, null, 0, acceptors, new SslConnectionFactory(ssl, "http/1.1"), new HttpConnectionFactory(config));
        } else if ("proxy-http".equals(u.getScheme())) {
            defaultPort = 8080;
            config.addCustomizer(new ForwardedRequestCustomizer());
            c = newServerConnector(server, acceptors, config);
        } else if ("proxy-https".equals(u.getScheme())) {
            defaultPort = 8080;
            config.addCustomizer(new ForwardedRequestCustomizer());
            config.addCustomizer((connector, channelConfig, request) -> {
                request.setScheme(HttpScheme.HTTPS.asString());
                request.setSecure(true);
            });
            c = newServerConnector(server, acceptors, config);
        } else {
            throw new IllegalArgumentException("Protocol '" + u.getScheme() + "' " + " not supported in httpd.listenurl '" + u + "';" + " only 'http', 'https', 'proxy-http, 'proxy-https'" + " are supported");
        }
        try {
            if (u.getHost() == null && (// 
            u.getAuthority().equals("*") || u.getAuthority().startsWith("*:"))) {
                // Bind to all local addresses. Port wasn't parsed right by URI
                // due to the illegal host of "*" so replace with a legal name
                // and parse the URI.
                // 
                final URI r = new URI(u.toString().replace('*', 'A')).parseServerAuthority();
                c.setHost(null);
                c.setPort(0 < r.getPort() ? r.getPort() : defaultPort);
            } else {
                final URI r = u.parseServerAuthority();
                c.setHost(r.getHost());
                c.setPort(0 <= r.getPort() ? r.getPort() : defaultPort);
            }
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Invalid httpd.listenurl " + u, e);
        }
        c.setInheritChannel(cfg.getBoolean("httpd", "inheritChannel", false));
        c.setReuseAddress(reuseAddress);
        c.setIdleTimeout(cfg.getTimeUnit("httpd", null, "idleTimeout", 30000L, MILLISECONDS));
        connectors[idx] = c;
    }
    return connectors;
}
Also used : Path(java.nio.file.Path) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) LifecycleListener(com.google.gerrit.extensions.events.LifecycleListener) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) Inject(com.google.inject.Inject) URISyntaxException(java.net.URISyntaxException) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) Config(org.eclipse.jgit.lib.Config) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Filter(javax.servlet.Filter) URI(java.net.URI) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener) Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) BlockingArrayQueue(org.eclipse.jetty.util.BlockingArrayQueue) EnumSet(java.util.EnumSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) GuiceFilter(com.google.inject.servlet.GuiceFilter) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) List(java.util.List) Singleton(com.google.inject.Singleton) AuthType(com.google.gerrit.extensions.client.AuthType) Handler(org.eclipse.jetty.server.Handler) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ThreadSettingsConfig(com.google.gerrit.server.config.ThreadSettingsConfig) HttpScheme(org.eclipse.jetty.http.HttpScheme) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HashMap(java.util.HashMap) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) ConnectionStatistics(org.eclipse.jetty.io.ConnectionStatistics) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) HttpLogFactory(com.google.gerrit.pgm.http.jetty.HttpLog.HttpLogFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) ManagementFactory(java.lang.management.ManagementFactory) Files(java.nio.file.Files) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Injector(com.google.inject.Injector) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) Log(org.eclipse.jetty.util.log.Log) SitePaths(com.google.gerrit.server.config.SitePaths) DispatcherType(javax.servlet.DispatcherType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Connector(org.eclipse.jetty.server.Connector) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) URISyntaxException(java.net.URISyntaxException) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URI(java.net.URI) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) AuthType(com.google.gerrit.extensions.client.AuthType)

Aggregations

ForwardedRequestCustomizer (org.eclipse.jetty.server.ForwardedRequestCustomizer)12 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)10 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)9 ServerConnector (org.eclipse.jetty.server.ServerConnector)8 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)5 Server (org.eclipse.jetty.server.Server)5 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)5 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)5 MBeanContainer (org.eclipse.jetty.jmx.MBeanContainer)4 ArrayList (java.util.ArrayList)3 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)3 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 ConnectionFactory (org.eclipse.jetty.server.ConnectionFactory)2 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)2 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)2 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)2 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)2 StdErrLog (org.eclipse.jetty.util.log.StdErrLog)2 ScheduledExecutorScheduler (org.eclipse.jetty.util.thread.ScheduledExecutorScheduler)2