Search in sources :

Example 16 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project jetty.project by eclipse.

the class ResourceHandlerTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    File dir = MavenTestingUtils.getTargetFile("test-classes/simple");
    File bigger = new File(dir, "bigger.txt");
    File big = new File(dir, "big.txt");
    try (OutputStream out = new FileOutputStream(bigger)) {
        for (int i = 0; i < 100; i++) {
            try (InputStream in = new FileInputStream(big)) {
                IO.copy(in, out);
            }
        }
    }
    bigger.deleteOnExit();
    // determine how the SCM of choice checked out the big.txt EOL
    // we can't just use whatever is the OS default.
    // because, for example, a windows system using git can be configured for EOL handling using
    // local, remote, file lists, patterns, etc, rendering assumptions about the OS EOL choice
    // wrong for unit tests.
    LN = System.getProperty("line.separator");
    try (BufferedReader reader = Files.newBufferedReader(big.toPath(), StandardCharsets.UTF_8)) {
        // a buffer large enough to capture at least 1 EOL
        char[] cbuf = new char[128];
        reader.read(cbuf);
        String sample = new String(cbuf);
        if (sample.contains("\r\n")) {
            LN = "\r\n";
        } else if (sample.contains("\n\r")) {
            LN = "\n\r";
        } else {
            LN = "\n";
        }
    }
    _server = new Server();
    _config = new HttpConfiguration();
    _config.setOutputBufferSize(2048);
    _connector = new ServerConnector(_server, new HttpConnectionFactory(_config));
    _local = new LocalConnector(_server);
    _server.setConnectors(new Connector[] { _connector, _local });
    _resourceHandler = new ResourceHandler();
    _resourceHandler.setResourceBase(MavenTestingUtils.getTargetFile("test-classes/simple").getAbsolutePath());
    _resourceHandler.setWelcomeFiles(new String[] { "welcome.txt" });
    _contextHandler = new ContextHandler("/resource");
    _contextHandler.setHandler(_resourceHandler);
    _server.setHandler(_contextHandler);
    _server.start();
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) LocalConnector(org.eclipse.jetty.server.LocalConnector) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) FileInputStream(java.io.FileInputStream) ServerConnector(org.eclipse.jetty.server.ServerConnector) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 17 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project jetty.project by eclipse.

the class SimpleServletServer method start.

public void start() throws Exception {
    // Configure Server
    server = new Server();
    if (ssl) {
        // HTTP Configuration
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(0);
        http_config.setOutputBufferSize(32768);
        http_config.setRequestHeaderSize(8192);
        http_config.setResponseHeaderSize(8192);
        http_config.setSendServerVersion(true);
        http_config.setSendDateHeader(false);
        sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
        sslContextFactory.setKeyStorePassword("storepwd");
        sslContextFactory.setKeyManagerPassword("keypwd");
        sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
        // SSL HTTP Configuration
        HttpConfiguration https_config = new HttpConfiguration(http_config);
        https_config.addCustomizer(new SecureRequestCustomizer());
        // SSL Connector
        connector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
        connector.setPort(0);
    } else {
        // Basic HTTP connector
        connector = new ServerConnector(server);
        connector.setPort(0);
    }
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    configureServletContextHandler(context);
    server.setHandler(context);
    // Serve capture servlet
    context.addServlet(new ServletHolder(servlet), "/*");
    // Start Server
    server.start();
    // Establish the Server URI
    String host = connector.getHost();
    if (host == null) {
        host = "localhost";
    }
    int port = connector.getLocalPort();
    serverUri = new URI(String.format("%s://%s:%d/", ssl ? "wss" : "ws", host, port));
    // Some debugging
    if (LOG.isDebugEnabled()) {
        LOG.debug(server.dump());
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) URI(java.net.URI)

Example 18 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project jetty.project by eclipse.

the class AbstractALPNTest method prepare.

protected InetSocketAddress prepare() throws Exception {
    server = new Server();
    HttpConfiguration httpConfiguration = new HttpConfiguration();
    HttpConnectionFactory h1 = new HttpConnectionFactory(httpConfiguration);
    HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(httpConfiguration);
    ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
    alpn.setDefaultProtocol(h1.getProtocol());
    connector = new ServerConnector(server, newSslContextFactory(), alpn, h1, h2);
    connector.setPort(0);
    connector.setIdleTimeout(30000);
    server.addConnector(connector);
    server.start();
    ALPN.debug = true;
    return new InetSocketAddress("localhost", connector.getLocalPort());
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ALPNServerConnectionFactory(org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory) InetSocketAddress(java.net.InetSocketAddress) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory)

Example 19 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project jetty.project by eclipse.

the class HTTP2CServerTest method testHTTP_2_0_DirectWithoutH2C.

@Test
public void testHTTP_2_0_DirectWithoutH2C() throws Exception {
    AtomicLong fills = new AtomicLong();
    // Remove "h2c", leaving only "http/1.1".
    connector.clearConnectionFactories();
    HttpConnectionFactory connectionFactory = new HttpConnectionFactory() {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            HttpConnection connection = new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {

                @Override
                public void onFillable() {
                    fills.incrementAndGet();
                    super.onFillable();
                }
            };
            return configure(connection, connector, endPoint);
        }
    };
    connector.addConnectionFactory(connectionFactory);
    connector.setDefaultProtocol(connectionFactory.getProtocol());
    // Now send a HTTP/2 direct request, which
    // will have the PRI * HTTP/2.0 preface.
    byteBufferPool = new MappedByteBufferPool();
    generator = new Generator(byteBufferPool);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        // We sent a HTTP/2 preface, but the server has no "h2c" connection
        // factory so it does not know how to handle this request.
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String responseLine = reader.readLine();
        Assert.assertThat(responseLine, Matchers.containsString(" 426 "));
        while (true) {
            if (reader.read() < 0)
                break;
        }
    }
    // Make sure we did not spin.
    Thread.sleep(1000);
    Assert.assertThat(fills.get(), Matchers.lessThan(5L));
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) InputStreamReader(java.io.InputStreamReader) HttpConnection(org.eclipse.jetty.server.HttpConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) EndPoint(org.eclipse.jetty.io.EndPoint) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) AtomicLong(java.util.concurrent.atomic.AtomicLong) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 20 with HttpConnectionFactory

use of org.eclipse.jetty.server.HttpConnectionFactory in project jetty.project by eclipse.

the class ProxyServletTest method startProxy.

private void startProxy(Map<String, String> initParams) throws Exception {
    QueuedThreadPool proxyPool = new QueuedThreadPool();
    proxyPool.setName("proxy");
    proxy = new Server(proxyPool);
    HttpConfiguration configuration = new HttpConfiguration();
    configuration.setSendDateHeader(false);
    configuration.setSendServerVersion(false);
    String value = initParams.get("outputBufferSize");
    if (value != null)
        configuration.setOutputBufferSize(Integer.valueOf(value));
    proxyConnector = new ServerConnector(proxy, new HttpConnectionFactory(configuration));
    proxy.addConnector(proxyConnector);
    proxyContext = new ServletContextHandler(proxy, "/", true, false);
    ServletHolder proxyServletHolder = new ServletHolder(proxyServlet);
    proxyServletHolder.setInitParameters(initParams);
    proxyContext.addServlet(proxyServletHolder, "/*");
    proxy.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)90 ServerConnector (org.eclipse.jetty.server.ServerConnector)79 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)69 Server (org.eclipse.jetty.server.Server)56 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)44 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)43 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)39 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)16 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)15 File (java.io.File)12 Connector (org.eclipse.jetty.server.Connector)12 IOException (java.io.IOException)11 ServletException (javax.servlet.ServletException)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 HTTP2ServerConnectionFactory (org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory)9 Before (org.junit.Before)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 ALPNServerConnectionFactory (org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory)8 Request (org.eclipse.jetty.server.Request)8