From 35dc3d9df9b23dfaf6a4a26f7f82ad848f01c1fb Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 1 Oct 2017 01:12:45 +0800 Subject: [PATCH] Update WebService --- .../magisk/asyncs/DownloadBusybox.java | 11 ++- .../magisk/asyncs/ProcessRepoZip.java | 8 +- .../topjohnwu/magisk/utils/WebService.java | 73 +++++++++---------- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java index 211c97ffc..5856a1d63 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java @@ -11,7 +11,7 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; +import java.net.HttpURLConnection; public class DownloadBusybox extends ParallelTask { @@ -31,22 +31,21 @@ public class DownloadBusybox extends ParallelTask { Utils.removeItem(getShell(), context.getApplicationInfo().dataDir + "/busybox"); try { FileOutputStream out = new FileOutputStream(busybox); - InputStream in = WebService.request(WebService.GET, + HttpURLConnection conn = WebService.request( Build.SUPPORTED_32_BIT_ABIS[0].contains("x86") ? BUSYBOX_X86 : BUSYBOX_ARM, null ); - if (in == null) throw new IOException(); - BufferedInputStream bis = new BufferedInputStream(in); + if (conn == null) throw new IOException(); + BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); byte[] buffer = new byte[4096]; int len; while ((len = bis.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); - bis.close(); - + conn.disconnect(); } catch (IOException e) { e.printStackTrace(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index 477f6b6d7..67f4369c2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -23,6 +23,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.net.HttpURLConnection; public class ProcessRepoZip extends ParallelTask { @@ -60,9 +61,9 @@ public class ProcessRepoZip extends ParallelTask { try { // Request zip from Internet - InputStream in = WebService.request(WebService.GET, mLink, null); - if (in == null) return false; - in = new BufferedInputStream(in); + HttpURLConnection conn = WebService.request(mLink, null); + if (conn == null) return false; + InputStream in = new BufferedInputStream(conn.getInputStream()); // Temp files File temp1 = new File(activity.getCacheDir(), "1.zip"); @@ -72,6 +73,7 @@ public class ProcessRepoZip extends ParallelTask { // First remove top folder in Github source zip, Web -> temp1 ZipUtils.removeTopFolder(in, temp1); + conn.disconnect(); publishProgress(); // Then sign the zip for the first time, temp1 -> temp2 diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java index 9c5ef2dc5..db29f44af 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java @@ -2,57 +2,50 @@ package com.topjohnwu.magisk.utils; import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.Map; -import javax.net.ssl.HttpsURLConnection; - public class WebService { - public final static int GET = 1; - public final static int POST = 2; - public static String getString(String url) { return getString(url, null); } public static String getString(String url, Map header) { - InputStream in = request(GET, url, header); - if (in == null) return ""; - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - int len; - StringBuilder builder = new StringBuilder(); - char buf[] = new char[4096]; - try { - while ((len = br.read(buf)) != -1) { - builder.append(buf, 0, len); - } - in.close(); - } catch (IOException e) { - e.printStackTrace(); - } - return builder.toString(); + HttpURLConnection conn = request(url, header); + if (conn == null) return ""; + return getString(conn); } - public static InputStream request(int method, String address, Map header) { - Logger.dev("WebService: Service call " + address); + public static String getString(HttpURLConnection conn) { + try { + StringBuilder builder = new StringBuilder(); + if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + int len; + char buf[] = new char[4096]; + while ((len = br.read(buf)) != -1) { + builder.append(buf, 0, len); + } + } + conn.disconnect(); + return builder.toString(); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + public static HttpURLConnection request(String address, Map header) { try { URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(15000); conn.setConnectTimeout(15000); - conn.setDoInput(true); - - if (method == POST) { - conn.setRequestMethod("POST"); - } else if (method == GET) { - conn.setRequestMethod("GET"); - } if (header != null) { for (Map.Entry entry : header.entrySet()) { @@ -60,20 +53,20 @@ public class WebService { } } - if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) { - if (header != null) { - header.clear(); - for (Map.Entry> entry : conn.getHeaderFields().entrySet()) { - List l = entry.getValue(); - header.put(entry.getKey(), l.get(l.size() - 1)); - } + conn.connect(); + + if (header != null) { + header.clear(); + for (Map.Entry> entry : conn.getHeaderFields().entrySet()) { + List l = entry.getValue(); + header.put(entry.getKey(), l.get(l.size() - 1)); } - return conn.getInputStream(); } + + return conn; } catch (Exception e) { e.printStackTrace(); + return null; } - return null; } - } \ No newline at end of file