| OLD | NEW |
| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 final URL url = new URL(urlStr); | 121 final URL url = new URL(urlStr); |
| 122 Log.d(TAG, "Downloading from: " + url); | 122 Log.d(TAG, "Downloading from: " + url); |
| 123 | 123 |
| 124 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); | 124 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); |
| 125 connection.setRequestMethod("GET"); | 125 connection.setRequestMethod("GET"); |
| 126 connection.setRequestProperty("Accept-Encoding", | 126 connection.setRequestProperty("Accept-Encoding", |
| 127 (compressedStream ? ENCODING_GZIP : ENCODING_IDENTITY)); | 127 (compressedStream ? ENCODING_GZIP : ENCODING_IDENTITY)); |
| 128 connection.connect(); | 128 connection.connect(); |
| 129 | 129 |
| 130 final ServerResponse response = new ServerResponse(); | 130 final ServerResponse response = new ServerResponse(); |
| 131 response.setResponseStatus(connection.getResponseCode()); | 131 try |
| 132 { |
| 133 response.setResponseStatus(connection.getResponseCode()); |
| 132 | 134 |
| 133 if (response.getResponseStatus() == 200) | 135 if (response.getResponseStatus() == 200) |
| 134 { | 136 { |
| 135 final InputStream inputStream = | 137 final InputStream inputStream = |
| 136 (compressedStream && ENCODING_GZIP.equals(connection.getContentEncodin
g()) | 138 (compressedStream && ENCODING_GZIP.equals(connection.getContentEncod
ing()) |
| 137 ? new GZIPInputStream(connection.getInputStream()) | 139 ? new GZIPInputStream(connection.getInputStream()) |
| 138 : connection.getInputStream()); | 140 : connection.getInputStream()); |
| 139 final BufferedReader reader = new BufferedReader(new InputStreamReader(i
nputStream, "UTF-8")); | 141 final BufferedReader reader = new BufferedReader(new InputStreamReader
(inputStream, "UTF-8")); |
| 140 final StringBuilder sb = new StringBuilder(); | 142 final StringBuilder sb = new StringBuilder(); |
| 141 | 143 |
| 142 String line; | 144 String line; |
| 143 try | |
| 144 { | |
| 145 while ((line = reader.readLine()) != null) | |
| 146 { | |
| 147 // We're only appending non-element-hiding filters here. | |
| 148 // | |
| 149 // See: | |
| 150 // https://issues.adblockplus.org/ticket/303 | |
| 151 // | |
| 152 // Follow-up issue for removing this hack: | |
| 153 // https://issues.adblockplus.org/ticket/1541 | |
| 154 // | |
| 155 if (this.elemhideEnabled || !isListedSubscriptionUrl(url) || line.in
dexOf('#') == -1) | |
| 156 { | |
| 157 sb.append(line); | |
| 158 sb.append('\n'); | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 finally | |
| 163 { | |
| 164 try | 145 try |
| 165 { | 146 { |
| 166 reader.close(); | 147 while ((line = reader.readLine()) != null) |
| 167 } | |
| 168 catch (IOException e) | |
| 169 { | |
| 170 // ignored | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 response.setStatus(NsStatus.OK); | |
| 175 response.setResponse(sb.toString()); | |
| 176 | |
| 177 if (connection.getHeaderFields().size() > 0) | |
| 178 { | |
| 179 List<HeaderEntry> responseHeaders = new LinkedList<HeaderEntry>(); | |
| 180 for (Map.Entry<String, List<String>> eachEntry : connection.getHeaderF
ields().entrySet()) | |
| 181 { | |
| 182 for (String eachValue : eachEntry.getValue()) | |
| 183 { | 148 { |
| 184 if (eachEntry.getKey() != null && eachValue != null) | 149 // We're only appending non-element-hiding filters here. |
| 150 // |
| 151 // See: |
| 152 // https://issues.adblockplus.org/ticket/303 |
| 153 // |
| 154 // Follow-up issue for removing this hack: |
| 155 // https://issues.adblockplus.org/ticket/1541 |
| 156 // |
| 157 if (this.elemhideEnabled || !isListedSubscriptionUrl(url) || line.
indexOf('#') == -1) |
| 185 { | 158 { |
| 186 responseHeaders.add(new HeaderEntry(eachEntry.getKey().toLowerCa
se(), eachValue)); | 159 sb.append(line); |
| 160 sb.append('\n'); |
| 187 } | 161 } |
| 188 } | 162 } |
| 189 } | 163 } |
| 190 response.setReponseHeaders(responseHeaders); | 164 finally |
| 165 { |
| 166 try |
| 167 { |
| 168 reader.close(); |
| 169 } |
| 170 catch (IOException e) |
| 171 { |
| 172 // ignored |
| 173 } |
| 174 } |
| 175 |
| 176 response.setStatus(NsStatus.OK); |
| 177 response.setResponse(sb.toString()); |
| 178 |
| 179 if (connection.getHeaderFields().size() > 0) |
| 180 { |
| 181 List<HeaderEntry> responseHeaders = new LinkedList<HeaderEntry>(); |
| 182 for (Map.Entry<String, List<String>> eachEntry : connection.getHeade
rFields().entrySet()) |
| 183 { |
| 184 for (String eachValue : eachEntry.getValue()) |
| 185 { |
| 186 if (eachEntry.getKey() != null && eachValue != null) |
| 187 { |
| 188 responseHeaders.add(new HeaderEntry(eachEntry.getKey().toLower
Case(), eachValue)); |
| 189 } |
| 190 } |
| 191 } |
| 192 response.setReponseHeaders(responseHeaders); |
| 193 } |
| 191 } | 194 } |
| 192 | 195 else |
| 196 { |
| 197 response.setStatus(NsStatus.ERROR_FAILURE); |
| 198 } |
| 199 } |
| 200 finally |
| 201 { |
| 193 connection.disconnect(); | 202 connection.disconnect(); |
| 194 } | 203 } |
| 195 else | |
| 196 { | |
| 197 response.setStatus(NsStatus.ERROR_FAILURE); | |
| 198 } | |
| 199 Log.d(TAG, "Downloading finished"); | 204 Log.d(TAG, "Downloading finished"); |
| 200 return response; | 205 return response; |
| 201 } | 206 } |
| 202 catch (final Throwable t) | 207 catch (final Throwable t) |
| 203 { | 208 { |
| 204 Log.e(TAG, "WebRequest failed", t); | 209 Log.e(TAG, "WebRequest failed", t); |
| 205 throw new AdblockPlusException("WebRequest failed", t); | 210 throw new AdblockPlusException("WebRequest failed", t); |
| 206 } | 211 } |
| 207 } | 212 } |
| 208 } | 213 } |
| OLD | NEW |