-
Notifications
You must be signed in to change notification settings - Fork 974
/
ProxyHandler.java
894 lines (790 loc) · 40.8 KB
/
ProxyHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.server.handlers.proxy;
import io.undertow.UndertowLogger;
import io.undertow.UndertowOptions;
import io.undertow.attribute.ExchangeAttribute;
import io.undertow.attribute.ExchangeAttributes;
import io.undertow.client.ClientCallback;
import io.undertow.client.ClientConnection;
import io.undertow.client.ClientExchange;
import io.undertow.client.ClientRequest;
import io.undertow.client.ClientResponse;
import io.undertow.client.ContinueNotification;
import io.undertow.client.ProxiedRequestAttachments;
import io.undertow.client.PushCallback;
import io.undertow.io.IoCallback;
import io.undertow.io.Sender;
import io.undertow.server.ExchangeCompletionListener;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.HttpUpgradeListener;
import io.undertow.server.RenegotiationRequiredException;
import io.undertow.server.SSLSessionInfo;
import io.undertow.server.handlers.builder.HandlerBuilder;
import io.undertow.server.protocol.http.HttpAttachments;
import io.undertow.server.protocol.http.HttpContinue;
import io.undertow.util.Attachable;
import io.undertow.util.AttachmentKey;
import io.undertow.util.Certificates;
import io.undertow.util.CopyOnWriteMap;
import io.undertow.util.HeaderMap;
import io.undertow.util.HeaderValues;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import io.undertow.util.NetworkUtils;
import io.undertow.util.SameThreadExecutor;
import io.undertow.util.StatusCodes;
import io.undertow.util.Transfer;
import org.jboss.logging.Logger;
import org.xnio.ChannelExceptionHandler;
import org.xnio.ChannelListener;
import org.xnio.ChannelListeners;
import org.xnio.IoUtils;
import org.xnio.StreamConnection;
import org.xnio.XnioExecutor;
import org.xnio.channels.StreamSinkChannel;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.security.cert.CertificateEncodingException;
import javax.security.cert.X509Certificate;
import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* An HTTP handler which proxies content to a remote server.
* <p>
* This handler acts like a filter. The {@link ProxyClient} has a chance to decide if it
* knows how to proxy the request. If it does then it will provide a connection that can
* used to connect to the remote server, otherwise the next handler will be invoked and the
* request will proceed as normal.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class ProxyHandler implements HttpHandler {
private static final int DEFAULT_MAX_RETRY_ATTEMPTS = Integer.getInteger("io.undertow.server.handlers.proxy.maxRetries", 1);
private static final Logger log = Logger.getLogger(ProxyHandler.class.getPackage().getName());
public static final String UTF_8 = StandardCharsets.UTF_8.name();
private final ProxyClient proxyClient;
private final int maxRequestTime;
private static final AttachmentKey<ProxyConnection> CONNECTION = AttachmentKey.create(ProxyConnection.class);
private static final AttachmentKey<HttpServerExchange> EXCHANGE = AttachmentKey.create(HttpServerExchange.class);
private static final AttachmentKey<XnioExecutor.Key> TIMEOUT_KEY = AttachmentKey.create(XnioExecutor.Key.class);
/**
* Map of additional headers to add to the request.
*/
private final Map<HttpString, ExchangeAttribute> requestHeaders = new CopyOnWriteMap<>();
private final HttpHandler next;
private final boolean rewriteHostHeader;
private final boolean reuseXForwarded;
private final int maxConnectionRetries;
public ProxyHandler(ProxyClient proxyClient, int maxRequestTime, HttpHandler next) {
this(proxyClient, maxRequestTime, next, false, false);
}
/**
*
* @param proxyClient the client to use to make the proxy call
* @param maxRequestTime the maximum amount of time to allow the request to be processed
* @param next the next handler in line
* @param rewriteHostHeader should the HOST header be rewritten to use the target host of the call.
* @param reuseXForwarded should any existing X-Forwarded-For header be used or should it be overwritten.
*/
public ProxyHandler(ProxyClient proxyClient, int maxRequestTime, HttpHandler next, boolean rewriteHostHeader, boolean reuseXForwarded) {
this(proxyClient, maxRequestTime, next, rewriteHostHeader, reuseXForwarded, DEFAULT_MAX_RETRY_ATTEMPTS);
}
/**
* @param proxyClient the client to use to make the proxy call
* @param maxRequestTime the maximum amount of time to allow the request to be processed
* @param next the next handler in line
* @param rewriteHostHeader should the HOST header be rewritten to use the target host of the call.
* @param reuseXForwarded should any existing X-Forwarded-For header be used or should it be overwritten.
* @param maxConnectionRetries
*/
public ProxyHandler(ProxyClient proxyClient, int maxRequestTime, HttpHandler next, boolean rewriteHostHeader, boolean reuseXForwarded, int maxConnectionRetries) {
this.proxyClient = proxyClient;
this.maxRequestTime = maxRequestTime;
this.next = next;
this.rewriteHostHeader = rewriteHostHeader;
this.reuseXForwarded = reuseXForwarded;
this.maxConnectionRetries = maxConnectionRetries;
}
public ProxyHandler(ProxyClient proxyClient, HttpHandler next) {
this(proxyClient, -1, next);
}
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final ProxyClient.ProxyTarget target = proxyClient.findTarget(exchange);
if (target == null) {
log.debugf("No proxy target for request to %s", exchange.getRequestURL());
next.handleRequest(exchange);
return;
}
if(exchange.isResponseStarted()) {
//we can't proxy a request that has already started, this is basically a server configuration error
UndertowLogger.REQUEST_LOGGER.cannotProxyStartedRequest(exchange);
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
return;
}
final long timeout = maxRequestTime > 0 ? System.currentTimeMillis() + maxRequestTime : 0;
final ProxyClientHandler clientHandler = new ProxyClientHandler(exchange, target, timeout, maxConnectionRetries);
if (timeout > 0) {
final XnioExecutor.Key key = exchange.getIoThread().executeAfter(new Runnable() {
@Override
public void run() {
clientHandler.cancel(exchange);
}
}, maxRequestTime, TimeUnit.MILLISECONDS);
exchange.putAttachment(TIMEOUT_KEY, key);
exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {
@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
key.remove();
nextListener.proceed();
}
});
}
exchange.dispatch(exchange.isInIoThread() ? SameThreadExecutor.INSTANCE : exchange.getIoThread(), clientHandler);
}
/**
* Adds a request header to the outgoing request. If the header resolves to null or an empty string
* it will not be added, however any existing header with the same name will be removed.
*
* @param header The header name
* @param attribute The header value attribute.
* @return this
*/
public ProxyHandler addRequestHeader(final HttpString header, final ExchangeAttribute attribute) {
requestHeaders.put(header, attribute);
return this;
}
/**
* Adds a request header to the outgoing request. If the header resolves to null or an empty string
* it will not be added, however any existing header with the same name will be removed.
*
* @param header The header name
* @param value The header value attribute.
* @return this
*/
public ProxyHandler addRequestHeader(final HttpString header, final String value) {
requestHeaders.put(header, ExchangeAttributes.constant(value));
return this;
}
/**
* Adds a request header to the outgoing request. If the header resolves to null or an empty string
* it will not be added, however any existing header with the same name will be removed.
* <p>
* The attribute value will be parsed, and the resulting exchange attribute will be used to create the actual header
* value.
*
* @param header The header name
* @param attribute The header value attribute.
* @return this
*/
public ProxyHandler addRequestHeader(final HttpString header, final String attribute, final ClassLoader classLoader) {
requestHeaders.put(header, ExchangeAttributes.parser(classLoader).parse(attribute));
return this;
}
/**
* Removes a request header
*
* @param header the header
* @return this
*/
public ProxyHandler removeRequestHeader(final HttpString header) {
requestHeaders.remove(header);
return this;
}
static void copyHeaders(final HeaderMap to, final HeaderMap from) {
long f = from.fastIterateNonEmpty();
HeaderValues values;
while (f != -1L) {
values = from.fiCurrent(f);
if(!to.contains(values.getHeaderName())) {
//don't over write existing headers, normally the map will be empty, if it is not we assume it is not for a reason
to.putAll(values.getHeaderName(), values);
}
f = from.fiNextNonEmpty(f);
}
}
public ProxyClient getProxyClient() {
return proxyClient;
}
private final class ProxyClientHandler implements ProxyCallback<ProxyConnection>, Runnable {
private int tries;
private final long timeout;
private final int maxRetryAttempts;
private final HttpServerExchange exchange;
private ProxyClient.ProxyTarget target;
ProxyClientHandler(HttpServerExchange exchange, ProxyClient.ProxyTarget target, long timeout, int maxRetryAttempts) {
this.exchange = exchange;
this.timeout = timeout;
this.maxRetryAttempts = maxRetryAttempts;
this.target = target;
}
@Override
public void run() {
proxyClient.getConnection(target, exchange, this, -1, TimeUnit.MILLISECONDS);
}
@Override
public void completed(final HttpServerExchange exchange, final ProxyConnection connection) {
exchange.putAttachment(CONNECTION, connection);
exchange.dispatch(SameThreadExecutor.INSTANCE, new ProxyAction(connection, exchange, requestHeaders, rewriteHostHeader, reuseXForwarded));
}
@Override
public void failed(final HttpServerExchange exchange) {
final long time = System.currentTimeMillis();
if (tries++ < maxRetryAttempts) {
if (timeout > 0 && time > timeout) {
cancel(exchange);
} else {
target = proxyClient.findTarget(exchange);
if (target != null) {
final long remaining = timeout > 0 ? timeout - time : -1;
proxyClient.getConnection(target, exchange, this, remaining, TimeUnit.MILLISECONDS);
} else {
couldNotResolveBackend(exchange); // The context was registered when we started, so return 503
}
}
} else {
couldNotResolveBackend(exchange);
}
}
@Override
public void queuedRequestFailed(HttpServerExchange exchange) {
failed(exchange);
}
@Override
public void couldNotResolveBackend(HttpServerExchange exchange) {
if (exchange.isResponseStarted()) {
IoUtils.safeClose(exchange.getConnection());
} else {
exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
exchange.endExchange();
}
}
void cancel(final HttpServerExchange exchange) {
final ProxyConnection connectionAttachment = exchange.getAttachment(CONNECTION);
if (connectionAttachment != null) {
ClientConnection clientConnection = connectionAttachment.getConnection();
UndertowLogger.PROXY_REQUEST_LOGGER.timingOutRequest(clientConnection.getPeerAddress() + "" + exchange.getRequestURI());
IoUtils.safeClose(clientConnection);
} else {
UndertowLogger.PROXY_REQUEST_LOGGER.timingOutRequest(exchange.getRequestURI());
}
if (exchange.isResponseStarted()) {
IoUtils.safeClose(exchange.getConnection());
} else {
exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
exchange.endExchange();
}
}
}
private static class ProxyAction implements Runnable {
private final ProxyConnection clientConnection;
private final HttpServerExchange exchange;
private final Map<HttpString, ExchangeAttribute> requestHeaders;
private final boolean rewriteHostHeader;
private final boolean reuseXForwarded;
ProxyAction(final ProxyConnection clientConnection, final HttpServerExchange exchange, Map<HttpString, ExchangeAttribute> requestHeaders,
boolean rewriteHostHeader, boolean reuseXForwarded) {
this.clientConnection = clientConnection;
this.exchange = exchange;
this.requestHeaders = requestHeaders;
this.rewriteHostHeader = rewriteHostHeader;
this.reuseXForwarded = reuseXForwarded;
}
@Override
public void run() {
final ClientRequest request = new ClientRequest();
StringBuilder requestURI = new StringBuilder();
try {
if (exchange.getRelativePath().isEmpty()) {
requestURI.append(encodeUrlPart(clientConnection.getTargetPath(), exchange));
} else {
if (clientConnection.getTargetPath().endsWith("/")) {
requestURI.append(clientConnection.getTargetPath().substring(0, clientConnection.getTargetPath().length() - 1));
requestURI.append(encodeUrlPart(exchange.getRelativePath(), exchange));
} else {
requestURI = requestURI.append(clientConnection.getTargetPath());
requestURI.append(encodeUrlPart(exchange.getRelativePath(), exchange));
}
}
boolean first = true;
if (!exchange.getPathParameters().isEmpty()) {
requestURI.append(';');
for (Map.Entry<String, Deque<String>> entry : exchange.getPathParameters().entrySet()) {
if (first) {
first = false;
} else {
requestURI.append('&');
}
for (String val : entry.getValue()) {
requestURI.append(URLEncoder.encode(entry.getKey(), UTF_8));
requestURI.append('=');
requestURI.append(URLEncoder.encode(val, UTF_8));
}
}
}
String qs = exchange.getQueryString();
if (qs != null && !qs.isEmpty()) {
requestURI.append('?');
requestURI.append(qs);
}
} catch (UnsupportedEncodingException e) {
//impossible
UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
return;
}
request.setPath(requestURI.toString())
.setMethod(exchange.getRequestMethod());
final HeaderMap inboundRequestHeaders = exchange.getRequestHeaders();
final HeaderMap outboundRequestHeaders = request.getRequestHeaders();
copyHeaders(outboundRequestHeaders, inboundRequestHeaders);
if (!exchange.isPersistent()) {
//just because the client side is non-persistent
//we don't want to close the connection to the backend
outboundRequestHeaders.put(Headers.CONNECTION, "keep-alive");
}
if("h2c".equals(exchange.getRequestHeaders().getFirst(Headers.UPGRADE))) {
//we don't allow h2c upgrade requests to be passed through to the backend
exchange.getRequestHeaders().remove(Headers.UPGRADE);
outboundRequestHeaders.put(Headers.CONNECTION, "keep-alive");
}
for (Map.Entry<HttpString, ExchangeAttribute> entry : requestHeaders.entrySet()) {
String headerValue = entry.getValue().readAttribute(exchange);
if (headerValue == null || headerValue.isEmpty()) {
outboundRequestHeaders.remove(entry.getKey());
} else {
outboundRequestHeaders.put(entry.getKey(), headerValue.replace('\n', ' '));
}
}
final SocketAddress address = exchange.getConnection().getPeerAddress();
final String remoteHost = (address != null && address instanceof InetSocketAddress) ? ((InetSocketAddress) address).getHostString() : "localhost";
request.putAttachment(ProxiedRequestAttachments.REMOTE_HOST, remoteHost);
if (reuseXForwarded && request.getRequestHeaders().contains(Headers.X_FORWARDED_FOR)) {
// We have an existing header so we shall simply append the host to the existing list
final String current = request.getRequestHeaders().getFirst(Headers.X_FORWARDED_FOR);
if (current == null || current.isEmpty()) {
// It was empty so just add it
request.getRequestHeaders().put(Headers.X_FORWARDED_FOR, remoteHost);
}
else {
// Add the new entry and reset the existing header
request.getRequestHeaders().put(Headers.X_FORWARDED_FOR, current + "," + remoteHost);
}
}
else {
// No existing header or not allowed to reuse the header so set it here
request.getRequestHeaders().put(Headers.X_FORWARDED_FOR, remoteHost);
}
//if we don't support push set a header saying so
//this is non standard, and a problem with the HTTP2 spec, but they did not want to listen
if(!exchange.getConnection().isPushSupported() && clientConnection.getConnection().isPushSupported()) {
request.getRequestHeaders().put(Headers.X_DISABLE_PUSH, "true");
}
// Set the protocol header and attachment
if(reuseXForwarded && exchange.getRequestHeaders().contains(Headers.X_FORWARDED_PROTO)) {
final String proto = exchange.getRequestHeaders().getFirst(Headers.X_FORWARDED_PROTO);
request.putAttachment(ProxiedRequestAttachments.IS_SSL, proto.equals("https"));
} else {
final String proto = exchange.getRequestScheme().equals("https") ? "https" : "http";
request.getRequestHeaders().put(Headers.X_FORWARDED_PROTO, proto);
request.putAttachment(ProxiedRequestAttachments.IS_SSL, proto.equals("https"));
}
// Set the server name
if(reuseXForwarded && exchange.getRequestHeaders().contains(Headers.X_FORWARDED_SERVER)) {
final String hostName = exchange.getRequestHeaders().getFirst(Headers.X_FORWARDED_SERVER);
request.putAttachment(ProxiedRequestAttachments.SERVER_NAME, hostName);
} else {
final String hostName = exchange.getHostName();
request.getRequestHeaders().put(Headers.X_FORWARDED_SERVER, hostName);
request.putAttachment(ProxiedRequestAttachments.SERVER_NAME, hostName);
}
if(!exchange.getRequestHeaders().contains(Headers.X_FORWARDED_HOST)) {
final String hostName = exchange.getHostName();
if(hostName != null) {
request.getRequestHeaders().put(Headers.X_FORWARDED_HOST, NetworkUtils.formatPossibleIpv6Address(hostName));
}
}
// Set the port
if(reuseXForwarded && exchange.getRequestHeaders().contains(Headers.X_FORWARDED_PORT)) {
try {
int port = Integer.parseInt(exchange.getRequestHeaders().getFirst(Headers.X_FORWARDED_PORT));
request.putAttachment(ProxiedRequestAttachments.SERVER_PORT, port);
} catch (NumberFormatException e) {
int port = exchange.getConnection().getLocalAddress(InetSocketAddress.class).getPort();
request.getRequestHeaders().put(Headers.X_FORWARDED_PORT, port);
request.putAttachment(ProxiedRequestAttachments.SERVER_PORT, port);
}
} else {
int port = exchange.getHostPort();
request.getRequestHeaders().put(Headers.X_FORWARDED_PORT, port);
request.putAttachment(ProxiedRequestAttachments.SERVER_PORT, port);
}
SSLSessionInfo sslSessionInfo = exchange.getConnection().getSslSessionInfo();
if (sslSessionInfo != null) {
X509Certificate[] peerCertificates;
try {
peerCertificates = sslSessionInfo.getPeerCertificateChain();
if (peerCertificates.length > 0) {
request.putAttachment(ProxiedRequestAttachments.SSL_CERT, Certificates.toPem(peerCertificates[0]));
}
} catch (SSLPeerUnverifiedException | CertificateEncodingException | RenegotiationRequiredException e) {
//ignore
}
request.putAttachment(ProxiedRequestAttachments.SSL_CYPHER, sslSessionInfo.getCipherSuite());
request.putAttachment(ProxiedRequestAttachments.SSL_SESSION_ID, sslSessionInfo.getSessionId());
}
if(rewriteHostHeader) {
InetSocketAddress targetAddress = clientConnection.getConnection().getPeerAddress(InetSocketAddress.class);
request.getRequestHeaders().put(Headers.HOST, targetAddress.getHostString() + ":" + targetAddress.getPort());
request.getRequestHeaders().put(Headers.X_FORWARDED_HOST, exchange.getRequestHeaders().getFirst(Headers.HOST));
}
if(log.isDebugEnabled()) {
log.debugf("Sending request %s to target %s for exchange %s", request, remoteHost, exchange);
}
clientConnection.getConnection().sendRequest(request, new ClientCallback<ClientExchange>() {
@Override
public void completed(final ClientExchange result) {
if(log.isDebugEnabled()) {
log.debugf("Sent request %s to target %s for exchange %s", request, remoteHost, exchange);
}
result.putAttachment(EXCHANGE, exchange);
boolean requiresContinueResponse = HttpContinue.requiresContinueResponse(exchange);
if (requiresContinueResponse) {
result.setContinueHandler(new ContinueNotification() {
@Override
public void handleContinue(final ClientExchange clientExchange) {
if(log.isDebugEnabled()) {
log.debugf("Relieved continue response to request %s to target %s for exchange %s", request, remoteHost, exchange);
}
HttpContinue.sendContinueResponse(exchange, new IoCallback() {
@Override
public void onComplete(final HttpServerExchange exchange, final Sender sender) {
//don't care
}
@Override
public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
IoUtils.safeClose(clientConnection.getConnection());
exchange.endExchange();
UndertowLogger.REQUEST_IO_LOGGER.ioException(exception);
}
});
}
});
}
//handle server push
if(exchange.getConnection().isPushSupported() && result.getConnection().isPushSupported()) {
result.setPushHandler(new PushCallback() {
@Override
public boolean handlePush(ClientExchange originalRequest, final ClientExchange pushedRequest) {
if(log.isDebugEnabled()) {
log.debugf("Sending push request %s received from %s to target %s for exchange %s", pushedRequest.getRequest(), request, remoteHost, exchange);
}
final ClientRequest request = pushedRequest.getRequest();
exchange.getConnection().pushResource(request.getPath(), request.getMethod(), request.getRequestHeaders(), new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
String path = request.getPath();
int i = path.indexOf("?");
if(i > 0) {
path = path.substring(0, i);
}
exchange.dispatch(SameThreadExecutor.INSTANCE, new ProxyAction(new ProxyConnection(pushedRequest.getConnection(), path), exchange, requestHeaders, rewriteHostHeader, reuseXForwarded));
}
});
return true;
}
});
}
result.setResponseListener(new ResponseCallback(exchange));
final IoExceptionHandler handler = new IoExceptionHandler(exchange, clientConnection.getConnection());
if(requiresContinueResponse) {
try {
if(!result.getRequestChannel().flush()) {
result.getRequestChannel().getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {
@Override
public void handleEvent(StreamSinkChannel channel) {
Transfer.initiateTransfer(exchange.getRequestChannel(), result.getRequestChannel(), ChannelListeners.closingChannelListener(), new HTTPTrailerChannelListener(exchange, result, exchange), handler, handler, exchange.getConnection().getByteBufferPool());
}
}, handler));
result.getRequestChannel().resumeWrites();
return;
}
} catch (IOException e) {
handler.handleException(result.getRequestChannel(), e);
}
}
HTTPTrailerChannelListener trailerListener = new HTTPTrailerChannelListener(exchange, result, exchange);
if(!exchange.isRequestComplete()) {
Transfer.initiateTransfer(exchange.getRequestChannel(), result.getRequestChannel(), ChannelListeners.closingChannelListener(), trailerListener, handler, handler, exchange.getConnection().getByteBufferPool());
} else {
trailerListener.handleEvent(result.getRequestChannel());
}
}
@Override
public void failed(IOException e) {
handleFailure(exchange, e);
}
});
}
}
static void handleFailure(HttpServerExchange exchange, IOException e) {
UndertowLogger.PROXY_REQUEST_LOGGER.proxyRequestFailed(exchange.getRequestURI(), e);
if(exchange.isResponseStarted()) {
IoUtils.safeClose(exchange.getConnection());
} else {
exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
exchange.endExchange();
}
}
private static final class ResponseCallback implements ClientCallback<ClientExchange> {
private final HttpServerExchange exchange;
private ResponseCallback(HttpServerExchange exchange) {
this.exchange = exchange;
}
@Override
public void completed(final ClientExchange result) {
final ClientResponse response = result.getResponse();
if(log.isDebugEnabled()) {
log.debugf("Received response %s for request %s for exchange %s", response, result.getRequest(), exchange);
}
final HeaderMap inboundResponseHeaders = response.getResponseHeaders();
final HeaderMap outboundResponseHeaders = exchange.getResponseHeaders();
exchange.setStatusCode(response.getResponseCode());
copyHeaders(outboundResponseHeaders, inboundResponseHeaders);
if (exchange.isUpgrade()) {
exchange.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
if(log.isDebugEnabled()) {
log.debugf("Upgraded request %s to for exchange %s", result.getRequest(), exchange);
}
StreamConnection clientChannel = null;
try {
clientChannel = result.getConnection().performUpgrade();
final ClosingExceptionHandler handler = new ClosingExceptionHandler(streamConnection, clientChannel);
Transfer.initiateTransfer(clientChannel.getSourceChannel(), streamConnection.getSinkChannel(), ChannelListeners.closingChannelListener(), ChannelListeners.writeShutdownChannelListener(ChannelListeners.<StreamSinkChannel>flushingChannelListener(ChannelListeners.closingChannelListener(), ChannelListeners.closingChannelExceptionHandler()), ChannelListeners.closingChannelExceptionHandler()), handler, handler, result.getConnection().getBufferPool());
Transfer.initiateTransfer(streamConnection.getSourceChannel(), clientChannel.getSinkChannel(), ChannelListeners.closingChannelListener(), ChannelListeners.writeShutdownChannelListener(ChannelListeners.<StreamSinkChannel>flushingChannelListener(ChannelListeners.closingChannelListener(), ChannelListeners.closingChannelExceptionHandler()), ChannelListeners.closingChannelExceptionHandler()), handler, handler, result.getConnection().getBufferPool());
} catch (IOException e) {
IoUtils.safeClose(streamConnection, clientChannel);
}
}
});
}
final IoExceptionHandler handler = new IoExceptionHandler(exchange, result.getConnection());
Transfer.initiateTransfer(result.getResponseChannel(), exchange.getResponseChannel(), ChannelListeners.closingChannelListener(), new HTTPTrailerChannelListener(result, exchange, exchange), handler, handler, exchange.getConnection().getByteBufferPool());
}
@Override
public void failed(IOException e) {
handleFailure(exchange, e);
}
}
private static final class HTTPTrailerChannelListener implements ChannelListener<StreamSinkChannel> {
private final Attachable source;
private final Attachable target;
private final HttpServerExchange exchange;
private HTTPTrailerChannelListener(final Attachable source, final Attachable target, HttpServerExchange exchange) {
this.source = source;
this.target = target;
this.exchange = exchange;
}
@Override
public void handleEvent(final StreamSinkChannel channel) {
HeaderMap trailers = source.getAttachment(HttpAttachments.REQUEST_TRAILERS);
if (trailers != null) {
target.putAttachment(HttpAttachments.RESPONSE_TRAILERS, trailers);
}
try {
channel.shutdownWrites();
if (!channel.flush()) {
channel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {
@Override
public void handleEvent(StreamSinkChannel channel) {
channel.suspendWrites();
channel.getWriteSetter().set(null);
}
}, ChannelListeners.closingChannelExceptionHandler()));
channel.resumeWrites();
} else {
channel.getWriteSetter().set(null);
channel.shutdownWrites();
}
} catch (IOException e) {
handleFailure(exchange, e);
} catch (Exception e) {
handleFailure(exchange, new IOException(e));
}
}
}
private static final class IoExceptionHandler implements ChannelExceptionHandler<Channel> {
private final HttpServerExchange exchange;
private final ClientConnection clientConnection;
private IoExceptionHandler(HttpServerExchange exchange, ClientConnection clientConnection) {
this.exchange = exchange;
this.clientConnection = clientConnection;
}
@Override
public void handleException(Channel channel, IOException exception) {
IoUtils.safeClose(channel);
IoUtils.safeClose(clientConnection);
if (exchange.isResponseStarted()) {
UndertowLogger.REQUEST_IO_LOGGER.debug("Exception reading from target server", exception);
if (!exchange.isResponseStarted()) {
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
} else {
IoUtils.safeClose(exchange.getConnection());
}
} else {
UndertowLogger.REQUEST_IO_LOGGER.ioException(exception);
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
}
}
}
public int getMaxConnectionRetries() {
return maxConnectionRetries;
}
private static final class ClosingExceptionHandler implements ChannelExceptionHandler<Channel> {
private final Closeable[] toClose;
private ClosingExceptionHandler(Closeable... toClose) {
this.toClose = toClose;
}
@Override
public void handleException(Channel channel, IOException exception) {
IoUtils.safeClose(channel);
IoUtils.safeClose(toClose);
}
}
/**
* perform URL encoding
* <p/>
* TODO: this whole thing is kinda crappy.
*
* @return
*/
private static String encodeUrlPart(final String part, HttpServerExchange exchange) throws UnsupportedEncodingException {
//we need to go through and check part by part that a section does not need encoding
StringBuilder sb = null;
Charset charset = null;
for(int i = 0; i < part.length(); ++i) {
char c = part.charAt(i);
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
c == '.' || c == '-' || c == '*' || c == '_' || c == '/') {
if(sb != null) {
sb.append(c);
}
} else {
if(sb == null) {
sb = new StringBuilder(part.substring(0, i));
charset = Charset.forName(exchange.getConnection().getUndertowOptions().get(UndertowOptions.URL_CHARSET, UTF_8));
}
if(c < 127 && charset.name().equals(UTF_8)) {
//minor optimisation
sb.append('%');
sb.append(Integer.toHexString(c));
} else {
ByteBuffer bytes = charset.encode(Character.toString(c));
while (bytes.hasRemaining()) {
byte b = bytes.get();
sb.append('%');
sb.append(Integer.toHexString(b & 0xFF));
}
}
}
}
return sb == null ? part : sb.toString();
}
public static class Builder implements HandlerBuilder {
@Override
public String name() {
return "reverse-proxy";
}
@Override
public Map<String, Class<?>> parameters() {
Map<String, Class<?>> params = new HashMap<>();
params.put("hosts", String[].class);
params.put("rewrite-host-header", Boolean.class);
return params;
}
@Override
public Set<String> requiredParameters() {
return Collections.singleton("hosts");
}
@Override
public String defaultParameter() {
return "hosts";
}
@Override
public HandlerWrapper build(Map<String, Object> config) {
String[] hosts = (String[]) config.get("hosts");
List<URI> uris = new ArrayList<>();
for(String host : hosts) {
try {
uris.add(new URI(host));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
Boolean rewriteHostHeader = (Boolean) config.get("rewrite-host-header");
return new Wrapper(uris, rewriteHostHeader);
}
}
private static class Wrapper implements HandlerWrapper {
private final List<URI> uris;
private final boolean rewriteHostHeader;
private Wrapper(List<URI> uris, Boolean rewriteHostHeader) {
this.uris = uris;
this.rewriteHostHeader = rewriteHostHeader != null && rewriteHostHeader;
}
@Override
public HttpHandler wrap(HttpHandler handler) {
final ProxyClient proxyClient;
if (uris.size() == 1) {
proxyClient = new SimpleProxyClientProvider(uris.get(0));
} else {
final LoadBalancingProxyClient loadBalancingProxyClient = new LoadBalancingProxyClient();
for (URI url : uris) {
loadBalancingProxyClient.addHost(url);
}
proxyClient = loadBalancingProxyClient;
}
return new ProxyHandler(proxyClient, -1, handler, rewriteHostHeader, false);
}
}
}