@Test public void getAndValidateHostHeader_should() { assertThat(getAndValidateHostHeader("localhost").get()).isEqualTo("localhost"); assertThat(getAndValidateHostHeader("localhost:5051").get()).isEqualTo("localhost"); assertThat(getAndValidateHostHeader(BAD_HOST)).isEmpty(); }
static Optional<String> getAndValidateHostHeader(final String hostHeader) { final Iterable<String> splitHostHeader = Splitter.on(':').split(hostHeader); final long hostPieces = stream(splitHostHeader).count(); if (hostPieces > 1) { if (hostPieces > 2 || !Iterables.get(splitHostHeader, 1).matches("\\d{1,5}+")) { return Optional.empty(); } } return Optional.ofNullable(Iterables.get(splitHostHeader, 0)); }
HostAllowlistUtils { static Optional<String> getAndValidateHostHeader(final String hostHeader) { final Iterable<String> splitHostHeader = Splitter.on(':').split(hostHeader); final long hostPieces = stream(splitHostHeader).count(); if (hostPieces > 1) { if (hostPieces > 2 || !Iterables.get(splitHostHeader, 1).matches("\\d{1,5}+")) { return Optional.empty(); } } return Optional.ofNullable(Iterables.get(splitHostHeader, 0)); } }
HostAllowlistUtils { static Optional<String> getAndValidateHostHeader(final String hostHeader) { final Iterable<String> splitHostHeader = Splitter.on(':').split(hostHeader); final long hostPieces = stream(splitHostHeader).count(); if (hostPieces > 1) { if (hostPieces > 2 || !Iterables.get(splitHostHeader, 1).matches("\\d{1,5}+")) { return Optional.empty(); } } return Optional.ofNullable(Iterables.get(splitHostHeader, 0)); }  }
HostAllowlistUtils { static Optional<String> getAndValidateHostHeader(final String hostHeader) { final Iterable<String> splitHostHeader = Splitter.on(':').split(hostHeader); final long hostPieces = stream(splitHostHeader).count(); if (hostPieces > 1) { if (hostPieces > 2 || !Iterables.get(splitHostHeader, 1).matches("\\d{1,5}+")) { return Optional.empty(); } } return Optional.ofNullable(Iterables.get(splitHostHeader, 0)); }   }
HostAllowlistUtils { static Optional<String> getAndValidateHostHeader(final String hostHeader) { final Iterable<String> splitHostHeader = Splitter.on(':').split(hostHeader); final long hostPieces = stream(splitHostHeader).count(); if (hostPieces > 1) { if (hostPieces > 2 || !Iterables.get(splitHostHeader, 1).matches("\\d{1,5}+")) { return Optional.empty(); } } return Optional.ofNullable(Iterables.get(splitHostHeader, 0)); }    }