net.lightbody.bmp.proxy.CaptureType Java Examples

The following examples show how to use net.lightbody.bmp.proxy.CaptureType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BrowserMobTest.java    From carina with Apache License 2.0 6 votes vote down vote up
@Test
public void testBrowserModProxyResponseFiltering() {
    List<String> content = new ArrayList<>();
    LocalTrustStoreBuilder localTrustStoreBuilder = new LocalTrustStoreBuilder();
    SSLContext sslContext = localTrustStoreBuilder.createSSLContext();
    SSLContext.setDefault(sslContext);

    ProxyPool.setupBrowserMobProxy();
    SystemProxy.setupProxy();
    BrowserMobProxy proxy = ProxyPool.getProxy();
    proxy.enableHarCaptureTypes(CaptureType.RESPONSE_CONTENT);
    proxy.newHar();

    proxy.addResponseFilter((request, contents, messageInfo) -> {
        LOGGER.info("Requested resource caught contents: " + contents.getTextContents());
        if (contents.getTextContents().contains(filterKey)) {
            content.add(contents.getTextContents());
        }
    });

    makeHttpRequest(testUrl, requestMethod);

    Assert.assertNotNull(proxy.getHar(), "Har is unexpectedly null!");
    Assert.assertEquals(content.size(), 1,"Filtered response number is not as expected!");
    Assert.assertTrue(content.get(0).contains(filterKey), "Response doesn't contain expected key!");
}
 
Example #2
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
protected void captureResponse(HttpResponse httpResponse) {
    HarResponse response = new HarResponse(httpResponse.getStatus().code(), httpResponse.getStatus().reasonPhrase(), httpResponse.getProtocolVersion().text());
    harEntry.setResponse(response);

    captureResponseHeaderSize(httpResponse);

    captureResponseMimeType(httpResponse);

    if (dataToCapture.contains(CaptureType.RESPONSE_COOKIES)) {
        captureResponseCookies(httpResponse);
    }

    if (dataToCapture.contains(CaptureType.RESPONSE_HEADERS)) {
        captureResponseHeaders(httpResponse);
    }

    if (BrowserMobHttpUtil.isRedirect(httpResponse)) {
        captureRedirectUrl(httpResponse);
    }
}
 
Example #3
Source File: HarCaptureFilter.java    From Dream-Catcher with MIT License 6 votes vote down vote up
protected void captureResponse(HttpResponse httpResponse) {
    Log.e("InnerHandle", "captureResponse " + harEntry.getId());
    HarResponse response = new HarResponse(httpResponse.getStatus().code(), httpResponse.getStatus().reasonPhrase(), httpResponse.getProtocolVersion().text());
    harEntry.setResponse(response);

    captureResponseHeaderSize(httpResponse);

    captureResponseMimeType(httpResponse);

    if (dataToCapture.contains(CaptureType.RESPONSE_COOKIES)) {
        captureResponseCookies(httpResponse);
    }

    if (dataToCapture.contains(CaptureType.RESPONSE_HEADERS)) {
        captureResponseHeaders(httpResponse);
    }

    if (BrowserMobHttpUtil.isRedirect(httpResponse)) {
        captureRedirectUrl(httpResponse);
    }
}
 
Example #4
Source File: ProxyStepDefinitions.java    From IridiumApplicationTesting with MIT License 6 votes vote down vote up
/**
 * EWnable HAR logging
 */
@When("^I enable HAR logging$")
public void enableHar() {
	final Optional<ProxyDetails<?>> proxy =
		State.getFeatureStateForThread().getProxyInterface(BrowsermobProxyUtilsImpl.PROXY_NAME);

	final EnumSet<CaptureType> captureTypes = CaptureType.getAllContentCaptureTypes();
	captureTypes.addAll(CaptureType.getCookieCaptureTypes());
	captureTypes.addAll(CaptureType.getHeaderCaptureTypes());

	proxy
		.flatMap(ProxyDetails::getInterface)
		.map(BrowserMobProxy.class::cast)
		.ifPresent(x -> {
			x.setHarCaptureTypes(captureTypes);
			x.newHar();
		});
}
 
Example #5
Source File: CaptureService.java    From CapturePacket with MIT License 6 votes vote down vote up
@Override
public void run() {
    try {
        mProxyServer = new BrowserMobProxyServer(mKeyStoreDir);
        mProxyServer.setTrustAllServers(true);
        mProxyServer.start(PROXY_PORT);

        mProxyServer.enableHarCaptureTypes(CaptureType.REQUEST_HEADERS, CaptureType.REQUEST_COOKIES,
                CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_HEADERS, CaptureType.REQUEST_COOKIES,
                CaptureType.RESPONSE_CONTENT);

        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
                .format(new Date(System.currentTimeMillis()));
        mProxyServer.newHar(time);
        mProxyState = STATE_SUCCESS;

    }catch (Throwable e){
        CrashReport.postCatchedException(e);
        mProxyState = STATE_FAIL;
    }
    if (mCaptureBinder != null) {
        mCaptureBinder.setProxyServer(mProxyServer);
        mCaptureBinder.setProxyState(mProxyState);
    }

}
 
Example #6
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 6 votes vote down vote up
protected void captureResponse(HttpResponse httpResponse) {
    HarResponse response = new HarResponse(httpResponse.getStatus().code(), httpResponse.getStatus().reasonPhrase(), httpResponse.getProtocolVersion().text());
    harEntry.setResponse(response);

    captureResponseHeaderSize(httpResponse);

    captureResponseMimeType(httpResponse);

    if (dataToCapture.contains(CaptureType.RESPONSE_COOKIES)) {
        captureResponseCookies(httpResponse);
    }

    if (dataToCapture.contains(CaptureType.RESPONSE_HEADERS)) {
        captureResponseHeaders(httpResponse);
    }

    if (BrowserMobHttpUtil.isRedirect(httpResponse)) {
        captureRedirectUrl(httpResponse);
    }
}
 
Example #7
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public void setHarCaptureTypes(Set<CaptureType> harCaptureSettings) {
    if (harCaptureSettings == null || harCaptureSettings.isEmpty()) {
        harCaptureTypes = EnumSet.noneOf(CaptureType.class);
    } else {
        harCaptureTypes = EnumSet.copyOf(harCaptureSettings);
    }
}
 
Example #8
Source File: CustomDriverProvider.java    From akita with Apache License 2.0 5 votes vote down vote up
/**
 * если установлен -Dproxy=true стартует прокси
 * har для прослушки указывается в application.properties
 * @param capabilities
 */
private void enableProxy(DesiredCapabilities capabilities) {
    proxy.setTrustAllServers(Boolean.valueOf(loadProperty(TRUST_ALL_SERVERS, "true")));
    proxy.start();

    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

    capabilities.setCapability(PROXY, seleniumProxy);
    capabilities.setCapability(ACCEPT_SSL_CERTS, Boolean.valueOf(loadProperty(ACCEPT_SSL_CERTS, "true")));
    capabilities.setCapability(SUPPORTS_JAVASCRIPT, Boolean.valueOf(loadProperty(SUPPORTS_JAVASCRIPT, "true")));

    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_CONTENT, CaptureType.RESPONSE_HEADERS);
    proxy.newHar(loadProperty(NEW_HAR));
}
 
Example #9
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
    // if a ServerResponseCaptureFilter is configured, delegate to it to collect the server's response. if it is not
    // configured, we still need to capture basic information (timings, HTTP status, etc.), just not content.
    if (responseCaptureFilter != null) {
        responseCaptureFilter.serverToProxyResponse(httpObject);
    }

    if (httpObject instanceof HttpResponse) {
        HttpResponse httpResponse = (HttpResponse) httpObject;

        captureResponse(httpResponse);
    }

    if (httpObject instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) httpObject;

        captureResponseSize(httpContent);
    }

    if (httpObject instanceof LastHttpContent) {
        if (dataToCapture.contains(CaptureType.RESPONSE_CONTENT)) {
            captureResponseContent(responseCaptureFilter.getHttpResponse(), responseCaptureFilter.getFullResponseContents());
        }

        harEntry.getResponse().setBodySize(responseBodySize.get());
    }

    return super.serverToProxyResponse(httpObject);
}
 
Example #10
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
protected void captureResponseContent(HttpResponse httpResponse, byte[] fullMessage) {
    // force binary if the content encoding is not supported
    boolean forceBinary = false;

    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in response from {}. Content will be treated as {}", originalRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    if (responseCaptureFilter.isResponseCompressed() && !responseCaptureFilter.isDecompressionSuccessful()) {
        log.warn("Unable to decompress content with encoding: {}. Contents will be encoded as base64 binary data.", responseCaptureFilter.getContentEncoding());

        forceBinary = true;
    }

    Charset charset;
    try {
        charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP response from {}. Content will not be captured in HAR.", contentType, originalRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents from {}", charset, originalRequest.getUri());
    }

    if (!forceBinary && BrowserMobHttpUtil.hasTextualContent(contentType)) {
        String text = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getResponse().getContent().setText(text);
    } else if (dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        harEntry.getResponse().getContent().setText(BaseEncoding.base64().encode(fullMessage));
        harEntry.getResponse().getContent().setEncoding("base64");
    }

    harEntry.getResponse().getContent().setSize(fullMessage.length);
}
 
Example #11
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
/**
 * Create a new instance of the HarCaptureFilter that will capture request and response information. If no har is specified in the
 * constructor, this filter will do nothing.
 * <p/>
 * Regardless of the CaptureTypes specified in <code>dataToCapture</code>, the HarCaptureFilter will always capture:
 * <ul>
 *     <li>Request and response sizes</li>
 *     <li>HTTP request and status lines</li>
 *     <li>Page timing information</li>
 * </ul>
 *
 * @param originalRequest the original HttpRequest from the HttpFiltersSource factory
 * @param har a reference to the ProxyServer's current HAR file at the time this request is received (can be null if HAR capture is not required)
 * @param currentPageRef the ProxyServer's currentPageRef at the time this request is received from the client
 * @param dataToCapture the data types to capture for this request. null or empty set indicates only basic information will be
 *                      captured (see {@link net.lightbody.bmp.proxy.CaptureType} for information on data collected for each CaptureType)
 */
public HarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef, Set<CaptureType> dataToCapture) {
    super(originalRequest, ctx);

    if (har == null) {
        throw new IllegalStateException("Attempted har capture when har is null");
    }

    if (ProxyUtils.isCONNECT(originalRequest)) {
        throw new IllegalStateException("Attempted har capture for HTTP CONNECT request");
    }

    this.clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();

    if (dataToCapture != null && !dataToCapture.isEmpty()) {
        this.dataToCapture = EnumSet.copyOf(dataToCapture);
    } else {
        this.dataToCapture = EnumSet.noneOf(CaptureType.class);
    }

    // we may need to capture both the request and the response, so set up the request/response filters and delegate to them when
    // the corresponding filter methods are invoked. to save time and memory, only set up the capturing filters when
    // we actually need to capture the data.
    if (this.dataToCapture.contains(CaptureType.REQUEST_CONTENT) || this.dataToCapture.contains(CaptureType.REQUEST_BINARY_CONTENT)) {
        requestCaptureFilter = new ClientRequestCaptureFilter(originalRequest);
    } else {
        requestCaptureFilter = null;
    }

    if (this.dataToCapture.contains(CaptureType.RESPONSE_CONTENT) || this.dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        responseCaptureFilter = new ServerResponseCaptureFilter(originalRequest, true);
    } else {
        responseCaptureFilter = null;
    }

    this.har = har;

    this.harEntry = new HarEntry(currentPageRef);
}
 
Example #12
Source File: BrowserMobProxyServer.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public void disableHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        disableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        disableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #13
Source File: BrowserMobProxyServer.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public void enableHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        enableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        enableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #14
Source File: BrowserMobProxyServer.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public void setHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        setHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        setHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #15
Source File: DefaultModule.java    From bromium with MIT License 5 votes vote down vote up
public BrowserMobProxy createBrowserMobProxy(int timeout, RequestFilter requestFilter, ResponseFilter responseFilter) {
    BrowserMobProxyServer proxy = new BrowserMobProxyServer();
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
    proxy.newHar("measurements");
    proxy.addRequestFilter(requestFilter);
    proxy.addResponseFilter(responseFilter);
    proxy.setIdleConnectionTimeout(timeout, TimeUnit.SECONDS);
    proxy.setRequestTimeout(timeout, TimeUnit.SECONDS);
    return proxy;
}
 
Example #16
Source File: AbstractProxyTest.java    From bobcat with Apache License 2.0 5 votes vote down vote up
DesiredCapabilities proxyCapabilities(BrowserMobProxy browserMobProxy) {
  browserMobProxy.enableHarCaptureTypes(
      CaptureType.REQUEST_HEADERS,
      CaptureType.RESPONSE_HEADERS,
      CaptureType.REQUEST_CONTENT,
      CaptureType.RESPONSE_CONTENT);

  Proxy seleniumProxy = ClientUtil.createSeleniumProxy(browserMobProxy);
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
  return capabilities;
}
 
Example #17
Source File: ProxyService.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    new AsyncTask<Void, Void, Boolean>() {
        @Override
        protected Boolean doInBackground(Void... params) {
            postEvent("Prepare to turn on proxy……");
            proxy.setTrustAllServers(true);
            try {
                proxy.start(9999);
            } catch (Exception e) {
                proxy.start();
            }
            ((DCApplication) getApplication()).setPort(proxy.getPort());
            postEvent("Proxy is bound to port " + proxy.getPort());
            proxy.enableHarCaptureTypes(CaptureType.REQUEST_HEADERS, CaptureType.REQUEST_COOKIES,
                    CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_HEADERS, CaptureType.RESPONSE_COOKIES,
                    CaptureType.RESPONSE_CONTENT, CaptureType.RESPONSE_BINARY_CONTENT, CaptureType.REQUEST_BINARY_CONTENT);
            Log.e("ProxyService", "Serve on port: " + proxy.getPort());
            postEvent("Start monitoring");
            proxy.newHar();
            EventBus.getDefault().post(new OperateEvent(OperateEvent.TARGET_PROXY, true));
            SimpleConnectorLifecycleManager.setProxyEnabled(true);
            Log.e("ProxyService", "Start monitoring");
            return Boolean.TRUE;
        }
    }.execute();
}
 
Example #18
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public void disableHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        disableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        disableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #19
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public void setHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        setHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        setHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #20
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public void enableHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        enableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        enableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #21
Source File: BrowserMobProxyServer.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public void setHarCaptureTypes(Set<CaptureType> harCaptureSettings) {
    if (harCaptureSettings == null || harCaptureSettings.isEmpty()) {
        harCaptureTypes = EnumSet.noneOf(CaptureType.class);
    } else {
        harCaptureTypes = EnumSet.copyOf(harCaptureSettings);
    }
}
 
Example #22
Source File: HarCaptureFilter.java    From Dream-Catcher with MIT License 5 votes vote down vote up
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
    // if a ServerResponseCaptureFilter is configured, delegate to it to collect the server's response. if it is not
    // configured, we still need to capture basic information (timings, HTTP status, etc.), just not content.
    Log.e("Capture", "serverToProxyResponse " + harEntry.getId());
    if (responseCaptureFilter != null) {
        responseCaptureFilter.serverToProxyResponse(httpObject);
    }

    if (httpObject instanceof HttpResponse) {
        HttpResponse httpResponse = (HttpResponse) httpObject;

        captureResponse(httpResponse);
    }

    if (httpObject instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) httpObject;

        captureResponseSize(httpContent);
    }

    if (httpObject instanceof LastHttpContent) {
        if (dataToCapture.contains(CaptureType.RESPONSE_CONTENT)) {
            captureResponseContent(responseCaptureFilter.getHttpResponse(), responseCaptureFilter.getFullResponseContents());
        }

        harResponse.getResponse().setBodySize(responseBodySize.get());
    }
    proxyManager.responseHeadersReceived(harResponse);

    return super.serverToProxyResponse(httpObject);
}
 
Example #23
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public void setHarCaptureTypes(Set<CaptureType> harCaptureSettings) {
    if (harCaptureSettings == null || harCaptureSettings.isEmpty()) {
        harCaptureTypes = EnumSet.noneOf(CaptureType.class);
    } else {
        harCaptureTypes = EnumSet.copyOf(harCaptureSettings);
    }
}
 
Example #24
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public void setHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        setHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        setHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #25
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public void enableHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        enableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        enableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #26
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public void disableHarCaptureTypes(CaptureType... captureTypes) {
    if (captureTypes == null) {
        disableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
    } else {
        disableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
    }
}
 
Example #27
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
/**
 * Create a new instance of the HarCaptureFilter that will capture request and response information. If no har is specified in the
 * constructor, this filter will do nothing.
 * <p/>
 * Regardless of the CaptureTypes specified in <code>dataToCapture</code>, the HarCaptureFilter will always capture:
 * <ul>
 *     <li>Request and response sizes</li>
 *     <li>HTTP request and status lines</li>
 *     <li>Page timing information</li>
 * </ul>
 *
 * @param originalRequest the original HttpRequest from the HttpFiltersSource factory
 * @param har a reference to the ProxyServer's current HAR file at the time this request is received (can be null if HAR capture is not required)
 * @param currentPageRef the ProxyServer's currentPageRef at the time this request is received from the client
 * @param dataToCapture the data types to capture for this request. null or empty set indicates only basic information will be
 *                      captured (see {@link net.lightbody.bmp.proxy.CaptureType} for information on data collected for each CaptureType)
 */
public HarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef, Set<CaptureType> dataToCapture) {
    super(originalRequest, ctx);

    if (har == null) {
        throw new IllegalStateException("Attempted har capture when har is null");
    }

    if (ProxyUtils.isCONNECT(originalRequest)) {
        throw new IllegalStateException("Attempted har capture for HTTP CONNECT request");
    }

    this.clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();

    if (dataToCapture != null && !dataToCapture.isEmpty()) {
        this.dataToCapture = EnumSet.copyOf(dataToCapture);
    } else {
        this.dataToCapture = EnumSet.noneOf(CaptureType.class);
    }

    // we may need to capture both the request and the response, so set up the request/response filters and delegate to them when
    // the corresponding filter methods are invoked. to save time and memory, only set up the capturing filters when
    // we actually need to capture the data.
    if (this.dataToCapture.contains(CaptureType.REQUEST_CONTENT) || this.dataToCapture.contains(CaptureType.REQUEST_BINARY_CONTENT)) {
        requestCaptureFilter = new ClientRequestCaptureFilter(originalRequest);
    } else {
        requestCaptureFilter = null;
    }

    if (this.dataToCapture.contains(CaptureType.RESPONSE_CONTENT) || this.dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        responseCaptureFilter = new ServerResponseCaptureFilter(originalRequest, true);
    } else {
        responseCaptureFilter = null;
    }

    this.har = har;

    this.harEntry = new HarEntry(currentPageRef);
}
 
Example #28
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
    // if a ServerResponseCaptureFilter is configured, delegate to it to collect the server's response. if it is not
    // configured, we still need to capture basic information (timings, HTTP status, etc.), just not content.
    if (responseCaptureFilter != null) {
        responseCaptureFilter.serverToProxyResponse(httpObject);
    }

    if (httpObject instanceof HttpResponse) {
        HttpResponse httpResponse = (HttpResponse) httpObject;

        captureResponse(httpResponse);
    }

    if (httpObject instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) httpObject;

        captureResponseSize(httpContent);
    }

    if (httpObject instanceof LastHttpContent) {
        if (dataToCapture.contains(CaptureType.RESPONSE_CONTENT)) {
            captureResponseContent(responseCaptureFilter.getHttpResponse(), responseCaptureFilter.getFullResponseContents());
        }

        harEntry.getResponse().setBodySize(responseBodySize.get());
    }

    return super.serverToProxyResponse(httpObject);
}
 
Example #29
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
protected void captureResponseContent(HttpResponse httpResponse, byte[] fullMessage) {
    // force binary if the content encoding is not supported
    boolean forceBinary = false;

    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in response from {}. Content will be treated as {}", originalRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    if (responseCaptureFilter.isResponseCompressed() && !responseCaptureFilter.isDecompressionSuccessful()) {
        log.warn("Unable to decompress content with encoding: {}. Contents will be encoded as base64 binary data.", responseCaptureFilter.getContentEncoding());

        forceBinary = true;
    }

    Charset charset;
    try {
        charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP response from {}. Content will not be captured in HAR.", contentType, originalRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents from {}", charset, originalRequest.getUri());
    }

    if (!forceBinary && BrowserMobHttpUtil.hasTextualContent(contentType)) {
        String text = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getResponse().getContent().setText(text);
    } else if (dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        harEntry.getResponse().getContent().setText(BaseEncoding.base64().encode(fullMessage));
        harEntry.getResponse().getContent().setEncoding("base64");
    }

    harEntry.getResponse().getContent().setSize(fullMessage.length);
}
 
Example #30
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
@Override
public EnumSet<CaptureType> getHarCaptureTypes() {
    return EnumSet.copyOf(harCaptureTypes);
}