Search in sources :

Example 86 with HttpString

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

the class ForwardedTestHelperHandler method handleRequest.

public void handleRequest(HttpServerExchange exchange) throws Exception {
    String address = exchange.getDestinationAddress().getAddress().toString();
    if (address.startsWith("/")) {
        address = "/" + TestSuiteEnvironment.formatPossibleIpv6Address(address.substring(1));
    } else {
        address = TestSuiteEnvironment.formatPossibleIpv6Address(address);
    }
    String value = exchange.getSourceAddress() + "|" + exchange.getRequestScheme() + "|" + address + ':' + exchange.getDestinationAddress().getPort();
    exchange.getResponseHeaders().put(new HttpString(FORWARD_TEST_HEADER), value);
    next.handleRequest(exchange);
}
Also used : HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Example 87 with HttpString

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

the class ListenerAdd method performRuntime.

@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException {
    final PathAddress address = context.getCurrentAddress();
    final PathAddress parent = address.getParent();
    final String name = context.getCurrentAddressValue();
    final String bindingRef = ListenerResourceDefinition.SOCKET_BINDING.resolveModelAttribute(context, model).asString();
    final String workerName = ListenerResourceDefinition.WORKER.resolveModelAttribute(context, model).asString();
    final String bufferPoolName = ListenerResourceDefinition.BUFFER_POOL.resolveModelAttribute(context, model).asString();
    final boolean enabled = ListenerResourceDefinition.ENABLED.resolveModelAttribute(context, model).asBoolean();
    final boolean peerHostLookup = ListenerResourceDefinition.RESOLVE_PEER_ADDRESS.resolveModelAttribute(context, model).asBoolean();
    final boolean secure = ListenerResourceDefinition.SECURE.resolveModelAttribute(context, model).asBoolean();
    OptionMap listenerOptions = OptionList.resolveOptions(context, model, ListenerResourceDefinition.LISTENER_OPTIONS);
    OptionMap socketOptions = OptionList.resolveOptions(context, model, ListenerResourceDefinition.SOCKET_OPTIONS);
    String serverName = parent.getLastElement().getValue();
    final CapabilityServiceBuilder<?> sb = context.getCapabilityServiceTarget().addCapability(ListenerResourceDefinition.LISTENER_CAPABILITY);
    final Consumer<ListenerService> serviceConsumer = sb.provides(ListenerResourceDefinition.LISTENER_CAPABILITY, UndertowService.listenerName(name));
    final ListenerService service = createService(serviceConsumer, name, serverName, context, model, listenerOptions, socketOptions);
    if (peerHostLookup) {
        service.addWrapperHandler(PeerNameResolvingHandler::new);
    }
    service.setEnabled(enabled);
    if (secure) {
        service.addWrapperHandler(MarkSecureHandler.WRAPPER);
    }
    List<String> disallowedMethods = ListenerResourceDefinition.DISALLOWED_METHODS.unwrap(context, model);
    if (!disallowedMethods.isEmpty()) {
        final Set<HttpString> methodSet = new HashSet<>();
        for (String i : disallowedMethods) {
            HttpString httpString = new HttpString(i.trim());
            methodSet.add(httpString);
        }
        service.addWrapperHandler(handler -> new DisallowedMethodsHandler(handler, methodSet));
    }
    sb.setInstance(service);
    service.getWorker().set(sb.requiresCapability(REF_IO_WORKER, XnioWorker.class, workerName));
    service.getBinding().set(sb.requiresCapability(REF_SOCKET_BINDING, SocketBinding.class, bindingRef));
    service.getBufferPool().set(sb.requiresCapability(Capabilities.CAPABILITY_BYTE_BUFFER_POOL, ByteBufferPool.class, bufferPoolName));
    service.getServerService().set(sb.requiresCapability(Capabilities.CAPABILITY_SERVER, Server.class, serverName));
    configureAdditionalDependencies(context, sb, model, service);
    sb.install();
}
Also used : SocketBinding(org.jboss.as.network.SocketBinding) ByteBufferPool(io.undertow.connector.ByteBufferPool) DisallowedMethodsHandler(io.undertow.server.handlers.DisallowedMethodsHandler) XnioWorker(org.xnio.XnioWorker) PeerNameResolvingHandler(io.undertow.server.handlers.PeerNameResolvingHandler) HttpString(io.undertow.util.HttpString) PathAddress(org.jboss.as.controller.PathAddress) OptionMap(org.xnio.OptionMap) HttpString(io.undertow.util.HttpString) HashSet(java.util.HashSet)

Example 88 with HttpString

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

the class CustomHttpHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    exchange.getResponseHeaders().put(new HttpString(RESPONSE_HEADER_ONE_NAME), String.valueOf(correctTcclInStaticInit));
    exchange.getResponseHeaders().put(new HttpString(RESPONSE_HEADER_TWO_NAME), String.valueOf(this.correctTcclInInstanceConstruction));
    next.handleRequest(exchange);
}
Also used : HttpString(io.undertow.util.HttpString)

Example 89 with HttpString

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

the class InMemorySessionTestCase method inMemorySessionTimeoutExpirationTest.

// https://issues.redhat.com/browse/UNDERTOW-1419
@Test
public void inMemorySessionTimeoutExpirationTest() throws IOException, InterruptedException {
    final int maxInactiveIntervalInSeconds = 1;
    final int accessorThreadSleepInMilliseconds = 200;
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    try {
        final SessionCookieConfig sessionConfig = new SessionCookieConfig();
        final SessionAttachmentHandler handler = new SessionAttachmentHandler(new InMemorySessionManager(""), sessionConfig);
        handler.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) {
                    // set 1 second timeout for this session expiration
                    manager.setDefaultSessionTimeout(maxInactiveIntervalInSeconds);
                    session = manager.createSession(exchange, sessionConfig);
                    session.setAttribute(COUNT, 0);
                    // let's call getAttribute() some times to be sure that the session timeout is no longer bumped
                    // by the method invocation
                    Runnable r = new Runnable() {

                        public void run() {
                            Session innerThreadSession = manager.getSession(exchange, sessionConfig);
                            int iterations = ((maxInactiveIntervalInSeconds * 1000) / accessorThreadSleepInMilliseconds);
                            for (int i = 0; i <= iterations; i++) {
                                try {
                                    Thread.sleep(accessorThreadSleepInMilliseconds);
                                } catch (InterruptedException e) {
                                    System.out.println(String.format("Unexpected error during Thread.sleep(): %s", e.getMessage()));
                                }
                                if (innerThreadSession != null) {
                                    try {
                                        System.out.println(String.format("Session is still valid. Attribute is: %s", innerThreadSession.getAttribute(COUNT).toString()));
                                        if (i == iterations) {
                                            System.out.println("Session should not still be valid!");
                                        }
                                    } catch (IllegalStateException e) {
                                        if ((e instanceof IllegalStateException) && e.getMessage().startsWith("UT000010")) {
                                            System.out.println(String.format("This is expected as session is not valid anymore: %s", e.getMessage()));
                                        } else {
                                            System.out.println(String.format("Unexpected exception while calling session.getAttribute(): %s", e.getMessage()));
                                        }
                                    }
                                }
                            }
                        }
                    };
                    Thread thread = new Thread(r);
                    thread.start();
                }
                // here the server is accessing one session attribute, so we're sure that the bumped timeout
                // issue is being replicated and we can test for regression
                Integer count = (Integer) session.getAttribute(COUNT);
                exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
                session.setAttribute(COUNT, ++count);
            }
        });
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/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());
        Thread.sleep(2 * 1000L);
        // after 2 seconds from the last call, the session expiration timeout hasn't been bumped anymore,
        // so now "COUNT" should be still set to 0 (zero)
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
    } finally {
        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) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 90 with HttpString

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

the class InMemorySessionTestCase method inMemoryMaxSessionsTest.

@Test
public void inMemoryMaxSessionsTest() throws IOException {
    TestHttpClient client1 = new TestHttpClient();
    client1.setCookieStore(new BasicCookieStore());
    TestHttpClient client2 = new TestHttpClient();
    client2.setCookieStore(new BasicCookieStore());
    try {
        final SessionCookieConfig sessionConfig = new SessionCookieConfig();
        final SessionAttachmentHandler handler = new SessionAttachmentHandler(new InMemorySessionManager("", 1, true), sessionConfig);
        handler.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.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        HttpResponse result = client1.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.getDefaultServerURL() + "/notamatchingpath");
        result = client1.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.getDefaultServerURL() + "/notamatchingpath");
        result = client2.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        result = client1.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
    } finally {
        client1.getConnectionManager().shutdown();
        client2.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) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

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