Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #209: HAR: use UTF-8, handle binary data #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
import com.nccgroup.loggerplusplus.logentry.LogEntryField;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -107,7 +113,9 @@ public void write(JsonWriter writer, List<LogEntry> logEntries) throws IOExcepti
writer.endObject();
}
writer.endArray(); // end params array
writer.name("text").value((String) logEntry.getValueByKey(LogEntryField.REQUEST_BODY));
// Most HAR implementations use encoding base64 for postData too,
// even though it's not supported by HAR specification
writeContentBody(writer, logEntry.getRequest().body().getBytes());
writer.endObject(); // end postData object
}

Expand Down Expand Up @@ -160,7 +168,7 @@ public void write(JsonWriter writer, List<LogEntry> logEntries) throws IOExcepti
writer.name("content").beginObject(); // start content object
writer.name("size").value(logEntry.getResponseBodyLength());
writer.name("mimeType").value(logEntry.getResponseContentType());
writer.name("text").value(String.valueOf(logEntry.getValueByKey(LogEntryField.RESPONSE_BODY)));
writeContentBody(writer, logEntry.getResponse().body().getBytes());
writer.endObject(); //end content object

writer.endObject(); // end response object
Expand All @@ -185,6 +193,19 @@ public void write(JsonWriter writer, List<LogEntry> logEntries) throws IOExcepti

}

private static void writeContentBody(JsonWriter writer, byte[] data) throws IOException {
// try to write write it as an UTF-8 string, and if it's not a valid
// UTF-8 string -- write it as binary data in base64 and set encoding
try {
final CharsetDecoder utf8Decoder = StandardCharsets.UTF_8.newDecoder();
final String value = utf8Decoder.decode(ByteBuffer.wrap(data)).toString();
writer.name("text").value(value);
} catch (CharacterCodingException e) {
writer.name("encoding").value("base64");
writer.name("text").value(Base64.getEncoder().encodeToString(data));
}
}

private List<HttpParameter> getRequestParametersByType(HttpRequest request, HttpParameterType paramType) {
return request.parameters().stream()
.filter(iParameter -> iParameter.type().equals(paramType))
Expand Down