Skip to content

Commit

Permalink
Merge pull request #328 from medavox/master
Browse files Browse the repository at this point in the history
Block Media Feature by @vosmith, cleaned by @medavox
  • Loading branch information
hollingsworthd authored Jul 7, 2018
2 parents 4b9b671 + a01e4c5 commit 55573c8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 53 deletions.
4 changes: 3 additions & 1 deletion CLA-rev2-digital.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ Vitali Kepin https://github.com/vkepin 2017-05-16
D. Hollingsworth https://github.com/hollingsworthd 2017-06-25
Nicholas DiPiazza https://github.com/nddipiazza/ 2017-08-08
Alena Liashenka https://github.com/eichhornchen27 2017-12-08
Uladzislau Arlouski https://github.com/uarlouski 2017-12-11
Uladzislau Arlouski https://github.com/uarlouski 2017-12-11
contributions by (Vincent Smith https://github.com/vosmith 2018-06-08)
included by (Adam Howard https://github.com/medavox 2018-07-06)
37 changes: 33 additions & 4 deletions src/com/machinepublishers/jbrowserdriver/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private enum PropertyName {
USER_DATA_DIRECTORY("jbd.userdatadirectory"),
CSRF_REQUEST_TOKEN("jbd.csrfreqtoken"),
CSRF_RESPONSE_TOKEN("jbd.csrfresptoken"),
BLOCK_MEDIA("jbd.blockmedia"),
@Deprecated
WIRE_CONSOLE("jbd.wireconsole"),
@Deprecated
Expand Down Expand Up @@ -226,6 +227,7 @@ public static class Builder {
private String csrfRequestToken;
private String csrfResponseToken;
private InetAddress nicAddress;
private boolean blockMedia;

/**
* Headers to be sent on each request.
Expand Down Expand Up @@ -1134,7 +1136,7 @@ public Builder javaExportModules(boolean javaExportModules) {
* <li>Java system property <code>jbd.userdatadirectory</code> overrides this setting. Use a string file path as the value.</li>
* <li>{@link Capabilities} name <code>jbd.userdatadirectory</code> alternately configures this setting. Use a string file path as the value.</li>
* </ul><p>
*
*
* @param userDataDirectory
* A directory to store user website data, e.g. localStorage.
* @return this Builder
Expand All @@ -1148,7 +1150,7 @@ public Builder userDataDirectory(File userDataDirectory) {

/**
* Used for binding to a specific NIC
*
*
* @param nicAddress
* @return this Builder
*/
Expand All @@ -1159,7 +1161,7 @@ public Builder localIp(InetAddress nicAddress) {

/**
* Enables CSRF token handling. Searches for XSRF-TOKEN in response headers and sends X-XSRF-TOKEN in request headers.
*
*
* @return this Builder
*/
public Builder csrf() {
Expand All @@ -1168,7 +1170,7 @@ public Builder csrf() {

/**
* Enables CSRF token handling
*
*
* @param requestToken
* The header to send in each request header
* @param responseToken
Expand All @@ -1181,6 +1183,26 @@ public Builder csrf(String requestToken, String responseToken) {
return this;
}

/**
* Whether requests for common media types should be blocked.
* <p>
* Based on the content-type or file extension of the requested asset
* <p>
* Defaults to <code>false</code>.
*
* <p><ul>
* <li>Java system property <code>jbd.blockmedia</code> overrides this setting.</li>
* <li>{@link Capabilities} name <code>jbd.blockmedia</code> alternately configures this setting.</li>
* </ul><p>
*
* @param blockMedia
* @return this Builder
*/
public Builder blockMedia(boolean blockMedia) {
this.blockMedia = blockMedia;
return this;
}

/**
* @deprecated Will be removed in v2.0.0. Instead use Settings Builder's logWire, logsMax, or logger.
*/
Expand Down Expand Up @@ -1306,6 +1328,7 @@ public Capabilities buildCapabilities() {
set(capabilities, PropertyName.PROXY_PASSWORD, proxy.password());
set(capabilities, PropertyName.PROXY_EXPECT_CONTINUE, proxy.expectContinue());
}
set(capabilities, PropertyName.BLOCK_MEDIA, this.blockMedia);

return capabilities;
}
Expand Down Expand Up @@ -1494,6 +1517,7 @@ private static File parse(Map capabilities, PropertyName name, File fallback) {
private final String csrfRequestToken;
private final String csrfResponseToken;
private final InetAddress nicAddress;
private final boolean blockMedia;

private Settings(Settings.Builder builder, Map properties) {
Settings.Builder defaults = Settings.builder();
Expand Down Expand Up @@ -1682,6 +1706,7 @@ private Settings(Settings.Builder builder, Map properties) {
scriptBuilder.append("'));})();");
scriptBuilder.append("</script>");
this.script = scriptBuilder.toString();
this.blockMedia = parse(properties, PropertyName.BLOCK_MEDIA, builder.blockMedia);
}

RequestHeaders headers() {
Expand Down Expand Up @@ -1859,4 +1884,8 @@ String getCsrfResponseToken() {
InetAddress getLocalIp() {
return nicAddress;
}

boolean blockMedia() {
return blockMedia;
}
}
98 changes: 51 additions & 47 deletions src/com/machinepublishers/jbrowserdriver/StreamConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class StreamConnection extends HttpURLConnection implements Closeable {
".svg", ".gif", ".jpeg", ".jpg", ".png",
".ico", ".webm", ".mp4", ".ogg", ".ogv",
".mp3", ".aac", ".wav", ".swf", ".woff",
".otf", ".ttf" })));
".otf", ".ttf", ".css" })));

private final Map<String, List<String>> reqHeaders = new LinkedHashMap<String, List<String>>();
private final Map<String, String> reqHeadersCasing = new HashMap<String, String>();
Expand Down Expand Up @@ -263,54 +263,58 @@ public void connect() throws IOException {
} else if (isBlocked(url.getHost())) {
skip.set(true);
} else if (SettingsManager.settings() != null) {
config.get()
.setCookieSpec("custom")
.setSocketTimeout(SettingsManager.settings().socketTimeout())
.setConnectTimeout(SettingsManager.settings().connectTimeout())
.setConnectionRequestTimeout(SettingsManager.settings().connectionReqTimeout())
.setLocalAddress(SettingsManager.settings().getLocalIp());
URI uri = null;
try {
uri = url.toURI();
} catch (URISyntaxException e) {
//decode components of the url first, because often the problem is partially encoded urls
uri = new URI(url.getProtocol(),
url.getAuthority(),
url.getPath() == null ? null : URLDecoder.decode(url.getPath(), "utf-8"),
url.getQuery() == null ? null : URLDecoder.decode(url.getQuery(), "utf-8"),
url.getRef() == null ? null : URLDecoder.decode(url.getRef(), "utf-8"));
}
if ("OPTIONS".equals(method.get())) {
req.set(new HttpOptions(uri));
} else if ("GET".equals(method.get())) {
req.set(new HttpGet(uri));
} else if ("HEAD".equals(method.get())) {
req.set(new HttpHead(uri));
} else if ("POST".equals(method.get())) {
req.set(new HttpPost(uri));
} else if ("PUT".equals(method.get())) {
req.set(new HttpPut(uri));
} else if ("DELETE".equals(method.get())) {
req.set(new HttpDelete(uri));
} else if ("TRACE".equals(method.get())) {
req.set(new HttpTrace(uri));
} else if ("PATCH".equals(method.get())) {
req.set(new HttpPatch(uri));
}
processHeaders(SettingsManager.settings(), req.get());
ProxyConfig proxy = SettingsManager.settings().proxy();
if (proxy != null && !proxy.directConnection() && !proxy.nonProxyHosts().contains(uri.getHost())) {
config.get().setExpectContinueEnabled(proxy.expectContinue());
InetSocketAddress proxyAddress = new InetSocketAddress(proxy.host(), proxy.port());
if (proxy.type() == ProxyConfig.Type.SOCKS) {
context.get().setAttribute("proxy.socks.address", proxyAddress);
} else {
config.get().setProxy(new HttpHost(proxy.host(), proxy.port()));
if (SettingsManager.settings().blockMedia() && isMedia()) {
skip.set(true);
} else {
config.get()
.setCookieSpec("custom")
.setSocketTimeout(SettingsManager.settings().socketTimeout())
.setConnectTimeout(SettingsManager.settings().connectTimeout())
.setConnectionRequestTimeout(SettingsManager.settings().connectionReqTimeout())
.setLocalAddress(SettingsManager.settings().getLocalIp());
URI uri = null;
try {
uri = url.toURI();
} catch (URISyntaxException e) {
//decode components of the url first, because often the problem is partially encoded urls
uri = new URI(url.getProtocol(),
url.getAuthority(),
url.getPath() == null ? null : URLDecoder.decode(url.getPath(), "utf-8"),
url.getQuery() == null ? null : URLDecoder.decode(url.getQuery(), "utf-8"),
url.getRef() == null ? null : URLDecoder.decode(url.getRef(), "utf-8"));
}
if ("OPTIONS".equals(method.get())) {
req.set(new HttpOptions(uri));
} else if ("GET".equals(method.get())) {
req.set(new HttpGet(uri));
} else if ("HEAD".equals(method.get())) {
req.set(new HttpHead(uri));
} else if ("POST".equals(method.get())) {
req.set(new HttpPost(uri));
} else if ("PUT".equals(method.get())) {
req.set(new HttpPut(uri));
} else if ("DELETE".equals(method.get())) {
req.set(new HttpDelete(uri));
} else if ("TRACE".equals(method.get())) {
req.set(new HttpTrace(uri));
} else if ("PATCH".equals(method.get())) {
req.set(new HttpPatch(uri));
}
processHeaders(SettingsManager.settings(), req.get());
ProxyConfig proxy = SettingsManager.settings().proxy();
if (proxy != null && !proxy.directConnection() && !proxy.nonProxyHosts().contains(uri.getHost())) {
config.get().setExpectContinueEnabled(proxy.expectContinue());
InetSocketAddress proxyAddress = new InetSocketAddress(proxy.host(), proxy.port());
if (proxy.type() == ProxyConfig.Type.SOCKS) {
context.get().setAttribute("proxy.socks.address", proxyAddress);
} else {
config.get().setProxy(new HttpHost(proxy.host(), proxy.port()));
}
}
context.get().setCookieStore(cookieStore);
context.get().setRequestConfig(config.get().build());
StatusMonitor.instance().monitor(url, this);
}
context.get().setCookieStore(cookieStore);
context.get().setRequestConfig(config.get().build());
StatusMonitor.instance().monitor(url, this);
}
}
} catch (Throwable t) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/machinepublishers/jbrowserdriver/ad-hosts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28641,4 +28641,4 @@ z.times.lv
ztrack.net
zu-yuan.com
zwierzu.zxy.me
z.zeroredirect2.com
z.zeroredirect2.com

0 comments on commit 55573c8

Please sign in to comment.