| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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.android; | 18 package org.adblockplus.android; |
| 19 | 19 |
| 20 import java.io.BufferedReader; | 20 import java.io.BufferedReader; |
| 21 import java.io.InputStreamReader; | 21 import java.io.InputStreamReader; |
| 22 import java.net.HttpURLConnection; | 22 import java.net.HttpURLConnection; |
| 23 import java.net.URL; | 23 import java.net.URL; |
| 24 import java.util.HashSet; |
| 24 import java.util.List; | 25 import java.util.List; |
| 25 import java.util.Locale; | |
| 26 | 26 |
| 27 import org.adblockplus.libadblockplus.AdblockPlusException; | 27 import org.adblockplus.libadblockplus.AdblockPlusException; |
| 28 import org.adblockplus.libadblockplus.FilterEngine; |
| 28 import org.adblockplus.libadblockplus.HeaderEntry; | 29 import org.adblockplus.libadblockplus.HeaderEntry; |
| 29 import org.adblockplus.libadblockplus.ServerResponse; | 30 import org.adblockplus.libadblockplus.ServerResponse; |
| 30 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | 31 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; |
| 31 import org.adblockplus.libadblockplus.WebRequest; | 32 import org.adblockplus.libadblockplus.WebRequest; |
| 32 import org.apache.commons.lang.StringUtils; | |
| 33 | 33 |
| 34 import android.util.Log; | 34 import android.util.Log; |
| 35 | 35 |
| 36 public class AndroidWebRequest extends WebRequest | 36 public class AndroidWebRequest extends WebRequest |
| 37 { | 37 { |
| 38 public final static String TAG = Utils.getTag(WebRequest.class); | 38 public final static String TAG = Utils.getTag(WebRequest.class); |
| 39 | 39 |
| 40 private static boolean mightBeFilterList(URL url) | 40 private final HashSet<String> subscriptionURLs = new HashSet<String>(); |
| 41 |
| 42 private boolean isListedSubscriptionUrl(final URL url) |
| 41 { | 43 { |
| 42 final String filename = url.getPath(); | 44 String toCheck = url.toString(); |
| 43 | 45 |
| 44 return StringUtils.isNotEmpty(filename) && filename.toLowerCase(Locale.US).t
rim().endsWith(".txt"); | 46 final int idx = toCheck.indexOf('?'); |
| 47 if (idx != -1) |
| 48 { |
| 49 toCheck = toCheck.substring(0, idx); |
| 50 } |
| 51 |
| 52 return this.subscriptionURLs.contains(toCheck); |
| 53 } |
| 54 |
| 55 protected void updateSubscriptionURLs(final FilterEngine engine) |
| 56 { |
| 57 for (final org.adblockplus.libadblockplus.Subscription s : engine.fetchAvail
ableSubscriptions()) |
| 58 { |
| 59 this.subscriptionURLs.add(s.getProperty("url").toString()); |
| 60 } |
| 61 this.subscriptionURLs.add(engine.getPref("subscriptions_exceptionsurl").toSt
ring()); |
| 45 } | 62 } |
| 46 | 63 |
| 47 @Override | 64 @Override |
| 48 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea
ders) | 65 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea
ders) |
| 49 { | 66 { |
| 50 try | 67 try |
| 51 { | 68 { |
| 52 final URL url = new URL(urlStr); | 69 final URL url = new URL(urlStr); |
| 53 Log.d(TAG, "Downloading from: " + url); | 70 Log.d(TAG, "Downloading from: " + url); |
| 54 | 71 |
| 55 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); | 72 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); |
| 56 connection.setRequestMethod("GET"); | 73 connection.setRequestMethod("GET"); |
| 57 connection.connect(); | 74 connection.connect(); |
| 58 | 75 |
| 59 final ServerResponse response = new ServerResponse(); | 76 final ServerResponse response = new ServerResponse(); |
| 60 response.setResponseStatus(connection.getResponseCode()); | 77 response.setResponseStatus(connection.getResponseCode()); |
| 61 | 78 |
| 62 if (response.getResponseStatus() == 200) | 79 if (response.getResponseStatus() == 200) |
| 63 { | 80 { |
| 64 final BufferedReader reader = new BufferedReader(new InputStreamReader(c
onnection.getInputStream(), "UTF-8")); | 81 final BufferedReader reader = new BufferedReader(new InputStreamReader(c
onnection.getInputStream(), "UTF-8")); |
| 65 final StringBuilder sb = new StringBuilder(); | 82 final StringBuilder sb = new StringBuilder(); |
| 66 | 83 |
| 67 if (mightBeFilterList(url)) | 84 if (isListedSubscriptionUrl(url)) |
| 68 { | 85 { |
| 86 Log.d(TAG, "Removing element hiding rules from: '" + url + "'"); |
| 87 |
| 69 String line; | 88 String line; |
| 70 while ((line = reader.readLine()) != null) | 89 while ((line = reader.readLine()) != null) |
| 71 { | 90 { |
| 72 // We're only appending non-element-hiding filters here. | 91 // We're only appending non-element-hiding filters here. |
| 73 // See: https://issues.adblockplus.org/ticket/303 | 92 // |
| 93 // See: |
| 94 // https://issues.adblockplus.org/ticket/303 |
| 95 // |
| 96 // Follow-up issue for removing this hack: |
| 97 // https://issues.adblockplus.org/ticket/1541 |
| 98 // |
| 74 if (line.indexOf('#') == -1) | 99 if (line.indexOf('#') == -1) |
| 75 { | 100 { |
| 76 sb.append(line); | 101 sb.append(line); |
| 77 sb.append('\n'); | 102 sb.append('\n'); |
| 78 } | 103 } |
| 79 } | 104 } |
| 80 } | 105 } |
| 81 else | 106 else |
| 82 { | 107 { |
| 83 int character; | 108 int character; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 98 response.setStatus(NsStatus.ERROR_FAILURE); | 123 response.setStatus(NsStatus.ERROR_FAILURE); |
| 99 } | 124 } |
| 100 return response; | 125 return response; |
| 101 } | 126 } |
| 102 catch (final Throwable t) | 127 catch (final Throwable t) |
| 103 { | 128 { |
| 104 throw new AdblockPlusException("WebRequest failed", t); | 129 throw new AdblockPlusException("WebRequest failed", t); |
| 105 } | 130 } |
| 106 } | 131 } |
| 107 } | 132 } |
| LEFT | RIGHT |