1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/entity/LaxContentLengthStrategy.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,135 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.entity; 1.32 + 1.33 +import ch.boye.httpclientandroidlib.Header; 1.34 +import ch.boye.httpclientandroidlib.HeaderElement; 1.35 +import ch.boye.httpclientandroidlib.HttpException; 1.36 +import ch.boye.httpclientandroidlib.HttpMessage; 1.37 +import ch.boye.httpclientandroidlib.ParseException; 1.38 +import ch.boye.httpclientandroidlib.ProtocolException; 1.39 +import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy; 1.40 +import ch.boye.httpclientandroidlib.params.HttpParams; 1.41 +import ch.boye.httpclientandroidlib.params.CoreProtocolPNames; 1.42 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.43 + 1.44 +/** 1.45 + * The lax implementation of the content length strategy. This class will ignore 1.46 + * unrecognized transfer encodings and malformed <code>Content-Length</code> 1.47 + * header values if the {@link CoreProtocolPNames#STRICT_TRANSFER_ENCODING} 1.48 + * parameter of the given message is not set or set to <code>false</code>. 1.49 + * <p> 1.50 + * This class recognizes "chunked" and "identitiy" transfer-coding only. 1.51 + * <p> 1.52 + * The following parameters can be used to customize the behavior of this class: 1.53 + * <ul> 1.54 + * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li> 1.55 + * </ul> 1.56 + * 1.57 + * @since 4.0 1.58 + */ 1.59 +public class LaxContentLengthStrategy implements ContentLengthStrategy { 1.60 + 1.61 + public LaxContentLengthStrategy() { 1.62 + super(); 1.63 + } 1.64 + 1.65 + public long determineLength(final HttpMessage message) throws HttpException { 1.66 + if (message == null) { 1.67 + throw new IllegalArgumentException("HTTP message may not be null"); 1.68 + } 1.69 + 1.70 + HttpParams params = message.getParams(); 1.71 + boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING); 1.72 + 1.73 + Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING); 1.74 + Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN); 1.75 + // We use Transfer-Encoding if present and ignore Content-Length. 1.76 + // RFC2616, 4.4 item number 3 1.77 + if (transferEncodingHeader != null) { 1.78 + HeaderElement[] encodings = null; 1.79 + try { 1.80 + encodings = transferEncodingHeader.getElements(); 1.81 + } catch (ParseException px) { 1.82 + throw new ProtocolException 1.83 + ("Invalid Transfer-Encoding header value: " + 1.84 + transferEncodingHeader, px); 1.85 + } 1.86 + if (strict) { 1.87 + // Currently only chunk and identity are supported 1.88 + for (int i = 0; i < encodings.length; i++) { 1.89 + String encoding = encodings[i].getName(); 1.90 + if (encoding != null && encoding.length() > 0 1.91 + && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING) 1.92 + && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) { 1.93 + throw new ProtocolException("Unsupported transfer encoding: " + encoding); 1.94 + } 1.95 + } 1.96 + } 1.97 + // The chunked encoding must be the last one applied RFC2616, 14.41 1.98 + int len = encodings.length; 1.99 + if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) { 1.100 + return IDENTITY; 1.101 + } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase( 1.102 + encodings[len - 1].getName()))) { 1.103 + return CHUNKED; 1.104 + } else { 1.105 + if (strict) { 1.106 + throw new ProtocolException("Chunk-encoding must be the last one applied"); 1.107 + } 1.108 + return IDENTITY; 1.109 + } 1.110 + } else if (contentLengthHeader != null) { 1.111 + long contentlen = -1; 1.112 + Header[] headers = message.getHeaders(HTTP.CONTENT_LEN); 1.113 + if (strict && headers.length > 1) { 1.114 + throw new ProtocolException("Multiple content length headers"); 1.115 + } 1.116 + for (int i = headers.length - 1; i >= 0; i--) { 1.117 + Header header = headers[i]; 1.118 + try { 1.119 + contentlen = Long.parseLong(header.getValue()); 1.120 + break; 1.121 + } catch (NumberFormatException e) { 1.122 + if (strict) { 1.123 + throw new ProtocolException("Invalid content length: " + header.getValue()); 1.124 + } 1.125 + } 1.126 + // See if we can have better luck with another header, if present 1.127 + } 1.128 + if (contentlen >= 0) { 1.129 + return contentlen; 1.130 + } else { 1.131 + return IDENTITY; 1.132 + } 1.133 + } else { 1.134 + return IDENTITY; 1.135 + } 1.136 + } 1.137 + 1.138 +}