Skip to content

Commit

Permalink
Merge branch '2.1.x' into 2.2.x
Browse files Browse the repository at this point in the history
Closes gh-21052
  • Loading branch information
philwebb committed Apr 21, 2020
2 parents 4e0fdbe + a2fdf23 commit 7d68c7c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
Expand Up @@ -148,7 +148,7 @@ public void start() throws WebServerException {
}
catch (IOException ex) {
if (connector instanceof NetworkConnector && findBindException(ex) != null) {
throw new PortInUseException(((NetworkConnector) connector).getPort());
throw new PortInUseException(((NetworkConnector) connector).getPort(), ex);
}
throw ex;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import java.util.List;
import java.util.function.Predicate;

import io.netty.channel.unix.Errors.NativeIoException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.netty.ChannelBindException;
Expand All @@ -47,7 +48,12 @@
*/
public class NettyWebServer implements WebServer {

private static final Predicate<HttpServerRequest> ALWAYS = (r) -> true;
/**
* Permission denied error code from {@code errno.h}.
*/
private static final int ERROR_NO_EACCES = -13;

private static final Predicate<HttpServerRequest> ALWAYS = (request) -> true;

private static final Log logger = LogFactory.getLog(NettyWebServer.class);

Expand Down Expand Up @@ -81,8 +87,8 @@ public void start() throws WebServerException {
}
catch (Exception ex) {
ChannelBindException bindException = findBindException(ex);
if (bindException != null) {
throw new PortInUseException(bindException.localPort());
if (bindException != null && !isPermissionDenied(bindException.getCause())) {
throw new PortInUseException(bindException.localPort(), ex);
}
throw new WebServerException("Unable to start Netty", ex);
}
Expand All @@ -91,6 +97,17 @@ public void start() throws WebServerException {
}
}

private boolean isPermissionDenied(Throwable bindExceptionCause) {
try {
if (bindExceptionCause instanceof NativeIoException) {
return ((NativeIoException) bindExceptionCause).expectedErr() == ERROR_NO_EACCES;
}
}
catch (Throwable ex) {
}
return false;
}

private DisposableServer startHttpServer() {
HttpServer server = this.httpServer;
if (this.routeProviders.isEmpty()) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -151,7 +151,7 @@ public void start() throws WebServerException {
List<Port> actualPorts = getActualPorts();
failedPorts.removeAll(actualPorts);
if (failedPorts.size() == 1) {
throw new PortInUseException(failedPorts.iterator().next().getNumber());
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
}
}
throw new WebServerException("Unable to start embedded Undertow", ex);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -109,7 +109,7 @@ public void start() throws WebServerException {
List<UndertowWebServer.Port> actualPorts = getActualPorts();
failedPorts.removeAll(actualPorts);
if (failedPorts.size() == 1) {
throw new PortInUseException(failedPorts.iterator().next().getNumber());
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
}
}
throw new WebServerException("Unable to start embedded Undertow", ex);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,7 +32,16 @@ public class PortInUseException extends WebServerException {
* @param port the port that was in use
*/
public PortInUseException(int port) {
super("Port " + port + " is already in use", null);
this(port, null);
}

/**
* Creates a new port in use exception for the given {@code port}.
* @param port the port that was in use
* @param cause the cause of the exception
*/
public PortInUseException(int port, Throwable cause) {
super("Port " + port + " is already in use", cause);
this.port = port;
}

Expand Down
Expand Up @@ -62,7 +62,7 @@ void exceptionIsThrownWhenPortIsAlreadyInUse() {
this.webServer.start();
factory.setPort(this.webServer.getPort());
assertThatExceptionOfType(PortInUseException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start)
.satisfies(this::portMatchesRequirement);
.satisfies(this::portMatchesRequirement).withCauseInstanceOf(Throwable.class);
}

private void portMatchesRequirement(PortInUseException exception) {
Expand Down

0 comments on commit 7d68c7c

Please sign in to comment.