Skip to content

Commit

Permalink
Add APIs for clearing request and response filters
Browse files Browse the repository at this point in the history
  • Loading branch information
idasbiste committed Oct 1, 2021
1 parent 679d90d commit 332a85d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
8 changes: 8 additions & 0 deletions browsermob-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
<argLine>-Xmx1g -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,23 @@ public interface BrowserMobProxy {
*/
void addLastHttpFilterFactory(HttpFiltersSource filterFactory);

/**
* Clears all the existent response filters that are used to examine/manipulate the request before sending it to the server.
*/
void clearResponseFilters();

/**
* Adds a new ResponseFilter that can be used to examine and manipulate the response before sending it to the client.
*
* @param filter filter instance
*/
void addResponseFilter(ResponseFilter filter);

/**
* Clears all the existent request filters that are used to examine/manipulate the request before sending it to the server.
*/
void clearRequestFilters();

/**
* Adds a new RequestFilter that can be used to examine and manipulate the request before sending it to the server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* A LittleProxy-based implementation of {@link net.lightbody.bmp.BrowserMobProxy}.
Expand Down Expand Up @@ -927,6 +928,11 @@ public void addLastHttpFilterFactory(HttpFiltersSource filterFactory) {
filterFactories.add(filterFactory);
}

@Override
public void clearResponseFilters() {
filterFactories.removeIf(filter -> filter instanceof ResponseFilterAdapter.FilterSource);
}

/**
* <b>Note:</b> The current implementation of this method forces a maximum response size of 2 MiB. To adjust the maximum response size, or
* to disable aggregation (which disallows access to the {@link net.lightbody.bmp.util.HttpMessageContents}), you may add the filter source
Expand All @@ -937,6 +943,11 @@ public void addResponseFilter(ResponseFilter filter) {
addLastHttpFilterFactory(new ResponseFilterAdapter.FilterSource(filter));
}

@Override
public void clearRequestFilters() {
filterFactories.removeIf(filter -> filter instanceof RequestFilterAdapter.FilterSource);
}

/**
* <b>Note:</b> The current implementation of this method forces a maximum request size of 2 MiB. To adjust the maximum request size, or
* to disable aggregation (which disallows access to the {@link net.lightbody.bmp.util.HttpMessageContents}), you may add the filter source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,10 @@ public void testResponseFilterCanModifyBinaryContents() throws IOException {
proxy = new BrowserMobProxyServer();
proxy.start();

proxy.addResponseFilter(new ResponseFilter() {
@Override
public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) {
if (!contents.isText()) {
if (Arrays.equals(originalBytes, contents.getBinaryContents())) {
contents.setBinaryContents(newBytes);
}
proxy.addResponseFilter((response, contents, messageInfo) -> {
if (!contents.isText()) {
if (Arrays.equals(originalBytes, contents.getBinaryContents())) {
contents.setBinaryContents(newBytes);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,24 @@ public void process(BrowserMobHttpRequest request, Har har) {
return Reply.saying().ok();
}

@Delete
@At("/:port/filter/request")
public Reply<?> clearRequestFilters(@Named("port") int port) {
LegacyProxyServer legacyProxy = proxyManager.get(port);
if (legacyProxy == null) {
return Reply.saying().notFound();
}

if (!(legacyProxy instanceof BrowserMobProxyServer)) {
return Reply.saying().badRequest();
}

BrowserMobProxy proxy = (BrowserMobProxy) legacyProxy;
proxy.clearRequestFilters();

return Reply.saying().ok();
}

@Post
@At("/:port/filter/request")
public Reply<?> addRequestFilter(@Named("port") int port, Request<String> request) throws IOException, ScriptException {
Expand All @@ -388,6 +406,24 @@ public Reply<?> addRequestFilter(@Named("port") int port, Request<String> reques
return Reply.saying().ok();
}

@Delete
@At("/:port/filter/response")
public Reply<?> clearResponseFilters(@Named("port") int port) {
LegacyProxyServer legacyProxy = proxyManager.get(port);
if (legacyProxy == null) {
return Reply.saying().notFound();
}

if (!(legacyProxy instanceof BrowserMobProxyServer)) {
return Reply.saying().badRequest();
}

BrowserMobProxy proxy = (BrowserMobProxy) legacyProxy;
proxy.clearResponseFilters();

return Reply.saying().ok();
}

@Post
@At("/:port/filter/response")
public Reply<?> addResponseFilter(@Named("port") int port, Request<String> request) throws IOException, ScriptException {
Expand Down

0 comments on commit 332a85d

Please sign in to comment.