1717
1818import java .net .InetSocketAddress ;
1919import java .net .SocketAddress ;
20- import java .net .URI ;
21- import java .net .URISyntaxException ;
2220import java .util .ArrayList ;
2321import java .util .Arrays ;
2422import java .util .Collections ;
3836import io .netty .handler .codec .http .HttpHeaders ;
3937import io .netty .handler .codec .http .HttpMethod ;
4038import io .netty .handler .codec .http .HttpResponseStatus ;
41- import io .netty .handler .codec .http .HttpUtil ;
4239import io .netty .handler .codec .http .HttpVersion ;
4340import io .netty .handler .ssl .SslClosedEngineException ;
4441import io .netty .resolver .AddressResolverGroup ;
5552import reactor .netty .ConnectionObserver ;
5653import reactor .netty .NettyOutbound ;
5754import reactor .netty .channel .AbortedException ;
58- import reactor .netty .http .HttpOperations ;
5955import reactor .netty .http .HttpProtocol ;
6056import reactor .netty .tcp .TcpClientConfig ;
6157import reactor .netty .transport .AddressUtils ;
@@ -455,7 +451,6 @@ static final class HttpClientHandler extends SocketAddress
455451 final BiFunction <? super HttpClientRequest , ? super NettyOutbound , ? extends Publisher <Void >>
456452 handler ;
457453 final boolean compress ;
458- final UriEndpointFactory uriEndpointFactory ;
459454 final WebsocketClientSpec websocketClientSpec ;
460455 final BiPredicate <HttpClientRequest , HttpClientResponse >
461456 followRedirectPredicate ;
@@ -468,7 +463,6 @@ static final class HttpClientHandler extends SocketAddress
468463 final Duration responseTimeout ;
469464
470465 volatile UriEndpoint toURI ;
471- volatile String resourceUrl ;
472466 volatile UriEndpoint fromURI ;
473467 volatile Supplier <String >[] redirectedFrom ;
474468 volatile boolean shouldRetry ;
@@ -484,34 +478,10 @@ static final class HttpClientHandler extends SocketAddress
484478 this .proxyProvider = configuration .proxyProvider ();
485479 this .responseTimeout = configuration .responseTimeout ;
486480 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-
493481 this .websocketClientSpec = configuration .websocketClientSpec ;
494482 this .shouldRetry = !configuration .retryDisabled ;
495483 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 );
515485 }
516486
517487 @ Override
@@ -526,18 +496,16 @@ public SocketAddress get() {
526496
527497 Publisher <Void > requestWithBody (HttpClientOperations ch ) {
528498 try {
529- ch .resourceUrl = this .resourceUrl ;
499+ UriEndpoint uriEndpoint = toURI ;
500+ ch .uriEndpoint = uriEndpoint ;
530501 ch .responseTimeout = responseTimeout ;
531502
532- UriEndpoint uri = toURI ;
533503 HttpHeaders headers = ch .getNettyRequest ()
534- .setUri (uri . getPathAndQuery ())
504+ .setUri (uriEndpoint . getRawUri ())
535505 .setMethod (method )
536506 .setProtocolVersion (HttpVersion .HTTP_1_1 )
537507 .headers ();
538508
539- ch .path = HttpOperations .resolvePath (ch .uri ());
540-
541509 if (!defaultHeaders .isEmpty ()) {
542510 headers .set (defaultHeaders );
543511 }
@@ -546,9 +514,8 @@ Publisher<Void> requestWithBody(HttpClientOperations ch) {
546514 headers .set (HttpHeaderNames .USER_AGENT , USER_AGENT );
547515 }
548516
549- SocketAddress remoteAddress = uri .getRemoteAddress ();
550517 if (!headers .contains (HttpHeaderNames .HOST )) {
551- headers .set (HttpHeaderNames .HOST , resolveHostHeaderValue ( remoteAddress ));
518+ headers .set (HttpHeaderNames .HOST , uriEndpoint . getHostHeader ( ));
552519 }
553520
554521 if (!headers .contains (HttpHeaderNames .ACCEPT )) {
@@ -608,46 +575,11 @@ Publisher<Void> requestWithBody(HttpClientOperations ch) {
608575 }
609576 }
610577
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-
626578 void redirect (String to ) {
627579 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 );
651583 }
652584
653585 @ SuppressWarnings ({"unchecked" , "rawtypes" })
0 commit comments