Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/android/AndroidWebRequest.java

Issue 6680224334872576: Issue 303 - Don't load element hiding rules (Closed)
Patch Set: URL query removal Created Feb. 4, 2015, 12:08 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/org/adblockplus/android/ABPEngine.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2015 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.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>();
38 private static final int BUFFER_GROWTH_DELTA = 65536; 41
42 private boolean isListedSubscriptionUrl(final URL url)
43 {
44 String toCheck = url.toString();
45
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());
62 }
39 63
40 @Override 64 @Override
41 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) 65 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders)
42 { 66 {
43 try 67 try
44 { 68 {
45 final URL url = new URL(urlStr); 69 final URL url = new URL(urlStr);
46 Log.d(this.TAG, "Downloading from: " + url); 70 Log.d(TAG, "Downloading from: " + url);
47 71
48 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); 72 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n();
49 connection.setRequestMethod("GET"); 73 connection.setRequestMethod("GET");
50 connection.connect(); 74 connection.connect();
51 75
52 final ServerResponse response = new ServerResponse(); 76 final ServerResponse response = new ServerResponse();
53 response.setResponseStatus(connection.getResponseCode()); 77 response.setResponseStatus(connection.getResponseCode());
54 78
55 if (response.getResponseStatus() == 200) 79 if (response.getResponseStatus() == 200)
56 { 80 {
57 final InputStream in = connection.getInputStream(); 81 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8"));
82 final StringBuilder sb = new StringBuilder();
58 83
59 final byte[] buffer = new byte[4096]; 84 if (isListedSubscriptionUrl(url))
85 {
86 Log.d(TAG, "Removing element hiding rules from: '" + url + "'");
60 87
61 byte[] out = new byte[INITIAL_BUFFER_SIZE]; 88 String line;
89 while ((line = reader.readLine()) != null)
90 {
91 // We're only appending non-element-hiding filters here.
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 //
99 if (line.indexOf('#') == -1)
100 {
101 sb.append(line);
102 sb.append('\n');
103 }
104 }
105 }
106 else
107 {
108 int character;
62 109
63 int pos = 0; 110 while ((character = reader.read()) != -1)
64 for (;;)
65 {
66 final int read = in.read(buffer);
67 if (read < 0)
68 { 111 {
69 break; 112 sb.append((char) character);
70 } 113 }
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 } 114 }
80 115
81 connection.disconnect(); 116 connection.disconnect();
82 117
83 response.setStatus(NsStatus.OK); 118 response.setStatus(NsStatus.OK);
84 response.setResponse(new String(out, 0, pos, "utf-8")); 119 response.setResponse(sb.toString());
85 } 120 }
86 else 121 else
87 { 122 {
88 response.setStatus(NsStatus.ERROR_FAILURE); 123 response.setStatus(NsStatus.ERROR_FAILURE);
89 } 124 }
90 return response; 125 return response;
91 } 126 }
92 catch (final Throwable t) 127 catch (final Throwable t)
93 { 128 {
94 throw new AdblockPlusException("WebRequest failed", t); 129 throw new AdblockPlusException("WebRequest failed", t);
95 } 130 }
96 } 131 }
97 } 132 }
OLDNEW
« no previous file with comments | « src/org/adblockplus/android/ABPEngine.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld