net.lightbody.bmp.core.har.HarPostData Java Examples

The following examples show how to use net.lightbody.bmp.core.har.HarPostData. 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: HarCaptureFilter.java    From CapturePacket with MIT License 4 votes vote down vote up
protected void captureRequestContent(HttpRequest httpRequest, byte[] fullMessage) {
    if (fullMessage.length == 0) {
        return;
    }

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

    HarPostData postData = new HarPostData();
    harEntry.getRequest().setPostData(postData);

    postData.setMimeType(contentType);

    boolean urlEncoded;
    if (contentType.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
        urlEncoded = true;
    } else {
        urlEncoded = false;
    }

    Charset charset;
    try {
         charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP request to {}. Content will not be captured in HAR.", contentType, httpRequest.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 to {}", charset, httpRequest.getUri());
    }

    if (urlEncoded) {
        String textContents = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(textContents, charset, false);

        ImmutableList.Builder<HarPostDataParam> paramBuilder = ImmutableList.builder();

        for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) {
            for (String value : entry.getValue()) {
                paramBuilder.add(new HarPostDataParam(entry.getKey(), value));
            }
        }

        harEntry.getRequest().getPostData().setParams(paramBuilder.build());
    } else {
        //TODO: implement capture of files and multipart form data

        // not URL encoded, so let's grab the body of the POST and capture that
        String postBody = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getRequest().getPostData().setText(postBody);
    }
}
 
Example #2
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
protected void captureRequestContent(HttpRequest httpRequest, byte[] fullMessage) {
    if (fullMessage.length == 0) {
        return;
    }

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

    HarPostData postData = new HarPostData();
    harEntry.getRequest().setPostData(postData);

    postData.setMimeType(contentType);

    boolean urlEncoded;
    if (contentType.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
        urlEncoded = true;
    } else {
        urlEncoded = false;
    }

    Charset charset;
    try {
         charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP request to {}. Content will not be captured in HAR.", contentType, httpRequest.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 to {}", charset, httpRequest.getUri());
    }

    if (urlEncoded) {
        String textContents = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(textContents, charset, false);

        ImmutableList.Builder<HarPostDataParam> paramBuilder = ImmutableList.builder();

        for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) {
            for (String value : entry.getValue()) {
                paramBuilder.add(new HarPostDataParam(entry.getKey(), value));
            }
        }

        harEntry.getRequest().getPostData().setParams(paramBuilder.build());
    } else {
        //TODO: implement capture of files and multipart form data

        // not URL encoded, so let's grab the body of the POST and capture that
        String postBody = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getRequest().getPostData().setText(postBody);
    }
}