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: Created Nov. 6, 2014, 1:36 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 | « no previous file | 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 <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;
23 import java.util.List; 24 import java.util.List;
25 import java.util.Locale;
24 26
25 import org.adblockplus.libadblockplus.AdblockPlusException; 27 import org.adblockplus.libadblockplus.AdblockPlusException;
26 import org.adblockplus.libadblockplus.HeaderEntry; 28 import org.adblockplus.libadblockplus.HeaderEntry;
27 import org.adblockplus.libadblockplus.ServerResponse; 29 import org.adblockplus.libadblockplus.ServerResponse;
30 import org.adblockplus.libadblockplus.ServerResponse.NsStatus;
28 import org.adblockplus.libadblockplus.WebRequest; 31 import org.adblockplus.libadblockplus.WebRequest;
29 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; 32 import org.apache.commons.lang.StringUtils;
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 String TAG = Utils.getTag(WebRequest.class);
36 39
37 private static final int INITIAL_BUFFER_SIZE = 65536; 40 private static boolean mightBeFilterList(URL url)
38 private static final int BUFFER_GROWTH_DELTA = 65536; 41 {
42 String filename = url.getFile();
43
44 if (StringUtils.isEmpty(filename))
45 {
46 return false;
47 }
48
49 final int params = filename.indexOf('?');
50
51 if (params != -1)
52 {
53 filename = filename.substring(0, params);
Felix Dahlke 2014/11/11 09:15:37 Why not just use url.getPath() instead of manually
René Jeschke 2014/11/11 11:55:55 Right, done that.
54 }
55
56 return filename.toLowerCase(Locale.US).trim().endsWith(".txt");
Felix Dahlke 2014/11/11 09:15:37 I think I might have thought of a better way to de
René Jeschke 2014/11/11 11:55:55 I think that this is a bit much of an overhead for
Felix Dahlke 2014/11/11 16:18:30 It currently doesn't, but I don't think hard codin
57 }
39 58
40 @Override 59 @Override
41 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) 60 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders)
42 { 61 {
43 try 62 try
44 { 63 {
45 final URL url = new URL(urlStr); 64 final URL url = new URL(urlStr);
46 Log.d(this.TAG, "Downloading from: " + url); 65 Log.d(this.TAG, "Downloading from: " + url);
47 66
67 final boolean mightBeFilterList = mightBeFilterList(url);
68
48 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); 69 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n();
49 connection.setRequestMethod("GET"); 70 connection.setRequestMethod("GET");
50 connection.connect(); 71 connection.connect();
51 72
52 final ServerResponse response = new ServerResponse(); 73 final ServerResponse response = new ServerResponse();
53 response.setResponseStatus(connection.getResponseCode()); 74 response.setResponseStatus(connection.getResponseCode());
54 75
55 if (response.getResponseStatus() == 200) 76 if (response.getResponseStatus() == 200)
56 { 77 {
57 final InputStream in = connection.getInputStream(); 78 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8"));
79 final StringBuilder sb = new StringBuilder();
58 80
59 final byte[] buffer = new byte[4096]; 81 String line;
60 82 while ((line = reader.readLine()) != null)
Felix Dahlke 2014/11/11 09:15:37 Using BufferedReader instead of System.arraycopy s
René Jeschke 2014/11/11 11:55:55 Ok, we'll read the rest char-by-char.
61 byte[] out = new byte[INITIAL_BUFFER_SIZE];
62
63 int pos = 0;
64 for (;;)
65 { 83 {
66 final int read = in.read(buffer); 84 // We're only appending non-element-hiding filters here.
67 if (read < 0) 85 // See: https://issues.adblockplus.org/ticket/303
86 if (!mightBeFilterList || line.indexOf('#') == -1)
68 { 87 {
69 break; 88 sb.append(line);
89 sb.append('\n');
70 } 90 }
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 } 91 }
80
81 connection.disconnect(); 92 connection.disconnect();
82 93
83 response.setStatus(NsStatus.OK); 94 response.setStatus(NsStatus.OK);
84 response.setResponse(new String(out, 0, pos, "utf-8")); 95 response.setResponse(sb.toString());
85 } 96 }
86 else 97 else
87 { 98 {
88 response.setStatus(NsStatus.ERROR_FAILURE); 99 response.setStatus(NsStatus.ERROR_FAILURE);
89 } 100 }
90 return response; 101 return response;
91 } 102 }
92 catch (final Throwable t) 103 catch (final Throwable t)
93 { 104 {
94 throw new AdblockPlusException("WebRequest failed", t); 105 throw new AdblockPlusException("WebRequest failed", t);
95 } 106 }
96 } 107 }
97 } 108 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld