From: David A. Velasco Date: Wed, 18 Jul 2012 12:20:09 +0000 (+0200) Subject: Added timeout to the connection test; timeoutted methods granted for HTTPS X-Git-Tag: oc-android-1.4.3~265 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/cffdf90bf3e0ea45267c133e51d4d834aab8c8de Added timeout to the connection test; timeoutted methods granted for HTTPS --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9c4b2f94..20ba3781 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,7 +18,7 @@ --> + android:versionName="0.1.165B" xmlns:android="http://schemas.android.com/apk/res/android"> diff --git a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java index 62131eff..4e178849 100644 --- a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java +++ b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java @@ -19,6 +19,7 @@ package eu.alefzero.owncloud.authenticator; import java.net.ConnectException; +import java.net.SocketTimeoutException; import java.net.UnknownHostException; import javax.net.ssl.SSLHandshakeException; @@ -39,6 +40,10 @@ import android.os.Handler; import android.util.Log; public class ConnectionCheckerRunnable implements Runnable { + + /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */ + public static final int TRY_CONNECTION_TIMEOUT = 5000; + private static final String TAG = "ConnectionCheckerRunnable"; private OnConnectCheckListener mListener; private String mUrl; @@ -99,7 +104,7 @@ public class ConnectionCheckerRunnable implements Runnable { GetMethod get = new GetMethod(uri.toString()); boolean retval = false; try { - int status = wc.executeMethod(get); + int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT); switch (status) { case HttpStatus.SC_OK: { String response = get.getResponseBodyAsString(); @@ -127,7 +132,8 @@ public class ConnectionCheckerRunnable implements Runnable { } catch (Exception e) { if (e instanceof UnknownHostException - || e instanceof ConnectException) { + || e instanceof ConnectException + || e instanceof SocketTimeoutException) { mLatestResult = ResultType.HOST_NOT_AVAILABLE; } else if (e instanceof JSONException) { mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED; diff --git a/src/eu/alefzero/owncloud/authenticator/EasySSLSocketFactory.java b/src/eu/alefzero/owncloud/authenticator/EasySSLSocketFactory.java index c4531ba6..f1de8f5f 100644 --- a/src/eu/alefzero/owncloud/authenticator/EasySSLSocketFactory.java +++ b/src/eu/alefzero/owncloud/authenticator/EasySSLSocketFactory.java @@ -176,13 +176,16 @@ public class EasySSLSocketFactory implements ProtocolSocketFactory { int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { - return socketfactory.createSocket(host, port, localAddress, + Socket socket = socketfactory.createSocket(host, port, localAddress, localPort); + socket.setSoTimeout(params.getSoTimeout()); + return socket; } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); + socket.setSoTimeout(params.getSoTimeout()); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java index 63d11bf6..7aa5ffbc 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java @@ -758,6 +758,9 @@ public class FileDetailFragment extends SherlockFragment implements private class RemoveRunnable implements Runnable { + /** Arbitrary timeout for deletion */ + public final static int DELETION_TIMEOUT = 5000; + Account mAccount; OCFile mFileToRemove; Handler mHandler; @@ -777,13 +780,10 @@ public class FileDetailFragment extends SherlockFragment implements Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath())); DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath())); - HttpMethodParams params = delete.getParams(); - params.setSoTimeout(1000); - delete.setParams(params); boolean success = false; try { - int status = wc.executeMethod(delete); + int status = wc.executeMethod(delete, DELETION_TIMEOUT); if (delete.succeeded()) { FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); fdsm.removeFile(mFileToRemove); diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index e248eb84..44b6e6b5 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -24,7 +24,8 @@ import java.io.IOException; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpConnectionManager; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.UsernamePasswordCredentials; @@ -123,15 +124,12 @@ public class WebdavClient extends HttpClient { public boolean downloadFile(String remoteFilepath, File targetPath) { boolean ret = false; GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath)); - HttpMethodParams params = get.getParams(); - params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit - get.setParams(params); // get.setHeader("Host", mUri.getHost()); // get.setHeader("User-Agent", "Android-ownCloud"); try { - int status = executeMethod(get); + int status = executeMethod(get, 0); Log.e(TAG, "status return: " + status); if (status == HttpStatus.SC_OK) { targetPath.createNewFile(); @@ -197,12 +195,9 @@ public class WebdavClient extends HttpClient { entity.setOnDatatransferProgressListener(mDataTransferListener); Log.e("ASD", f.exists() + " " + entity.getContentLength()); PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget)); - HttpMethodParams params = put.getParams(); - params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit - put.setParams(params); put.setRequestEntity(entity); Log.d(TAG, "" + put.getURI().toString()); - int status = executeMethod(put); + int status = executeMethod(put, 0); Log.d(TAG, "PUT method return with status " + status); Log.i(TAG, "Uploading, done"); @@ -253,4 +248,31 @@ public class WebdavClient extends HttpClient { } return true; } + + + /** + * Requests the received method with the received timeout (milliseconds). + * + * Executes the method through the inherited HttpClient.executedMethod(method). + * + * Sets the socket timeout for the HttpMethodBase method received. + * + * @param method HTTP method request. + * @param timeout Timeout to set, in milliseconds; <= 0 means infinite. + */ + public int executeMethod(HttpMethodBase method, int readTimeout) throws HttpException, IOException { + int oldSoTimeout = getParams().getSoTimeout(); + try { + if (readTimeout < 0) { + readTimeout = 0; + } + HttpMethodParams params = method.getParams(); + params.setSoTimeout(readTimeout); + method.setParams(params); // this should be enough... + getParams().setSoTimeout(readTimeout); // ... but this is necessary for HTTPS + return executeMethod(method); + } finally { + getParams().setSoTimeout(oldSoTimeout); + } + } } diff --git a/src/eu/alefzero/webdav/WebdavUtils.java b/src/eu/alefzero/webdav/WebdavUtils.java index b6c0f2e7..a42b9024 100644 --- a/src/eu/alefzero/webdav/WebdavUtils.java +++ b/src/eu/alefzero/webdav/WebdavUtils.java @@ -23,8 +23,6 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; -import eu.alefzero.owncloud.datamodel.OCFile; - import android.net.Uri; public class WebdavUtils { @@ -74,4 +72,5 @@ public class WebdavUtils { encodedPath = "/" + encodedPath; return encodedPath; } + }