[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-2167?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17393054#comment-17393054
 ] 

neng xu commented on HTTPCLIENT-2167:
-------------------------------------

Your test is right.  My previous statement is not accurate. 
But the problem (always close socket)  I reported is still true. 

For this line "if (!LangUtils.equals(this.route, route) || 
!LangUtils.equals(this.state, state))"
First call
this.route=null, 
route={s}->https://ecpr-et.wellsfargo.com:4444,  
this.state=null, 
state=null.
This line return true, then it calls closeConnection(CloseMode.GRACEFUL);

Second call
this.route={s}->https://ecpr-et.wellsfargo.com:4444,
route={s}->https://ecpr-et.wellsfargo.com:4444,
this.state=X500Principal (id=79)
                thisX500Name=X500Name (id=84)
CN=ruvra74a0164.wellsfargo.com, OU=TESTAPP, OU=ECPR, OU=EBS, O=Wells Fargo, C=US
state=null.

This line return true, then it calls closeConnection(CloseMode.GRACEFUL);

Note: My testing uses  SSL Mutual Authentication. It seems that first call set 
this.state with the principal of my client SSL certificate. 
"!LangUtils.equals(this.state, state)" always return true because state is 
null.  

Here is the code I create BasicHttpClientConnectionManager. 

        private synchronized BasicHttpClientConnectionManager 
createConnectionManager(boolean verifyHostname, String trustStoreFile,
                        String trustStorePassword, String keyStoreFile, String 
keyStorePassword) throws KeyManagementException,
                        UnrecoverableKeyException, NoSuchAlgorithmException, 
KeyStoreException, CertificateException, IOException {
                final SSLContext sslcontext;
                if(keyStoreFile == null ||  keyStorePassword == null) { 
                        sslcontext= SSLContexts.custom()
                                        .loadTrustMaterial(new 
File(trustStoreFile), trustStorePassword.toCharArray())
                                        .build();
                } else {
                        sslcontext= SSLContexts.custom()
                                .loadTrustMaterial(new File(trustStoreFile), 
trustStorePassword.toCharArray())
                                .loadKeyMaterial(new File(keyStoreFile), 
keyStorePassword.toCharArray(), keyStorePassword.toCharArray())
                                .build();
                }
                final SSLConnectionSocketFactory sslSocketFactory;
                if (verifyHostname) {
                        sslSocketFactory = 
SSLConnectionSocketFactoryBuilder.create().setSslContext(sslcontext).build();
                } else {
                        sslSocketFactory = 
SSLConnectionSocketFactoryBuilder.create()
                                        
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSslContext(sslcontext).build();
                }
                final Registry<ConnectionSocketFactory> socketFactoryRegistry = 
RegistryBuilder
                                
.<ConnectionSocketFactory>create().register("https", sslSocketFactory)
                                .register("http", new 
PlainConnectionSocketFactory()).build();
                return new 
BasicHttpClientConnectionManager(socketFactoryRegistry);
        }


> BasicHttpClientConnectionManager always close the socket connection 
> --------------------------------------------------------------------
>
>                 Key: HTTPCLIENT-2167
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2167
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>          Components: HttpClient (classic)
>    Affects Versions: 5.1
>            Reporter: neng xu
>            Priority: Major
>             Fix For: 5.1.1
>
>
> Test Case:
> Created ClosableHttpClient with BasicHttpClientConnectionManager.
> Used the ClosableHttpClient object to do two post and all successful. 
> The problem is keepAlive does not work and two posts create two socket 
> connections.
> The second post does not reuse the socket created by first post .
>  
> Fist post 
> 2021-08-03 09:48:28,291 DEBUG 
> [org.apache.hc.client5.http.impl.classic.InternalHttpClient] ex-00000001: 
> preparing request execution
> .....
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager] 
> Connection can be kept alive for 50 SECONDS
>  response code: 200
> Second post
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.InternalHttpClient] ex-00000002: 
> preparing request execution
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.InternalHttpClient] ex-00000002: 
> preparing request execution
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.protocol.RequestAddCookies] Cookie spec selected: 
> strict
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.protocol.RequestAddCookies] Cookie spec selected: 
> strict
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.protocol.RequestAuthCache] Auth cache not set in 
> the context
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.protocol.RequestAuthCache] Auth cache not set in 
> the context
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.ProtocolExec] ex-00000002: target 
> auth state: UNC
> HALLENGED
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.ProtocolExec] ex-00000002: proxy 
> auth state: UNCH
> ALLENGED
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.ConnectExec] ex-00000002: acquiring 
> connection wi
> th route \{s}->https://ecpr-et.wellsfargo.com:4444
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.InternalHttpClient] ex-00000002: 
> acquiring endpoi
> nt (50 SECONDS)
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager] Get 
> connection for r
> oute \{s}->https://ecpr-et.wellsfargo.com:4444
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager] Closing 
> connection G
> RACEFUL
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.io.DefaultManagedHttpClientConnection] 
> http-outgoing-0: c
> lose connection GRACEFUL
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.InternalHttpClient] ex-00000002: 
> acquired endpoin
> t InternalConnectionEndpoint-58aa1698
> 2021-08-03 09:48:28,868 DEBUG 
> [org.apache.hc.client5.http.impl.classic.ConnectExec] ex-00000002: opening 
> connection \{s}-
> >[https://ecpr-et.wellsfargo.com:4444|https://ecpr-et.wellsfargo.com:4444/]
> ................
> Note: Above trace showed that second post will close previous socket when 
> "Get connection for route \{s}->https://ecpr-et.wellsfargo.com:4444";
> Did debug and saw the issue at follow method. of 
> BasicHttpClientConnectionManager class
> synchronized ManagedHttpClientConnection getConnection(final HttpRoute route, 
> final Object state) throws IOException {
> ..................
>  Asserts.check(!this.leased, "Connection is still allocated");
>  if (!LangUtils.equals(this.route, route) || !LangUtils.equals(this.state, 
> state)) {
>  closeConnection(CloseMode.GRACEFUL);
>  }
> ..................
>  return this.conn;
>  }
> Bug is at line: "if (!LangUtils.equals(this.route, route) || 
> !LangUtils.equals(this.state, state)) {"
> For stateless HTTP call, the state is always null, LangUtils.equal() always 
> returns false.
> That line is always true even first post and second post has same route.  
> Therefore, it always call "closeConnection(CloseMode.GRACEFUL);" to close the 
> previous socket connection although it meets keepAlive requirement. 
>  
>  
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to