renatoathaydes / rawhttp

HTTP library to make it easy to deal with raw HTTP.
Apache License 2.0
201 stars 29 forks source link

Retrieve original raw bytes read from InputStream in RawHttpResponse/EagerHttpResponse #23

Closed illumi closed 4 years ago

illumi commented 4 years ago

As title, it would be useful to enable you to do something like byte[] getRaw() from one of these objects to get the original raw bytes used to construct the object.

To add: This is available from the EagerBodyReader, but not from an entire Request or Response entity.

illumi commented 4 years ago

What I've ended up doing is writing an InterceptingInputStream class which is a proxy between the sockets InputStream and what RawHttp is reading so I can funnel the bytes somewhere else and keep track of the raw data coming from the socket.

renatoathaydes commented 4 years ago

You could use writeTo(OutputStream) which is available in all components of HttpMessage to "replay" it. That's how RawHttp itself writes out a message.

getStartLine().writeTo(out);
getHeaders().writeTo(out);
Optional<? extends BodyReader> body = getBody();
if (body.isPresent()) {
    try (BodyReader bodyReader = body.get()) {
        bodyReader.writeTo(out, bufferSize);
    }
}
renatoathaydes commented 4 years ago

To add: This is available from the EagerBodyReader, but not from an entire Request or Response entity.

RawHttp messages are "live", which means they may not have been completely transferred yet until you call eagerly(). If there were a getBytes() method in them, you couldn't call that more than once, which is usually bad API that causes unexpected errors. Besides, the messages are immutable, and the only way to "cache" the bytes would be to make them mutable, which is very undesirable.