|
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 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.impl.entity; |
|
29 |
|
30 import ch.boye.httpclientandroidlib.Header; |
|
31 import ch.boye.httpclientandroidlib.HttpException; |
|
32 import ch.boye.httpclientandroidlib.HttpMessage; |
|
33 import ch.boye.httpclientandroidlib.HttpVersion; |
|
34 import ch.boye.httpclientandroidlib.ProtocolException; |
|
35 import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy; |
|
36 import ch.boye.httpclientandroidlib.protocol.HTTP; |
|
37 |
|
38 /** |
|
39 * The strict implementation of the content length strategy. This class |
|
40 * will throw {@link ProtocolException} if it encounters an unsupported |
|
41 * transfer encoding or a malformed <code>Content-Length</code> header |
|
42 * value. |
|
43 * <p> |
|
44 * This class recognizes "chunked" and "identitiy" transfer-coding only. |
|
45 * |
|
46 * @since 4.0 |
|
47 */ |
|
48 public class StrictContentLengthStrategy implements ContentLengthStrategy { |
|
49 |
|
50 public StrictContentLengthStrategy() { |
|
51 super(); |
|
52 } |
|
53 |
|
54 public long determineLength(final HttpMessage message) throws HttpException { |
|
55 if (message == null) { |
|
56 throw new IllegalArgumentException("HTTP message may not be null"); |
|
57 } |
|
58 // Although Transfer-Encoding is specified as a list, in practice |
|
59 // it is either missing or has the single value "chunked". So we |
|
60 // treat it as a single-valued header here. |
|
61 Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING); |
|
62 Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN); |
|
63 if (transferEncodingHeader != null) { |
|
64 String s = transferEncodingHeader.getValue(); |
|
65 if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) { |
|
66 if (message.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) { |
|
67 throw new ProtocolException( |
|
68 "Chunked transfer encoding not allowed for " + |
|
69 message.getProtocolVersion()); |
|
70 } |
|
71 return CHUNKED; |
|
72 } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(s)) { |
|
73 return IDENTITY; |
|
74 } else { |
|
75 throw new ProtocolException( |
|
76 "Unsupported transfer encoding: " + s); |
|
77 } |
|
78 } else if (contentLengthHeader != null) { |
|
79 String s = contentLengthHeader.getValue(); |
|
80 try { |
|
81 long len = Long.parseLong(s); |
|
82 return len; |
|
83 } catch (NumberFormatException e) { |
|
84 throw new ProtocolException("Invalid content length: " + s); |
|
85 } |
|
86 } else { |
|
87 return IDENTITY; |
|
88 } |
|
89 } |
|
90 |
|
91 } |