Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit 195c44a

Browse files
BMP 2.1.33 (#25)
* Extend browsermob proxy add headers with additional field in json `headersFilterRegexp` which allows you to add headers only to specific domain * update to openjdk8 * update to oraclejdk8 * Update litle proxy * Update little proxy * Update snapshot version
1 parent 2337f7f commit 195c44a

File tree

15 files changed

+193
-133
lines changed

15 files changed

+193
-133
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
sudo: false
22

33
language: java
4+
dist: trusty
45
jdk:
56
# Not running tests against openjdk7, since the SunEC is not included in travis-ci's version of openjdk7.
67
# Not running tests against oraclejdk7, since travis-ci no longer provides it.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Set a URL to blacklist | PUT | */proxy/[port]/blacklist* |<p>*regex* - The black
144144
Clears all URL patterns from the blacklist | DELETE | */proxy/[port]/blacklist* ||
145145
Limit the bandwidth through the proxy on the *[port]* | PUT | */proxy/[port]/limit* |<p>*downstreamKbps* - Sets the downstream bandwidth limit in kbps. Optional.</p><p>*upstreamKbps* - Sets the upstream bandwidth limit kbps. Optional, by default unlimited.</p><p>*downstreamMaxKB* - Specifies how many kilobytes in total the client is allowed to download through the proxy. Optional, by default unlimited.</p><p>*upstreamMaxKB* - Specifies how many kilobytes in total the client is allowed to upload through the proxy. Optional, by default unlimited.</p><p>*latency* - Add the given latency to each HTTP request. Optional, by default all requests are invoked without latency.</p><p>*enable* - A boolean that enable bandwidth limiter. Optional, by default to "false", but setting any of the properties above will implicitly enable throttling</p><p>*payloadPercentage* - Specifying what percentage of data sent is payload, e.g. use this to take into account overhead due to tcp/ip. Optional.</p><p>*maxBitsPerSecond* - The max bits per seconds you want this instance of StreamManager to respect. Optional.</p>
146146
Displays the amount of data remaining to be uploaded/downloaded until the limit is reached | GET | */proxy/[port]/limit* ||
147-
Set and override HTTP Request headers | POST | */proxy/[port]/headers* | Payload data should be **JSON** encoded set of headers. Where key is a header name (such as "User-Agent") and value is a value of HTTP header to setup (such as "BrowserMob-Agent"). Example: `{"User-Agent": "BrowserMob-Agent"}`|
147+
Set and override HTTP Request headers | POST | */proxy/[port]/headers* | Payload data should be **JSON** encoded set of headers and optionally `headersFilterRegexp: .*.*` . Where key is a header name (such as "User-Agent") and value is a value of HTTP header to setup (such as "BrowserMob-Agent"). Example: `{"User-Agent": "BrowserMob-Agent"}`|
148148
Overrides normal DNS lookups and remaps the given hosts with the associated IP address | POST | */proxy/[port]/hosts* | Payload data should be **JSON** encoded set of hosts. Where key is a host name (such as "example.com") and value is a IP address which associatied with host hame (such as "1.2.3.4"'). Example: `{"example.com": "1.2.3.4"}`|
149149
Sets automatic basic authentication for the specified domain | POST | */proxy/[port]/auth/basic/[domain]* | Payload data should be **JSON** encoded username and password name/value pairs. Example: `{"username": "myUsername", "password": "myPassword"}`|
150150
Wait till all request are being made | PUT | */proxy/[port]/wait* |<p>*quietPeriodInMs* - Wait till all request are being made. Optional.</p><p>*timeoutInMs* - Sets quiet period in milliseconds. Optional.</p>|
@@ -396,7 +396,7 @@ When you build the latest code from source, you'll have access to the latest sna
396396
<dependency>
397397
<groupId>net.lightbody.bmp</groupId>
398398
<artifactId>browsermob-core</artifactId>
399-
<version>2.1.33-SNAPSHOT</version>
399+
<version>2.1.34-SNAPSHOT</version>
400400
<scope>test</scope>
401401
</dependency>
402402
```

browsermob-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>browsermob-proxy</artifactId>
88
<groupId>net.lightbody.bmp</groupId>
9-
<version>2.1.33-SNAPSHOT</version>
9+
<version>2.1.34-SNAPSHOT</version>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
1212

browsermob-core/src/main/java/net/lightbody/bmp/BrowserMobProxy.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public interface BrowserMobProxy {
3939
* Starts the proxy on the specified port. The proxy will listen for connections on the network interface specified by the bindAddress, and will
4040
* also initiate connections to upstream servers on the same network interface.
4141
*
42-
* @param port port to listen on
42+
* @param port port to listen on
4343
* @param bindAddress address of the network interface on which the proxy will listen for connections and also attempt to connect to upstream servers.
4444
* @throws java.lang.IllegalStateException if the proxy has already been started
4545
*/
@@ -49,7 +49,7 @@ public interface BrowserMobProxy {
4949
* Starts the proxy on the specified port. The proxy will listen for connections on the network interface specified by the clientBindAddress, and will
5050
* initiate connections to upstream servers from the network interface specified by the serverBindAddress.
5151
*
52-
* @param port port to listen on
52+
* @param port port to listen on
5353
* @param clientBindAddress address of the network interface on which the proxy will listen for connections
5454
* @param serverBindAddress address of the network interface on which the proxy will connect to upstream servers
5555
* @throws java.lang.IllegalStateException if the proxy has already been started
@@ -68,7 +68,7 @@ public interface BrowserMobProxy {
6868
* @throws java.lang.IllegalStateException if the proxy has not been started.
6969
*/
7070
void stop();
71-
71+
7272
/**
7373
* Like {@link #stop()}, shuts down the proxy server and no longer accepts incoming connections, but does not wait for any existing
7474
* network traffic to cease. Any existing connections to clients or to servers may be force-killed immediately.
@@ -125,7 +125,7 @@ public interface BrowserMobProxy {
125125
/**
126126
* Starts a new HAR file with the specified page name and page title. Enables HAR capture if it was not previously enabled.
127127
*
128-
* @param initialPageRef initial page name of the new HAR file
128+
* @param initialPageRef initial page name of the new HAR file
129129
* @param initialPageTitle initial page title of the new HAR file
130130
* @return existing HAR file, or null if none exists or HAR capture was disabled
131131
*/
@@ -215,7 +215,7 @@ public interface BrowserMobProxy {
215215
* Starts a new HAR page using the specified pageRef as the page name and the pageTitle as the page title. Populates the
216216
* {@link net.lightbody.bmp.core.har.HarPageTimings#onLoad} value based on the amount of time the current page has been captured.
217217
*
218-
* @param pageRef name of the new page
218+
* @param pageRef name of the new page
219219
* @param pageTitle title of the new page
220220
* @return the HAR as it existed immediately after ending the current page
221221
* @throws java.lang.IllegalStateException if HAR capture has not been enabled via {@link #newHar()} or {@link #newHar(String)}
@@ -258,7 +258,7 @@ public interface BrowserMobProxy {
258258
* The minimum amount of time that will elapse between the time the proxy begins receiving a response from the server and the time the
259259
* proxy begins sending the response to the client.
260260
*
261-
* @param latency minimum latency, or 0 for no minimum
261+
* @param latency minimum latency, or 0 for no minimum
262262
* @param timeUnit TimeUnit for the latency
263263
*/
264264
void setLatency(long latency, TimeUnit timeUnit);
@@ -268,7 +268,7 @@ public interface BrowserMobProxy {
268268
* specified time, the proxy will respond with an HTTP 502 Bad Gateway. The default value is 60 seconds.
269269
*
270270
* @param connectionTimeout maximum time to wait to establish a connection to a server, or 0 to wait indefinitely
271-
* @param timeUnit TimeUnit for the connectionTimeout
271+
* @param timeUnit TimeUnit for the connectionTimeout
272272
*/
273273
void setConnectTimeout(int connectionTimeout, TimeUnit timeUnit);
274274

@@ -279,7 +279,7 @@ public interface BrowserMobProxy {
279279
* connection to the client <i>may</i> be closed abruptly. The default value is 60 seconds.
280280
*
281281
* @param idleConnectionTimeout maximum time to allow a connection to remain idle, or 0 to wait indefinitely.
282-
* @param timeUnit TimeUnit for the idleConnectionTimeout
282+
* @param timeUnit TimeUnit for the idleConnectionTimeout
283283
*/
284284
void setIdleConnectionTimeout(int idleConnectionTimeout, TimeUnit timeUnit);
285285

@@ -290,15 +290,15 @@ public interface BrowserMobProxy {
290290
* connection to the client <i>may</i> be closed abruptly. The default value is 0 (wait indefinitely).
291291
*
292292
* @param requestTimeout maximum time to wait for an HTTP response, or 0 to wait indefinitely
293-
* @param timeUnit TimeUnit for the requestTimeout
293+
* @param timeUnit TimeUnit for the requestTimeout
294294
*/
295295
void setRequestTimeout(int requestTimeout, TimeUnit timeUnit);
296296

297297
/**
298298
* Enables automatic authorization for the specified domain and auth type. Every request sent to the specified domain will contain the
299299
* specified authorization information.
300300
*
301-
* @param domain domain automatically send authorization information to
301+
* @param domain domain automatically send authorization information to
302302
* @param username authorization username
303303
* @param password authorization password
304304
* @param authType authorization type
@@ -340,7 +340,7 @@ public interface BrowserMobProxy {
340340
* For example, the following rewrite rule:
341341
*
342342
* <pre> {@code proxy.rewriteUrl("http://www\\.(yahoo|bing)\\.com/\\?(\\w+)=(\\w+)", "http://www.google.com/?originalDomain=$1&$2=$3");}</pre>
343-
*
343+
* <p>
344344
* will match an HTTP request (but <i>not</i> HTTPS!) to www.yahoo.com or www.bing.com with exactly 1 query parameter,
345345
* and replace it with a call to www.google.com with an 'originalDomain' query parameter, as well as the original query parameter.
346346
* <p/>
@@ -353,7 +353,7 @@ public interface BrowserMobProxy {
353353
* will result in the proxy making a request to:
354354
* <pre> {@code http://www.google.com?originalDomain=bing&anotherParam=anotherValue}</pre>
355355
*
356-
* @param urlPattern URL-matching regular expression
356+
* @param urlPattern URL-matching regular expression
357357
* @param replacementExpression an expression, which may optionally contain capture groups, which will replace any URL which matches urlPattern
358358
*/
359359
void rewriteUrl(String urlPattern, String replacementExpression);
@@ -410,8 +410,8 @@ public interface BrowserMobProxy {
410410
* <p/>
411411
* See {@link #blacklistRequests(String, int)} for details on the URL the urlPattern will match.
412412
*
413-
* @param urlPattern URL-matching regular expression to blacklist
414-
* @param statusCode HTTP status code to return
413+
* @param urlPattern URL-matching regular expression to blacklist
414+
* @param statusCode HTTP status code to return
415415
* @param httpMethodPattern regular expression matching a request's HTTP method
416416
*/
417417
void blacklistRequests(String urlPattern, int statusCode, String httpMethodPattern);
@@ -447,7 +447,7 @@ public interface BrowserMobProxy {
447447
* whitelist response code.
448448
*
449449
* @param urlPatterns URL-matching regular expressions to whitelist; null or an empty collection will enable an empty whitelist
450-
* @param statusCode HTTP status code to return to clients when a URL matches a pattern
450+
* @param statusCode HTTP status code to return to clients when a URL matches a pattern
451451
*/
452452
void whitelistRequests(Collection<String> urlPatterns, int statusCode);
453453

@@ -501,11 +501,18 @@ public interface BrowserMobProxy {
501501
/**
502502
* Adds a new HTTP header to every request. If the header already exists on the request, it will be replaced with the specified header.
503503
*
504-
* @param name name of the header to add
504+
* @param name name of the header to add
505505
* @param value new header's value
506506
*/
507507
void addHeader(String name, String value);
508508

509+
/**
510+
* Header filter regexp.
511+
*
512+
* @param headerFilterRegexp the header filter regexp
513+
*/
514+
void headerFilterRegexp(String headerFilterRegexp);
515+
509516
/**
510517
* Removes a header previously added with {@link #addHeader(String name, String value)}.
511518
*
@@ -545,8 +552,8 @@ public interface BrowserMobProxy {
545552
* for the quiet period within the specified timeout, otherwise returns false.
546553
*
547554
* @param quietPeriod amount of time after which network traffic will be considered "stopped"
548-
* @param timeout maximum amount of time to wait for network traffic to stop
549-
* @param timeUnit TimeUnit for the quietPeriod and timeout
555+
* @param timeout maximum amount of time to wait for network traffic to stop
556+
* @param timeUnit TimeUnit for the quietPeriod and timeout
550557
* @return true if network traffic is stopped, otherwise false
551558
*/
552559
boolean waitForQuiescence(long quietPeriod, long timeout, TimeUnit timeUnit);
@@ -587,7 +594,7 @@ public interface BrowserMobProxy {
587594
* {@link org.littleshoot.proxy.HttpFilters} instance (typically, a subclass of {@link org.littleshoot.proxy.HttpFiltersAdapter}).
588595
* To disable or bypass a filter on a per-request basis, the filterRequest() method may return null.
589596
*
590-
* @param filterFactory factory to generate HttpFilters
597+
* @param filterFactory factory to generate HttpFilters
591598
*/
592599
void addLastHttpFilterFactory(HttpFiltersSource filterFactory);
593600

browsermob-core/src/main/java/net/lightbody/bmp/BrowserMobProxyServer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public class BrowserMobProxyServer implements BrowserMobProxy {
145145
*/
146146
private volatile int connectTimeoutMs;
147147

148+
/**
149+
* Regexp to check request url and inject headers if url match regexp.
150+
*/
151+
private static String headersFilterRegexp;
152+
148153
/**
149154
* The amount of time a connection to a server can remain idle while receiving data from the server.
150155
*/
@@ -592,6 +597,11 @@ public void addHeaders(Map<String, String> headers) {
592597
this.additionalHeaders = newHeaders;
593598
}
594599

600+
@Override
601+
public void headerFilterRegexp(String headerFilterRegexp) {
602+
this.headersFilterRegexp = headerFilterRegexp;
603+
}
604+
595605
@Override
596606
public void setLatency(long latency, TimeUnit timeUnit) {
597607
this.latencyMs = (int) TimeUnit.MILLISECONDS.convert(latency, timeUnit);
@@ -1052,8 +1062,8 @@ public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerCont
10521062

10531063
addHttpFilterFactory(new HttpFiltersSourceAdapter() {
10541064
@Override
1055-
public HttpFilters filterRequest(HttpRequest originalRequest) {
1056-
return new AddHeadersFilter(originalRequest, additionalHeaders);
1065+
public HttpFilters filterRequest(HttpRequest originalRequest,ChannelHandlerContext ctx) {
1066+
return new AddHeadersFilter(originalRequest,ctx, additionalHeaders, headersFilterRegexp);
10571067
}
10581068
});
10591069

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package net.lightbody.bmp.filters;
22

3+
import io.netty.channel.ChannelHandlerContext;
34
import io.netty.handler.codec.http.HttpObject;
45
import io.netty.handler.codec.http.HttpRequest;
56
import io.netty.handler.codec.http.HttpResponse;
6-
import org.littleshoot.proxy.HttpFiltersAdapter;
7+
import org.apache.commons.lang3.StringUtils;
78

89
import java.util.Collections;
910
import java.util.Map;
@@ -12,29 +13,48 @@
1213
* Adds the headers specified in the constructor to this request. The filter does not make a defensive copy of the map, so there is no guarantee
1314
* that the map at the time of construction will contain the same values when the filter is actually invoked, if the map is modified concurrently.
1415
*/
15-
public class AddHeadersFilter extends HttpFiltersAdapter {
16+
public class AddHeadersFilter extends HttpsAwareFiltersAdapter {
1617
private final Map<String, String> additionalHeaders;
18+
private static String headersSpecificFilter;
1719

18-
public AddHeadersFilter(HttpRequest originalRequest, Map<String, String> additionalHeaders) {
19-
super(originalRequest);
20+
public AddHeadersFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Map<String, String> additionalHeaders, String headersSpecificFilter) {
21+
super(originalRequest, ctx);
2022

2123
if (additionalHeaders != null) {
2224
this.additionalHeaders = additionalHeaders;
2325
} else {
2426
this.additionalHeaders = Collections.emptyMap();
2527
}
28+
if (StringUtils.isNotEmpty(headersSpecificFilter)) {
29+
setHeadersSpecificFilter(headersSpecificFilter);
30+
}
2631
}
2732

2833
@Override
2934
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
3035
if (httpObject instanceof HttpRequest) {
3136
HttpRequest httpRequest = (HttpRequest) httpObject;
32-
33-
for (Map.Entry<String, String> header : additionalHeaders.entrySet()) {
34-
httpRequest.headers().add(header.getKey(), header.getValue());
37+
if (StringUtils.isNotEmpty(headersSpecificFilter)) {
38+
if (getFullUrl(httpRequest).matches(headersSpecificFilter)) {
39+
for (Map.Entry<String, String> header : additionalHeaders.entrySet()) {
40+
httpRequest.headers().add(header.getKey(), header.getValue());
41+
}
42+
}
43+
} else {
44+
for (Map.Entry<String, String> header : additionalHeaders.entrySet()) {
45+
httpRequest.headers().add(header.getKey(), header.getValue());
46+
}
3547
}
3648
}
3749

3850
return null;
3951
}
52+
53+
public String getHeadersSpecificFilter() {
54+
return headersSpecificFilter;
55+
}
56+
57+
private synchronized void setHeadersSpecificFilter(String headersSpecificFilter) {
58+
this.headersSpecificFilter = headersSpecificFilter;
59+
}
4060
}

browsermob-dist/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>browsermob-proxy</artifactId>
66
<groupId>net.lightbody.bmp</groupId>
7-
<version>2.1.33-SNAPSHOT</version>
7+
<version>2.1.34-SNAPSHOT</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010

browsermob-legacy/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>browsermob-proxy</artifactId>
88
<groupId>net.lightbody.bmp</groupId>
9-
<version>2.1.33-SNAPSHOT</version>
9+
<version>2.1.34-SNAPSHOT</version>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
1212

browsermob-legacy/src/main/java/net/lightbody/bmp/proxy/LegacyProxyServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
/**
2020
* Describes the legacy BrowserMob Proxy 2.0 interface. Clients <b>should not</b> implement or use this interface.
21-
*
21+
* <p>
2222
* Use {@link BrowserMobProxy}.
2323
*/
2424
public interface LegacyProxyServer {
@@ -122,6 +122,8 @@ public interface LegacyProxyServer {
122122

123123
void addHeader(String name, String value);
124124

125+
void headerFilterRegexp(String headerFilterRegexp);
126+
125127
void setCaptureHeaders(boolean captureHeaders);
126128

127129
void setCaptureContent(boolean captureContent);

0 commit comments

Comments
 (0)