mobile/android/thirdparty/ch/boye/httpclientandroidlib/protocol/ResponseContent.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.

     1 /*
     2  * ====================================================================
     3  * Licensed to the Apache Software Foundation (ASF) under one
     4  * or more contributor license agreements.  See the NOTICE file
     5  * distributed with this work for additional information
     6  * regarding copyright ownership.  The ASF licenses this file
     7  * to you under the Apache License, Version 2.0 (the
     8  * "License"); you may not use this file except in compliance
     9  * with the License.  You may obtain a copy of the License at
    10  *
    11  *   http://www.apache.org/licenses/LICENSE-2.0
    12  *
    13  * Unless required by applicable law or agreed to in writing,
    14  * software distributed under the License is distributed on an
    15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    16  * KIND, either express or implied.  See the License for the
    17  * specific language governing permissions and limitations
    18  * under the License.
    19  * ====================================================================
    20  *
    21  * This software consists of voluntary contributions made by many
    22  * individuals on behalf of the Apache Software Foundation.  For more
    23  * information on the Apache Software Foundation, please see
    24  * <http://www.apache.org/>.
    25  *
    26  */
    28 package ch.boye.httpclientandroidlib.protocol;
    30 import java.io.IOException;
    32 import ch.boye.httpclientandroidlib.HttpEntity;
    33 import ch.boye.httpclientandroidlib.HttpException;
    34 import ch.boye.httpclientandroidlib.HttpResponse;
    35 import ch.boye.httpclientandroidlib.HttpResponseInterceptor;
    36 import ch.boye.httpclientandroidlib.HttpStatus;
    37 import ch.boye.httpclientandroidlib.HttpVersion;
    38 import ch.boye.httpclientandroidlib.ProtocolVersion;
    39 import ch.boye.httpclientandroidlib.ProtocolException;
    41 /**
    42  * ResponseContent is the most important interceptor for outgoing responses.
    43  * It is responsible for delimiting content length by adding
    44  * <code>Content-Length</code> or <code>Transfer-Content</code> headers based
    45  * on the properties of the enclosed entity and the protocol version.
    46  * This interceptor is required for correct functioning of server side protocol
    47  * processors.
    48  *
    49  * @since 4.0
    50  */
    51 public class ResponseContent implements HttpResponseInterceptor {
    53     public ResponseContent() {
    54         super();
    55     }
    57     public void process(final HttpResponse response, final HttpContext context)
    58             throws HttpException, IOException {
    59         if (response == null) {
    60             throw new IllegalArgumentException("HTTP response may not be null");
    61         }
    62         if (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
    63             throw new ProtocolException("Transfer-encoding header already present");
    64         }
    65         if (response.containsHeader(HTTP.CONTENT_LEN)) {
    66             throw new ProtocolException("Content-Length header already present");
    67         }
    68         ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
    69         HttpEntity entity = response.getEntity();
    70         if (entity != null) {
    71             long len = entity.getContentLength();
    72             if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
    73                 response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
    74             } else if (len >= 0) {
    75                 response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
    76             }
    77             // Specify a content type if known
    78             if (entity.getContentType() != null && !response.containsHeader(
    79                     HTTP.CONTENT_TYPE )) {
    80                 response.addHeader(entity.getContentType());
    81             }
    82             // Specify a content encoding if known
    83             if (entity.getContentEncoding() != null && !response.containsHeader(
    84                     HTTP.CONTENT_ENCODING)) {
    85                 response.addHeader(entity.getContentEncoding());
    86             }
    87         } else {
    88             int status = response.getStatusLine().getStatusCode();
    89             if (status != HttpStatus.SC_NO_CONTENT
    90                     && status != HttpStatus.SC_NOT_MODIFIED
    91                     && status != HttpStatus.SC_RESET_CONTENT) {
    92                 response.addHeader(HTTP.CONTENT_LEN, "0");
    93             }
    94         }
    95     }
    97 }

mercurial