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  package org.apache.hc.core5.http.impl.io;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.InterruptedIOException;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.apache.hc.core5.http.ClassicHttpRequest;
34  import org.apache.hc.core5.http.ConnectionClosedException;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.MessageConstraintException;
37  import org.apache.hc.core5.http.config.H1Config;
38  import org.apache.hc.core5.http.io.SessionInputBuffer;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  /**
43   * Unit tests for {@link DefaultHttpRequestParser}.
44   */
45  public class TestRequestParser {
46  
47      @Test
48      public void testBasicMessageParsing() throws Exception {
49          final String s =
50              "GET / HTTP/1.1\r\n" +
51              "Host: localhost\r\n" +
52              "User-Agent: whatever\r\n" +
53              "Cookie: c1=stuff\r\n" +
54              "\r\n";
55          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
56          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
57  
58          final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
59          final ClassicHttpRequest httprequest = parser.parse(inBuffer, inputStream);
60  
61          Assert.assertEquals("GET", httprequest.getMethod());
62          Assert.assertEquals("/", httprequest.getPath());
63          final Header[] headers = httprequest.getHeaders();
64          Assert.assertEquals(3, headers.length);
65      }
66  
67      @Test(expected = ConnectionClosedException.class)
68      public void testConnectionClosedException() throws Exception {
69          final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
70          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
71  
72          final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
73          parser.parse(inBuffer, inputStream);
74      }
75  
76      @Test
77      public void testBasicMessageParsingLeadingEmptyLines() throws Exception {
78          final String s =
79                  "\r\n" +
80                  "\r\n" +
81                  "GET / HTTP/1.1\r\n" +
82                  "Host: localhost\r\n" +
83                  "\r\n";
84          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
85          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
86  
87          final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(
88                  H1Config.custom().setMaxEmptyLineCount(3).build());
89          final ClassicHttpRequest httprequest = parser.parse(inBuffer, inputStream);
90  
91          Assert.assertEquals("GET", httprequest.getMethod());
92          Assert.assertEquals("/", httprequest.getPath());
93          final Header[] headers = httprequest.getHeaders();
94          Assert.assertEquals(1, headers.length);
95      }
96  
97      @Test(expected = MessageConstraintException.class)
98      public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception {
99          final String s =
100                 "\r\n" +
101                 "\r\n" +
102                 "\r\n" +
103                 "GET / HTTP/1.1\r\n" +
104                 "Host: localhost\r\n" +
105                 "\r\n";
106         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
107         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
108 
109         final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(
110                 H1Config.custom().setMaxEmptyLineCount(3).build());
111         parser.parse(inBuffer, inputStream);
112     }
113 
114     @Test
115     public void testMessageParsingTimeout() throws Exception {
116         final String s =
117             "GET \000/ HTTP/1.1\r\000\n" +
118             "Host: loca\000lhost\r\n" +
119             "User-Agent: whatever\r\n" +
120             "Coo\000kie: c1=stuff\r\n" +
121             "\000\r\n";
122         final TimeoutByteArrayInputStreamrrayInputStream.html#TimeoutByteArrayInputStream">TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
123         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
124 
125         final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
126 
127         int timeoutCount = 0;
128 
129         ClassicHttpRequest httprequest = null;
130         for (int i = 0; i < 10; i++) {
131             try {
132                 httprequest = parser.parse(inBuffer, inputStream);
133                 break;
134             } catch (final InterruptedIOException ex) {
135                 timeoutCount++;
136             }
137 
138         }
139         Assert.assertNotNull(httprequest);
140         Assert.assertEquals(5, timeoutCount);
141 
142         Assert.assertEquals("GET", httprequest.getMethod());
143         Assert.assertEquals("/", httprequest.getPath());
144         final Header[] headers = httprequest.getHeaders();
145         Assert.assertEquals(3, headers.length);
146     }
147 
148 }
149