Skip to content

Commit

Permalink
fix(http): disconnect active connections if call or bridge is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsChaceD authored Aug 18, 2023
1 parent 26f2ccd commit a1ed6cc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import android.Manifest;
import android.webkit.JavascriptInterface;
import com.getcapacitor.CapConfig;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginConfig;
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 = {
Expand All @@ -20,26 +24,52 @@
)
public class CapacitorHttp extends Plugin {

private Map<Runnable, PluginCall> 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<Runnable, PluginCall> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a1ed6cc

Please sign in to comment.