| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 /* | 1 /* | 
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 3 * Copyright (C) 2006-2014 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.BufferedReader; | 20 import java.io.BufferedReader; | 
| 21 import java.io.InputStreamReader; | 21 import java.io.InputStreamReader; | 
| 22 import java.net.HttpURLConnection; | 22 import java.net.HttpURLConnection; | 
| 23 import java.net.URL; | 23 import java.net.URL; | 
| 24 import java.util.HashSet; | |
| 24 import java.util.List; | 25 import java.util.List; | 
| 25 import java.util.Locale; | |
| 26 | 26 | 
| 27 import org.adblockplus.libadblockplus.AdblockPlusException; | 27 import org.adblockplus.libadblockplus.AdblockPlusException; | 
| 28 import org.adblockplus.libadblockplus.FilterEngine; | |
| 28 import org.adblockplus.libadblockplus.HeaderEntry; | 29 import org.adblockplus.libadblockplus.HeaderEntry; | 
| 29 import org.adblockplus.libadblockplus.ServerResponse; | 30 import org.adblockplus.libadblockplus.ServerResponse; | 
| 30 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | 31 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; | 
| 31 import org.adblockplus.libadblockplus.WebRequest; | 32 import org.adblockplus.libadblockplus.WebRequest; | 
| 32 import org.apache.commons.lang.StringUtils; | |
| 33 | 33 | 
| 34 import android.util.Log; | 34 import android.util.Log; | 
| 35 | 35 | 
| 36 public class AndroidWebRequest extends WebRequest | 36 public class AndroidWebRequest extends WebRequest | 
| 37 { | 37 { | 
| 38 public final String TAG = Utils.getTag(WebRequest.class); | 38 public final static String TAG = Utils.getTag(WebRequest.class); | 
| 39 | 39 | 
| 40 private static boolean mightBeFilterList(URL url) | 40 private final HashSet<String> subscriptionURLs = new HashSet<String>(); | 
| 41 | |
| 42 private boolean isListedSubscriptionUrl(final URL url) | |
| 41 { | 43 { | 
| 42 String filename = url.getFile(); | 44 String toCheck = url.toString(); | 
| 43 | 45 | 
| 44 if (StringUtils.isEmpty(filename)) | 46 final int idx = toCheck.indexOf('?'); | 
| 47 if (idx != -1) | |
| 45 { | 48 { | 
| 46 return false; | 49 toCheck = toCheck.substring(0, idx); | 
| 47 } | 50 } | 
| 48 | 51 | 
| 49 final int params = filename.indexOf('?'); | 52 return this.subscriptionURLs.contains(toCheck); | 
| 53 } | |
| 50 | 54 | 
| 51 if (params != -1) | 55 protected void updateSubscriptionURLs(final FilterEngine engine) | 
| 56 { | |
| 57 for (final org.adblockplus.libadblockplus.Subscription s : engine.fetchAvail ableSubscriptions()) | |
| 52 { | 58 { | 
| 53 filename = filename.substring(0, params); | 59 this.subscriptionURLs.add(s.getProperty("url").toString()); | 
| 
 
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 } | 60 } | 
| 55 | 61 this.subscriptionURLs.add(engine.getPref("subscriptions_exceptionsurl").toSt ring()); | 
| 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 } | 62 } | 
| 58 | 63 | 
| 59 @Override | 64 @Override | 
| 60 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) | 65 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders) | 
| 61 { | 66 { | 
| 62 try | 67 try | 
| 63 { | 68 { | 
| 64 final URL url = new URL(urlStr); | 69 final URL url = new URL(urlStr); | 
| 65 Log.d(this.TAG, "Downloading from: " + url); | 70 Log.d(TAG, "Downloading from: " + url); | 
| 66 | |
| 67 final boolean mightBeFilterList = mightBeFilterList(url); | |
| 68 | 71 | 
| 69 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); | 72 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n(); | 
| 70 connection.setRequestMethod("GET"); | 73 connection.setRequestMethod("GET"); | 
| 71 connection.connect(); | 74 connection.connect(); | 
| 72 | 75 | 
| 73 final ServerResponse response = new ServerResponse(); | 76 final ServerResponse response = new ServerResponse(); | 
| 74 response.setResponseStatus(connection.getResponseCode()); | 77 response.setResponseStatus(connection.getResponseCode()); | 
| 75 | 78 | 
| 76 if (response.getResponseStatus() == 200) | 79 if (response.getResponseStatus() == 200) | 
| 77 { | 80 { | 
| 78 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8")); | 81 final BufferedReader reader = new BufferedReader(new InputStreamReader(c onnection.getInputStream(), "UTF-8")); | 
| 79 final StringBuilder sb = new StringBuilder(); | 82 final StringBuilder sb = new StringBuilder(); | 
| 80 | 83 | 
| 81 String line; | 84 if (isListedSubscriptionUrl(url)) | 
| 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.
 
 | |
| 83 { | 85 { | 
| 84 // We're only appending non-element-hiding filters here. | 86 Log.d(TAG, "Removing element hiding rules from: '" + url + "'"); | 
| 85 // See: https://issues.adblockplus.org/ticket/303 | 87 | 
| 86 if (!mightBeFilterList || line.indexOf('#') == -1) | 88 String line; | 
| 89 while ((line = reader.readLine()) != null) | |
| 87 { | 90 { | 
| 88 sb.append(line); | 91 // We're only appending non-element-hiding filters here. | 
| 89 sb.append('\n'); | 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 } | |
| 90 } | 104 } | 
| 91 } | 105 } | 
| 106 else | |
| 107 { | |
| 108 int character; | |
| 109 | |
| 110 while ((character = reader.read()) != -1) | |
| 111 { | |
| 112 sb.append((char) character); | |
| 113 } | |
| 114 } | |
| 115 | |
| 92 connection.disconnect(); | 116 connection.disconnect(); | 
| 93 | 117 | 
| 94 response.setStatus(NsStatus.OK); | 118 response.setStatus(NsStatus.OK); | 
| 95 response.setResponse(sb.toString()); | 119 response.setResponse(sb.toString()); | 
| 96 } | 120 } | 
| 97 else | 121 else | 
| 98 { | 122 { | 
| 99 response.setStatus(NsStatus.ERROR_FAILURE); | 123 response.setStatus(NsStatus.ERROR_FAILURE); | 
| 100 } | 124 } | 
| 101 return response; | 125 return response; | 
| 102 } | 126 } | 
| 103 catch (final Throwable t) | 127 catch (final Throwable t) | 
| 104 { | 128 { | 
| 105 throw new AdblockPlusException("WebRequest failed", t); | 129 throw new AdblockPlusException("WebRequest failed", t); | 
| 106 } | 130 } | 
| 107 } | 131 } | 
| 108 } | 132 } | 
| LEFT | RIGHT |