17
17
18
18
import java .net .InetSocketAddress ;
19
19
import java .net .SocketAddress ;
20
- import java .net .URI ;
21
- import java .net .URISyntaxException ;
22
20
import java .util .ArrayList ;
23
21
import java .util .Arrays ;
24
22
import java .util .Collections ;
38
36
import io .netty .handler .codec .http .HttpHeaders ;
39
37
import io .netty .handler .codec .http .HttpMethod ;
40
38
import io .netty .handler .codec .http .HttpResponseStatus ;
41
- import io .netty .handler .codec .http .HttpUtil ;
42
39
import io .netty .handler .codec .http .HttpVersion ;
43
40
import io .netty .handler .ssl .SslClosedEngineException ;
44
41
import io .netty .resolver .AddressResolverGroup ;
55
52
import reactor .netty .ConnectionObserver ;
56
53
import reactor .netty .NettyOutbound ;
57
54
import reactor .netty .channel .AbortedException ;
58
- import reactor .netty .http .HttpOperations ;
59
55
import reactor .netty .http .HttpProtocol ;
60
56
import reactor .netty .tcp .TcpClientConfig ;
61
57
import reactor .netty .transport .AddressUtils ;
@@ -455,7 +451,6 @@ static final class HttpClientHandler extends SocketAddress
455
451
final BiFunction <? super HttpClientRequest , ? super NettyOutbound , ? extends Publisher <Void >>
456
452
handler ;
457
453
final boolean compress ;
458
- final UriEndpointFactory uriEndpointFactory ;
459
454
final WebsocketClientSpec websocketClientSpec ;
460
455
final BiPredicate <HttpClientRequest , HttpClientResponse >
461
456
followRedirectPredicate ;
@@ -468,7 +463,6 @@ static final class HttpClientHandler extends SocketAddress
468
463
final Duration responseTimeout ;
469
464
470
465
volatile UriEndpoint toURI ;
471
- volatile String resourceUrl ;
472
466
volatile UriEndpoint fromURI ;
473
467
volatile Supplier <String >[] redirectedFrom ;
474
468
volatile boolean shouldRetry ;
@@ -484,34 +478,10 @@ static final class HttpClientHandler extends SocketAddress
484
478
this .proxyProvider = configuration .proxyProvider ();
485
479
this .responseTimeout = configuration .responseTimeout ;
486
480
this .defaultHeaders = configuration .headers ;
487
-
488
- String baseUrl = configuration .baseUrl ;
489
-
490
- this .uriEndpointFactory =
491
- new UriEndpointFactory (configuration .remoteAddress (), configuration .isSecure (), URI_ADDRESS_MAPPER );
492
-
493
481
this .websocketClientSpec = configuration .websocketClientSpec ;
494
482
this .shouldRetry = !configuration .retryDisabled ;
495
483
this .handler = configuration .body ;
496
-
497
- if (configuration .uri == null ) {
498
- String uri = configuration .uriStr ;
499
-
500
- uri = uri == null ? "/" : uri ;
501
-
502
- if (baseUrl != null && uri .startsWith ("/" )) {
503
- if (baseUrl .endsWith ("/" )) {
504
- baseUrl = baseUrl .substring (0 , baseUrl .length () - 1 );
505
- }
506
- uri = baseUrl + uri ;
507
- }
508
-
509
- this .toURI = uriEndpointFactory .createUriEndpoint (uri , configuration .websocketClientSpec != null );
510
- }
511
- else {
512
- this .toURI = uriEndpointFactory .createUriEndpoint (configuration .uri , configuration .websocketClientSpec != null );
513
- }
514
- this .resourceUrl = toURI .toExternalForm ();
484
+ this .toURI = UriEndpoint .create (configuration .uri , configuration .baseUrl , configuration .uriStr , configuration .remoteAddress (), configuration .isSecure (), configuration .websocketClientSpec != null );
515
485
}
516
486
517
487
@ Override
@@ -526,18 +496,16 @@ public SocketAddress get() {
526
496
527
497
Publisher <Void > requestWithBody (HttpClientOperations ch ) {
528
498
try {
529
- ch .resourceUrl = this .resourceUrl ;
499
+ UriEndpoint uriEndpoint = toURI ;
500
+ ch .uriEndpoint = uriEndpoint ;
530
501
ch .responseTimeout = responseTimeout ;
531
502
532
- UriEndpoint uri = toURI ;
533
503
HttpHeaders headers = ch .getNettyRequest ()
534
- .setUri (uri . getPathAndQuery ())
504
+ .setUri (uriEndpoint . getRawUri ())
535
505
.setMethod (method )
536
506
.setProtocolVersion (HttpVersion .HTTP_1_1 )
537
507
.headers ();
538
508
539
- ch .path = HttpOperations .resolvePath (ch .uri ());
540
-
541
509
if (!defaultHeaders .isEmpty ()) {
542
510
headers .set (defaultHeaders );
543
511
}
@@ -546,9 +514,8 @@ Publisher<Void> requestWithBody(HttpClientOperations ch) {
546
514
headers .set (HttpHeaderNames .USER_AGENT , USER_AGENT );
547
515
}
548
516
549
- SocketAddress remoteAddress = uri .getRemoteAddress ();
550
517
if (!headers .contains (HttpHeaderNames .HOST )) {
551
- headers .set (HttpHeaderNames .HOST , resolveHostHeaderValue ( remoteAddress ));
518
+ headers .set (HttpHeaderNames .HOST , uriEndpoint . getHostHeader ( ));
552
519
}
553
520
554
521
if (!headers .contains (HttpHeaderNames .ACCEPT )) {
@@ -608,46 +575,11 @@ Publisher<Void> requestWithBody(HttpClientOperations ch) {
608
575
}
609
576
}
610
577
611
- static String resolveHostHeaderValue (@ Nullable SocketAddress remoteAddress ) {
612
- if (remoteAddress instanceof InetSocketAddress ) {
613
- InetSocketAddress address = (InetSocketAddress ) remoteAddress ;
614
- String host = HttpUtil .formatHostnameForHttp (address );
615
- int port = address .getPort ();
616
- if (port != 80 && port != 443 ) {
617
- host = host + ':' + port ;
618
- }
619
- return host ;
620
- }
621
- else {
622
- return "localhost" ;
623
- }
624
- }
625
-
626
578
void redirect (String to ) {
627
579
Supplier <String >[] redirectedFrom = this .redirectedFrom ;
628
- UriEndpoint toURITemp ;
629
- UriEndpoint from = toURI ;
630
- SocketAddress address = from .getRemoteAddress ();
631
- if (address instanceof InetSocketAddress ) {
632
- try {
633
- URI redirectUri = new URI (to );
634
- if (!redirectUri .isAbsolute ()) {
635
- URI requestUri = new URI (resourceUrl );
636
- redirectUri = requestUri .resolve (redirectUri );
637
- }
638
- toURITemp = uriEndpointFactory .createUriEndpoint (redirectUri , from .isWs ());
639
- }
640
- catch (URISyntaxException e ) {
641
- throw new IllegalArgumentException ("Cannot resolve location header" , e );
642
- }
643
- }
644
- else {
645
- toURITemp = uriEndpointFactory .createUriEndpoint (from , to , () -> address );
646
- }
647
- fromURI = from ;
648
- toURI = toURITemp ;
649
- resourceUrl = toURITemp .toExternalForm ();
650
- this .redirectedFrom = addToRedirectedFromArray (redirectedFrom , from );
580
+ fromURI = toURI ;
581
+ toURI = toURI .redirect (to );
582
+ this .redirectedFrom = addToRedirectedFromArray (redirectedFrom , fromURI );
651
583
}
652
584
653
585
@ SuppressWarnings ({"unchecked" , "rawtypes" })
0 commit comments