Search in sources :

Example 6 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project light-4j by networknt.

the class CorsUtilTest method testDefaultOrigin.

/**
 * Test of defaultOrigin method, of class CorsUtil.
 */
@Test
public void testDefaultOrigin() {
    HeaderMap headerMap = new HeaderMap();
    headerMap.add(HOST, "localhost:80");
    HttpServerExchange exchange = new HttpServerExchange(null, headerMap, new HeaderMap(), 10);
    exchange.setRequestScheme("http");
    assertThat(CorsUtil.defaultOrigin(exchange), is("http://localhost"));
    headerMap.clear();
    headerMap.add(HOST, "www.example.com:8080");
    assertThat(CorsUtil.defaultOrigin(exchange), is("http://www.example.com:8080"));
    headerMap.clear();
    headerMap.add(HOST, "www.example.com:443");
    exchange.setRequestScheme("https");
    assertThat(CorsUtil.defaultOrigin(exchange), is("https://www.example.com"));
    headerMap.clear();
    exchange.setRequestScheme("http");
    headerMap.add(HOST, "[::1]:80");
    assertThat(CorsUtil.defaultOrigin(exchange), is("http://[::1]"));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HeaderMap(io.undertow.util.HeaderMap) Test(org.junit.Test)

Example 7 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project light-4j by networknt.

the class CorsUtilTest method testMatchOrigin.

/**
 * Test of matchOrigin method, of class CorsUtil.
 */
@Test
public void testMatchOrigin() throws Exception {
    HeaderMap headerMap = new HeaderMap();
    headerMap.add(HOST, "localhost:80");
    headerMap.add(ORIGIN, "http://localhost");
    HttpServerExchange exchange = new HttpServerExchange(null, headerMap, new HeaderMap(), 10);
    exchange.setRequestScheme("http");
    exchange.setRequestMethod(HttpString.EMPTY);
    Collection<String> allowedOrigins = null;
    assertThat(CorsUtil.matchOrigin(exchange, allowedOrigins), is("http://localhost"));
    allowedOrigins = Collections.singletonList("http://www.example.com:9990");
    // Default origin
    assertThat(CorsUtil.matchOrigin(exchange, allowedOrigins), is("http://localhost"));
    headerMap.clear();
    headerMap.add(HOST, "localhost:80");
    headerMap.add(ORIGIN, "http://www.example.com:9990");
    assertThat(CorsUtil.matchOrigin(exchange, allowedOrigins), is("http://www.example.com:9990"));
    headerMap.clear();
    headerMap.add(HOST, "localhost:80");
    headerMap.add(ORIGIN, "http://www.example.com");
    assertThat(CorsUtil.matchOrigin(exchange, allowedOrigins), is(nullValue()));
    headerMap.addAll(ORIGIN, Arrays.asList("http://localhost:8080", "http://www.example.com:9990", "http://localhost"));
    allowedOrigins = Arrays.asList("http://localhost", "http://www.example.com:9990");
    assertThat(CorsUtil.matchOrigin(exchange, allowedOrigins), is("http://localhost"));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HeaderMap(io.undertow.util.HeaderMap) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 8 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project core-ng-project by neowu.

the class RequestParserTest method requestURLIsTooLong.

@Test
void requestURLIsTooLong() {
    HttpServerExchange exchange = new HttpServerExchange(null, -1);
    exchange.getRequestHeaders().put(Headers.HOST, "localhost");
    exchange.setRequestURI("/path");
    StringBuilder builder = new StringBuilder(1000);
    for (int i = 0; i < 100; i++) {
        builder.append("1234567890");
    }
    exchange.setQueryString(builder.toString());
    RequestImpl request = new RequestImpl(exchange, null);
    request.scheme = "http";
    request.port = 80;
    BadRequestException exception = assertThrows(BadRequestException.class, () -> parser.requestURL(request, exchange));
    assertThat(exception.getMessage()).contains("requestURL is too long");
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) BadRequestException(core.framework.web.exception.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 9 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project divolte-collector by divolte.

the class AsyncRequestBodyReceiver method receive.

public void receive(final BiConsumer<InputStream, Integer> callback, final HttpServerExchange exchange) {
    Objects.requireNonNull(callback);
    if (logger.isDebugEnabled()) {
        logger.debug("Pre-allocating buffer with {} chunks.", preallocateChunks.get());
    }
    if (exchange.isRequestComplete()) {
        logger.debug("Request already completed; zero length content body.");
        callback.accept(EMPTY_INPUT_STREAM, 0);
        return;
    }
    // Determine the number of buffer-slots to use, trusting a well-formed content-length
    // header if that's present.
    final Optional<Integer> contentLength = Optional.ofNullable(exchange.getRequestHeaders().getFirst(Headers.CONTENT_LENGTH)).map(Ints::tryParse);
    if (logger.isDebugEnabled()) {
        logger.debug("Content length claimed by request: {}", contentLength);
    }
    final int provisionChunks = contentLength.map(AsyncRequestBodyReceiver::calculateChunks).orElseGet(preallocateChunks::get);
    if (provisionChunks > maximumChunks) {
        logger.info("Rejecting request; anticipated content length ({}) will exceed exceed maximum allowed {})", contentLength, maximumChunks * ChunkyByteBuffer.CHUNK_SIZE);
        rejectLargeRequest(exchange);
        return;
    }
    // If we've reached this point we're always going to accept the body, so continuing is fine.
    if (HttpContinue.requiresContinueResponse(exchange)) {
        logger.debug("Request requires permission to proceed; allowing to continue.");
        HttpContinue.sendContinueResponse(exchange, new IoCallback() {

            @Override
            public void onComplete(final HttpServerExchange exchange, final Sender sender) {
                receive(callback, exchange, contentLength, provisionChunks);
            }

            @Override
            public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
                logger.error("Error allowing request to continue.");
                exchange.endExchange();
            }
        });
    } else {
        receive(callback, exchange, contentLength, provisionChunks);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) Ints(com.google.common.primitives.Ints) IoCallback(io.undertow.io.IoCallback) IOException(java.io.IOException)

Example 10 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project ovirt-engine-sdk-java by oVirt.

the class ServerTest method startServer.

protected void startServer(String host) {
    // Create the handler that returns a valid authentication token:
    HttpHandler ssoLoginHandler = new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.setStatusCode(200);
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
            exchange.getResponseSender().send(String.format("{\"access_token\":\"%s\"}", TOKEN));
        }
    };
    HttpHandler ssoLogoutHandler = new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.setStatusCode(200);
            exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
            exchange.getResponseSender().send("{}");
        }
    };
    // Configure the handlers for password and Kerberos authentication:
    testHandler().addPrefixPath(testPrefix() + "/sso/oauth/token", ssoLoginHandler);
    testHandler().addPrefixPath(testPrefix() + "/sso/oauth/token-http-auth", ssoLoginHandler);
    testHandler().addPrefixPath(testPrefix() + "/services/sso-logout", ssoLogoutHandler);
    builder.setHandler(testHandler());
    // Create and start the web server:
    SSLContext context = createSslContext(host);
    builder.addHttpsListener(testPort(), host, context);
    server = builder.build();
    server.start();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) SSLContext(javax.net.ssl.SSLContext)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)270 HttpHandler (io.undertow.server.HttpHandler)126 Test (org.junit.Test)109 IOException (java.io.IOException)87 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)43 TestHttpClient (io.undertow.testutils.TestHttpClient)42 HttpGet (org.apache.http.client.methods.HttpGet)40 HttpResponse (org.apache.http.HttpResponse)37 HttpString (io.undertow.util.HttpString)36 Header (org.apache.http.Header)24 Undertow (io.undertow.Undertow)19 ByteBuffer (java.nio.ByteBuffer)19 Map (java.util.Map)16 SessionConfig (io.undertow.server.session.SessionConfig)15 Sender (io.undertow.io.Sender)14 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 HeaderValues (io.undertow.util.HeaderValues)12 URI (java.net.URI)12