mobile/android/thirdparty/ch/boye/httpclientandroidlib/protocol/RequestContent.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.HttpEntityEnclosingRequest;
    34 import ch.boye.httpclientandroidlib.HttpException;
    35 import ch.boye.httpclientandroidlib.HttpRequest;
    36 import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
    37 import ch.boye.httpclientandroidlib.HttpVersion;
    38 import ch.boye.httpclientandroidlib.ProtocolVersion;
    39 import ch.boye.httpclientandroidlib.ProtocolException;
    41 /**
    42  * RequestContent is the most important interceptor for outgoing requests.
    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 client side protocol
    47  * processors.
    48  *
    49  * @since 4.0
    50  */
    51 public class RequestContent implements HttpRequestInterceptor {
    53     public RequestContent() {
    54         super();
    55     }
    57     public void process(final HttpRequest request, final HttpContext context)
    58             throws HttpException, IOException {
    59         if (request == null) {
    60             throw new IllegalArgumentException("HTTP request may not be null");
    61         }
    62         if (request instanceof HttpEntityEnclosingRequest) {
    63             if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
    64                 throw new ProtocolException("Transfer-encoding header already present");
    65             }
    66             if (request.containsHeader(HTTP.CONTENT_LEN)) {
    67                 throw new ProtocolException("Content-Length header already present");
    68             }
    69             ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
    70             HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
    71             if (entity == null) {
    72                 request.addHeader(HTTP.CONTENT_LEN, "0");
    73                 return;
    74             }
    75             // Must specify a transfer encoding or a content length
    76             if (entity.isChunked() || entity.getContentLength() < 0) {
    77                 if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
    78                     throw new ProtocolException(
    79                             "Chunked transfer encoding not allowed for " + ver);
    80                 }
    81                 request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
    82             } else {
    83                 request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
    84             }
    85             // Specify a content type if known
    86             if (entity.getContentType() != null && !request.containsHeader(
    87                     HTTP.CONTENT_TYPE )) {
    88                 request.addHeader(entity.getContentType());
    89             }
    90             // Specify a content encoding if known
    91             if (entity.getContentEncoding() != null && !request.containsHeader(
    92                     HTTP.CONTENT_ENCODING)) {
    93                 request.addHeader(entity.getContentEncoding());
    94             }
    95         }
    96     }
    98 }

mercurial