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.hc.core5.http.impl.io;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.InterruptedIOException;
34  import java.nio.charset.StandardCharsets;
35  import java.util.Arrays;
36  import java.util.List;
37  
38  import org.apache.hc.core5.function.Supplier;
39  import org.apache.hc.core5.http.ConnectionClosedException;
40  import org.apache.hc.core5.http.Header;
41  import org.apache.hc.core5.http.MalformedChunkCodingException;
42  import org.apache.hc.core5.http.MessageConstraintException;
43  import org.apache.hc.core5.http.StreamClosedException;
44  import org.apache.hc.core5.http.TruncatedChunkException;
45  import org.apache.hc.core5.http.io.SessionInputBuffer;
46  import org.apache.hc.core5.http.io.SessionOutputBuffer;
47  import org.apache.hc.core5.http.message.BasicHeader;
48  import org.junit.Assert;
49  import org.junit.Test;
50  
51  public class TestChunkCoding {
52  
53      private final static String CHUNKED_INPUT
54          = "10;key=\"value\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
55  
56      private final static String CHUNKED_RESULT
57          = "123456789012345612345";
58  
59      @Test
60      public void testChunkedInputStreamLargeBuffer() throws IOException {
61          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
62          final ByteArrayInputStream inputStream = new ByteArrayInputStream(CHUNKED_INPUT.getBytes(StandardCharsets.ISO_8859_1));
63          final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
64          final byte[] buffer = new byte[300];
65          final ByteArrayOutputStream out = new ByteArrayOutputStream();
66          int len;
67          while ((len = in.read(buffer)) > 0) {
68              out.write(buffer, 0, len);
69          }
70          Assert.assertEquals(-1, in.read(buffer));
71          Assert.assertEquals(-1, in.read(buffer));
72  
73          in.close();
74  
75          final String result = new String(out.toByteArray(), StandardCharsets.ISO_8859_1);
76          Assert.assertEquals(result, CHUNKED_RESULT);
77  
78          final Header[] footers = in.getFooters();
79          Assert.assertNotNull(footers);
80          Assert.assertEquals(2, footers.length);
81          Assert.assertEquals("Footer1", footers[0].getName());
82          Assert.assertEquals("abcde", footers[0].getValue());
83          Assert.assertEquals("Footer2", footers[1].getName());
84          Assert.assertEquals("fghij", footers[1].getValue());
85      }
86  
87      //Test for when buffer is smaller than chunk size.
88      @Test
89      public void testChunkedInputStreamSmallBuffer() throws IOException {
90          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
91          final ByteArrayInputStream inputStream = new ByteArrayInputStream(CHUNKED_INPUT.getBytes(StandardCharsets.ISO_8859_1));
92          final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
93  
94          final byte[] buffer = new byte[7];
95          final ByteArrayOutputStream out = new ByteArrayOutputStream();
96          int len;
97          while ((len = in.read(buffer)) > 0) {
98              out.write(buffer, 0, len);
99          }
100         Assert.assertEquals(-1, in.read(buffer));
101         Assert.assertEquals(-1, in.read(buffer));
102 
103         in.close();
104 
105         final Header[] footers = in.getFooters();
106         Assert.assertNotNull(footers);
107         Assert.assertEquals(2, footers.length);
108         Assert.assertEquals("Footer1", footers[0].getName());
109         Assert.assertEquals("abcde", footers[0].getValue());
110         Assert.assertEquals("Footer2", footers[1].getName());
111         Assert.assertEquals("fghij", footers[1].getValue());
112     }
113 
114     // One byte read
115     @Test
116     public void testChunkedInputStreamOneByteRead() throws IOException {
117         final String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
118         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
119         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
120         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
121         int ch;
122         int i = '0';
123         while ((ch = in.read()) != -1) {
124             Assert.assertEquals(i, ch);
125             i++;
126         }
127         Assert.assertEquals(-1, in.read());
128         Assert.assertEquals(-1, in.read());
129 
130         in.close();
131     }
132 
133     @Test
134     public void testAvailable() throws IOException {
135         final String s = "5\r\n12345\r\n0\r\n";
136         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
137         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
138         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
139         Assert.assertEquals(0, in.available());
140         in.read();
141         Assert.assertEquals(4, in.available());
142         in.close();
143     }
144 
145     @Test
146     public void testChunkedInputStreamClose() throws IOException {
147         final String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
148         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
149         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
150         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
151         in.close();
152         in.close();
153         try {
154             in.read();
155             Assert.fail("StreamClosedException expected");
156         } catch (final StreamClosedException expected) {
157         }
158         final byte[] tmp = new byte[10];
159         try {
160             in.read(tmp);
161             Assert.fail("StreamClosedException expected");
162         } catch (final StreamClosedException expected) {
163         }
164         try {
165             in.read(tmp, 0, tmp.length);
166             Assert.fail("StreamClosedException expected");
167         } catch (final StreamClosedException expected) {
168         }
169     }
170 
171     // Missing closing chunk
172     @Test(expected=ConnectionClosedException.class)
173     public void testChunkedInputStreamNoClosingChunk() throws IOException {
174         final String s = "5\r\n01234\r\n";
175         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
176         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
177         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
178         final byte[] tmp = new byte[5];
179         Assert.assertEquals(5, in.read(tmp));
180         in.read();
181         in.close();
182     }
183 
184     // Truncated stream (missing closing CRLF)
185     @Test(expected=MalformedChunkCodingException.class)
186     public void testCorruptChunkedInputStreamTruncatedCRLF() throws IOException {
187         final String s = "5\r\n01234";
188         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
189         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
190         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
191         final byte[] tmp = new byte[5];
192         Assert.assertEquals(5, in.read(tmp));
193         in.read();
194         in.close();
195     }
196 
197     // Missing \r\n at the end of the first chunk
198     @Test(expected=MalformedChunkCodingException.class)
199     public void testCorruptChunkedInputStreamMissingCRLF() throws IOException {
200         final String s = "5\r\n012345\r\n56789\r\n0\r\n";
201         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
202         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
203         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
204         final byte[] buffer = new byte[300];
205         final ByteArrayOutputStream out = new ByteArrayOutputStream();
206         int len;
207         while ((len = in.read(buffer)) > 0) {
208             out.write(buffer, 0, len);
209         }
210         in.close();
211     }
212 
213     // Missing LF
214     @Test(expected=MalformedChunkCodingException.class)
215     public void testCorruptChunkedInputStreamMissingLF() throws IOException {
216         final String s = "5\r01234\r\n5\r\n56789\r\n0\r\n";
217         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
218         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
219         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
220         in.read();
221         in.close();
222     }
223 
224     // Invalid chunk size
225     @Test(expected = MalformedChunkCodingException.class)
226     public void testCorruptChunkedInputStreamInvalidSize() throws IOException {
227         final String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n";
228         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
229         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
230         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
231         in.read();
232         in.close();
233     }
234 
235     // Negative chunk size
236     @Test(expected = MalformedChunkCodingException.class)
237     public void testCorruptChunkedInputStreamNegativeSize() throws IOException {
238         final String s = "-5\r\n01234\r\n5\r\n56789\r\n0\r\n";
239         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
240         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
241         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
242         in.read();
243         in.close();
244     }
245 
246     // Truncated chunk
247     @Test(expected = TruncatedChunkException.class)
248     public void testCorruptChunkedInputStreamTruncatedChunk() throws IOException {
249         final String s = "3\r\n12";
250         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
251         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
252         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
253         final byte[] buffer = new byte[300];
254         Assert.assertEquals(2, in.read(buffer));
255         in.read(buffer);
256         in.close();
257     }
258 
259     // Invalid footer
260     @Test(expected = MalformedChunkCodingException.class)
261     public void testCorruptChunkedInputStreamInvalidFooter() throws IOException {
262         final String s = "1\r\n0\r\n0\r\nstuff\r\n";
263         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
264         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
265         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
266         in.read();
267         in.read();
268         in.close();
269     }
270 
271     @Test
272     public void testCorruptChunkedInputStreamClose() throws IOException {
273         final String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n";
274         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
275         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
276         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
277         try {
278             in.read();
279             Assert.fail("MalformedChunkCodingException expected");
280         } catch (final MalformedChunkCodingException ex) {
281         }
282         in.close();
283     }
284 
285     @Test
286     public void testEmptyChunkedInputStream() throws IOException {
287         final String s = "0\r\n";
288         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
289         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
290         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
291         final byte[] buffer = new byte[300];
292         final ByteArrayOutputStream out = new ByteArrayOutputStream();
293         int len;
294         while ((len = in.read(buffer)) > 0) {
295             out.write(buffer, 0, len);
296         }
297         Assert.assertEquals(0, out.size());
298         in.close();
299     }
300 
301     @Test
302     public void testTooLongChunkHeader() throws IOException {
303         final String s = "5; and some very looooong commend\r\n12345\r\n0\r\n";
304         final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16);
305         final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
306         final ChunkedInputStream in1 = new ChunkedInputStream(inBuffer1, inputStream1);
307         final byte[] buffer = new byte[300];
308         Assert.assertEquals(5, in1.read(buffer));
309         in1.close();
310 
311         final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16, 10);
312         final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
313         try (ChunkedInputStream in2 = new ChunkedInputStream(inBuffer2, inputStream2)) {
314             in2.read(buffer);
315             Assert.fail("MessageConstraintException expected");
316         } catch (final MessageConstraintException ex) {
317         }
318     }
319 
320     @Test
321     public void testChunkedOutputStreamClose() throws IOException {
322         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
323         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
324         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048);
325         out.close();
326         out.close();
327         try {
328             out.write(new byte[] {1,2,3});
329             Assert.fail("IOException should have been thrown");
330         } catch (final IOException ex) {
331             // expected
332         }
333         try {
334             out.write(1);
335             Assert.fail("IOException should have been thrown");
336         } catch (final IOException ex) {
337             // expected
338         }
339     }
340 
341     @Test
342     public void testChunkedConsistence() throws IOException {
343         final String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
344         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
345         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
346         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048);
347         out.write(input.getBytes(StandardCharsets.ISO_8859_1));
348         out.flush();
349         out.close();
350         out.close();
351         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
352         final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
353         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
354 
355         final byte[] d = new byte[10];
356         final ByteArrayOutputStream result = new ByteArrayOutputStream();
357         int len = 0;
358         while ((len = in.read(d)) > 0) {
359             result.write(d, 0, len);
360         }
361 
362         final String output = new String(result.toByteArray(), StandardCharsets.ISO_8859_1);
363         Assert.assertEquals(input, output);
364         in.close();
365     }
366 
367     @Test
368     public void testChunkedOutputStreamWithTrailers() throws IOException {
369         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
370         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
371         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2, new Supplier<List<? extends Header>>() {
372             @Override
373             public List<? extends Header> get() {
374                 return Arrays.asList(
375                         new BasicHeader("E", ""),
376                         new BasicHeader("Y", "Z"));
377                 }
378             }
379         );
380         out.write('x');
381         out.finish();
382         out.close();
383 
384         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
385         Assert.assertEquals("1\r\nx\r\n0\r\nE: \r\nY: Z\r\n\r\n", content);
386     }
387 
388     @Test
389     public void testChunkedOutputStream() throws IOException {
390         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
391         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
392         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
393         out.write('1');
394         out.write('2');
395         out.write('3');
396         out.write('4');
397         out.finish();
398         out.close();
399 
400         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
401         Assert.assertEquals("2\r\n12\r\n2\r\n34\r\n0\r\n\r\n", content);
402     }
403 
404     @Test
405     public void testChunkedOutputStreamLargeChunk() throws IOException {
406         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
407         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
408         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
409         out.write(new byte[] {'1', '2', '3', '4'});
410         out.finish();
411         out.close();
412 
413         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
414         Assert.assertEquals("4\r\n1234\r\n0\r\n\r\n", content);
415     }
416 
417     @Test
418     public void testChunkedOutputStreamSmallChunk() throws IOException {
419         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
420         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
421         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
422         out.write('1');
423         out.finish();
424         out.close();
425 
426         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
427         Assert.assertEquals("1\r\n1\r\n0\r\n\r\n", content);
428     }
429 
430     @Test
431     public void testResumeOnSocketTimeoutInData() throws IOException {
432         final String s = "5\r\n01234\r\n5\r\n5\0006789\r\na\r\n0123\000456789\r\n0\r\n";
433         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
434         final TimeoutByteArrayInputStreamrrayInputStream.html#TimeoutByteArrayInputStream">TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
435         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
436 
437         final byte[] tmp = new byte[3];
438 
439         int bytesRead = 0;
440         int timeouts = 0;
441 
442         int i = 0;
443         while (i != -1) {
444             try {
445                 i = in.read(tmp);
446                 if (i > 0) {
447                     bytesRead += i;
448                 }
449             } catch (final InterruptedIOException ex) {
450                 timeouts++;
451             }
452         }
453         Assert.assertEquals(20, bytesRead);
454         Assert.assertEquals(2, timeouts);
455         in.close();
456 }
457 
458     @Test
459     public void testResumeOnSocketTimeoutInChunk() throws IOException {
460         final String s = "5\000\r\000\n\00001234\r\n\0005\r\n56789\r\na\r\n0123456789\r\n\0000\r\n";
461         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
462         final TimeoutByteArrayInputStreamrrayInputStream.html#TimeoutByteArrayInputStream">TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
463         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
464 
465         final byte[] tmp = new byte[3];
466 
467         int bytesRead = 0;
468         int timeouts = 0;
469 
470         int i = 0;
471         while (i != -1) {
472             try {
473                 i = in.read(tmp);
474                 if (i > 0) {
475                     bytesRead += i;
476                 }
477             } catch (final InterruptedIOException ex) {
478                 timeouts++;
479             }
480         }
481         Assert.assertEquals(20, bytesRead);
482         Assert.assertEquals(5, timeouts);
483         in.close();
484     }
485 
486     // Test for when buffer is larger than chunk size
487     @Test
488     public void testHugeChunk() throws IOException {
489 
490         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
491         final ByteArrayInputStream inputStream = new ByteArrayInputStream("1234567890abcdef\r\n01234567".getBytes(
492                 StandardCharsets.ISO_8859_1));
493         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
494 
495         final ByteArrayOutputStream out = new ByteArrayOutputStream();
496         for (int i = 0; i < 8; ++i) {
497             out.write(in.read());
498         }
499 
500         final String result = new String(out.toByteArray(), StandardCharsets.ISO_8859_1);
501         Assert.assertEquals("01234567", result);
502     }
503 
504 }
505