package io.micronaut.http.server.util;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpRequest;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class {
private static final String = "for";
private static final String = "by";
private static final String = "host";
private static final String = "proto";
private static final String = ";";
private static final String = ",";
private static final String = "=";
private static final String = "X-Forwarded-Proto";
private static final String = "X-Forwarded-Host";
private static final String = "X-Forwarded-For";
private static final String = "X-Forwarded-Port";
private List<String> = new ArrayList<>();
private String = null;
private String = null;
private String = null;
private Integer = null;
public (HttpRequest request) {
HttpHeaders headers = request.getHeaders();
if (headers.contains(HttpHeaders.FORWARDED)) {
for (String header: headers.getAll(HttpHeaders.FORWARDED)) {
header = StringUtils.trimToNull(header);
while (StringUtils.isNotEmpty(header)) {
int parameterEnd = header.indexOf(PARAM_DELIMITER);
String parameter;
if (parameterEnd > -1) {
parameter = header.substring(0, parameterEnd);
header = header.substring(parameterEnd + 1);
} else {
parameter = header;
header = "";
}
int firstPair = parameter.indexOf(PAIR_DELIMITER);
String pairName = parameter.substring(0, firstPair);
if (pairName.equalsIgnoreCase(FOR)) {
processFor(parameter);
} else if (pairName.equalsIgnoreCase(BY)) {
processBy(parameter);
} else if (pairName.equalsIgnoreCase(PROTO)) {
processProto(parameter);
} else if (pairName.equalsIgnoreCase(HOST)) {
processHost(parameter);
}
}
}
} else {
forwardedProto = StringUtils.trimToNull(headers.get(X_FORWARDED_PROTO));
forwardedHost = headers.get(X_FORWARDED_HOST);
try {
if (forwardedHost != null && forwardedHost.contains(":")) {
String[] parts = forwardedHost.split(":");
forwardedHost = parts[0];
forwardedPort = Integer.valueOf(parts[1]);
} else {
String portHeader = headers.get(X_FORWARDED_PORT);
if (portHeader != null) {
forwardedPort = Integer.valueOf(portHeader);
}
}
} catch (NumberFormatException ignored) { }
String forwardedForHeader = headers.get(X_FORWARDED_FOR);
if (forwardedForHeader != null) {
forwardedFor = Arrays.stream(forwardedForHeader.split(ELEMENT_DELIMITER))
.map(String::trim)
.collect(Collectors.toList());
}
}
}
@NonNull
public List<String> () {
return forwardedFor;
}
public String () {
return forwardedBy;
}
public String () {
return forwardedHost;
}
public String () {
return forwardedProto;
}
public Integer () {
return forwardedPort;
}
private void (String parameter) {
forwardedFor.addAll(Arrays.stream(parameter.split(ELEMENT_DELIMITER))
.map(pair -> pair.split(PAIR_DELIMITER))
.filter(pair -> pair.length == 2)
.map(pair -> pair[1])
.map(String::trim)
.map(value -> {
if (value.startsWith("\"")) {
return value.substring(1, value.length() - 1);
} else {
return value;
}
})
.collect(Collectors.toList()));
}
private void (String parameter) {
forwardedBy = processSimpleParameter(parameter);
}
private void (String parameter) {
forwardedProto = processSimpleParameter(parameter);
}
private void (String parameter) {
forwardedHost = processSimpleParameter(parameter);
}
private String (String parameter) {
String[] pair = parameter.split(PAIR_DELIMITER);
if (pair.length == 2) {
String value = pair[1];
if (value.startsWith("\"")) {
return value.substring(1, value.length() - 1);
} else {
return value;
}
} else {
return null;
}
}
}