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.Header;
35  import org.apache.hc.core5.http.Method;
36  import org.apache.hc.core5.http.RequestHeaderFieldsTooLargeException;
37  import org.apache.hc.core5.http.config.Http1Config;
38  import org.apache.hc.core5.http.io.SessionInputBuffer;
39  import org.junit.jupiter.api.Assertions;
40  import org.junit.jupiter.api.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          Assertions.assertEquals(Method.GET.name(), httprequest.getMethod());
62          Assertions.assertEquals("/", httprequest.getPath());
63          final Header[] headers = httprequest.getHeaders();
64          Assertions.assertEquals(3, headers.length);
65      }
66  
67      @Test
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          final ClassicHttpRequest request = parser.parse(inBuffer, inputStream);
74          Assertions.assertNull(request);
75      }
76  
77      @Test
78      public void testBasicMessageParsingLeadingEmptyLines() throws Exception {
79          final String s =
80                  "\r\n" +
81                  "\r\n" +
82                  "GET / HTTP/1.1\r\n" +
83                  "Host: localhost\r\n" +
84                  "\r\n";
85          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
86          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
87  
88          final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(
89                  Http1Config.custom().setMaxEmptyLineCount(3).build());
90          final ClassicHttpRequest httprequest = parser.parse(inBuffer, inputStream);
91  
92          Assertions.assertEquals(Method.GET.name(), httprequest.getMethod());
93          Assertions.assertEquals("/", httprequest.getPath());
94          final Header[] headers = httprequest.getHeaders();
95          Assertions.assertEquals(1, headers.length);
96      }
97  
98      @Test
99      public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception {
100         final String s =
101                 "\r\n" +
102                 "\r\n" +
103                 "\r\n" +
104                 "GET / HTTP/1.1\r\n" +
105                 "Host: localhost\r\n" +
106                 "\r\n";
107         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
108         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
109 
110         final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(
111                 Http1Config.custom().setMaxEmptyLineCount(3).build());
112         Assertions.assertThrows(RequestHeaderFieldsTooLargeException.class, () ->
113                 parser.parse(inBuffer, inputStream));
114     }
115 
116     @Test
117     public void testMessageParsingTimeout() throws Exception {
118         final String s =
119             "GET \000/ HTTP/1.1\r\000\n" +
120             "Host: loca\000lhost\r\n" +
121             "User-Agent: whatever\r\n" +
122             "Coo\000kie: c1=stuff\r\n" +
123             "\000\r\n";
124         final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
125         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
126 
127         final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
128 
129         int timeoutCount = 0;
130 
131         ClassicHttpRequest httprequest = null;
132         for (int i = 0; i < 10; i++) {
133             try {
134                 httprequest = parser.parse(inBuffer, inputStream);
135                 break;
136             } catch (final InterruptedIOException ex) {
137                 timeoutCount++;
138             }
139 
140         }
141         Assertions.assertNotNull(httprequest);
142         Assertions.assertEquals(5, timeoutCount);
143 
144         Assertions.assertEquals(Method.GET.name(), httprequest.getMethod());
145         Assertions.assertEquals("/", httprequest.getPath());
146         final Header[] headers = httprequest.getHeaders();
147         Assertions.assertEquals(3, headers.length);
148     }
149 
150 }
151