Bladeren bron

Added timeout to the connection test; timeoutted methods granted for HTTPS

David A. Velasco 13 jaren geleden
bovenliggende
commit
cffdf90bf3

+ 1 - 1
AndroidManifest.xml

@@ -18,7 +18,7 @@
  -->
 <manifest package="eu.alefzero.owncloud"
     android:versionCode="1"
-    android:versionName="0.1.164B" xmlns:android="http://schemas.android.com/apk/res/android">
+    android:versionName="0.1.165B" xmlns:android="http://schemas.android.com/apk/res/android">
 
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />

+ 8 - 2
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;

+ 4 - 1
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;

+ 4 - 4
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);

+ 31 - 9
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);
+        }
+    }
 }

+ 1 - 2
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;
     }
+    
 }