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

Side by Side Diff: src/org/adblockplus/brazil/RequestHandler.java

Issue 11172036: ABP/Android libadblockplus integration (Closed)
Patch Set: Created July 22, 2013, 8:52 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
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-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 16 matching lines...) Expand all
27 import java.net.MalformedURLException; 27 import java.net.MalformedURLException;
28 import java.net.URL; 28 import java.net.URL;
29 import java.net.UnknownHostException; 29 import java.net.UnknownHostException;
30 import java.util.List; 30 import java.util.List;
31 import java.util.regex.Pattern; 31 import java.util.regex.Pattern;
32 import java.util.zip.GZIPInputStream; 32 import java.util.zip.GZIPInputStream;
33 import java.util.zip.InflaterInputStream; 33 import java.util.zip.InflaterInputStream;
34 34
35 import org.adblockplus.ChunkedOutputStream; 35 import org.adblockplus.ChunkedOutputStream;
36 import org.adblockplus.android.AdblockPlus; 36 import org.adblockplus.android.AdblockPlus;
37 import org.apache.commons.lang.StringUtils;
37 import org.literateprograms.BoyerMoore; 38 import org.literateprograms.BoyerMoore;
38 39
39 import sunlabs.brazil.server.Request; 40 import sunlabs.brazil.server.Request;
40 import sunlabs.brazil.server.Server; 41 import sunlabs.brazil.server.Server;
41 import sunlabs.brazil.util.MatchString; 42 import sunlabs.brazil.util.MatchString;
42 import sunlabs.brazil.util.http.HttpInputStream; 43 import sunlabs.brazil.util.http.HttpInputStream;
43 import sunlabs.brazil.util.http.HttpRequest; 44 import sunlabs.brazil.util.http.HttpRequest;
44 import android.util.Log; 45 import android.util.Log;
45 46
46 /** 47 /**
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 application = AdblockPlus.getApplication(); 92 application = AdblockPlus.getApplication();
92 via = " " + server.hostName + ":" + server.listen.getLocalPort() + " (" + se rver.name + ")"; 93 via = " " + server.hostName + ":" + server.listen.getLocalPort() + " (" + se rver.name + ")";
93 94
94 return true; 95 return true;
95 } 96 }
96 97
97 @Override 98 @Override
98 public boolean respond(Request request) throws IOException 99 public boolean respond(Request request) throws IOException
99 { 100 {
100 boolean block = false; 101 boolean block = false;
101 String reqHost = null;
102 String refHost = null;
103
104 String referrer = request.getRequestHeader("referer");
105 102
106 try 103 try
107 { 104 {
108 reqHost = (new URL(request.url)).getHost(); 105 block = application.matches(request.url, request.query, request.getRequest Header("referer"), request.getRequestHeader("accept"));
109 if (referrer != null)
110 refHost = (new URL(referrer)).getHost();
111 }
112 catch (MalformedURLException e)
113 {
114 // We are transparent, it's not our deal if it's malformed.
115 }
116
117 try
118 {
119 block = application.matches(request.url, request.query, reqHost, refHost, request.getRequestHeader("accept"));
120 } 106 }
121 catch (Exception e) 107 catch (Exception e)
122 { 108 {
123 Log.e(prefix, "Filter error", e); 109 Log.e(prefix, "Filter error", e);
124 } 110 }
125 111
126 request.log(Server.LOG_LOG, prefix, block + ": " + request.url + " ("+ refHo st +")"); 112 request.log(Server.LOG_LOG, prefix, block + ": " + request.url);
127 113
128 int count = request.server.requestCount; 114 int count = request.server.requestCount;
129 if (shouldLogHeaders) 115 if (shouldLogHeaders)
130 { 116 {
131 System.err.println(dumpHeaders(count, request, request.headers, true)); 117 System.err.println(dumpHeaders(count, request, request.headers, true));
132 } 118 }
133 119
134 if (block) 120 if (block)
135 { 121 {
136 request.sendHeaders(204, null, 0); 122 request.sendHeaders(204, null, 0);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 request.responseHeaders.add("Via", target.status.substring(0, 8) + via); 189 request.responseHeaders.add("Via", target.status.substring(0, 8) + via);
204 } 190 }
205 catch (StringIndexOutOfBoundsException e) 191 catch (StringIndexOutOfBoundsException e)
206 { 192 {
207 request.responseHeaders.add("Via", via); 193 request.responseHeaders.add("Via", via);
208 } 194 }
209 195
210 // Detect if we need to add ElemHide filters 196 // Detect if we need to add ElemHide filters
211 String type = request.responseHeaders.get("Content-Type"); 197 String type = request.responseHeaders.get("Content-Type");
212 198
213 String selectors = null; 199 String[] selectors = null;
214 if (type != null && type.toLowerCase().startsWith("text/html")) 200 if (type != null && type.toLowerCase().startsWith("text/html"))
215 { 201 {
202 String reqHost = "";
203
204 try
205 {
206 reqHost = (new URL(request.url)).getHost();
207 }
208 catch (MalformedURLException e)
209 {
210 // We are transparent, it's not our deal if it's malformed.
211 }
212
216 selectors = application.getSelectorsForDomain(reqHost); 213 selectors = application.getSelectorsForDomain(reqHost);
217 } 214 }
218 // If no filters are applicable just pass through the response 215 // If no filters are applicable just pass through the response
219 if (selectors == null || target.getResponseCode() != 200) 216 if (selectors == null || target.getResponseCode() != 200)
220 { 217 {
221 int contentLength = target.getContentLength(); 218 int contentLength = target.getContentLength();
222 if (contentLength == 0) 219 if (contentLength == 0)
223 { 220 {
224 // we do not use request.sendResponse to avoid arbitrary 221 // we do not use request.sendResponse to avoid arbitrary
225 // 200 -> 204 response code conversion 222 // 200 -> 204 response code conversion
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 } 294 }
298 size -= count; 295 size -= count;
299 try 296 try
300 { 297 {
301 // Search for <html> tag 298 // Search for <html> tag
302 if (!sent && count > 0) 299 if (!sent && count > 0)
303 { 300 {
304 List<Integer> matches = matcher.match(buf, 0, count); 301 List<Integer> matches = matcher.match(buf, 0, count);
305 if (!matches.isEmpty()) 302 if (!matches.isEmpty())
306 { 303 {
307 // TODO Do we need to set encoding here?
308 byte[] addon = selectors.getBytes();
309 // Add filters right before match 304 // Add filters right before match
310 int m = matches.get(0); 305 int m = matches.get(0);
311 out.write(buf, 0, m); 306 out.write(buf, 0, m);
312 out.write(addon); 307 out.write("<style type=\"text/css\">\n".getBytes());
308 // TODO Do we need to set encoding here?
Wladimir Palant 2013/09/12 11:31:14 Yes, pretty certainly. We need to get the encoding
309 out.write(StringUtils.join(selectors, ",\r\n").getBytes());
310 out.write("{ display: none !important }</style>\n".getBytes());
313 out.write(buf, m, count - m); 311 out.write(buf, m, count - m);
314 sent = true; 312 sent = true;
315 continue; 313 continue;
316 } 314 }
317 } 315 }
318 out.write(buf, 0, count); 316 out.write(buf, 0, count);
319 } 317 }
320 catch (IOException e) 318 catch (IOException e)
321 { 319 {
322 break; 320 break;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 request.sendError(500, msg); 364 request.sendError(500, msg);
367 Log.e(prefix, msg, e); 365 Log.e(prefix, msg, e);
368 } 366 }
369 finally 367 finally
370 { 368 {
371 target.close(); 369 target.close();
372 } 370 }
373 return true; 371 return true;
374 } 372 }
375 } 373 }
OLDNEW
« src/org/adblockplus/android/Preferences.java ('K') | « src/org/adblockplus/android/ProxyService.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld