mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DeflateDecompressingEntity.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 *
michael@0 4 * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0 5 * contributor license agreements. See the NOTICE file distributed with
michael@0 6 * this work for additional information regarding copyright ownership.
michael@0 7 * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0 8 * (the "License"); you may not use this file except in compliance with
michael@0 9 * the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing, software
michael@0 14 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 16 * See the License for the specific language governing permissions and
michael@0 17 * limitations under the License.
michael@0 18 * ====================================================================
michael@0 19 *
michael@0 20 * This software consists of voluntary contributions made by many
michael@0 21 * individuals on behalf of the Apache Software Foundation. For more
michael@0 22 * information on the Apache Software Foundation, please see
michael@0 23 * <http://www.apache.org/>.
michael@0 24 *
michael@0 25 */
michael@0 26 package ch.boye.httpclientandroidlib.client.entity;
michael@0 27
michael@0 28 import java.io.IOException;
michael@0 29 import java.io.InputStream;
michael@0 30 import java.io.PushbackInputStream;
michael@0 31 import java.util.zip.DataFormatException;
michael@0 32 import java.util.zip.Inflater;
michael@0 33 import java.util.zip.InflaterInputStream;
michael@0 34
michael@0 35 import ch.boye.httpclientandroidlib.Header;
michael@0 36 import ch.boye.httpclientandroidlib.HttpEntity;
michael@0 37 import ch.boye.httpclientandroidlib.entity.HttpEntityWrapper;
michael@0 38
michael@0 39 /**
michael@0 40 * {@link HttpEntityWrapper} responsible for handling deflate Content Coded responses. In RFC2616
michael@0 41 * terms, <code>deflate</code> means a <code>zlib</code> stream as defined in RFC1950. Some server
michael@0 42 * implementations have misinterpreted RFC2616 to mean that a <code>deflate</code> stream as
michael@0 43 * defined in RFC1951 should be used (or maybe they did that since that's how IE behaves?). It's
michael@0 44 * confusing that <code>deflate</code> in HTTP 1.1 means <code>zlib</code> streams rather than
michael@0 45 * <code>deflate</code> streams. We handle both types in here, since that's what is seen on the
michael@0 46 * internet. Moral - prefer <code>gzip</code>!
michael@0 47 *
michael@0 48 * @see GzipDecompressingEntity
michael@0 49 *
michael@0 50 * @since 4.1
michael@0 51 */
michael@0 52 public class DeflateDecompressingEntity extends DecompressingEntity {
michael@0 53
michael@0 54 /**
michael@0 55 * Creates a new {@link DeflateDecompressingEntity} which will wrap the specified
michael@0 56 * {@link HttpEntity}.
michael@0 57 *
michael@0 58 * @param entity
michael@0 59 * a non-null {@link HttpEntity} to be wrapped
michael@0 60 */
michael@0 61 public DeflateDecompressingEntity(final HttpEntity entity) {
michael@0 62 super(entity);
michael@0 63 }
michael@0 64
michael@0 65 /**
michael@0 66 * Returns the non-null InputStream that should be returned to by all requests to
michael@0 67 * {@link #getContent()}.
michael@0 68 *
michael@0 69 * @return a non-null InputStream
michael@0 70 * @throws IOException if there was a problem
michael@0 71 */
michael@0 72 @Override
michael@0 73 InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
michael@0 74 /*
michael@0 75 * A zlib stream will have a header.
michael@0 76 *
michael@0 77 * CMF | FLG [| DICTID ] | ...compressed data | ADLER32 |
michael@0 78 *
michael@0 79 * * CMF is one byte.
michael@0 80 *
michael@0 81 * * FLG is one byte.
michael@0 82 *
michael@0 83 * * DICTID is four bytes, and only present if FLG.FDICT is set.
michael@0 84 *
michael@0 85 * Sniff the content. Does it look like a zlib stream, with a CMF, etc? c.f. RFC1950,
michael@0 86 * section 2.2. http://tools.ietf.org/html/rfc1950#page-4
michael@0 87 *
michael@0 88 * We need to see if it looks like a proper zlib stream, or whether it is just a deflate
michael@0 89 * stream. RFC2616 calls zlib streams deflate. Confusing, isn't it? That's why some servers
michael@0 90 * implement deflate Content-Encoding using deflate streams, rather than zlib streams.
michael@0 91 *
michael@0 92 * We could start looking at the bytes, but to be honest, someone else has already read
michael@0 93 * the RFCs and implemented that for us. So we'll just use the JDK libraries and exception
michael@0 94 * handling to do this. If that proves slow, then we could potentially change this to check
michael@0 95 * the first byte - does it look like a CMF? What about the second byte - does it look like
michael@0 96 * a FLG, etc.
michael@0 97 */
michael@0 98
michael@0 99 /* We read a small buffer to sniff the content. */
michael@0 100 byte[] peeked = new byte[6];
michael@0 101
michael@0 102 PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.length);
michael@0 103
michael@0 104 int headerLength = pushback.read(peeked);
michael@0 105
michael@0 106 if (headerLength == -1) {
michael@0 107 throw new IOException("Unable to read the response");
michael@0 108 }
michael@0 109
michael@0 110 /* We try to read the first uncompressed byte. */
michael@0 111 byte[] dummy = new byte[1];
michael@0 112
michael@0 113 Inflater inf = new Inflater();
michael@0 114
michael@0 115 try {
michael@0 116 int n;
michael@0 117 while ((n = inf.inflate(dummy)) == 0) {
michael@0 118 if (inf.finished()) {
michael@0 119
michael@0 120 /* Not expecting this, so fail loudly. */
michael@0 121 throw new IOException("Unable to read the response");
michael@0 122 }
michael@0 123
michael@0 124 if (inf.needsDictionary()) {
michael@0 125
michael@0 126 /* Need dictionary - then it must be zlib stream with DICTID part? */
michael@0 127 break;
michael@0 128 }
michael@0 129
michael@0 130 if (inf.needsInput()) {
michael@0 131 inf.setInput(peeked);
michael@0 132 }
michael@0 133 }
michael@0 134
michael@0 135 if (n == -1) {
michael@0 136 throw new IOException("Unable to read the response");
michael@0 137 }
michael@0 138
michael@0 139 /*
michael@0 140 * We read something without a problem, so it's a valid zlib stream. Just need to reset
michael@0 141 * and return an unused InputStream now.
michael@0 142 */
michael@0 143 pushback.unread(peeked, 0, headerLength);
michael@0 144 return new InflaterInputStream(pushback);
michael@0 145 } catch (DataFormatException e) {
michael@0 146
michael@0 147 /* Presume that it's an RFC1951 deflate stream rather than RFC1950 zlib stream and try
michael@0 148 * again. */
michael@0 149 pushback.unread(peeked, 0, headerLength);
michael@0 150 return new InflaterInputStream(pushback, new Inflater(true));
michael@0 151 }
michael@0 152 }
michael@0 153
michael@0 154 /**
michael@0 155 * {@inheritDoc}
michael@0 156 */
michael@0 157 @Override
michael@0 158 public Header getContentEncoding() {
michael@0 159
michael@0 160 /* This HttpEntityWrapper has dealt with the Content-Encoding. */
michael@0 161 return null;
michael@0 162 }
michael@0 163
michael@0 164 /**
michael@0 165 * {@inheritDoc}
michael@0 166 */
michael@0 167 @Override
michael@0 168 public long getContentLength() {
michael@0 169
michael@0 170 /* Length of inflated content is unknown. */
michael@0 171 return -1;
michael@0 172 }
michael@0 173
michael@0 174 }

mercurial