Java源码示例:net.lightbody.bmp.proxy.CaptureType
示例1
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);
}
}
示例2
@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);
}
}
示例3
/**
* 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();
});
}
示例4
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);
}
}
示例5
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);
}
}
示例6
@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!");
}
示例7
@Override
public void setHarCaptureTypes(Set<CaptureType> harCaptureSettings) {
if (harCaptureSettings == null || harCaptureSettings.isEmpty()) {
harCaptureTypes = EnumSet.noneOf(CaptureType.class);
} else {
harCaptureTypes = EnumSet.copyOf(harCaptureSettings);
}
}
示例8
@Override
public void setHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
setHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
setHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例9
@Override
public void enableHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
enableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
enableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例10
@Override
public void disableHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
disableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
disableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例11
/**
* 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);
}
示例12
@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);
}
示例13
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);
}
示例14
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;
}
示例15
/**
* если установлен -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));
}
示例16
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;
}
示例17
@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();
}
示例18
@Override
public void setHarCaptureTypes(Set<CaptureType> harCaptureSettings) {
if (harCaptureSettings == null || harCaptureSettings.isEmpty()) {
harCaptureTypes = EnumSet.noneOf(CaptureType.class);
} else {
harCaptureTypes = EnumSet.copyOf(harCaptureSettings);
}
}
示例19
@Override
public void setHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
setHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
setHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例20
@Override
public void enableHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
enableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
enableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例21
@Override
public void disableHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
disableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
disableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例22
@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);
}
示例23
@Override
public void setHarCaptureTypes(Set<CaptureType> harCaptureSettings) {
if (harCaptureSettings == null || harCaptureSettings.isEmpty()) {
harCaptureTypes = EnumSet.noneOf(CaptureType.class);
} else {
harCaptureTypes = EnumSet.copyOf(harCaptureSettings);
}
}
示例24
@Override
public void setHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
setHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
setHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例25
@Override
public void enableHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
enableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
enableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例26
@Override
public void disableHarCaptureTypes(CaptureType... captureTypes) {
if (captureTypes == null) {
disableHarCaptureTypes(EnumSet.noneOf(CaptureType.class));
} else {
disableHarCaptureTypes(EnumSet.copyOf(Arrays.asList(captureTypes)));
}
}
示例27
/**
* 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);
}
示例28
@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);
}
示例29
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);
}
示例30
@Override
public EnumSet<CaptureType> getHarCaptureTypes() {
return EnumSet.copyOf(harCaptureTypes);
}