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

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

Issue 29678581: Issue 6000 - Rename "libadblockplus-android" (Closed)
Patch Set: Created Jan. 24, 2018, 6:53 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 package org.adblockplus.libadblockplus.android;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26 import java.util.HashSet;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.zip.GZIPInputStream;
31
32 import org.adblockplus.libadblockplus.AdblockPlusException;
33 import org.adblockplus.libadblockplus.FilterEngine;
34 import org.adblockplus.libadblockplus.HeaderEntry;
35 import org.adblockplus.libadblockplus.JsValue;
36 import org.adblockplus.libadblockplus.ServerResponse;
37 import org.adblockplus.libadblockplus.ServerResponse.NsStatus;
38 import org.adblockplus.libadblockplus.WebRequest;
39
40 import android.util.Log;
41
42 public class AndroidWebRequest implements WebRequest
43 {
44 protected static final String ENCODING_GZIP = "gzip";
45 protected static final String ENCODING_IDENTITY = "identity";
46
47 public final static String TAG = Utils.getTag(WebRequest.class);
48
49 private final HashSet<String> subscriptionURLs = new HashSet<String>();
50 private final boolean elemhideEnabled;
51 private final boolean compressedStream;
52
53 /**
54 * Ctor
55 * @param enableElemhide Enable element hiding?
56 * Element hiding requires significantly more memory
57 * but allows better ad blocking
58 * @param compressedStream Request for gzip compressed stream from the server
59 */
60 public AndroidWebRequest(boolean enableElemhide, boolean compressedStream)
61 {
62 this.elemhideEnabled = enableElemhide;
63 this.compressedStream = compressedStream;
64 }
65
66 public AndroidWebRequest()
67 {
68 this(false, true);
69 }
70
71 private boolean isListedSubscriptionUrl(final URL url)
72 {
73 String toCheck = url.toString();
74
75 final int idx = toCheck.indexOf('?');
76 if (idx != -1)
77 {
78 toCheck = toCheck.substring(0, idx);
79 }
80
81 return this.subscriptionURLs.contains(toCheck);
82 }
83
84 protected void updateSubscriptionURLs(final FilterEngine engine)
85 {
86 for (final org.adblockplus.libadblockplus.Subscription s : engine.fetchAvail ableSubscriptions())
87 {
88 try
89 {
90 JsValue jsUrl = s.getProperty("url");
91 try
92 {
93 this.subscriptionURLs.add(jsUrl.toString());
94 }
95 finally
96 {
97 jsUrl.dispose();
98 }
99 }
100 finally
101 {
102 s.dispose();
103 }
104 }
105 JsValue jsPref = engine.getPref("subscriptions_exceptionsurl");
106 try
107 {
108 this.subscriptionURLs.add(jsPref.toString());
109 }
110 finally
111 {
112 jsPref.dispose();
113 }
114 }
115
116 @Override
117 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea ders)
118 {
119 try
120 {
121 final URL url = new URL(urlStr);
122 Log.d(TAG, "Downloading from: " + url);
123
124 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio n();
125 connection.setRequestMethod("GET");
126 connection.setRequestProperty("Accept-Encoding",
127 (compressedStream ? ENCODING_GZIP : ENCODING_IDENTITY));
128 connection.connect();
129
130 final ServerResponse response = new ServerResponse();
131 try
132 {
133 response.setResponseStatus(connection.getResponseCode());
134
135 if (response.getResponseStatus() == 200)
136 {
137 final InputStream inputStream =
138 (compressedStream && ENCODING_GZIP.equals(connection.getContentEncod ing())
139 ? new GZIPInputStream(connection.getInputStream())
140 : connection.getInputStream());
141 final BufferedReader reader = new BufferedReader(new InputStreamReader (inputStream, "UTF-8"));
142 final StringBuilder sb = new StringBuilder();
143
144 String line;
145 try
146 {
147 while ((line = reader.readLine()) != null)
148 {
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)
158 {
159 sb.append(line);
160 sb.append('\n');
161 }
162 }
163 }
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 }
194 }
195 else
196 {
197 response.setStatus(NsStatus.ERROR_FAILURE);
198 }
199 }
200 finally
201 {
202 connection.disconnect();
203 }
204 Log.d(TAG, "Downloading finished");
205 return response;
206 }
207 catch (final Throwable t)
208 {
209 Log.e(TAG, "WebRequest failed", t);
210 throw new AdblockPlusException("WebRequest failed", t);
211 }
212 }
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld