@@ -154,7 +154,7 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
154154 applicationProtocols = new List < SslApplicationProtocol > { applicationProtocol } ;
155155 }
156156
157- return GetServerConnection ( proxyServer , session , isConnect , applicationProtocols , noCache , cancellationToken ) ;
157+ return GetServerConnection ( proxyServer , session , isConnect , applicationProtocols , noCache , false , cancellationToken ) ! ;
158158 }
159159
160160 /// <summary>
@@ -165,10 +165,11 @@ internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer,
165165 /// <param name="isConnect">Is this a CONNECT request.</param>
166166 /// <param name="applicationProtocols"></param>
167167 /// <param name="noCache">if set to <c>true</c> [no cache].</param>
168+ /// <param name="prefetch">if set to <c>true</c> [prefetch].</param>
168169 /// <param name="cancellationToken">The cancellation token for this async task.</param>
169170 /// <returns></returns>
170- internal async Task < TcpServerConnection > GetServerConnection ( ProxyServer proxyServer , SessionEventArgsBase session , bool isConnect ,
171- List < SslApplicationProtocol > ? applicationProtocols , bool noCache , CancellationToken cancellationToken )
171+ internal async Task < TcpServerConnection ? > GetServerConnection ( ProxyServer proxyServer , SessionEventArgsBase session , bool isConnect ,
172+ List < SslApplicationProtocol > ? applicationProtocols , bool noCache , bool prefetch , CancellationToken cancellationToken )
172173 {
173174 var customUpStreamProxy = session . CustomUpStreamProxy ;
174175
@@ -208,7 +209,7 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer proxySe
208209 var upStreamEndPoint = session . HttpClient . UpStreamEndPoint ?? proxyServer . UpStreamEndPoint ;
209210 var upStreamProxy = customUpStreamProxy ?? ( isHttps ? proxyServer . UpStreamHttpsProxy : proxyServer . UpStreamHttpProxy ) ;
210211 return await GetServerConnection ( proxyServer , host , port , session . HttpClient . Request . HttpVersion , isHttps ,
211- applicationProtocols , isConnect , session , upStreamEndPoint , upStreamProxy , noCache , cancellationToken ) ;
212+ applicationProtocols , isConnect , session , upStreamEndPoint , upStreamProxy , noCache , prefetch , cancellationToken ) ;
212213 }
213214
214215 /// <summary>
@@ -225,12 +226,13 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer proxySe
225226 /// <param name="upStreamEndPoint">The local upstream endpoint to make request via.</param>
226227 /// <param name="externalProxy">The external proxy to make request via.</param>
227228 /// <param name="noCache">Not from cache/create new connection.</param>
229+ /// <param name="prefetch">if set to <c>true</c> [prefetch].</param>
228230 /// <param name="cancellationToken">The cancellation token for this async task.</param>
229231 /// <returns></returns>
230- internal async Task < TcpServerConnection > GetServerConnection ( ProxyServer proxyServer , string remoteHostName , int remotePort ,
232+ internal async Task < TcpServerConnection ? > GetServerConnection ( ProxyServer proxyServer , string remoteHostName , int remotePort ,
231233 Version httpVersion , bool isHttps , List < SslApplicationProtocol > ? applicationProtocols , bool isConnect ,
232234 SessionEventArgsBase sessionArgs , IPEndPoint ? upStreamEndPoint , IExternalProxy ? externalProxy ,
233- bool noCache , CancellationToken cancellationToken )
235+ bool noCache , bool prefetch , CancellationToken cancellationToken )
234236 {
235237 var sslProtocol = sessionArgs . ClientConnection . SslProtocol ;
236238 var cacheKey = GetConnectionCacheKey ( remoteHostName , remotePort ,
@@ -259,7 +261,7 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer proxySe
259261 }
260262
261263 var connection = await createServerConnection ( remoteHostName , remotePort , httpVersion , isHttps , sslProtocol ,
262- applicationProtocols , isConnect , proxyServer , sessionArgs , upStreamEndPoint , externalProxy , cacheKey , cancellationToken ) ;
264+ applicationProtocols , isConnect , proxyServer , sessionArgs , upStreamEndPoint , externalProxy , cacheKey , prefetch , cancellationToken ) ;
263265
264266 return connection ;
265267 }
@@ -279,12 +281,13 @@ internal async Task<TcpServerConnection> GetServerConnection(ProxyServer proxySe
279281 /// <param name="upStreamEndPoint">The local upstream endpoint to make request via.</param>
280282 /// <param name="externalProxy">The external proxy to make request via.</param>
281283 /// <param name="cacheKey">The connection cache key</param>
284+ /// <param name="prefetch">if set to <c>true</c> [prefetch].</param>
282285 /// <param name="cancellationToken">The cancellation token for this async task.</param>
283286 /// <returns></returns>
284- private async Task < TcpServerConnection > createServerConnection ( string remoteHostName , int remotePort ,
287+ private async Task < TcpServerConnection ? > createServerConnection ( string remoteHostName , int remotePort ,
285288 Version httpVersion , bool isHttps , SslProtocols sslProtocol , List < SslApplicationProtocol > ? applicationProtocols , bool isConnect ,
286289 ProxyServer proxyServer , SessionEventArgsBase sessionArgs , IPEndPoint ? upStreamEndPoint , IExternalProxy ? externalProxy , string cacheKey ,
287- CancellationToken cancellationToken )
290+ bool prefetch , CancellationToken cancellationToken )
288291 {
289292 // deny connection to proxy end points to avoid infinite connection loop.
290293 if ( Server . ProxyEndPoints . Any ( x => x . Port == remotePort )
@@ -350,6 +353,11 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
350353 var ipAddresses = await Dns . GetHostAddressesAsync ( hostname ) ;
351354 if ( ipAddresses == null || ipAddresses . Length == 0 )
352355 {
356+ if ( prefetch )
357+ {
358+ return null ;
359+ }
360+
353361 throw new Exception ( $ "Could not resolve the hostname { hostname } ") ;
354362 }
355363
@@ -486,10 +494,15 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
486494 {
487495 sessionArgs . CustomUpStreamProxyUsed = newUpstreamProxy ;
488496 sessionArgs . TimeLine [ "Retrying Upstream Proxy Connection" ] = DateTime . UtcNow ;
489- return await createServerConnection ( remoteHostName , remotePort , httpVersion , isHttps , sslProtocol , applicationProtocols , isConnect , proxyServer , sessionArgs , upStreamEndPoint , externalProxy , cacheKey , cancellationToken ) ;
497+ return await createServerConnection ( remoteHostName , remotePort , httpVersion , isHttps , sslProtocol , applicationProtocols , isConnect , proxyServer , sessionArgs , upStreamEndPoint , externalProxy , cacheKey , prefetch , cancellationToken ) ;
490498 }
491499 }
492500
501+ if ( prefetch )
502+ {
503+ return null ;
504+ }
505+
493506 throw new Exception ( $ "Could not establish connection to { hostname } ", lastException ) ;
494507 }
495508
@@ -502,7 +515,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
502515
503516 stream = new HttpServerStream ( new NetworkStream ( tcpServerSocket , true ) , proxyServer . BufferPool , cancellationToken ) ;
504517
505- if ( ( externalProxy != null && externalProxy . ProxyType == ExternalProxyType . Http ) && ( isConnect || isHttps ) )
518+ if ( externalProxy != null && externalProxy . ProxyType == ExternalProxyType . Http && ( isConnect || isHttps ) )
506519 {
507520 var authority = $ "{ remoteHostName } :{ remotePort } ". GetByteString ( ) ;
508521 var connectRequest = new ConnectRequest ( authority )
@@ -658,7 +671,7 @@ internal async Task Release(TcpServerConnection connection, bool close = false)
658671 }
659672 }
660673
661- internal async Task Release ( Task < TcpServerConnection > ? connectionCreateTask , bool closeServerConnection )
674+ internal async Task Release ( Task < TcpServerConnection ? > ? connectionCreateTask , bool closeServerConnection )
662675 {
663676 if ( connectionCreateTask == null )
664677 {
0 commit comments