mobile/android/thirdparty/ch/boye/httpclientandroidlib/protocol/ResponseConnControl.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.Header;
    33 import ch.boye.httpclientandroidlib.HttpEntity;
    34 import ch.boye.httpclientandroidlib.HttpException;
    35 import ch.boye.httpclientandroidlib.HttpRequest;
    36 import ch.boye.httpclientandroidlib.HttpResponse;
    37 import ch.boye.httpclientandroidlib.HttpResponseInterceptor;
    38 import ch.boye.httpclientandroidlib.HttpStatus;
    39 import ch.boye.httpclientandroidlib.HttpVersion;
    40 import ch.boye.httpclientandroidlib.ProtocolVersion;
    42 /**
    43  * ResponseConnControl is responsible for adding <code>Connection</code> header
    44  * to the outgoing responses, which is essential for managing persistence of
    45  * <code>HTTP/1.0</code> connections. This interceptor is recommended for
    46  * server side protocol processors.
    47  *
    48  * @since 4.0
    49  */
    50 public class ResponseConnControl implements HttpResponseInterceptor {
    52     public ResponseConnControl() {
    53         super();
    54     }
    56     public void process(final HttpResponse response, final HttpContext context)
    57             throws HttpException, IOException {
    58         if (response == null) {
    59             throw new IllegalArgumentException("HTTP response may not be null");
    60         }
    61         if (context == null) {
    62             throw new IllegalArgumentException("HTTP context may not be null");
    63         }
    64         // Always drop connection after certain type of responses
    65         int status = response.getStatusLine().getStatusCode();
    66         if (status == HttpStatus.SC_BAD_REQUEST ||
    67                 status == HttpStatus.SC_REQUEST_TIMEOUT ||
    68                 status == HttpStatus.SC_LENGTH_REQUIRED ||
    69                 status == HttpStatus.SC_REQUEST_TOO_LONG ||
    70                 status == HttpStatus.SC_REQUEST_URI_TOO_LONG ||
    71                 status == HttpStatus.SC_SERVICE_UNAVAILABLE ||
    72                 status == HttpStatus.SC_NOT_IMPLEMENTED) {
    73             response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
    74             return;
    75         }
    76         // Always drop connection for HTTP/1.0 responses and below
    77         // if the content body cannot be correctly delimited
    78         HttpEntity entity = response.getEntity();
    79         if (entity != null) {
    80             ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
    81             if (entity.getContentLength() < 0 &&
    82                     (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0))) {
    83                 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
    84                 return;
    85             }
    86         }
    87         // Drop connection if requested by the client
    88         HttpRequest request = (HttpRequest)
    89             context.getAttribute(ExecutionContext.HTTP_REQUEST);
    90         if (request != null) {
    91             Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
    92             if (header != null) {
    93                 response.setHeader(HTTP.CONN_DIRECTIVE, header.getValue());
    94             }
    95         }
    96     }
    98 }

mercurial