47
47
import io .helidon .http .encoding .ContentEncodingContext ;
48
48
import io .helidon .webserver .CloseConnectionException ;
49
49
import io .helidon .webserver .ConnectionContext ;
50
+ import io .helidon .webserver .ProxyProtocolData ;
50
51
import io .helidon .webserver .http .DirectTransportRequest ;
51
52
import io .helidon .webserver .http .HttpRouting ;
52
53
import io .helidon .webserver .http1 .spi .Http1Upgrader ;
53
54
import io .helidon .webserver .spi .ServerConnection ;
54
55
56
+ import static io .helidon .http .HeaderNames .X_FORWARDED_FOR ;
57
+ import static io .helidon .http .HeaderNames .X_FORWARDED_PORT ;
55
58
import static io .helidon .http .HeaderNames .X_HELIDON_CN ;
56
59
import static java .lang .System .Logger .Level .TRACE ;
57
60
import static java .lang .System .Logger .Level .WARNING ;
@@ -128,6 +131,9 @@ public boolean canInterrupt() {
128
131
public void handle (Semaphore requestSemaphore ) throws InterruptedException {
129
132
this .myThread = Thread .currentThread ();
130
133
try {
134
+ // look for protocol data
135
+ ProxyProtocolData proxyProtocolData = ctx .proxyProtocolData ().orElse (null );
136
+
131
137
// handle connection until an exception (or explicit connection close)
132
138
while (canRun ) {
133
139
// prologue (first line of request)
@@ -145,6 +151,18 @@ public void handle(Semaphore requestSemaphore) throws InterruptedException {
145
151
.ifPresent (name -> headers .set (X_HELIDON_CN , name ));
146
152
recvListener .headers (ctx , headers );
147
153
154
+ // proxy protocol related headers X-Forwarded-For and X-Forwarded-Port
155
+ if (proxyProtocolData != null ) {
156
+ String sourceAddress = proxyProtocolData .sourceAddress ();
157
+ if (!sourceAddress .isEmpty ()) {
158
+ headers .add (X_FORWARDED_FOR , sourceAddress );
159
+ }
160
+ int sourcePort = proxyProtocolData .sourcePort ();
161
+ if (sourcePort != -1 ) {
162
+ headers .add (X_FORWARDED_PORT , sourcePort );
163
+ }
164
+ }
165
+
148
166
if (canUpgrade ) {
149
167
if (headers .contains (HeaderNames .UPGRADE )) {
150
168
Http1Upgrader upgrader = upgradeProviderMap .get (headers .get (HeaderNames .UPGRADE ).get ());
0 commit comments