michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl.entity;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.Header;
michael@0: import ch.boye.httpclientandroidlib.HeaderElement;
michael@0: import ch.boye.httpclientandroidlib.HttpException;
michael@0: import ch.boye.httpclientandroidlib.HttpMessage;
michael@0: import ch.boye.httpclientandroidlib.ParseException;
michael@0: import ch.boye.httpclientandroidlib.ProtocolException;
michael@0: import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy;
michael@0: import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0: import ch.boye.httpclientandroidlib.params.CoreProtocolPNames;
michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0:
michael@0: /**
michael@0: * The lax implementation of the content length strategy. This class will ignore
michael@0: * unrecognized transfer encodings and malformed Content-Length
michael@0: * header values if the {@link CoreProtocolPNames#STRICT_TRANSFER_ENCODING}
michael@0: * parameter of the given message is not set or set to false
.
michael@0: *
michael@0: * This class recognizes "chunked" and "identitiy" transfer-coding only.
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this class:
michael@0: *
michael@0: * - {@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: public class LaxContentLengthStrategy implements ContentLengthStrategy {
michael@0:
michael@0: public LaxContentLengthStrategy() {
michael@0: super();
michael@0: }
michael@0:
michael@0: public long determineLength(final HttpMessage message) throws HttpException {
michael@0: if (message == null) {
michael@0: throw new IllegalArgumentException("HTTP message may not be null");
michael@0: }
michael@0:
michael@0: HttpParams params = message.getParams();
michael@0: boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
michael@0:
michael@0: Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
michael@0: Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
michael@0: // We use Transfer-Encoding if present and ignore Content-Length.
michael@0: // RFC2616, 4.4 item number 3
michael@0: if (transferEncodingHeader != null) {
michael@0: HeaderElement[] encodings = null;
michael@0: try {
michael@0: encodings = transferEncodingHeader.getElements();
michael@0: } catch (ParseException px) {
michael@0: throw new ProtocolException
michael@0: ("Invalid Transfer-Encoding header value: " +
michael@0: transferEncodingHeader, px);
michael@0: }
michael@0: if (strict) {
michael@0: // Currently only chunk and identity are supported
michael@0: for (int i = 0; i < encodings.length; i++) {
michael@0: String encoding = encodings[i].getName();
michael@0: if (encoding != null && encoding.length() > 0
michael@0: && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING)
michael@0: && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
michael@0: throw new ProtocolException("Unsupported transfer encoding: " + encoding);
michael@0: }
michael@0: }
michael@0: }
michael@0: // The chunked encoding must be the last one applied RFC2616, 14.41
michael@0: int len = encodings.length;
michael@0: if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
michael@0: return IDENTITY;
michael@0: } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
michael@0: encodings[len - 1].getName()))) {
michael@0: return CHUNKED;
michael@0: } else {
michael@0: if (strict) {
michael@0: throw new ProtocolException("Chunk-encoding must be the last one applied");
michael@0: }
michael@0: return IDENTITY;
michael@0: }
michael@0: } else if (contentLengthHeader != null) {
michael@0: long contentlen = -1;
michael@0: Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
michael@0: if (strict && headers.length > 1) {
michael@0: throw new ProtocolException("Multiple content length headers");
michael@0: }
michael@0: for (int i = headers.length - 1; i >= 0; i--) {
michael@0: Header header = headers[i];
michael@0: try {
michael@0: contentlen = Long.parseLong(header.getValue());
michael@0: break;
michael@0: } catch (NumberFormatException e) {
michael@0: if (strict) {
michael@0: throw new ProtocolException("Invalid content length: " + header.getValue());
michael@0: }
michael@0: }
michael@0: // See if we can have better luck with another header, if present
michael@0: }
michael@0: if (contentlen >= 0) {
michael@0: return contentlen;
michael@0: } else {
michael@0: return IDENTITY;
michael@0: }
michael@0: } else {
michael@0: return IDENTITY;
michael@0: }
michael@0: }
michael@0:
michael@0: }