Skip to content

Commit 7cd50a3

Browse files
committed
Context getHostAndPort without securePort set -> NPE fix jooby-project#2074
1 parent 7cf322c commit 7cd50a3

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,8 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.exception.RegistryException;
9-
import io.jooby.exception.TypeMismatchException;
10-
import io.jooby.internal.HashValue;
11-
import io.jooby.internal.MissingValue;
12-
import io.jooby.internal.SingleValue;
13-
import io.jooby.internal.UrlParser;
14-
import io.jooby.internal.ValueConverters;
15-
import org.slf4j.Logger;
8+
import static java.util.Optional.ofNullable;
169

17-
import javax.annotation.Nonnull;
18-
import javax.annotation.Nullable;
1910
import java.io.FileInputStream;
2011
import java.io.IOException;
2112
import java.io.InputStream;
@@ -33,6 +24,19 @@
3324
import java.util.Map;
3425
import java.util.Optional;
3526

27+
import javax.annotation.Nonnull;
28+
import javax.annotation.Nullable;
29+
30+
import org.slf4j.Logger;
31+
32+
import io.jooby.exception.RegistryException;
33+
import io.jooby.exception.TypeMismatchException;
34+
import io.jooby.internal.HashValue;
35+
import io.jooby.internal.MissingValue;
36+
import io.jooby.internal.SingleValue;
37+
import io.jooby.internal.UrlParser;
38+
import io.jooby.internal.ValueConverters;
39+
3640
/***
3741
* Like {@link Context} but with couple of default methods.
3842
*
@@ -270,7 +274,10 @@ public interface DefaultContext extends Context {
270274
? header("X-Forwarded-Host").toOptional()
271275
: Optional.empty();
272276
String value = header
273-
.orElseGet(() -> header("Host").value(getServerHost() + ":" + getServerPort()));
277+
.orElseGet(() ->
278+
ofNullable(header("Host").valueOrNull())
279+
.orElseGet(() -> getServerHost() + ":" + getServerPort())
280+
);
274281
int i = value.indexOf(',');
275282
String host = i > 0 ? value.substring(0, i).trim() : value;
276283
if (host.startsWith("[") && host.endsWith("]")) {
@@ -286,7 +293,10 @@ public interface DefaultContext extends Context {
286293

287294
@Override default int getServerPort() {
288295
ServerOptions options = getRouter().getServerOptions();
289-
return isSecure() ? options.getSecurePort() : options.getPort();
296+
return isSecure()
297+
// Buggy proxy where it report a https scheme but there is no HTTPS configured option
298+
? ofNullable(options.getSecurePort()).orElse(options.getPort())
299+
: options.getPort();
290300
}
291301

292302
@Override default int getPort() {

0 commit comments

Comments
 (0)