Search in sources :

Example 91 with HttpString

use of io.undertow.util.HttpString in project undertow by undertow-io.

the class SSLSessionTestCase method testSslSession.

@Test
public void testSslSession() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        InMemorySessionManager sessionManager = new InMemorySessionManager("");
        final SslSessionConfig sessionConfig = new SslSessionConfig(sessionManager);
        final SessionAttachmentHandler handler = new SessionAttachmentHandler(sessionManager, sessionConfig).setNext(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                final SessionManager manager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
                Session session = manager.getSession(exchange, sessionConfig);
                if (session == null) {
                    session = manager.createSession(exchange, sessionConfig);
                    session.setAttribute(COUNT, 0);
                }
                Integer count = (Integer) session.getAttribute(COUNT);
                exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
                session.setAttribute(COUNT, ++count);
            }
        });
        DefaultServer.startSSLServer();
        client.setSSLContext(DefaultServer.getClientSSLContext());
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        Header[] header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("1", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("2", header[0].getValue());
        Assert.assertEquals(0, client.getCookieStore().getCookies().size());
    } finally {
        DefaultServer.stopSSLServer();
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) SessionManager(io.undertow.server.session.SessionManager) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SslSessionConfig(io.undertow.server.session.SslSessionConfig) Header(org.apache.http.Header) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 92 with HttpString

use of io.undertow.util.HttpString in project undertow by undertow-io.

the class LoadBalancingProxyHTTP2TestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    int port = DefaultServer.getHostPort("default");
    final HttpHandler handler1 = getRootHandler("s1", "server1");
    server1 = Undertow.builder().addHttpsListener(port + 1, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            handler1.handleRequest(exchange);
        }
    }).build();
    final HttpHandler handler2 = getRootHandler("s2", "server2");
    server2 = Undertow.builder().addHttpsListener(port + 2, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setSocketOption(Options.REUSE_ADDRESSES, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            handler2.handleRequest(exchange);
        }
    }).build();
    server1.start();
    server2.start();
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    DefaultServer.setRootHandler(ProxyHandler.builder().setProxyClient(new LoadBalancingProxyClient().setConnectionsPerThread(4).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 1, null, null, null), "s1", ssl, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 2, null, null, null), "s2", ssl, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true))).setMaxRequestTime(10000).setMaxConnectionRetries(2).build());
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Http2ServerConnection(io.undertow.server.protocol.http2.Http2ServerConnection) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 93 with HttpString

use of io.undertow.util.HttpString in project undertow by undertow-io.

the class SimpleParserTestCase method testSameHttpStringReturned.

@Test
public void testSameHttpStringReturned() throws BadRequestException {
    byte[] in = "GET http://www.somehost.net/somepath HTTP/1.1\nHost: \t www.somehost.net\nAccept-Charset:\tsome\n \t  value\n\r\n".getBytes();
    final ParseState context1 = new ParseState(10);
    HttpServerExchange result1 = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.EMPTY).handle(ByteBuffer.wrap(in), context1, result1);
    final ParseState context2 = new ParseState(10);
    HttpServerExchange result2 = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.EMPTY).handle(ByteBuffer.wrap(in), context2, result2);
    Assert.assertSame(result1.getProtocol(), result2.getProtocol());
    Assert.assertSame(result1.getRequestMethod(), result2.getRequestMethod());
    for (final HttpString header : result1.getRequestHeaders().getHeaderNames()) {
        boolean found = false;
        for (final HttpString header2 : result1.getRequestHeaders().getHeaderNames()) {
            if (header == header2) {
                found = true;
                break;
            }
        }
        if (header.equals(Headers.HOST)) {
            Assert.assertSame(Headers.HOST, header);
        }
        Assert.assertTrue("Could not found header " + header, found);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 94 with HttpString

use of io.undertow.util.HttpString in project undertow by undertow-io.

the class ServletInitialHandler method dispatchMockRequest.

@Override
public void dispatchMockRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    final DefaultByteBufferPool bufferPool = new DefaultByteBufferPool(false, 1024, 0, 0);
    MockServerConnection connection = new MockServerConnection(bufferPool);
    HttpServerExchange exchange = new HttpServerExchange(connection);
    exchange.setRequestScheme(request.getScheme());
    exchange.setRequestMethod(new HttpString(request.getMethod()));
    exchange.setProtocol(Protocols.HTTP_1_0);
    exchange.setResolvedPath(request.getContextPath());
    String relative;
    if (request.getPathInfo() == null) {
        relative = request.getServletPath();
    } else {
        relative = request.getServletPath() + request.getPathInfo();
    }
    exchange.setRelativePath(relative);
    final ServletPathMatch info = paths.getServletHandlerByPath(request.getServletPath());
    final HttpServletResponseImpl oResponse = new HttpServletResponseImpl(exchange, servletContext);
    final HttpServletRequestImpl oRequest = new HttpServletRequestImpl(exchange, servletContext);
    final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), oRequest, oResponse, info);
    servletRequestContext.setServletRequest(request);
    servletRequestContext.setServletResponse(response);
    // set the max request size if applicable
    if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0 && isMultiPartExchange(exchange)) {
        exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize());
    }
    exchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);
    exchange.startBlocking(new ServletBlockingHttpExchange(exchange));
    servletRequestContext.setServletPathMatch(info);
    try {
        dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new ServletException(e);
    }
}
Also used : DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) HttpString(io.undertow.util.HttpString) HttpServletResponseImpl(io.undertow.servlet.spec.HttpServletResponseImpl) ServletBlockingHttpExchange(io.undertow.servlet.core.ServletBlockingHttpExchange) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) ServletException(javax.servlet.ServletException) HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) HttpString(io.undertow.util.HttpString)

Example 95 with HttpString

use of io.undertow.util.HttpString in project undertow by undertow-io.

the class HttpServletResponseImpl method setTrailerFields.

@Override
public void setTrailerFields(Supplier<Map<String, String>> supplier) {
    if (exchange.isResponseStarted()) {
        throw UndertowServletMessages.MESSAGES.responseAlreadyCommited();
    }
    if (exchange.getProtocol() == Protocols.HTTP_1_0) {
        throw UndertowServletMessages.MESSAGES.trailersNotSupported("HTTP/1.0 request");
    } else if (exchange.getProtocol() == Protocols.HTTP_1_1) {
        if (exchange.getResponseHeaders().contains(Headers.CONTENT_LENGTH)) {
            throw UndertowServletMessages.MESSAGES.trailersNotSupported("not chunked");
        }
    }
    this.trailerSupplier = supplier;
    exchange.putAttachment(HttpAttachments.RESPONSE_TRAILER_SUPPLIER, () -> {
        HeaderMap trailers = new HeaderMap();
        Map<String, String> map = supplier.get();
        for (Map.Entry<String, String> e : map.entrySet()) {
            trailers.put(new HttpString(e.getKey()), e.getValue());
        }
        return trailers;
    });
}
Also used : HeaderMap(io.undertow.util.HeaderMap) HttpString(io.undertow.util.HttpString) HeaderMap(io.undertow.util.HeaderMap) Map(java.util.Map) HttpString(io.undertow.util.HttpString)

Aggregations

HttpString (io.undertow.util.HttpString)147 HeaderMap (io.undertow.util.HeaderMap)31 HttpServerExchange (io.undertow.server.HttpServerExchange)27 Test (org.junit.Test)25 ClientRequest (io.undertow.client.ClientRequest)23 Map (java.util.Map)23 ClientResponse (io.undertow.client.ClientResponse)21 IOException (java.io.IOException)21 HashMap (java.util.HashMap)21 URI (java.net.URI)19 ClientConnection (io.undertow.client.ClientConnection)17 HttpHandler (io.undertow.server.HttpHandler)17 Http2Client (com.networknt.client.Http2Client)14 ClientException (com.networknt.exception.ClientException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 User (com.networknt.portal.usermanagement.model.common.model.user.User)13 URISyntaxException (java.net.URISyntaxException)13 ArrayList (java.util.ArrayList)13 List (java.util.List)13 CountDownLatch (java.util.concurrent.CountDownLatch)11