@Put @At("/:port/har") public Reply<?> newHar(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String initialPageRef = request.param("initialPageRef"); String initialPageTitle = request.param("initialPageTitle"); Har oldHar = proxy.newHar(initialPageRef, initialPageTitle); String captureHeaders = request.param("captureHeaders"); String captureContent = request.param("captureContent"); String captureBinaryContent = request.param("captureBinaryContent"); proxy.setCaptureHeaders(Boolean.parseBoolean(captureHeaders)); proxy.setCaptureContent(Boolean.parseBoolean(captureContent)); proxy.setCaptureBinaryContent(Boolean.parseBoolean(captureBinaryContent)); if (oldHar != null) { return Reply.with(oldHar).as(Json.class); } else { return Reply.saying().noContent(); } }
@Post public Reply<?> newProxy(Request<String> request) { String systemProxyHost = System.getProperty("http.proxyHost"); String systemProxyPort = System.getProperty("http.proxyPort"); String httpProxy = request.param("httpProxy"); Hashtable<String, String> options = new Hashtable<String, String>(); // If the upstream proxy is specified via query params that should override any default system // level proxy. if (httpProxy != null) { options.put("httpProxy", httpProxy); } else if ((systemProxyHost != null) && (systemProxyPort != null)) { options.put("httpProxy", String.format("%s:%s", systemProxyHost, systemProxyPort)); } String paramBindAddr = request.param("bindAddress"); Integer paramPort = request.param("port") == null ? null : Integer.parseInt(request.param("port")); String useEccString = request.param("useEcc"); boolean useEcc = Boolean.parseBoolean(useEccString); LOG.debug("POST proxy instance on bindAddress `{}` & port `{}`", paramBindAddr, paramPort); LegacyProxyServer proxy; try { proxy = proxyManager.create(options, paramPort, paramBindAddr, useEcc); } catch (ProxyExistsException ex) { return Reply.with(new ProxyDescriptor(ex.getPort())).status(455).as(Json.class); } catch (ProxyPortsExhaustedException ex) { return Reply.saying().status(456); } catch (Exception ex) { StringWriter s = new StringWriter(); ex.printStackTrace(new PrintWriter(s)); return Reply.with(s).as(Text.class).status(550); } return Reply.with(new ProxyDescriptor(proxy.getPort())).as(Json.class); }
@Override public boolean shouldCall(Request request) { boolean should; if (null != selectParams) { for (Map.Entry<String, String> select : selectParams.entrySet()) { if (!select.getValue().equals(request.param(select.getKey()))) { should = false; } } } if (null != selectHeaders) { for (Map.Entry<String, String> header : selectHeaders.entrySet()) { if (!header.getValue().equals(request.header(header.getKey()))) { should = false; } } } // (JFA) Might be a good idea to pass the value of should as a request attribute // so an action can see if what was the value before getting invoked and take a decision based // on it. should = action.shouldCall(request); return should; }
private String getEntityBodyFromRequest(Request<String> request) throws IOException { String contentTypeHeader = request.header("Content-Type"); Charset charset = BrowserMobHttpUtil.deriveCharsetFromContentTypeHeader(contentTypeHeader); ByteArrayOutputStream entityBodyBytes = new ByteArrayOutputStream(); request.readTo(entityBodyBytes); return new String(entityBodyBytes.toByteArray(), charset); }
@Put @At("/:port/rewrite") public Reply<?> rewriteUrl(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String match = request.param("matchRegex"); String replace = request.param("replace"); proxy.rewriteUrl(match, replace); return Reply.saying().ok(); }
@Put @At("/:port/har/pageRef") public Reply<?> setPage(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String pageRef = request.param("pageRef"); String pageTitle = request.param("pageTitle"); proxy.newPage(pageRef, pageTitle); return Reply.saying().ok(); }
@Put @At("/:port/wait") public Reply<?> wait(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String quietPeriodInMs = request.param("quietPeriodInMs"); String timeoutInMs = request.param("timeoutInMs"); proxy.waitForNetworkTrafficToStop( Integer.parseInt(quietPeriodInMs), Integer.parseInt(timeoutInMs)); return Reply.saying().ok(); }
@Put @At("/:port/whitelist") public Reply<?> whitelist(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String regex = request.param("regex"); int responseCode = parseResponseCode(request.param("status")); proxy.whitelistRequests(regex.split(","), responseCode); return Reply.saying().ok(); }
@Put @At("/:port/blacklist") public Reply<?> blacklist(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String blacklist = request.param("regex"); int responseCode = parseResponseCode(request.param("status")); String method = request.param("method"); proxy.blacklistRequests(blacklist, responseCode, method); return Reply.saying().ok(); }
@Put @At("/:port/retry") public Reply<?> retryCount(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String count = request.param("retrycount"); proxy.setRetryCount(Integer.parseInt(count)); return Reply.saying().ok(); }
@Put @At("/:port/timeout") public Reply<?> timeout(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } String requestTimeout = request.param("requestTimeout"); if (requestTimeout != null) { try { proxy.setRequestTimeout(Integer.parseInt(requestTimeout)); } catch (NumberFormatException e) { } } String readTimeout = request.param("readTimeout"); if (readTimeout != null) { try { proxy.setSocketOperationTimeout(Integer.parseInt(readTimeout)); } catch (NumberFormatException e) { } } String connectionTimeout = request.param("connectionTimeout"); if (connectionTimeout != null) { try { proxy.setConnectionTimeout(Integer.parseInt(connectionTimeout)); } catch (NumberFormatException e) { } } String dnsCacheTimeout = request.param("dnsCacheTimeout"); if (dnsCacheTimeout != null) { try { proxy.setDNSCacheTimeout(Integer.parseInt(dnsCacheTimeout)); } catch (NumberFormatException e) { } } return Reply.saying().ok(); }
@Post @At("/:port/auth/basic/:domain") public Reply<?> autoBasicAuth( @Named("port") int port, @Named("domain") String domain, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } Map<String, String> credentials = request.read(HashMap.class).as(Json.class); proxy.autoBasicAuthorization(domain, credentials.get("username"), credentials.get("password")); return Reply.saying().ok(); }
private String getEntityBodyFromRequest(Request<String> request) throws IOException { String contentTypeHeader = request.header("Content-Type"); Charset charset = null; try { charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader); } catch (UnsupportedCharsetException e) { java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause(); LOG.error( "Character set declared in Content-Type header is not supported. Content-Type header: {}", contentTypeHeader, cause); throw cause; } if (charset == null) { charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET; } ByteArrayOutputStream entityBodyBytes = new ByteArrayOutputStream(); request.readTo(entityBodyBytes); return new String(entityBodyBytes.toByteArray(), charset); }
@Post @At("/:port/headers") public Reply<?> updateHeaders(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } Map<String, String> headers = request.read(Map.class).as(Json.class); for (Map.Entry<String, String> entry : headers.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); proxy.addHeader(key, value); } return Reply.saying().ok(); }
@Post @At("/:port/hosts") public Reply<?> remapHosts(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } @SuppressWarnings("unchecked") Map<String, String> headers = request.read(Map.class).as(Json.class); for (Map.Entry<String, String> entry : headers.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); proxy.remapHost(key, value); proxy.setDNSCacheTimeout(0); proxy.clearDNSCache(); } return Reply.saying().ok(); }
@Post @At("/:port/interceptor/request") public Reply<?> addRequestInterceptor(@Named("port") int port, Request<String> request) throws IOException, ScriptException { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } if (!(proxy instanceof ProxyServer)) { return Reply.saying().badRequest(); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); request.readTo(baos); ScriptEngineManager mgr = new ScriptEngineManager(); final ScriptEngine engine = mgr.getEngineByName("JavaScript"); Compilable compilable = (Compilable) engine; final CompiledScript script = compilable.compile(baos.toString()); proxy.addRequestInterceptor( new RequestInterceptor() { @Override public void process(BrowserMobHttpRequest request, Har har) { Bindings bindings = engine.createBindings(); bindings.put("request", request); bindings.put("har", har); bindings.put("log", LOG); try { script.eval(bindings); } catch (ScriptException e) { LOG.error("Could not execute JS-based response interceptor", e); } } }); return Reply.saying().ok(); }
@Put @At("/:port/limit") public Reply<?> limit(@Named("port") int port, Request<String> request) { LegacyProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } StreamManager streamManager = proxy.getStreamManager(); String upstreamKbps = request.param("upstreamKbps"); if (upstreamKbps != null) { try { streamManager.setUpstreamKbps(Integer.parseInt(upstreamKbps)); streamManager.enable(); } catch (NumberFormatException e) { } } String upstreamBps = request.param("upstreamBps"); if (upstreamBps != null) { try { ((BrowserMobProxy) proxy).setWriteBandwidthLimit(Integer.parseInt(upstreamBps)); } catch (NumberFormatException e) { } } String downstreamKbps = request.param("downstreamKbps"); if (downstreamKbps != null) { try { streamManager.setDownstreamKbps(Integer.parseInt(downstreamKbps)); streamManager.enable(); } catch (NumberFormatException e) { } } String downstreamBps = request.param("downstreamBps"); if (downstreamBps != null) { try { ((BrowserMobProxy) proxy).setReadBandwidthLimit(Integer.parseInt(downstreamBps)); } catch (NumberFormatException e) { } } String upstreamMaxKB = request.param("upstreamMaxKB"); if (upstreamMaxKB != null) { try { streamManager.setUpstreamMaxKB(Integer.parseInt(upstreamMaxKB)); streamManager.enable(); } catch (NumberFormatException e) { } } String downstreamMaxKB = request.param("downstreamMaxKB"); if (downstreamMaxKB != null) { try { streamManager.setDownstreamMaxKB(Integer.parseInt(downstreamMaxKB)); streamManager.enable(); } catch (NumberFormatException e) { } } String latency = request.param("latency"); if (latency != null) { try { streamManager.setLatency(Integer.parseInt(latency)); streamManager.enable(); } catch (NumberFormatException e) { } } String payloadPercentage = request.param("payloadPercentage"); if (payloadPercentage != null) { try { streamManager.setPayloadPercentage(Integer.parseInt(payloadPercentage)); } catch (NumberFormatException e) { } } String maxBitsPerSecond = request.param("maxBitsPerSecond"); if (maxBitsPerSecond != null) { try { streamManager.setMaxBitsPerSecondThreshold(Integer.parseInt(maxBitsPerSecond)); } catch (NumberFormatException e) { } } String enable = request.param("enable"); if (enable != null) { if (Boolean.parseBoolean(enable)) { streamManager.enable(); } else { streamManager.disable(); } } return Reply.saying().ok(); }