@Override public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) { for (ResponseFilterRule rule: ruleList) { if(rule.getEnable()) { if (contents.isText() && messageInfo.getUrl().contains(rule.getUrl())) { String originContent = contents.getTextContents(); if (originContent != null) { contents.setTextContents(contents.getTextContents().replaceAll( rule.getReplaceRegex(), rule.getReplaceContent())); } } } } } });
@Override public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) { if (!active) return; responses.add(new Response(messageInfo.getUrl(), response.getStatus().code(), response.getStatus().reasonPhrase(), toMap(response.headers()), contents.getContentType(), contents.getTextContents() )); if (response.getStatus().code() < 200 || response.getStatus().code() >= 300) return; String fileName = getFileName(response); if (fileName == null) return; File file = downloader.prepareTargetFile(config, fileName); try { FileUtils.writeByteArrayToFile(file, contents.getBinaryContents()); downloadedFiles.add(file); } catch (IOException e) { log.log(Level.SEVERE, "Failed to save downloaded file to " + file.getAbsolutePath() + " for url " + messageInfo.getUrl(), e); } }
@Override public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) { if (contents.getBinaryContents().length > threshold) { log.warning("Too large response " + messageInfo.getUrl() + ": " + contents.getBinaryContents().length + " bytes"); if (log.isLoggable(Level.FINEST)) { log.finest("Response content: " + contents.getTextContents()); } } } }
@Override public void filterResponse(HttpResponse httpResponse, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) { if (shouldInjectJavascriptPredicate.test(httpMessageInfo.getOriginalRequest())) { httpMessageContents.setTextContents(injectionCode + httpMessageContents.getTextContents()); } } }
@Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { // only filter when the original HttpRequest comes through. the RequestFilterAdapter is not designed to filter // any subsequent HttpContents. if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; HttpMessageContents contents; if (httpObject instanceof FullHttpMessage) { FullHttpMessage httpContent = (FullHttpMessage) httpObject; contents = new HttpMessageContents(httpContent); } else { // the HTTP object is not a FullHttpMessage, which means that message contents are not available on this request and cannot be modified. contents = null; } HttpMessageInfo messageInfo = new HttpMessageInfo(originalRequest, ctx, isHttps(), getFullUrl(httpRequest), getOriginalUrl()); HttpResponse response = requestFilter.filterRequest(httpRequest, contents, messageInfo); if (response != null) { return response; } } return null; }
/** * Returns true if this message's Content-Type header indicates that it contains a textual data type. See {@link BrowserMobHttpUtil#hasTextualContent(String)}. * * @return true if the Content-Type header is a textual type, otherwise false */ public boolean isText() { return BrowserMobHttpUtil.hasTextualContent(getContentType()); } }
@Override public void filterResponse(HttpResponse httpResponse, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) { logger.debug(replayingState.getSubstitutions().toString()); Optional<String> substitutionsInjectionOptional = getSubstitutionEmittingCode(); if (shouldInjectJavascriptPredicate.test(httpMessageInfo.getOriginalRequest())) { httpMessageContents.setTextContents(injectionCode + substitutionsInjectionOptional.orElse("") + httpMessageContents.getTextContents()); } replayingState.removeHttpRequestFromQueue(httpMessageInfo.getOriginalRequest()); replayingState.signalizeIfNoHttpQueriesInQueue(); }
@Override public HttpObject serverToProxyResponse(HttpObject httpObject) { // only filter when the original HttpResponse comes through. the ResponseFilterAdapter is not designed to filter // any subsequent HttpContents. if (httpObject instanceof HttpResponse) { HttpResponse httpResponse = (HttpResponse) httpObject; HttpMessageContents contents; if (httpObject instanceof FullHttpMessage) { FullHttpMessage httpContent = (FullHttpMessage) httpObject; contents = new HttpMessageContents(httpContent); } else { // the HTTP object is not a FullHttpMessage, which means that message contents will not be available on this response and cannot be modified. contents = null; } HttpMessageInfo messageInfo = new HttpMessageInfo(originalRequest, ctx, isHttps(), getFullUrl(modifiedHttpRequest), getOriginalUrl()); responseFilter.filterResponse(httpResponse, contents, messageInfo); } return super.serverToProxyResponse(httpObject); }
/** * Retrieves the character set of the entity body. If the Content-Type is not a textual type, this value is meaningless. * If no character set is specified, this method will return the default ISO-8859-1 character set. If the Content-Type * specifies a character set, but the character set is not supported on this platform, this method throws an * {@link java.nio.charset.UnsupportedCharsetException}. * * @return the entity body's character set * @throws java.nio.charset.UnsupportedCharsetException if the character set declared in the message is not supported on this platform */ public Charset getCharset() throws java.nio.charset.UnsupportedCharsetException { String contentTypeHeader = getContentType(); Charset charset = null; try { charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader); } catch (UnsupportedCharsetException e) { java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause(); log.error("Character set specified in Content-Type header is not supported on this platform. Content-Type header: {}", contentTypeHeader, cause); throw cause; } if (charset == null) { return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET; } return charset; }
@Override public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo) { if (contents.getBinaryContents().length > threshold) { log.warning("Too large request " + messageInfo.getUrl() + ": " + contents.getBinaryContents().length + " bytes"); if (log.isLoggable(Level.FINEST)) { log.finest("Request content: " + contents.getTextContents()); } } return null; } }
@Override public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) { if (rewrites.isEmpty()) { return; } String reqUrl = messageInfo.getOriginalUrl(); for (RewriteItem rewriteItem : rewrites) { if (reqUrl.matches(rewriteItem.getHost())) { // headers rewrite LOGGER.debug("Rewrite rule will be applied for host: ".concat(reqUrl)); applyHeaders(response, rewriteItem.getHeaders()); // body rewrite String content = contents.getTextContents(); content.replaceAll(rewriteItem.getRegex(), rewriteItem.getReplacement()); contents.setTextContents(content); } } }
@Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { // only filter when the original HttpRequest comes through. the RequestFilterAdapter is not designed to filter // any subsequent HttpContents. if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; HttpMessageContents contents; if (httpObject instanceof FullHttpMessage) { FullHttpMessage httpContent = (FullHttpMessage) httpObject; contents = new HttpMessageContents(httpContent); } else { // the HTTP object is not a FullHttpMessage, which means that message contents are not available on this request and cannot be modified. contents = null; } HttpMessageInfo messageInfo = new HttpMessageInfo(originalRequest, ctx, isHttps(), getFullUrl(httpRequest), getOriginalUrl()); HttpResponse response = requestFilter.filterRequest(httpRequest, contents, messageInfo); if (response != null) { return response; } } return null; }
/** * Returns true if this message's Content-Type header indicates that it contains a textual data type. See {@link BrowserMobHttpUtil#hasTextualContent(String)}. * * @return true if the Content-Type header is a textual type, otherwise false */ public boolean isText() { return BrowserMobHttpUtil.hasTextualContent(getContentType()); } }
@Override public HttpResponse filterRequest(HttpRequest rq, HttpMessageContents contents, HttpMessageInfo messageInfo) { if (rewrites.isEmpty()) { return null; } String reqUrl = rq.getUri(); for (RewriteItem rewriteItem : rewrites) { if(reqUrl.matches(rewriteItem.getHost())) { // headers rewrite LOGGER.debug("Rewrite rule will be applied for host: ".concat(reqUrl)); rq = applyHeaders(rq, rewriteItem.getHeaders()); // body rewrite String content = contents.getTextContents(); content.replaceAll(rewriteItem.getRegex(), rewriteItem.getReplacement()); contents.setTextContents(content); } } return null; }
@Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { // only filter when the original HttpRequest comes through. the RequestFilterAdapter is not designed to filter // any subsequent HttpContents. if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; HttpMessageContents contents; if (httpObject instanceof FullHttpMessage) { FullHttpMessage httpContent = (FullHttpMessage) httpObject; contents = new HttpMessageContents(httpContent); } else { // the HTTP object is not a FullHttpMessage, which means that message contents are not available on this request and cannot be modified. contents = null; } HttpMessageInfo messageInfo = new HttpMessageInfo(originalRequest, ctx, isHttps(), getFullUrl(httpRequest), getOriginalUrl()); HttpResponse response = requestFilter.filterRequest(httpRequest, contents, messageInfo); if (response != null) { return response; } } return null; }
/** * Returns true if this message's Content-Type header indicates that it contains a textual data type. See {@link BrowserMobHttpUtil#hasTextualContent(String)}. * * @return true if the Content-Type header is a textual type, otherwise false */ public boolean isText() { return BrowserMobHttpUtil.hasTextualContent(getContentType()); } }
@Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { // only filter when the original HttpRequest comes through. the RequestFilterAdapter is not designed to filter // any subsequent HttpContents. if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; HttpMessageContents contents; if (httpObject instanceof FullHttpMessage) { FullHttpMessage httpContent = (FullHttpMessage) httpObject; contents = new HttpMessageContents(httpContent); } else { // the HTTP object is not a FullHttpMessage, which means that message contents are not available on this request and cannot be modified. contents = null; } HttpMessageInfo messageInfo = new HttpMessageInfo(originalRequest, ctx, isHttps(), getFullUrl(httpRequest), getOriginalUrl()); HttpResponse response = requestFilter.filterRequest(httpRequest, contents, messageInfo); if (response != null) { return response; } } return null; }
/** * Retrieves the character set of the entity body. If the Content-Type is not a textual type, this value is meaningless. * If no character set is specified, this method will return the default ISO-8859-1 character set. If the Content-Type * specifies a character set, but the character set is not supported on this platform, this method throws an * {@link java.nio.charset.UnsupportedCharsetException}. * * @return the entity body's character set * @throws java.nio.charset.UnsupportedCharsetException if the character set declared in the message is not supported on this platform */ public Charset getCharset() throws java.nio.charset.UnsupportedCharsetException { String contentTypeHeader = getContentType(); Charset charset = null; try { charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader); } catch (UnsupportedCharsetException e) { java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause(); log.error("Character set specified in Content-Type header is not supported on this platform. Content-Type header: {}", contentTypeHeader, cause); throw cause; } if (charset == null) { return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET; } return charset; }
@Override public HttpObject serverToProxyResponse(HttpObject httpObject) { // only filter when the original HttpResponse comes through. the ResponseFilterAdapter is not designed to filter // any subsequent HttpContents. if (httpObject instanceof HttpResponse) { HttpResponse httpResponse = (HttpResponse) httpObject; HttpMessageContents contents; if (httpObject instanceof FullHttpMessage) { FullHttpMessage httpContent = (FullHttpMessage) httpObject; contents = new HttpMessageContents(httpContent); } else { // the HTTP object is not a FullHttpMessage, which means that message contents will not be available on this response and cannot be modified. contents = null; } HttpMessageInfo messageInfo = new HttpMessageInfo(originalRequest, ctx, isHttps(), getFullUrl(modifiedHttpRequest), getOriginalUrl()); responseFilter.filterResponse(httpResponse, contents, messageInfo); } return super.serverToProxyResponse(httpObject); }
/** * Retrieves the character set of the entity body. If the Content-Type is not a textual type, this value is meaningless. * If no character set is specified, this method will return the default ISO-8859-1 character set. If the Content-Type * specifies a character set, but the character set is not supported on this platform, this method throws an * {@link java.nio.charset.UnsupportedCharsetException}. * * @return the entity body's character set * @throws java.nio.charset.UnsupportedCharsetException if the character set declared in the message is not supported on this platform */ public Charset getCharset() throws java.nio.charset.UnsupportedCharsetException { String contentTypeHeader = getContentType(); Charset charset = null; try { charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader); } catch (UnsupportedCharsetException e) { java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause(); log.error("Character set specified in Content-Type header is not supported on this platform. Content-Type header: {}", contentTypeHeader, cause); throw cause; } if (charset == null) { return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET; } return charset; }