Skip to content

Commit cc89198

Browse files
authored
Fix socket closed in Android. Revert changes from 745 (#760)
Issue: Socket is closed al llamar httpclient a partir de offline https://issues.genexus.com/viewissue.aspx?104236
1 parent 48dc702 commit cc89198

File tree

2 files changed

+69
-74
lines changed

2 files changed

+69
-74
lines changed

common/src/main/java/HTTPClient/HTTPConnection.java

+61-72
Original file line numberDiff line numberDiff line change
@@ -3323,11 +3323,7 @@ Response sendRequest(Request req, int con_timeout)
33233323
Log.write(Log.CONN, "Conn: Retrying request");
33243324
continue;
33253325
}
3326-
finally
3327-
{
3328-
if (sock != null) sock.close();
3329-
}
3330-
3326+
33313327
break;
33323328
}
33333329

@@ -3407,39 +3403,38 @@ Response sendRequest(Request req, int con_timeout)
34073403
private Socket getSocket(int con_timeout) throws IOException
34083404
{
34093405
Socket sock = null;
3410-
try {
34113406

3412-
String actual_host;
3413-
int actual_port;
3407+
String actual_host;
3408+
int actual_port;
34143409

3415-
if (Proxy_Host != null)
3416-
{
3417-
actual_host = Proxy_Host;
3418-
actual_port = Proxy_Port;
3419-
}
3420-
else
3421-
{
3422-
actual_host = Host;
3423-
actual_port = Port;
3424-
}
3410+
if (Proxy_Host != null)
3411+
{
3412+
actual_host = Proxy_Host;
3413+
actual_port = Proxy_Port;
3414+
}
3415+
else
3416+
{
3417+
actual_host = Host;
3418+
actual_port = Port;
3419+
}
34253420

3426-
Log.write(Log.CONN, "Conn: Creating Socket: " + actual_host + ":" +
3421+
Log.write(Log.CONN, "Conn: Creating Socket: " + actual_host + ":" +
34273422
actual_port);
34283423

3429-
if (con_timeout == 0) // normal connection establishment
3424+
if (con_timeout == 0) // normal connection establishment
3425+
{
3426+
if (Socks_client != null)
3427+
sock = Socks_client.getSocket(actual_host, actual_port);
3428+
else
34303429
{
3431-
if (Socks_client != null)
3432-
sock = Socks_client.getSocket(actual_host, actual_port);
3433-
else
3430+
// try all A records
3431+
InetAddress[] addr_list = InetAddress.getAllByName(actual_host);
3432+
for (int idx=0; idx<addr_list.length; idx++)
34343433
{
3435-
// try all A records
3436-
InetAddress[] addr_list = InetAddress.getAllByName(actual_host);
3437-
for (int idx=0; idx<addr_list.length; idx++)
3434+
try
34383435
{
3439-
try
3436+
if (Protocol == HTTPS)
34403437
{
3441-
if (Protocol == HTTPS)
3442-
{
34433438
//@gusbro
34443439
/* if (LocalAddr == null)
34453440
sock = new SSLSocket(addr_list[idx], actual_port,
@@ -3450,63 +3445,57 @@ private Socket getSocket(int con_timeout) throws IOException
34503445
ssl_ctxt);
34513446
((SSLSocket) sock).setAutoHandshake(false);
34523447
*/
3453-
if(LocalAddr == null)
3454-
sock = sslConnection.getSSLSocket(addr_list[idx], actual_port);
3455-
else sock = sslConnection.getSSLSocket(addr_list[idx], actual_port, LocalAddr, LocalPort);
3448+
if(LocalAddr == null)
3449+
sock = sslConnection.getSSLSocket(addr_list[idx], actual_port);
3450+
else sock = sslConnection.getSSLSocket(addr_list[idx], actual_port, LocalAddr, LocalPort);
34563451

3457-
//@iroqueta
3458-
sock.setTcpNoDelay(tcpNoDelay);
3452+
//@iroqueta
3453+
sock.setTcpNoDelay(tcpNoDelay);
34593454
//@gusbro\
3460-
}
3461-
else
3462-
{
3463-
if (LocalAddr == null)
3464-
sock = new Socket(addr_list[idx], actual_port);
3465-
else
3466-
sock = new Socket(addr_list[idx], actual_port,
3467-
LocalAddr, LocalPort);
3468-
//@iroqueta
3469-
sock.setTcpNoDelay(tcpNoDelay);
3470-
}
3471-
break; // success
34723455
}
3473-
catch (SocketException se)
3456+
else
34743457
{
3475-
if (idx == addr_list.length-1)
3476-
throw se; // we tried them all
3458+
if (LocalAddr == null)
3459+
sock = new Socket(addr_list[idx], actual_port);
3460+
else
3461+
sock = new Socket(addr_list[idx], actual_port,
3462+
LocalAddr, LocalPort);
3463+
//@iroqueta
3464+
sock.setTcpNoDelay(tcpNoDelay);
34773465
}
3466+
break; // success
3467+
}
3468+
catch (SocketException se)
3469+
{
3470+
if (idx == addr_list.length-1)
3471+
throw se; // we tried them all
34783472
}
34793473
}
34803474
}
3481-
else
3482-
{
3483-
EstablishConnection con =
3475+
}
3476+
else
3477+
{
3478+
EstablishConnection con =
34843479
new EstablishConnection(actual_host, actual_port, Socks_client);
3485-
//@iroqueta
3486-
con.setTcpNoDelay(tcpNoDelay);
3480+
//@iroqueta
3481+
con.setTcpNoDelay(tcpNoDelay);
34873482

3488-
con.start();
3489-
try
3490-
{ con.join((long) con_timeout); }
3491-
catch (InterruptedException ie)
3492-
{ }
3483+
con.start();
3484+
try
3485+
{ con.join((long) con_timeout); }
3486+
catch (InterruptedException ie)
3487+
{ }
34933488

3494-
if (con.getException() != null)
3495-
throw con.getException();
3489+
if (con.getException() != null)
3490+
throw con.getException();
3491+
if ((sock = con.getSocket()) == null)
3492+
{
3493+
con.forget();
34963494
if ((sock = con.getSocket()) == null)
3497-
{
3498-
con.forget();
3499-
if ((sock = con.getSocket()) == null)
3500-
throw new InterruptedIOException("Connection establishment timed out");
3501-
}
3495+
throw new InterruptedIOException("Connection establishment timed out");
35023496
}
3503-
3504-
return sock;
3505-
}
3506-
finally
3507-
{
3508-
if (sock != null) sock.close();
35093497
}
3498+
return sock;
35103499
}
35113500

35123501

common/src/main/java/HTTPClient/SocksClient.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,15 @@ Socket getSocket(String host, int port, InetAddress localAddr,
212212

213213
return sock;
214214
}
215-
finally
215+
catch (IOException ioe)
216216
{
217-
if (sock != null) sock.close();
217+
if (sock != null)
218+
{
219+
try { sock.close(); }
220+
catch (IOException ee) {}
221+
}
222+
223+
throw ioe;
218224
}
219225
}
220226

0 commit comments

Comments
 (0)