Skip to content

Commit 0df36c9

Browse files
committed
Conflicts: pom.xml src/main/java/net/lightbody/bmp/proxy/ProxyServer.java src/main/java/net/lightbody/bmp/proxy/http/BrowserMobHttpClient.java
2 parents 0566689 + c34bf5e commit 0df36c9

File tree

13 files changed

+144
-65
lines changed

13 files changed

+144
-65
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ Once that is done, a new proxy will be available on the port returned. All you h
5353
- pageRef - the string name of the first page ref that should be used in the HAR. Defaults to "Page N" where N is the next page number.
5454
- DELETE /proxy/[port] - shuts down the proxy and closes the port
5555
- GET /proxy/[port]/har - returns the JSON/HAR content representing all the HTTP traffic passed through the proxy
56+
- GET /proxy/[port]/whitelist - Displays whitelisted items
5657
- PUT /proxy/[port]/whitelist - Sets a list of URL patterns to whitelist. Takes the following parameters:
5758
- regex - a comma separated list of regular expressions
5859
- status - the HTTP status code to return for URLs that do not match the whitelist
5960
- DELETE /proxy/[port]/whitelist - Clears all URL patterns from the whitelist
61+
- GET /proxy/[port]/blacklist - Displays blacklisted items
6062
- PUT /proxy/[port]/blacklist - Set a URL to blacklist. Takes the following parameters:
6163
- regex - the blacklist regular expression
6264
- status - the HTTP status code to return for URLs that are blacklisted
@@ -207,3 +209,11 @@ NodeJS Support
207209
--------------
208210

209211
NodeJS bindings for browswermob-proxy are available [here](https://github.com/zzo/browsermob-node). Built-in support for [Selenium](http://seleniumhq.org) or use [CapserJS-on-PhantomJS](http://casperjs.org) or anything else to drive traffic for HAR generation.
212+
213+
Creating the batch files from source
214+
------------------------------------
215+
216+
You'll need maven (`brew install maven` if you're on OS X); use the `release` profile to generate the batch files from this repository. Optionally, proceed at your own risk and append the `-DskipTests` option if the tests are failing.
217+
218+
[~]$ mvn -P release
219+
[~]$ mvn -DskipTests -P release

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@
9999
</profiles>
100100

101101
<build>
102+
<resources>
103+
<resource>
104+
<directory>src/main/resources</directory>
105+
<filtering>true</filtering>
106+
<includes>
107+
<include>**/**</include>
108+
</includes>
109+
</resource>
110+
</resources>
102111
<defaultGoal>install</defaultGoal>
103112
<plugins>
104113
<plugin>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.lightbody.bmp.proxy;
2+
3+
import java.util.regex.Pattern;
4+
5+
public class BlacklistEntry
6+
{
7+
private Pattern pattern;
8+
private int responseCode;
9+
10+
public BlacklistEntry(String pattern, int responseCode) {
11+
this.pattern = Pattern.compile(pattern);
12+
this.responseCode = responseCode;
13+
}
14+
15+
public Pattern getPattern() {
16+
return this.pattern;
17+
}
18+
19+
public int getResponseCode() {
20+
return this.responseCode;
21+
}
22+
}

src/main/java/net/lightbody/bmp/proxy/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public static void main(String[] args) throws Exception {
2727
configureLogging();
2828

2929
String version = "UNKNOWN/DEVELOPMENT";
30-
InputStream is = Main.class.getResourceAsStream("/META-INF/maven/net.lightbody.bmp/browsermob-proxy/pom.properties");
30+
InputStream is = Main.class.getResourceAsStream("/version.prop");
31+
3132
if (is != null) {
3233
Properties props = new Properties();
3334
props.load(is);
@@ -41,7 +42,7 @@ protected void configureSitebricks() {
4142
}
4243
});
4344

44-
LOG.info("Starting BrowserMob Proxy version %s", version);
45+
LOG.info("Starting BrowserMob Proxy version {}", version);
4546

4647
Server server = injector.getInstance(Server.class);
4748
GuiceServletContextListener gscl = new GuiceServletContextListener() {

src/main/java/net/lightbody/bmp/proxy/ProxyServer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.SocketException;
66
import java.net.UnknownHostException;
77
import java.util.Date;
8+
import java.util.List;
89
import java.util.Map;
910
import java.util.concurrent.TimeUnit;
1011
import java.util.concurrent.atomic.AtomicInteger;
@@ -31,7 +32,6 @@
3132
import org.java_bandwidthlimiter.StreamManager;
3233
import org.openqa.selenium.Proxy;
3334

34-
3535
public class ProxyServer {
3636
private static final HarNameVersion CREATOR = new HarNameVersion("BrowserMob Proxy", "2.0");
3737
private static final Log LOG = new Log();
@@ -299,6 +299,14 @@ public void clearRewriteRules() {
299299
public void blacklistRequests(String pattern, int responseCode) {
300300
client.blacklistRequests(pattern, responseCode);
301301
}
302+
303+
public List<BlacklistEntry> getBlacklistedRequests() {
304+
return client.getBlacklistedRequests();
305+
}
306+
307+
public WhitelistEntry getWhitelistRequests() {
308+
return client.getWhitelistRequests();
309+
}
302310

303311
public void clearBlacklist() {
304312
client.clearBlacklist();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.lightbody.bmp.proxy;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CopyOnWriteArrayList;
5+
import java.util.regex.Pattern;
6+
7+
public class WhitelistEntry {
8+
private List<Pattern> patterns = new CopyOnWriteArrayList<Pattern>();
9+
private int responseCode;
10+
11+
public WhitelistEntry(String[] patterns, int responseCode) {
12+
for (String pattern : patterns) {
13+
this.patterns.add(Pattern.compile(pattern));
14+
}
15+
this.responseCode = responseCode;
16+
}
17+
18+
public List<Pattern> getPatterns() {
19+
return this.patterns;
20+
}
21+
22+
public int getResponseCode() {
23+
return this.responseCode;
24+
}
25+
}

src/main/java/net/lightbody/bmp/proxy/bricks/ProxyResource.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ public Reply<?> setPage(@Named("port") int port, Request request) {
126126
return Reply.saying().ok();
127127
}
128128

129+
@Get
130+
@At("/:port/blacklist")
131+
public Reply<?> getBlacklist(@Named("port") int port, Request request) {
132+
ProxyServer proxy = proxyManager.get(port);
133+
if (proxy == null) {
134+
return Reply.saying().notFound();
135+
}
136+
137+
return Reply.with(proxy.getBlacklistedRequests()).as(Json.class);
138+
}
139+
129140
@Put
130141
@At("/:port/blacklist")
131142
public Reply<?> blacklist(@Named("port") int port, Request request) {
@@ -149,8 +160,19 @@ public Reply<?> clearBlacklist(@Named("port") int port, Request request) {
149160
return Reply.saying().notFound();
150161
}
151162

152-
proxy.clearBlacklist();
153-
return Reply.saying().ok();
163+
proxy.clearBlacklist();
164+
return Reply.saying().ok();
165+
}
166+
167+
@Get
168+
@At("/:port/whitelist")
169+
public Reply<?> getWhitelist(@Named("port") int port, Request request) {
170+
ProxyServer proxy = proxyManager.get(port);
171+
if (proxy == null) {
172+
return Reply.saying().notFound();
173+
}
174+
175+
return Reply.with(proxy.getWhitelistRequests()).as(Json.class);
154176
}
155177

156178
@Put
@@ -171,13 +193,13 @@ public Reply<?> whitelist(@Named("port") int port, Request request) {
171193
@Delete
172194
@At("/:port/whitelist")
173195
public Reply<?> clearWhitelist(@Named("port") int port, Request request) {
174-
ProxyServer proxy = proxyManager.get(port);
196+
ProxyServer proxy = proxyManager.get(port);
175197
if (proxy == null) {
176198
return Reply.saying().notFound();
177199
}
178200

179-
proxy.clearWhitelist();
180-
return Reply.saying().ok();
201+
proxy.clearWhitelist();
202+
return Reply.saying().ok();
181203
}
182204

183205
@Post

src/main/java/net/lightbody/bmp/proxy/http/BrowserMobHttpClient.java

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,11 @@
2929
import java.util.zip.InflaterInputStream;
3030
import java.util.zip.Inflater;
3131

32-
import net.lightbody.bmp.core.har.Har;
33-
import net.lightbody.bmp.core.har.HarCookie;
34-
import net.lightbody.bmp.core.har.HarEntry;
35-
import net.lightbody.bmp.core.har.HarNameValuePair;
36-
import net.lightbody.bmp.core.har.HarNameVersion;
37-
import net.lightbody.bmp.core.har.HarPostData;
38-
import net.lightbody.bmp.core.har.HarPostDataParam;
39-
import net.lightbody.bmp.core.har.HarRequest;
40-
import net.lightbody.bmp.core.har.HarResponse;
41-
import net.lightbody.bmp.core.har.HarTimings;
42-
import net.lightbody.bmp.proxy.util.Base64;
43-
import net.lightbody.bmp.proxy.util.CappedByteArrayOutputStream;
44-
import net.lightbody.bmp.proxy.util.ClonedOutputStream;
45-
import net.lightbody.bmp.proxy.util.IOUtils;
46-
import net.lightbody.bmp.proxy.util.Log;
32+
import net.lightbody.bmp.core.har.*;
33+
import net.lightbody.bmp.proxy.BlacklistEntry;
34+
import net.lightbody.bmp.proxy.WhitelistEntry;
35+
import net.lightbody.bmp.proxy.util.*;
36+
4737
import net.sf.uadetector.ReadableUserAgent;
4838
import net.sf.uadetector.UserAgentStringParser;
4939
import net.sf.uadetector.service.UADetectorServiceFactory;
@@ -158,6 +148,7 @@ public class BrowserMobHttpClient {
158148
*/
159149
private TrustingSSLSocketFactory sslSocketFactory;
160150

151+
161152
private PoolingHttpClientConnectionManager httpClientConnMgr;
162153

163154
/**
@@ -182,6 +173,7 @@ public class BrowserMobHttpClient {
182173
/**
183174
* List of accepted URL patterns
184175
*/
176+
185177
private WhitelistEntry whitelistEntry = null;
186178

187179
/**
@@ -657,7 +649,7 @@ private BrowserMobHttpResponse execute(BrowserMobHttpRequest req, int depth) {
657649
// guard against concurrent modification of whitelistEntry
658650
if (whitelistEntry != null) {
659651
boolean found = false;
660-
for (Pattern pattern : whitelistEntry.patterns) {
652+
for (Pattern pattern : whitelistEntry.getPatterns()) {
661653
if (pattern.matcher(url).matches()) {
662654
found = true;
663655
break;
@@ -666,16 +658,15 @@ private BrowserMobHttpResponse execute(BrowserMobHttpRequest req, int depth) {
666658

667659
// url does not match whitelist, set the response code
668660
if (!found) {
669-
mockResponseCode = whitelistEntry.responseCode;
661+
mockResponseCode = whitelistEntry.getResponseCode();
670662
}
671663
}
672664
}
673665

674666
if (blacklistEntries != null) {
675667
for (BlacklistEntry blacklistEntry : blacklistEntries) {
676-
// url does match whitelist, set the response code
677-
if (blacklistEntry.pattern.matcher(url).matches()) {
678-
mockResponseCode = blacklistEntry.responseCode;
668+
if (blacklistEntry.getPattern().matcher(url).matches()) {
669+
mockResponseCode = blacklistEntry.getResponseCode();
679670
break;
680671
}
681672
}
@@ -710,8 +701,8 @@ private BrowserMobHttpResponse execute(BrowserMobHttpRequest req, int depth) {
710701
// clear out any connection-related information so that it's not stale from previous use of this thread.
711702
RequestInfo.clear(url, entry);
712703

713-
entry.setRequest(new HarRequest(method.getMethod(), url, method.getProtocolVersion().getProtocol()));
714-
entry.setResponse(new HarResponse(-999, "NO RESPONSE", method.getProtocolVersion().getProtocol()));
704+
entry.setRequest(new HarRequest(method.getMethod(), url, method.getProtocolVersion().toString()));
705+
entry.setResponse(new HarResponse(-999, "NO RESPONSE", method.getProtocolVersion().toString()));
715706
if (this.har != null && harPageRef != null) {
716707
har.getLog().addEntry(entry);
717708
}
@@ -882,6 +873,7 @@ public HeaderElement[] getElements() throws ParseException {
882873
entry.getResponse().setBodySize(bytes);
883874
entry.getResponse().getContent().setSize(bytes);
884875
entry.getResponse().setStatus(statusCode);
876+
entry.getResponse().setHttpVersion(response.getProtocolVersion().toString());
885877
if (statusLine != null) {
886878
entry.getResponse().setStatusText(statusLine.getReasonPhrase());
887879
}
@@ -1190,18 +1182,26 @@ public void blacklistRequests(String pattern, int responseCode) {
11901182
blacklistEntries.add(new BlacklistEntry(pattern, responseCode));
11911183
}
11921184

1185+
public List<BlacklistEntry> getBlacklistedRequests() {
1186+
return blacklistEntries;
1187+
}
1188+
11931189
public void clearBlacklist() {
1194-
blacklistEntries.clear();
1190+
blacklistEntries.clear();
1191+
}
1192+
1193+
public WhitelistEntry getWhitelistRequests() {
1194+
return whitelistEntry;
11951195
}
11961196

11971197
public synchronized void whitelistRequests(String[] patterns, int responseCode) {
1198-
// synchronized to guard against concurrent modification
1198+
// synchronized to guard against concurrent modification
11991199
whitelistEntry = new WhitelistEntry(patterns, responseCode);
12001200
}
12011201

12021202
public synchronized void clearWhitelist() {
1203-
// synchronized to guard against concurrent modification
1204-
whitelistEntry = null;
1203+
// synchronized to guard against concurrent modification
1204+
whitelistEntry = null;
12051205
}
12061206

12071207
public void addHeader(String name, String value) {
@@ -1345,29 +1345,6 @@ public void abort() {
13451345
}
13461346
}
13471347

1348-
private class WhitelistEntry {
1349-
private List<Pattern> patterns = new CopyOnWriteArrayList<Pattern>();
1350-
// the HTTP status code to return for URLs that do not match the whitelist
1351-
private int responseCode;
1352-
1353-
private WhitelistEntry(String[] patterns, int responseCode) {
1354-
for (String pattern : patterns) {
1355-
this.patterns.add(Pattern.compile(pattern));
1356-
}
1357-
this.responseCode = responseCode;
1358-
}
1359-
}
1360-
1361-
private class BlacklistEntry {
1362-
private Pattern pattern;
1363-
private int responseCode;
1364-
1365-
private BlacklistEntry(String pattern, int responseCode) {
1366-
this.pattern = Pattern.compile(pattern);
1367-
this.responseCode = responseCode;
1368-
}
1369-
}
1370-
13711348
private class RewriteRule {
13721349
private Pattern match;
13731350
private String replace;

src/main/resources/version.prop

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version=${project.version}

src/test/java/net/lightbody/bmp/proxy/CookieTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package net.lightbody.bmp.proxy;
22

3-
import junit.framework.Assert;
43
import net.lightbody.bmp.core.har.Har;
54
import net.lightbody.bmp.core.har.HarCookie;
65
import net.lightbody.bmp.core.har.HarEntry;
76
import net.lightbody.bmp.proxy.util.IOUtils;
87
import org.apache.http.client.methods.HttpGet;
98
import org.apache.http.impl.cookie.BasicClientCookie;
9+
import org.junit.Assert;
1010
import org.junit.Test;
1111

1212
import java.io.IOException;

src/test/java/net/lightbody/bmp/proxy/MailingListIssuesTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.lightbody.bmp.proxy;
22

3-
import junit.framework.Assert;
3+
import org.junit.Assert;
44
import net.lightbody.bmp.core.har.*;
55
import net.lightbody.bmp.core.util.ThreadUtils;
66
import net.lightbody.bmp.proxy.http.BrowserMobHttpRequest;
@@ -214,6 +214,10 @@ public void testThatTraditionalPostParamsAreCaptured() throws IOException, Inter
214214
Assert.assertEquals(1, postdata.getParams().size());
215215
Assert.assertEquals("foo", postdata.getParams().get(0).getName());
216216
Assert.assertEquals("bar", postdata.getParams().get(0).getValue());
217+
/** TODO
218+
It runs fine until the bar assert which is different.
219+
it expects bar but gets bar\u0000\u0000\u0000\u0000...foo=bar where the \u0000 repeats a lot, total char array for value has size of 8195
220+
*/
217221
}
218222

219223
@Test
@@ -385,7 +389,7 @@ public void issue27() throws Exception{
385389

386390
// show that we can capture the HTML of the root page
387391
String text = har.getLog().getEntries().get(0).getResponse().getContent().getText();
388-
Assert.assertTrue(text.contains("<title>Whats My User Agent?</title>"));
392+
Assert.assertTrue(text.contains("<title>\r\n\tWhat's My User Agent?\r\n</title>"));
389393
} finally {
390394
server.stop();
391395
if (driver != null) {

0 commit comments

Comments
 (0)