From a1ed6cc6f07465d683b95e3796d944f863a7b857 Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Fri, 18 Aug 2023 10:36:30 -0700 Subject: [PATCH] fix(http): disconnect active connections if call or bridge is destroyed --- .../getcapacitor/plugin/CapacitorHttp.java | 54 ++++++++++++++----- .../util/CapacitorHttpUrlConnection.java | 4 ++ .../plugin/util/HttpRequestHandler.java | 8 ++- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java index 9a11c2db3..7d07c984a 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java @@ -2,7 +2,6 @@ import android.Manifest; import android.webkit.JavascriptInterface; -import com.getcapacitor.CapConfig; import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; import com.getcapacitor.PluginCall; @@ -10,7 +9,12 @@ import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; import com.getcapacitor.annotation.Permission; +import com.getcapacitor.plugin.util.CapacitorHttpUrlConnection; import com.getcapacitor.plugin.util.HttpRequestHandler; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @CapacitorPlugin( permissions = { @@ -20,26 +24,52 @@ ) public class CapacitorHttp extends Plugin { + private Map activeRequests = new HashMap<>(); + private final ExecutorService executor = Executors.newCachedThreadPool(); + @Override public void load() { this.bridge.getWebView().addJavascriptInterface(this, "CapacitorHttpAndroidInterface"); super.load(); } - private void http(final PluginCall call, final String httpMethod) { - Runnable asyncHttpCall = new Runnable() { - @Override - public void run() { + @Override + protected void handleOnDestroy() { + super.handleOnDestroy(); + + for (Map.Entry entry : activeRequests.entrySet()) { + Runnable job = entry.getKey(); + PluginCall call = entry.getValue(); + + if (call.getData().has("activeCapacitorHttpUrlConnection")) { try { - JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge()); - call.resolve(response); - } catch (Exception e) { - call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e); - } + CapacitorHttpUrlConnection connection = (CapacitorHttpUrlConnection) call + .getData() + .get("activeCapacitorHttpUrlConnection"); + connection.disconnect(); + call.getData().remove("activeCapacitorHttpUrlConnection"); + } catch (Exception ignored) {} + } + + getBridge().releaseCall(call); + } + + activeRequests.clear(); + executor.shutdownNow(); + } + + private void http(final PluginCall call, final String httpMethod) { + Runnable asyncHttpCall = () -> { + try { + JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge()); + call.resolve(response); + } catch (Exception e) { + call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e); } }; - Thread httpThread = new Thread(asyncHttpCall); - httpThread.start(); + + activeRequests.put(asyncHttpCall, call); + executor.submit(asyncHttpCall); } @JavascriptInterface diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java index 35ab35e09..3f4ee8055 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java @@ -52,6 +52,10 @@ public HttpURLConnection getHttpConnection() { return connection; } + public void disconnect() { + connection.disconnect(); + } + /** * Set the value of the {@code allowUserInteraction} field of * this {@code URLConnection}. diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java index 8b06a2fd0..5077e9b85 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java @@ -411,9 +411,15 @@ public static JSObject request(PluginCall call, String httpMethod, Bridge bridge } } + call.getData().put("activeCapacitorHttpUrlConnection", connection); connection.connect(); - return buildResponse(connection, responseType); + JSObject response = buildResponse(connection, responseType); + + connection.disconnect(); + call.getData().remove("activeCapacitorHttpUrlConnection"); + + return response; } private static Boolean isDomainExcludedFromSSL(Bridge bridge, URL url) {