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; |
23 import java.util.List; | 24 import java.util.List; |
24 | 25 |
25 import org.adblockplus.libadblockplus.AdblockPlusException; | 26 import org.adblockplus.libadblockplus.AdblockPlusException; |
26 import org.adblockplus.libadblockplus.HeaderEntry; | 27 import org.adblockplus.libadblockplus.HeaderEntry; |
27 import org.adblockplus.libadblockplus.ServerResponse; | 28 import org.adblockplus.libadblockplus.ServerResponse; |
28 import org.adblockplus.libadblockplus.WebRequest; | 29 import org.adblockplus.libadblockplus.WebRequest; |
29 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | 30 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; |
30 | 31 |
31 import android.util.Log; | 32 import android.util.Log; |
32 | 33 |
33 public class AndroidWebRequest extends WebRequest | 34 public class AndroidWebRequest extends WebRequest |
34 { | 35 { |
35 public final String TAG = Utils.getTag(WebRequest.class); | 36 public final String TAG = Utils.getTag(WebRequest.class); |
36 | 37 |
37 private static final int INITIAL_BUFFER_SIZE = 65536; | |
38 private static final int BUFFER_GROWTH_DELTA = 65536; | |
39 | |
40 @Override | 38 @Override |
41 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) | 39 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) |
42 { | 40 { |
43 try | 41 try |
44 { | 42 { |
45 final URL url = new URL(urlStr); | 43 final URL url = new URL(urlStr); |
46 Log.d(this.TAG, "Downloading from: " + url); | 44 Log.d(this.TAG, "Downloading from: " + url); |
47 | 45 |
48 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); | 46 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); |
49 connection.setRequestMethod("GET"); | 47 connection.setRequestMethod("GET"); |
50 connection.connect(); | 48 connection.connect(); |
51 | 49 |
52 final ServerResponse response = new ServerResponse(); | 50 final ServerResponse response = new ServerResponse(); |
53 response.setResponseStatus(connection.getResponseCode()); | 51 response.setResponseStatus(connection.getResponseCode()); |
54 | 52 |
55 if (response.getResponseStatus() == 200) | 53 if (response.getResponseStatus() == 200) |
56 { | 54 { |
57 final InputStream in = connection.getInputStream(); | 55 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8")); |
56 final StringBuilder sb = new StringBuilder(); | |
58 | 57 |
59 final byte[] buffer = new byte[4096]; | 58 String line; |
60 | 59 |
61 byte[] out = new byte[INITIAL_BUFFER_SIZE]; | 60 while ((line = reader.readLine()) != null) |
62 | |
63 int pos = 0; | |
64 for (;;) | |
65 { | 61 { |
66 final int read = in.read(buffer); | 62 // We're only appending non-element-hiding filters here. |
67 if (read < 0) | 63 // See: https://issues.adblockplus.org/ticket/303 |
64 if (line.indexOf('#') == -1) | |
Felix Dahlke
2014/10/01 15:34:12
This is quite problematic IMO, since we're not onl
René Jeschke
2014/10/01 15:37:48
1. I can check for .txt files, but I don't know if
| |
68 { | 65 { |
69 break; | 66 sb.append(line); |
67 sb.append('\n'); | |
70 } | 68 } |
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 } | 69 } |
80 | 70 |
81 connection.disconnect(); | 71 connection.disconnect(); |
82 | 72 |
83 response.setStatus(NsStatus.OK); | 73 response.setStatus(NsStatus.OK); |
84 response.setResponse(new String(out, 0, pos, "utf-8")); | 74 response.setResponse(sb.toString()); |
85 } | 75 } |
86 else | 76 else |
87 { | 77 { |
88 response.setStatus(NsStatus.ERROR_FAILURE); | 78 response.setStatus(NsStatus.ERROR_FAILURE); |
89 } | 79 } |
90 return response; | 80 return response; |
91 } | 81 } |
92 catch (final Throwable t) | 82 catch (final Throwable t) |
93 { | 83 { |
94 throw new AdblockPlusException("WebRequest failed", t); | 84 throw new AdblockPlusException("WebRequest failed", t); |
95 } | 85 } |
96 } | 86 } |
97 } | 87 } |
OLD | NEW |