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         final ChunkedInputStream in2 = new ChunkedInputStream(inBuffer2, inputStream2);
314         try {
315             in2.read(buffer);
316             Assert.fail("MessageConstraintException expected");
317         } catch (final MessageConstraintException ex) {
318         } finally {
319             try {
320                 in2.close();
321             } catch (final MessageConstraintException ex) {
322             }
323         }
324     }
325 
326     @Test
327     public void testChunkedOutputStreamClose() throws IOException {
328         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
329         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
330         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048);
331         out.close();
332         out.close();
333         try {
334             out.write(new byte[] {1,2,3});
335             Assert.fail("IOException should have been thrown");
336         } catch (final IOException ex) {
337             // expected
338         }
339         try {
340             out.write(1);
341             Assert.fail("IOException should have been thrown");
342         } catch (final IOException ex) {
343             // expected
344         }
345     }
346 
347     @Test
348     public void testChunkedConsistence() throws IOException {
349         final String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
350         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
351         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
352         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048);
353         out.write(input.getBytes(StandardCharsets.ISO_8859_1));
354         out.flush();
355         out.close();
356         out.close();
357         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
358         final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
359         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
360 
361         final byte[] d = new byte[10];
362         final ByteArrayOutputStream result = new ByteArrayOutputStream();
363         int len = 0;
364         while ((len = in.read(d)) > 0) {
365             result.write(d, 0, len);
366         }
367 
368         final String output = new String(result.toByteArray(), StandardCharsets.ISO_8859_1);
369         Assert.assertEquals(input, output);
370         in.close();
371     }
372 
373     @Test
374     public void testChunkedOutputStreamWithTrailers() throws IOException {
375         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
376         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
377         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2, new Supplier<List<? extends Header>>() {
378             @Override
379             public List<? extends Header> get() {
380                 return Arrays.asList(
381                         new BasicHeader("E", ""),
382                         new BasicHeader("Y", "Z"));
383                 }
384             }
385         );
386         out.write('x');
387         out.finish();
388         out.close();
389 
390         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
391         Assert.assertEquals("1\r\nx\r\n0\r\nE: \r\nY: Z\r\n\r\n", content);
392     }
393 
394     @Test
395     public void testChunkedOutputStream() throws IOException {
396         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
397         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
398         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
399         out.write('1');
400         out.write('2');
401         out.write('3');
402         out.write('4');
403         out.finish();
404         out.close();
405 
406         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
407         Assert.assertEquals("2\r\n12\r\n2\r\n34\r\n0\r\n\r\n", content);
408     }
409 
410     @Test
411     public void testChunkedOutputStreamLargeChunk() throws IOException {
412         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
413         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
414         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
415         out.write(new byte[] {'1', '2', '3', '4'});
416         out.finish();
417         out.close();
418 
419         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
420         Assert.assertEquals("4\r\n1234\r\n0\r\n\r\n", content);
421     }
422 
423     @Test
424     public void testChunkedOutputStreamSmallChunk() throws IOException {
425         final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
426         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
427         final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
428         out.write('1');
429         out.finish();
430         out.close();
431 
432         final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
433         Assert.assertEquals("1\r\n1\r\n0\r\n\r\n", content);
434     }
435 
436     @Test
437     public void testResumeOnSocketTimeoutInData() throws IOException {
438         final String s = "5\r\n01234\r\n5\r\n5\0006789\r\na\r\n0123\000456789\r\n0\r\n";
439         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
440         final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
441         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
442 
443         final byte[] tmp = new byte[3];
444 
445         int bytesRead = 0;
446         int timeouts = 0;
447 
448         int i = 0;
449         while (i != -1) {
450             try {
451                 i = in.read(tmp);
452                 if (i > 0) {
453                     bytesRead += i;
454                 }
455             } catch (final InterruptedIOException ex) {
456                 timeouts++;
457             }
458         }
459         Assert.assertEquals(20, bytesRead);
460         Assert.assertEquals(2, timeouts);
461         in.close();
462 }
463 
464     @Test
465     public void testResumeOnSocketTimeoutInChunk() throws IOException {
466         final String s = "5\000\r\000\n\00001234\r\n\0005\r\n56789\r\na\r\n0123456789\r\n\0000\r\n";
467         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
468         final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
469         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
470 
471         final byte[] tmp = new byte[3];
472 
473         int bytesRead = 0;
474         int timeouts = 0;
475 
476         int i = 0;
477         while (i != -1) {
478             try {
479                 i = in.read(tmp);
480                 if (i > 0) {
481                     bytesRead += i;
482                 }
483             } catch (final InterruptedIOException ex) {
484                 timeouts++;
485             }
486         }
487         Assert.assertEquals(20, bytesRead);
488         Assert.assertEquals(5, timeouts);
489         in.close();
490     }
491 
492     // Test for when buffer is larger than chunk size
493     @Test
494     public void testHugeChunk() throws IOException {
495 
496         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
497         final ByteArrayInputStream inputStream = new ByteArrayInputStream("1234567890abcdef\r\n01234567".getBytes(
498                 StandardCharsets.ISO_8859_1));
499         final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
500 
501         final ByteArrayOutputStream out = new ByteArrayOutputStream();
502         for (int i = 0; i < 8; ++i) {
503             out.write(in.read());
504         }
505 
506         final String result = new String(out.toByteArray(), StandardCharsets.ISO_8859_1);
507         Assert.assertEquals("01234567", result);
508     }
509 
510 }
511