View Javadoc

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 org.apache.http.impl.nio.codecs;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  import java.nio.channels.ReadableByteChannel;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import org.apache.http.Header;
37  import org.apache.http.MalformedChunkCodingException;
38  import org.apache.http.TruncatedChunkException;
39  import org.apache.http.message.BufferedHeader;
40  import org.apache.http.nio.reactor.SessionInputBuffer;
41  import org.apache.http.annotation.NotThreadSafe;
42  import org.apache.http.impl.io.HttpTransportMetricsImpl;
43  import org.apache.http.ParseException;
44  import org.apache.http.protocol.HTTP;
45  import org.apache.http.util.CharArrayBuffer;
46  
47  /**
48   * Implements chunked transfer coding. The content is received in small chunks.
49   * Entities transferred using this encoder can be of unlimited length.
50   *
51   * @since 4.0
52   */
53  @NotThreadSafe
54  public class ChunkDecoder extends AbstractContentDecoder {
55  
56      private static final int READ_CONTENT   = 0;
57      private static final int READ_FOOTERS  = 1;
58      private static final int COMPLETED      = 2;
59  
60      private int state;
61      private boolean endOfChunk;
62      private boolean endOfStream;
63  
64      private CharArrayBuffer lineBuf;
65      private int chunkSize;
66      private int pos;
67  
68      private final List<CharArrayBuffer> trailerBufs;
69  
70      private Header[] footers;
71  
72      public ChunkDecoder(
73              final ReadableByteChannel channel,
74              final SessionInputBuffer buffer,
75              final HttpTransportMetricsImpl metrics) {
76          super(channel, buffer, metrics);
77          this.state = READ_CONTENT;
78          this.chunkSize = -1;
79          this.pos = 0;
80          this.endOfChunk = false;
81          this.endOfStream = false;
82          this.trailerBufs = new ArrayList<CharArrayBuffer>();
83      }
84  
85      private void readChunkHead() throws IOException {
86          if (this.endOfChunk) {
87              if (this.buffer.length() < 2) {
88                  return;
89              }
90              int cr = this.buffer.read();
91              int lf = this.buffer.read();
92              if (cr != HTTP.CR || lf != HTTP.LF) {
93                  throw new MalformedChunkCodingException("CRLF expected at end of chunk");
94              }
95              this.endOfChunk = false;
96          }
97          if (this.lineBuf == null) {
98              this.lineBuf = new CharArrayBuffer(32);
99          } else {
100             this.lineBuf.clear();
101         }
102         if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
103             int separator = this.lineBuf.indexOf(';');
104             if (separator < 0) {
105                 separator = this.lineBuf.length();
106             }
107             try {
108                 String s = this.lineBuf.substringTrimmed(0, separator);
109                 this.chunkSize = Integer.parseInt(s, 16);
110             } catch (NumberFormatException e) {
111                 throw new MalformedChunkCodingException("Bad chunk header");
112             }
113             this.pos = 0;
114         }
115     }
116 
117     private void parseHeader() {
118         CharArrayBuffer current = this.lineBuf;
119         int count = this.trailerBufs.size();
120         if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) {
121             // Handle folded header line
122             CharArrayBuffer previous = this.trailerBufs.get(count - 1);
123             int i = 0;
124             while (i < current.length()) {
125                 char ch = current.charAt(i);
126                 if (ch != ' ' && ch != '\t') {
127                     break;
128                 }
129                 i++;
130             }
131             previous.append(' ');
132             previous.append(current, i, current.length() - i);
133         } else {
134             this.trailerBufs.add(current);
135             this.lineBuf = null;
136         }
137     }
138 
139     private void processFooters() throws IOException {
140         int count = this.trailerBufs.size();
141         if (count > 0) {
142             this.footers = new Header[this.trailerBufs.size()];
143             for (int i = 0; i < this.trailerBufs.size(); i++) {
144                 CharArrayBuffer buffer = this.trailerBufs.get(i);
145                 try {
146                     this.footers[i] = new BufferedHeader(buffer);
147                 } catch (ParseException ex) {
148                     throw new IOException(ex.getMessage());
149                 }
150             }
151         }
152         this.trailerBufs.clear();
153     }
154 
155     public int read(final ByteBuffer dst) throws IOException {
156         if (dst == null) {
157             throw new IllegalArgumentException("Byte buffer may not be null");
158         }
159         if (this.state == COMPLETED) {
160             return -1;
161         }
162 
163         int totalRead = 0;
164         while (this.state != COMPLETED) {
165 
166             if (!this.buffer.hasData() || this.chunkSize == -1) {
167                 int bytesRead = this.buffer.fill(this.channel);
168                 if (bytesRead > 0) {
169                     this.metrics.incrementBytesTransferred(bytesRead);
170                 }
171                 if (bytesRead == -1) {
172                     this.endOfStream = true;
173                 }
174             }
175 
176             switch (this.state) {
177             case READ_CONTENT:
178 
179                 if (this.chunkSize == -1) {
180                     readChunkHead();
181                     if (this.chunkSize == -1) {
182                         // Unable to read a chunk head
183                         if (this.endOfStream) {
184                             this.state = COMPLETED;
185                             this.completed = true;
186                         }
187                         return totalRead;
188                     }
189                     if (this.chunkSize == 0) {
190                         // Last chunk. Read footers
191                         this.chunkSize = -1;
192                         this.state = READ_FOOTERS;
193                         break;
194                     }
195                 }
196                 int maxLen = this.chunkSize - this.pos;
197                 int len = this.buffer.read(dst, maxLen);
198                 if (len > 0) {
199                     this.pos += len;
200                     totalRead += len;
201                 } else {
202                     if (!this.buffer.hasData() && this.endOfStream) {
203                         this.state = COMPLETED;
204                         this.completed = true;
205                         throw new TruncatedChunkException("Truncated chunk "
206                                 + "( expected size: " + this.chunkSize
207                                 + "; actual size: " + this.pos + ")");
208                     }
209                 }
210 
211                 if (this.pos == this.chunkSize) {
212                     // At the end of the chunk
213                     this.chunkSize = -1;
214                     this.pos = 0;
215                     this.endOfChunk = true;
216                     break;
217                 }
218                 return totalRead;
219             case READ_FOOTERS:
220                 if (this.lineBuf == null) {
221                     this.lineBuf = new CharArrayBuffer(32);
222                 } else {
223                     this.lineBuf.clear();
224                 }
225                 if (!this.buffer.readLine(this.lineBuf, this.endOfStream)) {
226                     // Unable to read a footer
227                     if (this.endOfStream) {
228                         this.state = COMPLETED;
229                         this.completed = true;
230                     }
231                     return totalRead;
232                 }
233                 if (this.lineBuf.length() > 0) {
234                     parseHeader();
235                 } else {
236                     this.state = COMPLETED;
237                     this.completed = true;
238                     processFooters();
239                 }
240                 break;
241             }
242 
243         }
244         return totalRead;
245     }
246 
247     public Header[] getFooters() {
248         if (this.footers != null) {
249             return this.footers.clone();
250         } else {
251             return new Header[] {};
252         }
253     }
254 
255     @Override
256     public String toString() {
257         StringBuilder buffer = new StringBuilder();
258         buffer.append("[chunk-coded; completed: ");
259         buffer.append(this.completed);
260         buffer.append("]");
261         return buffer.toString();
262     }
263 
264 }