HTTPCLIENT-1432: Lazy decompressing of HttpEntity#getContent() to avoid EOFExceptionin case of an empty response with 'Content-Encoding: gzip' header.

Contributed by Yihua Huang <code4crafter at gmail.com>

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1539302 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-11-06 10:37:54 +00:00
parent f3a6831d47
commit 0b3ef4b0d0
3 changed files with 84 additions and 6 deletions

View File

@ -1,6 +1,10 @@
Changes since 4.3.1 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 <code4crafter at gmail.com>
* [HTTPCLIENT-1431] (Regression) deprecated connection manager cannot be used with * [HTTPCLIENT-1431] (Regression) deprecated connection manager cannot be used with
a custom LayeredSchemeSocketFactory. a custom LayeredSchemeSocketFactory.
Contributed by Oleg Kalnichevski <olegk at apache.org> Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -66,12 +66,7 @@ abstract class DecompressingEntity extends HttpEntityWrapper {
private InputStream getDecompressingStream() throws IOException { private InputStream getDecompressingStream() throws IOException {
final InputStream in = wrappedEntity.getContent(); final InputStream in = wrappedEntity.getContent();
try { return new LazyDecompressingInputStream(in, this);
return decorate(in);
} catch (final IOException ex) {
in.close();
throw ex;
}
} }
/** /**

View File

@ -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
* <http://www.apache.org/>.
*
*/
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();
}
}