Skip to content

Commit 183482b

Browse files
committed
use static functions from ApiServlet and code refactoring
1 parent 0ebddf7 commit 183482b

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

client/src/main/java/org/apache/cloudstack/ACSRequestLog.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@
3232

3333
import static org.apache.commons.configuration.DataConfiguration.DEFAULT_DATE_FORMAT;
3434

35-
import javax.inject.Inject;
36-
3735
public class ACSRequestLog extends NCSARequestLog {
38-
@Inject
39-
ApiServlet apiServlet;
40-
4136
private static final ThreadLocal<StringBuilder> buffers =
4237
ThreadLocal.withInitial(() -> new StringBuilder(256));
4338

@@ -58,7 +53,7 @@ public void log(Request request, Response response) {
5853
StringBuilder sb = buffers.get();
5954
sb.setLength(0);
6055

61-
InetAddress remoteAddress = apiServlet.getClientAddress(request);
56+
InetAddress remoteAddress = ApiServlet.getClientAddress(request);
6257
sb.append(remoteAddress)
6358
.append(" - - [")
6459
.append(dateCache.format(request.getTimeStamp()))

server/src/main/java/com/cloud/api/ApiServlet.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -679,38 +679,34 @@ private boolean isSpecificAPI(String commandName) {
679679
}
680680
return false;
681681
}
682-
boolean doUseForwardHeaders() {
682+
static boolean doUseForwardHeaders() {
683683
return Boolean.TRUE.equals(ApiServer.useForwardHeader.value());
684684
}
685685

686-
String[] proxyNets() {
686+
static String[] proxyNets() {
687687
return ApiServer.proxyForwardList.value().split(",");
688688
}
689689
//This method will try to get login IP of user even if servlet is behind reverseProxy or loadBalancer
690-
public InetAddress getClientAddress(final HttpServletRequest request) throws UnknownHostException {
691-
String ip = null;
692-
InetAddress pretender = InetAddress.getByName(request.getRemoteAddr());
693-
if(doUseForwardHeaders()) {
694-
if (NetUtils.isIpInCidrList(pretender, proxyNets())) {
690+
public static InetAddress getClientAddress(final HttpServletRequest request) throws UnknownHostException {
691+
final String remote = request.getRemoteAddr();
692+
if (doUseForwardHeaders()) {
693+
final InetAddress remoteAddr = InetAddress.getByName(remote);
694+
if (NetUtils.isIpInCidrList(remoteAddr, proxyNets())) {
695695
for (String header : getClientAddressHeaders()) {
696696
header = header.trim();
697-
ip = getCorrectIPAddress(request.getHeader(header));
697+
final String ip = getCorrectIPAddress(request.getHeader(header));
698698
if (StringUtils.isNotBlank(ip)) {
699-
LOGGER.debug(String.format("found ip %s in header %s ", ip, header));
700-
break;
699+
LOGGER.debug("found ip {} in header {}", ip, header);
700+
return InetAddress.getByName(ip);
701701
}
702-
} // no address found in header so ip is blank and use remote addr
703-
} // else not an allowed proxy address, ip is blank and use remote addr
704-
}
705-
if (StringUtils.isBlank(ip)) {
706-
LOGGER.trace(String.format("no ip found in headers, returning remote address %s.", pretender.getHostAddress()));
707-
return pretender;
702+
}
703+
}
708704
}
709-
710-
return InetAddress.getByName(ip);
705+
LOGGER.trace("no ip found in headers, returning remote address {}.", remote);
706+
return InetAddress.getByName(remote);
711707
}
712708

713-
private String[] getClientAddressHeaders() {
709+
private static String[] getClientAddressHeaders() {
714710
return ApiServer.listOfForwardHeaders.value().split(",");
715711
}
716712

0 commit comments

Comments
 (0)