Philippe Mouawad
2018-05-07 15:15:26 UTC
Hello,
There are sometimes a requirement to automatically gzip the request body
whenever Content-Encoding is set to gzip.
Would you find it interesting to add this feature to HttpClient ?
The implementation would look like:
private static final class GzipHttpRequestInterceptor implements
HttpRequestInterceptor {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
if(request instanceof HttpEntityEnclosingRequest) {
Header header = request.getFirstHeader("Content-Encoding");
if(header != null && "gzip".equals(header.getValue())) {
HttpEntityEnclosingRequest enclosingRequest =
(HttpEntityEnclosingRequest) request;
HttpEntity entity = enclosingRequest.getEntity();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOS = new
GZIPOutputStream(out)) {
entity.writeTo(gzipOS);
}
enclosingRequest.setEntity(new
ByteArrayEntity(out.toByteArray()));
}
}
}
}
Use could add it to processing like this:
- builder.addInterceptorFirst(new GzipHttpRequestInterceptor() );
Or it could be enabled by default.
Questions:
- Would you accept it as a PR ?
- Do you see immediate improvements in the provided code ?
Thanks
There are sometimes a requirement to automatically gzip the request body
whenever Content-Encoding is set to gzip.
Would you find it interesting to add this feature to HttpClient ?
The implementation would look like:
private static final class GzipHttpRequestInterceptor implements
HttpRequestInterceptor {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
if(request instanceof HttpEntityEnclosingRequest) {
Header header = request.getFirstHeader("Content-Encoding");
if(header != null && "gzip".equals(header.getValue())) {
HttpEntityEnclosingRequest enclosingRequest =
(HttpEntityEnclosingRequest) request;
HttpEntity entity = enclosingRequest.getEntity();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOS = new
GZIPOutputStream(out)) {
entity.writeTo(gzipOS);
}
enclosingRequest.setEntity(new
ByteArrayEntity(out.toByteArray()));
}
}
}
}
Use could add it to processing like this:
- builder.addInterceptorFirst(new GzipHttpRequestInterceptor() );
Or it could be enabled by default.
Questions:
- Would you accept it as a PR ?
- Do you see immediate improvements in the provided code ?
Thanks
--
Regards
Philippe Mouawad.
Ubik-Ingénierie
Regards
Philippe Mouawad.
Ubik-Ingénierie