如何让 browsermob-proxy 记录所有响应主体

How do I get browsermob-proxy to record ALL response bodies

我在使用 browsermob-proxy 及其 har 导出功能时遇到问题。某些响应正文未记录(缺少整个 "text" 字段)

我的设置(使用 browsermob-proxy 2.1.4)

curl -X POST http://localhost:8080/proxy?port=9091

curl -X PUT "http://localhost:8080/proxy/9091/har?captureHeaders=true&captureCookies=true&captureContent=true"

有些回复没问题:

"response": { "status": 201, "statusText": "Created", "httpVersion": "HTTP/1.1", "cookies": [], "headers": [{ "name": "Cache-Control", "value": "max-age=0, no-cache, no-store" }, { "name": "Content-Type", "value": "application/json" }, { "name": "Date", "value": "Thu, 15 Feb 2018 13:07:39 GMT" }, { "name": "Location", "value": ... }, { "name": "Pragma", "value": "no-cache" }, { "name": "Render-Time", "value": "8" }, { "name": "Server", "value": "openresty" }, { "name": "Strict-Transport-Security", "value": "max-age=31536000; includeSubDomains" }, { "name": "transfer-encoding", "value": "chunked" }, { "name": "Connection", "value": "keep-alive" }], "content": { "size": 8607, "mimeType": "application/json", "text": "{ <actual json body> }", "comment": "" }, ...

但有些不是(可能是特殊的 content/mime 类型?或者可能是 gzip 内容编码?)

"response": { "status": 200, "statusText": "OK", "httpVersion": "HTTP/1.1", "cookies": [], "headers": [{ "name": "Cache-Control", "value": "max-age=0, no-cache, no-store" }, { "name": "Content-Encoding", "value": "gzip" }, { "name": "Content-Type", "value": "application/some.custom.type-v1+json" }, { "name": "Date", "value": "Thu, 15 Feb 2018 13:07:39 GMT" }, { "name": "Pragma", "value": "no-cache" }, { "name": "Render-Time", "value": "92" }, { "name": "Server", "value": "openresty" }, { "name": "Strict-Transport-Security", "value": "max-age=31536000; includeSubDomains" }, { "name": "Vary", "value": "Accept-Encoding" }, { "name": "Content-Length", "value": "1978" }, { "name": "Connection", "value": "keep-alive" }], "content": { "size": 7429, "mimeType": "application/some.custom.type-v1+json", "comment": "" < there's nothing else here!! > }, "redirectURL": "", "headersSize": 444, "bodySize": 1978, "comment": "" }, ...

哦,是的,两个请求都通过 https,使用 MITM。

我想我找到了自己问题的答案

在 BrowserMobHttpClient.java 中我发现了以下内容:

private boolean hasTextualContent(String contentType) {
    return contentType != null && contentType.startsWith("text/") ||
            contentType.startsWith("application/x-javascript") ||
            contentType.startsWith("application/javascript")  ||
            contentType.startsWith("application/json")  ||
            contentType.startsWith("application/xml")  ||
            contentType.startsWith("application/xhtml+xml");
}

看来我得去制作一个 browsermob-proxy 的自定义版本才能让它工作。