| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 package org.adblockplus.libadblockplus.android; | 18 package org.adblockplus.libadblockplus.android; |
| 19 | 19 |
| 20 import java.io.BufferedReader; | 20 import java.io.BufferedReader; |
| 21 import java.io.InputStream; |
| 21 import java.io.InputStreamReader; | 22 import java.io.InputStreamReader; |
| 22 import java.net.HttpURLConnection; | 23 import java.net.HttpURLConnection; |
| 23 import java.net.URL; | 24 import java.net.URL; |
| 24 import java.util.HashSet; | 25 import java.util.HashSet; |
| 25 import java.util.LinkedList; | 26 import java.util.LinkedList; |
| 26 import java.util.List; | 27 import java.util.List; |
| 27 import java.util.Map; | 28 import java.util.Map; |
| 29 import java.util.zip.GZIPInputStream; |
| 28 | 30 |
| 29 import org.adblockplus.libadblockplus.AdblockPlusException; | 31 import org.adblockplus.libadblockplus.AdblockPlusException; |
| 30 import org.adblockplus.libadblockplus.FilterEngine; | 32 import org.adblockplus.libadblockplus.FilterEngine; |
| 31 import org.adblockplus.libadblockplus.HeaderEntry; | 33 import org.adblockplus.libadblockplus.HeaderEntry; |
| 32 import org.adblockplus.libadblockplus.ServerResponse; | 34 import org.adblockplus.libadblockplus.ServerResponse; |
| 33 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | 35 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; |
| 34 import org.adblockplus.libadblockplus.WebRequest; | 36 import org.adblockplus.libadblockplus.WebRequest; |
| 35 | 37 |
| 36 import android.util.Log; | 38 import android.util.Log; |
| 37 | 39 |
| 38 public class AndroidWebRequest extends WebRequest | 40 public class AndroidWebRequest extends WebRequest |
| 39 { | 41 { |
| 42 protected static final String ENCODING_GZIP = "gzip"; |
| 43 protected static final String ENCODING_IDENTITY = "identity"; |
| 44 |
| 40 public final static String TAG = Utils.getTag(WebRequest.class); | 45 public final static String TAG = Utils.getTag(WebRequest.class); |
| 41 | 46 |
| 42 private final HashSet<String> subscriptionURLs = new HashSet<String>(); | 47 private final HashSet<String> subscriptionURLs = new HashSet<String>(); |
| 43 private final boolean elemhideEnabled; | 48 private final boolean elemhideEnabled; |
| 49 private final boolean compressedStream; |
| 44 | 50 |
| 45 public AndroidWebRequest(boolean enableElemhide) | 51 /** |
| 52 * Ctor |
| 53 * @param enableElemhide Enable element hiding? |
| 54 * Element hiding requires significantly more memory |
| 55 * but allows better ad blocking |
| 56 * @param compressedStream Request for gzip compressed stream from the server |
| 57 */ |
| 58 public AndroidWebRequest(boolean enableElemhide, boolean compressedStream) |
| 46 { | 59 { |
| 47 this.elemhideEnabled = enableElemhide; | 60 this.elemhideEnabled = enableElemhide; |
| 61 this.compressedStream = compressedStream; |
| 48 } | 62 } |
| 49 | 63 |
| 50 public AndroidWebRequest() | 64 public AndroidWebRequest() |
| 51 { | 65 { |
| 52 this(false); | 66 this(false, true); |
| 53 } | 67 } |
| 54 | 68 |
| 55 private boolean isListedSubscriptionUrl(final URL url) | 69 private boolean isListedSubscriptionUrl(final URL url) |
| 56 { | 70 { |
| 57 String toCheck = url.toString(); | 71 String toCheck = url.toString(); |
| 58 | 72 |
| 59 final int idx = toCheck.indexOf('?'); | 73 final int idx = toCheck.indexOf('?'); |
| 60 if (idx != -1) | 74 if (idx != -1) |
| 61 { | 75 { |
| 62 toCheck = toCheck.substring(0, idx); | 76 toCheck = toCheck.substring(0, idx); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 77 @Override | 91 @Override |
| 78 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea
ders) | 92 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea
ders) |
| 79 { | 93 { |
| 80 try | 94 try |
| 81 { | 95 { |
| 82 final URL url = new URL(urlStr); | 96 final URL url = new URL(urlStr); |
| 83 Log.d(TAG, "Downloading from: " + url); | 97 Log.d(TAG, "Downloading from: " + url); |
| 84 | 98 |
| 85 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); | 99 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); |
| 86 connection.setRequestMethod("GET"); | 100 connection.setRequestMethod("GET"); |
| 101 connection.setRequestProperty("Accept-Encoding", |
| 102 (compressedStream ? ENCODING_GZIP : ENCODING_IDENTITY)); |
| 87 connection.connect(); | 103 connection.connect(); |
| 88 | 104 |
| 89 final ServerResponse response = new ServerResponse(); | 105 final ServerResponse response = new ServerResponse(); |
| 90 response.setResponseStatus(connection.getResponseCode()); | 106 response.setResponseStatus(connection.getResponseCode()); |
| 91 | 107 |
| 92 if (response.getResponseStatus() == 200) | 108 if (response.getResponseStatus() == 200) |
| 93 { | 109 { |
| 94 final BufferedReader reader = new BufferedReader(new InputStreamReader(c
onnection.getInputStream(), "UTF-8")); | 110 final InputStream inputStream = |
| 111 (compressedStream && ENCODING_GZIP.equals(connection.getContentEncodin
g()) |
| 112 ? new GZIPInputStream(connection.getInputStream()) |
| 113 : connection.getInputStream()); |
| 114 final BufferedReader reader = new BufferedReader(new InputStreamReader(i
nputStream, "UTF-8")); |
| 95 final StringBuilder sb = new StringBuilder(); | 115 final StringBuilder sb = new StringBuilder(); |
| 96 | 116 |
| 97 String line; | 117 String line; |
| 98 while ((line = reader.readLine()) != null) | 118 while ((line = reader.readLine()) != null) |
| 99 { | 119 { |
| 100 // We're only appending non-element-hiding filters here. | 120 // We're only appending non-element-hiding filters here. |
| 101 // | 121 // |
| 102 // See: | 122 // See: |
| 103 // https://issues.adblockplus.org/ticket/303 | 123 // https://issues.adblockplus.org/ticket/303 |
| 104 // | 124 // |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 Log.d(TAG, "Downloading finished"); | 160 Log.d(TAG, "Downloading finished"); |
| 141 return response; | 161 return response; |
| 142 } | 162 } |
| 143 catch (final Throwable t) | 163 catch (final Throwable t) |
| 144 { | 164 { |
| 145 Log.e(TAG, "WebRequest failed", t); | 165 Log.e(TAG, "WebRequest failed", t); |
| 146 throw new AdblockPlusException("WebRequest failed", t); | 166 throw new AdblockPlusException("WebRequest failed", t); |
| 147 } | 167 } |
| 148 } | 168 } |
| 149 } | 169 } |
| OLD | NEW |