| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 package org.adblockplus.brazil; | 1 package org.adblockplus.brazil; | 
| 2 | 2 | 
| 3 import java.io.EOFException; | 3 import java.io.EOFException; | 
| 4 import java.io.FilterInputStream; | 4 import java.io.FilterInputStream; | 
| 5 import java.io.FilterOutputStream; | 5 import java.io.FilterOutputStream; | 
| 6 import java.io.IOException; | 6 import java.io.IOException; | 
| 7 import java.io.InterruptedIOException; | 7 import java.io.InterruptedIOException; | 
| 8 import java.io.OutputStream; | 8 import java.io.OutputStream; | 
| 9 import java.net.ConnectException; | 9 import java.net.ConnectException; | 
| 10 import java.net.MalformedURLException; | 10 import java.net.MalformedURLException; | 
| 11 import java.net.URL; | 11 import java.net.URL; | 
| 12 import java.net.UnknownHostException; | 12 import java.net.UnknownHostException; | 
| 13 import java.util.List; | 13 import java.util.List; | 
| 14 import java.util.Properties; | 14 import java.util.regex.Pattern; | 
| 15 import java.util.zip.GZIPInputStream; | 15 import java.util.zip.GZIPInputStream; | 
| 16 import java.util.zip.InflaterInputStream; | 16 import java.util.zip.InflaterInputStream; | 
| 17 | 17 | 
| 18 import org.adblockplus.ChunkedOutputStream; | 18 import org.adblockplus.ChunkedOutputStream; | 
| 19 import org.adblockplus.android.AdblockPlus; | 19 import org.adblockplus.android.AdblockPlus; | 
| 20 import org.literateprograms.BoyerMoore; | 20 import org.literateprograms.BoyerMoore; | 
| 21 | 21 | 
| 22 import sunlabs.brazil.server.Handler; | |
| 23 import sunlabs.brazil.server.Request; | 22 import sunlabs.brazil.server.Request; | 
| 24 import sunlabs.brazil.server.Server; | 23 import sunlabs.brazil.server.Server; | 
| 25 import sunlabs.brazil.util.MatchString; | 24 import sunlabs.brazil.util.MatchString; | 
| 26 import sunlabs.brazil.util.http.HttpInputStream; | 25 import sunlabs.brazil.util.http.HttpInputStream; | 
| 27 import sunlabs.brazil.util.http.HttpRequest; | 26 import sunlabs.brazil.util.http.HttpRequest; | 
| 28 import sunlabs.brazil.util.http.MimeHeaders; | 27 import sunlabs.brazil.util.http.MimeHeaders; | 
| 29 import android.util.Log; | 28 import android.util.Log; | 
| 30 | 29 | 
| 31 /** | 30 /** | 
| 32 * The <code>RequestHandler</code> implements a proxy service optionally | 31 * The <code>RequestHandler</code> implements a proxy service optionally | 
| (...skipping 22 matching lines...) Expand all Loading... | |
| 55 * | 54 * | 
| 56 * <pre> | 55 * <pre> | 
| 57 * handler=adblock | 56 * handler=adblock | 
| 58 * adblock.class=org.adblockplus.brazil.RequestHandler | 57 * adblock.class=org.adblockplus.brazil.RequestHandler | 
| 59 * </pre> | 58 * </pre> | 
| 60 * | 59 * | 
| 61 * See the description under {@link sunlabs.brazil.server.Handler#respond | 60 * See the description under {@link sunlabs.brazil.server.Handler#respond | 
| 62 * respond} for a more detailed explanation. | 61 * respond} for a more detailed explanation. | 
| 63 */ | 62 */ | 
| 64 | 63 | 
| 65 public class RequestHandler implements Handler | 64 public class RequestHandler extends BaseRequestHandler | 
| 66 { | 65 { | 
| 67 public static final String PROXY_HOST = "proxyHost"; | |
| 68 public static final String PROXY_PORT = "proxyPort"; | |
| 69 public static final String AUTH = "auth"; | |
| 70 | |
| 71 private AdblockPlus application; | 66 private AdblockPlus application; | 
| 72 private String prefix; | |
| 73 | |
| 74 private String via; | 67 private String via; | 
| 75 | 68 private static Pattern RE_HTTP = Pattern.compile("^https?:"); | 
| 76 private String proxyHost; | |
| 77 private int proxyPort = 80; | |
| 78 private String auth; | |
| 79 | 69 | 
| 80 private boolean shouldLogHeaders; | 70 private boolean shouldLogHeaders; | 
| 81 | 71 | 
| 82 @Override | 72 @Override | 
| 83 public boolean init(Server server, String prefix) | 73 public boolean init(Server server, String prefix) | 
| 84 { | 74 { | 
| 85 this.prefix = prefix; | 75 super.init(server, prefix); | 
| 76 | |
| 86 application = AdblockPlus.getApplication(); | 77 application = AdblockPlus.getApplication(); | 
| 87 | 78 shouldLogHeaders = (server.props.getProperty(prefix + "proxylog") != null); | 
| 88 Properties props = server.props; | |
| 89 | |
| 90 proxyHost = props.getProperty(prefix + PROXY_HOST); | |
| 91 | |
| 92 String s = props.getProperty(prefix + PROXY_PORT); | |
| 93 try | |
| 94 { | |
| 95 proxyPort = Integer.decode(s).intValue(); | |
| 96 } | |
| 97 catch (Exception e) | |
| 98 { | |
| 99 // use default port | |
| 100 } | |
| 101 | |
| 102 auth = props.getProperty(prefix + AUTH); | |
| 103 | |
| 104 shouldLogHeaders = (props.getProperty(prefix + "proxylog") != null); | |
| 105 | |
| 106 via = " " + server.hostName + ":" + server.listen.getLocalPort() + " (" + se rver.name + ")"; | 79 via = " " + server.hostName + ":" + server.listen.getLocalPort() + " (" + se rver.name + ")"; | 
| 107 | 80 | 
| 108 return true; | 81 return true; | 
| 109 } | 82 } | 
| 110 | 83 | 
| 111 @Override | 84 @Override | 
| 112 public boolean respond(Request request) throws IOException | 85 public boolean respond(Request request) throws IOException | 
| 113 { | 86 { | 
| 114 boolean block = false; | 87 boolean block = false; | 
| 115 String reqHost = null; | 88 String reqHost = null; | 
| 116 String refHost = null; | 89 String refHost = null; | 
| 117 | 90 | 
| 91 String referrer = request.getRequestHeader("referer"); | |
| 92 | |
| 118 try | 93 try | 
| 119 { | 94 { | 
| 120 reqHost = (new URL(request.url)).getHost(); | 95 reqHost = (new URL(request.url)).getHost(); | 
| 121 refHost = (new URL(request.getRequestHeader("referer"))).getHost(); | 96 if (referrer != null) | 
| 97 refHost = (new URL(referrer)).getHost(); | |
| 122 } | 98 } | 
| 123 catch (MalformedURLException e) | 99 catch (MalformedURLException e) | 
| 124 { | 100 { | 
| 125 // We are transparent, it's not our deal if it's malformed. | 101 // We are transparent, it's not our deal if it's malformed. | 
| 126 } | 102 } | 
| 127 | 103 | 
| 128 try | 104 try | 
| 129 { | 105 { | 
| 130 block = application.matches(request.url, request.query, reqHost, refHost, request.getRequestHeader("accept")); | 106 if (referrer != null) | 
| 107 block = application.matches(request.url, request.query, reqHost, refHost , request.getRequestHeader("accept")); | |
| 131 } | 108 } | 
| 132 catch (Exception e) | 109 catch (Exception e) | 
| 133 { | 110 { | 
| 134 Log.e(prefix, "Filter error", e); | 111 Log.e(prefix, "Filter error", e); | 
| 135 } | 112 } | 
| 136 | 113 | 
| 137 request.log(Server.LOG_LOG, prefix, block + ": " + request.url); | 114 request.log(Server.LOG_LOG, prefix, block + ": " + request.url + " ("+ refHo st +")"); | 
| 138 | 115 | 
| 139 int count = request.server.requestCount; | 116 int count = request.server.requestCount; | 
| 140 if (shouldLogHeaders) | 117 if (shouldLogHeaders) | 
| 141 { | 118 { | 
| 142 System.err.println(dumpHeaders(count, request, request.headers, true)); | 119 System.err.println(dumpHeaders(count, request, request.headers, true)); | 
| 
 
Felix Dahlke
2012/11/09 14:40:46
Haven't seen this code before, it was further down
 
Andrey Novikov
2012/11/12 08:53:16
For the same reason. It's not Android specific dom
 
 | |
| 143 } | 120 } | 
| 144 | 121 | 
| 145 if (block) | 122 if (block) | 
| 146 { | 123 { | 
| 147 request.sendHeaders(204, null, 0); | 124 request.sendHeaders(204, null, 0); | 
| 
 
Felix Dahlke
2012/11/09 14:40:46
This part is new, I presume it wasn't to address a
 
Andrey Novikov
2012/11/12 08:53:16
No, it's a fix I introduced. I can not wait for re
 
 | |
| 148 return true; | 125 return true; | 
| 149 } | 126 } | 
| 150 | 127 | 
| 151 // Do not further process non-http requests | 128 // Do not further process non-http requests | 
| 152 if (request.url.startsWith("http:") == false && request.url.startsWith("http s:") == false) | 129 if (!RE_HTTP.matcher(request.url).find()) | 
| 153 { | 130 { | 
| 154 return false; | 131 return false; | 
| 155 } | 132 } | 
| 156 | 133 | 
| 157 String url = request.url; | 134 String url = request.url; | 
| 158 | 135 | 
| 159 if ((request.query != null) && (request.query.length() > 0)) | 136 if ((request.query != null) && (request.query.length() > 0)) | 
| 160 { | 137 { | 
| 161 url += "?" + request.query; | 138 url += "?" + request.query; | 
| 162 } | 139 } | 
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 } | 378 } | 
| 402 | 379 | 
| 403 for (int i = 0; i < headers.size(); i++) | 380 for (int i = 0; i < headers.size(); i++) | 
| 404 { | 381 { | 
| 405 sb.append(prompt).append(headers.getKey(i)); | 382 sb.append(prompt).append(headers.getKey(i)); | 
| 406 sb.append(": ").append(headers.get(i)).append("\n"); | 383 sb.append(": ").append(headers.get(i)).append("\n"); | 
| 407 } | 384 } | 
| 408 return (sb.toString()); | 385 return (sb.toString()); | 
| 409 } | 386 } | 
| 410 } | 387 } | 
| LEFT | RIGHT |