Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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.InputStream; | 20 import java.io.BufferedReader; |
21 import java.io.InputStreamReader; | |
21 import java.net.HttpURLConnection; | 22 import java.net.HttpURLConnection; |
22 import java.net.URL; | 23 import java.net.URL; |
24 import java.util.HashSet; | |
23 import java.util.List; | 25 import java.util.List; |
24 | 26 |
25 import org.adblockplus.libadblockplus.AdblockPlusException; | 27 import org.adblockplus.libadblockplus.AdblockPlusException; |
28 import org.adblockplus.libadblockplus.FilterEngine; | |
26 import org.adblockplus.libadblockplus.HeaderEntry; | 29 import org.adblockplus.libadblockplus.HeaderEntry; |
27 import org.adblockplus.libadblockplus.ServerResponse; | 30 import org.adblockplus.libadblockplus.ServerResponse; |
31 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | |
28 import org.adblockplus.libadblockplus.WebRequest; | 32 import org.adblockplus.libadblockplus.WebRequest; |
29 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | |
30 | 33 |
31 import android.util.Log; | 34 import android.util.Log; |
32 | 35 |
33 public class AndroidWebRequest extends WebRequest | 36 public class AndroidWebRequest extends WebRequest |
34 { | 37 { |
35 public final String TAG = Utils.getTag(WebRequest.class); | 38 public final static String TAG = Utils.getTag(WebRequest.class); |
36 | 39 |
37 private static final int INITIAL_BUFFER_SIZE = 65536; | 40 private final HashSet<String> subscriptionURLs = new HashSet<String>(); |
Felix Dahlke
2014/11/19 10:16:37
You're not using HashSet specific API, so why not
René Jeschke
2014/11/24 13:15:57
Because it it bloating the import statements above
Felix Dahlke
2014/11/27 05:41:57
Ah, fair enough. It's a bit unusual in Java-world
| |
38 private static final int BUFFER_GROWTH_DELTA = 65536; | 41 |
42 private boolean isListedSubscriptionURL(final URL url) | |
Felix Dahlke
2014/11/19 10:16:37
Seems we're usually capitalising acronyms as "Url"
René Jeschke
2014/11/24 13:15:57
Right, will change that.
| |
43 { | |
44 final String toCheck; | |
45 | |
46 if (url.getQuery() != null) | |
Felix Dahlke
2014/11/19 10:16:37
Why cut the query string off manually again? As I
René Jeschke
2014/11/24 13:15:57
'getPath() only returns the path, I need the full
| |
47 { | |
48 final String tmp = url.toString(); | |
49 final int idx = tmp.indexOf('?'); | |
50 toCheck = idx != -1 ? tmp.substring(0, idx) : tmp; | |
51 } | |
52 else | |
53 { | |
54 toCheck = url.toString(); | |
55 } | |
56 | |
57 return this.subscriptionURLs.contains(toCheck); | |
58 } | |
59 | |
60 protected void updateSubscriptionURLs(final FilterEngine engine) | |
61 { | |
62 for (final org.adblockplus.libadblockplus.Subscription s : engine.fetchAvail ableSubscriptions()) | |
Felix Dahlke
2014/11/19 10:16:37
fetchAvailableSubscriptions will show the list of
René Jeschke
2014/11/24 13:15:57
I really want to avoid connecting this hack with a
Felix Dahlke
2014/11/27 05:41:57
That's fair enough, yeah. In that case I suggest w
René Jeschke
2015/01/23 13:28:10
Done.
| |
63 { | |
64 this.subscriptionURLs.add(s.getProperty("url").toString()); | |
65 } | |
66 } | |
39 | 67 |
40 @Override | 68 @Override |
41 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) | 69 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) |
42 { | 70 { |
43 try | 71 try |
44 { | 72 { |
45 final URL url = new URL(urlStr); | 73 final URL url = new URL(urlStr); |
46 Log.d(this.TAG, "Downloading from: " + url); | 74 Log.d(TAG, "Downloading from: " + url); |
47 | 75 |
48 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); | 76 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); |
49 connection.setRequestMethod("GET"); | 77 connection.setRequestMethod("GET"); |
50 connection.connect(); | 78 connection.connect(); |
51 | 79 |
52 final ServerResponse response = new ServerResponse(); | 80 final ServerResponse response = new ServerResponse(); |
53 response.setResponseStatus(connection.getResponseCode()); | 81 response.setResponseStatus(connection.getResponseCode()); |
54 | 82 |
55 if (response.getResponseStatus() == 200) | 83 if (response.getResponseStatus() == 200) |
56 { | 84 { |
57 final InputStream in = connection.getInputStream(); | 85 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8")); |
86 final StringBuilder sb = new StringBuilder(); | |
58 | 87 |
59 final byte[] buffer = new byte[4096]; | 88 if (isListedSubscriptionURL(url)) |
89 { | |
90 Log.d(TAG, "Enabling elemhide removal."); | |
Felix Dahlke
2014/11/19 10:16:37
Maybe rather "Removing element hiding rules from "
René Jeschke
2014/11/24 13:15:57
I didn't want to spam the log output so I chose a
| |
60 | 91 |
61 byte[] out = new byte[INITIAL_BUFFER_SIZE]; | 92 String line; |
93 while ((line = reader.readLine()) != null) | |
94 { | |
95 // FIXME This is a hack! | |
Felix Dahlke
2014/11/19 10:16:37
Can we strike the "FIXME" part? :) We shouldn't ha
René Jeschke
2014/11/24 13:15:57
FIXME has the advantage that you get notified abou
Felix Dahlke
2014/11/27 05:41:57
Unless you have hundreds of FIXMEs and TODOs, whic
René Jeschke
2015/01/23 13:28:10
Ok, removed the 'FIXME', but left the rest of the
Felix Dahlke
2015/01/27 08:40:51
That's quite alright, I'm a fan of this comment. S
| |
96 // | |
97 // We're only appending non-element-hiding filters here. | |
98 // | |
99 // See: | |
100 // https://issues.adblockplus.org/ticket/303 | |
101 // | |
102 // Follow-up issue for removing this hack: | |
103 // https://issues.adblockplus.org/ticket/1541 | |
104 // | |
105 if (line.indexOf('#') == -1) | |
106 { | |
107 sb.append(line); | |
108 sb.append('\n'); | |
109 } | |
110 } | |
111 } | |
112 else | |
113 { | |
114 int character; | |
62 | 115 |
63 int pos = 0; | 116 while ((character = reader.read()) != -1) |
64 for (;;) | |
65 { | |
66 final int read = in.read(buffer); | |
67 if (read < 0) | |
68 { | 117 { |
69 break; | 118 sb.append((char) character); |
70 } | 119 } |
71 if (pos + read > out.length) | |
72 { | |
73 final byte[] old = out; | |
74 out = new byte[out.length + BUFFER_GROWTH_DELTA]; | |
75 System.arraycopy(old, 0, out, 0, pos); | |
76 } | |
77 System.arraycopy(buffer, 0, out, pos, read); | |
78 pos += read; | |
79 } | 120 } |
80 | 121 |
81 connection.disconnect(); | 122 connection.disconnect(); |
82 | 123 |
83 response.setStatus(NsStatus.OK); | 124 response.setStatus(NsStatus.OK); |
84 response.setResponse(new String(out, 0, pos, "utf-8")); | 125 response.setResponse(sb.toString()); |
85 } | 126 } |
86 else | 127 else |
87 { | 128 { |
88 response.setStatus(NsStatus.ERROR_FAILURE); | 129 response.setStatus(NsStatus.ERROR_FAILURE); |
89 } | 130 } |
90 return response; | 131 return response; |
91 } | 132 } |
92 catch (final Throwable t) | 133 catch (final Throwable t) |
93 { | 134 { |
94 throw new AdblockPlusException("WebRequest failed", t); | 135 throw new AdblockPlusException("WebRequest failed", t); |
95 } | 136 } |
96 } | 137 } |
97 } | 138 } |
OLD | NEW |