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 | |
55 final boolean mightBeFilterList = mightBeFilterList(url); | |
56 | 71 |
57 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); | 72 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); |
58 connection.setRequestMethod("GET"); | 73 connection.setRequestMethod("GET"); |
59 connection.connect(); | 74 connection.connect(); |
60 | 75 |
61 final ServerResponse response = new ServerResponse(); | 76 final ServerResponse response = new ServerResponse(); |
62 response.setResponseStatus(connection.getResponseCode()); | 77 response.setResponseStatus(connection.getResponseCode()); |
63 | 78 |
64 if (response.getResponseStatus() == 200) | 79 if (response.getResponseStatus() == 200) |
65 { | 80 { |
66 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")); |
67 final StringBuilder sb = new StringBuilder(); | 82 final StringBuilder sb = new StringBuilder(); |
68 | 83 |
69 if (mightBeFilterList) | 84 if (isListedSubscriptionUrl(url)) |
70 { | 85 { |
| 86 Log.d(TAG, "Removing element hiding rules from: '" + url + "'"); |
| 87 |
71 String line; | 88 String line; |
72 while ((line = reader.readLine()) != null) | 89 while ((line = reader.readLine()) != null) |
73 { | 90 { |
74 // We're only appending non-element-hiding filters here. | 91 // We're only appending non-element-hiding filters here. |
75 // 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 // |
76 if (line.indexOf('#') == -1) | 99 if (line.indexOf('#') == -1) |
77 { | 100 { |
78 sb.append(line); | 101 sb.append(line); |
79 sb.append('\n'); | 102 sb.append('\n'); |
80 } | 103 } |
81 } | 104 } |
82 } | 105 } |
83 else | 106 else |
84 { | 107 { |
85 int character; | 108 int character; |
(...skipping 14 matching lines...) Expand all Loading... |
100 response.setStatus(NsStatus.ERROR_FAILURE); | 123 response.setStatus(NsStatus.ERROR_FAILURE); |
101 } | 124 } |
102 return response; | 125 return response; |
103 } | 126 } |
104 catch (final Throwable t) | 127 catch (final Throwable t) |
105 { | 128 { |
106 throw new AdblockPlusException("WebRequest failed", t); | 129 throw new AdblockPlusException("WebRequest failed", t); |
107 } | 130 } |
108 } | 131 } |
109 } | 132 } |
LEFT | RIGHT |