Browse Source

close SSO streams

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 5 years ago
parent
commit
3a9d9e8c23

+ 1 - 1
scripts/analysis/findbugs-results.txt

@@ -1 +1 @@
-384
+382

+ 12 - 6
src/main/java/com/nextcloud/android/sso/InputStreamBinder.java

@@ -143,7 +143,9 @@ public class InputStreamBinder extends IInputStreamService.Stub {
             InputStream exceptionStream = serializeObjectToInputStreamV2(exception, response.getPlainHeadersString());
             InputStream exceptionStream = serializeObjectToInputStreamV2(exception, response.getPlainHeadersString());
             InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, response.getBody());
             InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, response.getBody());
 
 
-            return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
+            return ParcelFileDescriptorUtil.pipeFrom(resultStream,
+                                                     thread -> Log.d(TAG, "Done sending result"),
+                                                     response.getMethod());
         } catch (IOException e) {
         } catch (IOException e) {
             Log_OC.e(TAG, "Error while sending response back to client app", e);
             Log_OC.e(TAG, "Error while sending response back to client app", e);
         }
         }
@@ -163,6 +165,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
         final InputStream requestBodyInputStream = requestBodyParcelFileDescriptor != null ?
         final InputStream requestBodyInputStream = requestBodyParcelFileDescriptor != null ?
             new ParcelFileDescriptor.AutoCloseInputStream(requestBodyParcelFileDescriptor) : null;
             new ParcelFileDescriptor.AutoCloseInputStream(requestBodyParcelFileDescriptor) : null;
         Exception exception = null;
         Exception exception = null;
+        HttpMethodBase httpMethod = null;
         InputStream httpStream = new InputStream() {
         InputStream httpStream = new InputStream() {
             @Override
             @Override
             public int read() {
             public int read() {
@@ -173,7 +176,8 @@ public class InputStreamBinder extends IInputStreamService.Stub {
         try {
         try {
             // Start request and catch exceptions
             // Start request and catch exceptions
             NextcloudRequest request = deserializeObjectAndCloseStream(is);
             NextcloudRequest request = deserializeObjectAndCloseStream(is);
-            httpStream = processRequest(request, requestBodyInputStream);
+            httpMethod = processRequest(request, requestBodyInputStream);
+            httpStream = httpMethod.getResponseBodyAsStream();
         } catch (Exception e) {
         } catch (Exception e) {
             Log_OC.e(TAG, "Error during Nextcloud request", e);
             Log_OC.e(TAG, "Error during Nextcloud request", e);
             exception = e;
             exception = e;
@@ -188,7 +192,9 @@ public class InputStreamBinder extends IInputStreamService.Stub {
             } else {
             } else {
                 resultStream = exceptionStream;
                 resultStream = exceptionStream;
             }
             }
-            return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
+            return ParcelFileDescriptorUtil.pipeFrom(resultStream,
+                                                     thread -> Log.d(TAG, "Done sending result"),
+                                                     httpMethod);
         } catch (IOException e) {
         } catch (IOException e) {
             Log_OC.e(TAG, "Error while sending response back to client app", e);
             Log_OC.e(TAG, "Error while sending response back to client app", e);
         }
         }
@@ -309,7 +315,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
         return method;
         return method;
     }
     }
 
 
-    private InputStream processRequest(final NextcloudRequest request, final InputStream requestBodyInputStream)
+    private HttpMethodBase processRequest(final NextcloudRequest request, final InputStream requestBodyInputStream)
         throws UnsupportedOperationException,
         throws UnsupportedOperationException,
         com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
         com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
         OperationCanceledException, AuthenticatorException, IOException {
         OperationCanceledException, AuthenticatorException, IOException {
@@ -354,7 +360,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
 
 
         // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
         // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
         if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
         if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
-            return method.getResponseBodyAsStream();
+            return method;
         } else {
         } else {
             StringBuilder total = new StringBuilder();
             StringBuilder total = new StringBuilder();
             InputStream inputStream = method.getResponseBodyAsStream();
             InputStream inputStream = method.getResponseBodyAsStream();
@@ -419,7 +425,7 @@ public class InputStreamBinder extends IInputStreamService.Stub {
 
 
         // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
         // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
         if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
         if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
-            return new Response(method.getResponseBodyAsStream(), method.getResponseHeaders());
+            return new Response(method);
         } else {
         } else {
             StringBuilder total = new StringBuilder();
             StringBuilder total = new StringBuilder();
             InputStream inputStream = method.getResponseBodyAsStream();
             InputStream inputStream = method.getResponseBodyAsStream();

+ 11 - 3
src/main/java/com/nextcloud/android/sso/Response.java

@@ -25,7 +25,9 @@ package com.nextcloud.android.sso;
 import com.google.gson.Gson;
 import com.google.gson.Gson;
 
 
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpMethodBase;
 
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
@@ -33,6 +35,7 @@ import java.util.List;
 public class Response {
 public class Response {
     private InputStream body;
     private InputStream body;
     private Header[] headers;
     private Header[] headers;
+    private HttpMethodBase method;
 
 
     public Response() {
     public Response() {
         headers = new Header[0];
         headers = new Header[0];
@@ -44,9 +47,10 @@ public class Response {
         };
         };
     }
     }
 
 
-    public Response(InputStream inputStream, Header[] headers) {
-        this.body = inputStream;
-        this.headers = headers;
+    public Response(HttpMethodBase methodBase) throws IOException {
+        this.method = methodBase;
+        this.body = methodBase.getResponseBodyAsStream();
+        this.headers = methodBase.getResponseHeaders();
     }
     }
 
 
     public String getPlainHeadersString() {
     public String getPlainHeadersString() {
@@ -63,4 +67,8 @@ public class Response {
     public InputStream getBody() {
     public InputStream getBody() {
         return this.body;
         return this.body;
     }
     }
+
+    public HttpMethodBase getMethod() {
+        return method;
+    }
 }
 }

+ 29 - 31
src/main/java/com/nextcloud/android/sso/aidl/ParcelFileDescriptorUtil.java

@@ -24,6 +24,8 @@ import android.util.Log;
 
 
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.common.utils.Log_OC;
 
 
+import org.apache.commons.httpclient.HttpMethodBase;
+
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStream;
@@ -32,45 +34,37 @@ public final class ParcelFileDescriptorUtil {
 
 
     private ParcelFileDescriptorUtil() { }
     private ParcelFileDescriptorUtil() { }
 
 
-    public static ParcelFileDescriptor pipeFrom(InputStream inputStream, IThreadListener listener)
+    public static ParcelFileDescriptor pipeFrom(InputStream inputStream,
+                                                IThreadListener listener,
+                                                HttpMethodBase method)
             throws IOException {
             throws IOException {
         ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
         ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
         ParcelFileDescriptor readSide = pipe[0];
         ParcelFileDescriptor readSide = pipe[0];
         ParcelFileDescriptor writeSide = pipe[1];
         ParcelFileDescriptor writeSide = pipe[1];
 
 
         // start the transfer thread
         // start the transfer thread
-        new TransferThread(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writeSide),
-                listener)
+        new TransferThread(inputStream,
+                           new ParcelFileDescriptor.AutoCloseOutputStream(writeSide),
+                           listener,
+                           method)
                 .start();
                 .start();
 
 
         return readSide;
         return readSide;
     }
     }
 
 
-    public static ParcelFileDescriptor pipeTo(OutputStream outputStream, IThreadListener listener)
-            throws IOException {
-        ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
-        ParcelFileDescriptor readSide = pipe[0];
-        ParcelFileDescriptor writeSide = pipe[1];
-
-        // start the transfer thread
-        new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(readSide), outputStream,
-                listener)
-                .start();
-
-        return writeSide;
-    }
-
     public static class TransferThread extends Thread {
     public static class TransferThread extends Thread {
         private static final String TAG = TransferThread.class.getCanonicalName();
         private static final String TAG = TransferThread.class.getCanonicalName();
-        private final InputStream mIn;
-        private final OutputStream mOut;
-        private final IThreadListener mListener;
+        private final InputStream inputStream;
+        private final OutputStream outputStream;
+        private final IThreadListener threadListener;
+        private final HttpMethodBase method;
 
 
-        TransferThread(InputStream in, OutputStream out, IThreadListener listener) {
+        TransferThread(InputStream in, OutputStream out, IThreadListener listener, HttpMethodBase method) {
             super("ParcelFileDescriptor Transfer Thread");
             super("ParcelFileDescriptor Transfer Thread");
-            mIn = in;
-            mOut = out;
-            mListener = listener;
+            inputStream = in;
+            outputStream = out;
+            threadListener = listener;
+            this.method = method;
             setDaemon(true);
             setDaemon(true);
         }
         }
 
 
@@ -80,26 +74,30 @@ public final class ParcelFileDescriptorUtil {
             int len;
             int len;
 
 
             try {
             try {
-                while ((len = mIn.read(buf)) > 0) {
-                    mOut.write(buf, 0, len);
+                while ((len = inputStream.read(buf)) > 0) {
+                    outputStream.write(buf, 0, len);
                 }
                 }
-                mOut.flush(); // just to be safe
+                outputStream.flush(); // just to be safe
             } catch (IOException e) {
             } catch (IOException e) {
                 Log.e("TransferThread", "writing failed");
                 Log.e("TransferThread", "writing failed");
             } finally {
             } finally {
                 try {
                 try {
-                    mIn.close();
+                    inputStream.close();
                 } catch (IOException e) {
                 } catch (IOException e) {
                     Log_OC.e(TAG, e.getMessage());
                     Log_OC.e(TAG, e.getMessage());
                 }
                 }
                 try {
                 try {
-                    mOut.close();
+                    outputStream.close();
                 } catch (IOException e) {
                 } catch (IOException e) {
                     Log_OC.e(TAG, e.getMessage());
                     Log_OC.e(TAG, e.getMessage());
                 }
                 }
             }
             }
-            if (mListener != null) {
-                mListener.onThreadFinished(this);
+            if (threadListener != null) {
+                threadListener.onThreadFinished(this);
+            }
+
+            if (method != null) {
+                method.releaseConnection();
             }
             }
         }
         }
     }
     }