From 0b3ef4b0d0a0306ec95beff35b2342f6b4a34d6b Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 6 Nov 2013 10:37:54 +0000 Subject: [PATCH] HTTPCLIENT-1432: Lazy decompressing of HttpEntity#getContent() to avoid EOFExceptionin case of an empty response with 'Content-Encoding: gzip' header. Contributed by Yihua Huang git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1539302 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 4 + .../client/entity/DecompressingEntity.java | 7 +- .../entity/LazyDecompressingInputStream.java | 79 +++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 80027dea7..f5559d10c 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,10 @@ Changes since 4.3.1 ------------------- +* [HTTPCLIENT-1432] Lazy decompressing of HttpEntity#getContent() to avoid EOFException + in case of an empty response with 'Content-Encoding: gzip' header. + Contributed by Yihua Huang + * [HTTPCLIENT-1431] (Regression) deprecated connection manager cannot be used with a custom LayeredSchemeSocketFactory. Contributed by Oleg Kalnichevski diff --git a/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java index ac1bc315d..f0e16db91 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java @@ -66,12 +66,7 @@ public DecompressingEntity(final HttpEntity wrapped) { private InputStream getDecompressingStream() throws IOException { final InputStream in = wrappedEntity.getContent(); - try { - return decorate(in); - } catch (final IOException ex) { - in.close(); - throw ex; - } + return new LazyDecompressingInputStream(in, this); } /** diff --git a/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java b/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java new file mode 100644 index 000000000..26e4981bf --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/client/entity/LazyDecompressingInputStream.java @@ -0,0 +1,79 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.http.client.entity; + +import org.apache.http.annotation.NotThreadSafe; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Lazy init InputStream wrapper. + */ +@NotThreadSafe +class LazyDecompressingInputStream extends InputStream { + + private final InputStream wrappedStream; + + private final DecompressingEntity decompressingEntity; + + private InputStream wrapperStream; + + public LazyDecompressingInputStream( + final InputStream wrappedStream, + final DecompressingEntity decompressingEntity) { + this.wrappedStream = wrappedStream; + this.decompressingEntity = decompressingEntity; + } + + @Override + public int read() throws IOException { + initWrapper(); + return wrapperStream.read(); + } + + @Override + public int available() throws IOException { + initWrapper(); + return wrapperStream.available(); + } + + private void initWrapper() throws IOException { + if (wrapperStream == null) { + wrapperStream = decompressingEntity.decorate(wrappedStream); + } + } + + @Override + public void close() throws IOException { + if (wrapperStream != null) { + wrapperStream.close(); + } + wrappedStream.close(); + } + +}