Skip to content

Commit dcec66e

Browse files
authored
Merge pull request #531 from jekh/set-chained-proxy-before-start
Throw exception when calling setChainedProxy without bootstrapping a ChainedProxyManager
2 parents 177f346 + be37698 commit dcec66e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,11 @@ public interface BrowserMobProxy {
552552
boolean waitForQuiescence(long quietPeriod, long timeout, TimeUnit timeUnit);
553553

554554
/**
555-
* Sets an upstream proxy that this proxy will use to connect to external hosts.
555+
* Instructs this proxy to route traffic through an upstream proxy.
556556
*
557-
* @param chainedProxyAddress address and port of the upstream proxy, or null to remove an upstream proxy
557+
* <b>Note:</b> A chained proxy must be set before the proxy is started, though it can be changed after the proxy is started.
558+
*
559+
* @param chainedProxyAddress address of the upstream proxy
558560
*/
559561
void setChainedProxy(InetSocketAddress chainedProxyAddress);
560562

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ public class BrowserMobProxyServer implements BrowserMobProxy {
198198
*/
199199
private final AtomicBoolean harCaptureFilterEnabled = new AtomicBoolean(false);
200200

201+
/**
202+
* Set to true when LittleProxy has been bootstrapped with the default chained proxy. This allows modifying the chained proxy
203+
* after the proxy has been started.
204+
*/
205+
private final AtomicBoolean bootstrappedWithDefaultChainedProxy = new AtomicBoolean(false);
206+
201207
/**
202208
* The address of an upstream chained proxy to route traffic through.
203209
*/
@@ -327,6 +333,10 @@ public int getMaximumResponseBufferSizeInBytes() {
327333
if (chainedProxyManager != null) {
328334
bootstrap.withChainProxyManager(chainedProxyManager);
329335
} else if (upstreamProxyAddress != null) {
336+
// indicate that the proxy was bootstrapped with the default chained proxy manager, which allows changing the
337+
// chained proxy after the proxy is started.
338+
bootstrappedWithDefaultChainedProxy.set(true);
339+
330340
bootstrap.withChainProxyManager(new ChainedProxyManager() {
331341
@Override
332342
public void lookupChainedProxies(HttpRequest httpRequest, Queue<ChainedProxy> chainedProxies) {
@@ -860,15 +870,19 @@ public boolean waitForQuiescence(long quietPeriod, long timeout, TimeUnit timeUn
860870
}
861871

862872
/**
863-
* Instructs this proxy to route traffic through an upstream proxy. Proxy chaining is not compatible with man-in-the-middle
864-
* SSL, so HAR capture will be disabled for HTTPS traffic when using an upstream proxy.
865-
* <p>
866-
* <b>Note:</b> Using {@link #setChainedProxyManager(ChainedProxyManager)} will supersede any value set by this method.
873+
* Instructs this proxy to route traffic through an upstream proxy.
874+
*
875+
* <b>Note:</b> Using {@link #setChainedProxyManager(ChainedProxyManager)} will supersede any value set by this method. A chained
876+
* proxy must be set before the proxy is started, though it can be changed after the proxy is started.
867877
*
868878
* @param chainedProxyAddress address of the upstream proxy
869879
*/
870880
@Override
871881
public void setChainedProxy(InetSocketAddress chainedProxyAddress) {
882+
if (isStarted() && !bootstrappedWithDefaultChainedProxy.get()) {
883+
throw new IllegalStateException("Cannot set a chained proxy after the proxy is started if the proxy was started without a chained proxy.");
884+
}
885+
872886
upstreamProxyAddress = chainedProxyAddress;
873887
}
874888

@@ -881,6 +895,8 @@ public InetSocketAddress getChainedProxy() {
881895
* Allows access to the LittleProxy {@link ChainedProxyManager} for fine-grained control of the chained proxies. To enable a single
882896
* chained proxy, {@link BrowserMobProxy#setChainedProxy(InetSocketAddress)} is generally more convenient.
883897
*
898+
* <b>Note:</b> The chained proxy manager must be enabled before calling {@link #start()}.
899+
*
884900
* @param chainedProxyManager chained proxy manager to enable
885901
*/
886902
public void setChainedProxyManager(ChainedProxyManager chainedProxyManager) {

0 commit comments

Comments
 (0)