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]"));
}
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"));
}
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");
}
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);
}
}
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();
}
Aggregations