Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl.entity; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.HttpException; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.HttpMessage; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.entity.BasicHttpEntity; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.impl.io.ChunkedInputStream; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.impl.io.ContentLengthInputStream; |
michael@0 | 40 | import ch.boye.httpclientandroidlib.impl.io.IdentityInputStream; |
michael@0 | 41 | import ch.boye.httpclientandroidlib.io.SessionInputBuffer; |
michael@0 | 42 | import ch.boye.httpclientandroidlib.protocol.HTTP; |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * HTTP entity deserializer. |
michael@0 | 46 | * <p> |
michael@0 | 47 | * This entity deserializer supports "chunked" and "identitiy" transfer-coding |
michael@0 | 48 | * and content length delimited content. |
michael@0 | 49 | * <p> |
michael@0 | 50 | * This class relies on a specific implementation of |
michael@0 | 51 | * {@link ContentLengthStrategy} to determine the content length or transfer |
michael@0 | 52 | * encoding of the entity. |
michael@0 | 53 | * <p> |
michael@0 | 54 | * This class generates an instance of {@link HttpEntity} based on |
michael@0 | 55 | * properties of the message. The content of the entity will be decoded |
michael@0 | 56 | * transparently for the consumer. |
michael@0 | 57 | * |
michael@0 | 58 | * @since 4.0 |
michael@0 | 59 | */ |
michael@0 | 60 | public class EntityDeserializer { |
michael@0 | 61 | |
michael@0 | 62 | private final ContentLengthStrategy lenStrategy; |
michael@0 | 63 | |
michael@0 | 64 | public EntityDeserializer(final ContentLengthStrategy lenStrategy) { |
michael@0 | 65 | super(); |
michael@0 | 66 | if (lenStrategy == null) { |
michael@0 | 67 | throw new IllegalArgumentException("Content length strategy may not be null"); |
michael@0 | 68 | } |
michael@0 | 69 | this.lenStrategy = lenStrategy; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * Creates a {@link BasicHttpEntity} based on properties of the given |
michael@0 | 74 | * message. The content of the entity is created by wrapping |
michael@0 | 75 | * {@link SessionInputBuffer} with a content decoder depending on the |
michael@0 | 76 | * transfer mechanism used by the message. |
michael@0 | 77 | * <p> |
michael@0 | 78 | * This method is called by the public |
michael@0 | 79 | * {@link #deserialize(SessionInputBuffer, HttpMessage)}. |
michael@0 | 80 | * |
michael@0 | 81 | * @param inbuffer the session input buffer. |
michael@0 | 82 | * @param message the message. |
michael@0 | 83 | * @return HTTP entity. |
michael@0 | 84 | * @throws HttpException in case of HTTP protocol violation. |
michael@0 | 85 | * @throws IOException in case of an I/O error. |
michael@0 | 86 | */ |
michael@0 | 87 | protected BasicHttpEntity doDeserialize( |
michael@0 | 88 | final SessionInputBuffer inbuffer, |
michael@0 | 89 | final HttpMessage message) throws HttpException, IOException { |
michael@0 | 90 | BasicHttpEntity entity = new BasicHttpEntity(); |
michael@0 | 91 | |
michael@0 | 92 | long len = this.lenStrategy.determineLength(message); |
michael@0 | 93 | if (len == ContentLengthStrategy.CHUNKED) { |
michael@0 | 94 | entity.setChunked(true); |
michael@0 | 95 | entity.setContentLength(-1); |
michael@0 | 96 | entity.setContent(new ChunkedInputStream(inbuffer)); |
michael@0 | 97 | } else if (len == ContentLengthStrategy.IDENTITY) { |
michael@0 | 98 | entity.setChunked(false); |
michael@0 | 99 | entity.setContentLength(-1); |
michael@0 | 100 | entity.setContent(new IdentityInputStream(inbuffer)); |
michael@0 | 101 | } else { |
michael@0 | 102 | entity.setChunked(false); |
michael@0 | 103 | entity.setContentLength(len); |
michael@0 | 104 | entity.setContent(new ContentLengthInputStream(inbuffer, len)); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE); |
michael@0 | 108 | if (contentTypeHeader != null) { |
michael@0 | 109 | entity.setContentType(contentTypeHeader); |
michael@0 | 110 | } |
michael@0 | 111 | Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING); |
michael@0 | 112 | if (contentEncodingHeader != null) { |
michael@0 | 113 | entity.setContentEncoding(contentEncodingHeader); |
michael@0 | 114 | } |
michael@0 | 115 | return entity; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * Creates an {@link HttpEntity} based on properties of the given message. |
michael@0 | 120 | * The content of the entity is created by wrapping |
michael@0 | 121 | * {@link SessionInputBuffer} with a content decoder depending on the |
michael@0 | 122 | * transfer mechanism used by the message. |
michael@0 | 123 | * <p> |
michael@0 | 124 | * The content of the entity is NOT retrieved by this method. |
michael@0 | 125 | * |
michael@0 | 126 | * @param inbuffer the session input buffer. |
michael@0 | 127 | * @param message the message. |
michael@0 | 128 | * @return HTTP entity. |
michael@0 | 129 | * @throws HttpException in case of HTTP protocol violation. |
michael@0 | 130 | * @throws IOException in case of an I/O error. |
michael@0 | 131 | */ |
michael@0 | 132 | public HttpEntity deserialize( |
michael@0 | 133 | final SessionInputBuffer inbuffer, |
michael@0 | 134 | final HttpMessage message) throws HttpException, IOException { |
michael@0 | 135 | if (inbuffer == null) { |
michael@0 | 136 | throw new IllegalArgumentException("Session input buffer may not be null"); |
michael@0 | 137 | } |
michael@0 | 138 | if (message == null) { |
michael@0 | 139 | throw new IllegalArgumentException("HTTP message may not be null"); |
michael@0 | 140 | } |
michael@0 | 141 | return doDeserialize(inbuffer, message); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | } |