aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpServletRequestUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpServletRequestUtils.java')
-rw-r--r--container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpServletRequestUtils.java38
1 files changed, 38 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpServletRequestUtils.java b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpServletRequestUtils.java
new file mode 100644
index 00000000000..e7b9f459d2e
--- /dev/null
+++ b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpServletRequestUtils.java
@@ -0,0 +1,38 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.jdisc.http.server.jetty;
+
+import org.eclipse.jetty.server.HttpConnection;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author bjorncs
+ */
+public class HttpServletRequestUtils {
+ private HttpServletRequestUtils() {}
+
+ public static HttpConnection getConnection(HttpServletRequest request) {
+ return (HttpConnection)request.getAttribute("org.eclipse.jetty.server.HttpConnection");
+ }
+
+ /**
+ * Note: {@link HttpServletRequest#getLocalPort()} may return the local port of the load balancer / reverse proxy if proxy-protocol is enabled.
+ * @return the actual local port of the underlying Jetty connector
+ */
+ public static int getConnectorLocalPort(HttpServletRequest request) {
+ JDiscServerConnector connector = (JDiscServerConnector) getConnection(request).getConnector();
+ int actualLocalPort = connector.getLocalPort();
+ int localPortIfConnectorUnopened = -1;
+ int localPortIfConnectorClosed = -2;
+ if (actualLocalPort == localPortIfConnectorUnopened || actualLocalPort == localPortIfConnectorClosed) {
+ int configuredLocalPort = connector.listenPort();
+ int localPortEphemeralPort = 0;
+ if (configuredLocalPort == localPortEphemeralPort) {
+ throw new IllegalStateException("Unable to determine connector's listen port");
+ }
+ return configuredLocalPort;
+ }
+ return actualLocalPort;
+ }
+
+}