1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.Method;
37 import org.apache.hc.core5.http.RequestHeaderFieldsTooLargeException;
38 import org.apache.hc.core5.http.config.Http1Config;
39 import org.apache.hc.core5.http.io.SessionInputBuffer;
40 import org.junit.Assert;
41 import org.junit.Test;
42
43
44
45
46 public class TestRequestParser {
47
48 @Test
49 public void testBasicMessageParsing() throws Exception {
50 final String s =
51 "GET / HTTP/1.1\r\n" +
52 "Host: localhost\r\n" +
53 "User-Agent: whatever\r\n" +
54 "Cookie: c1=stuff\r\n" +
55 "\r\n";
56 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
57 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
58
59 final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
60 final ClassicHttpRequest httprequest = parser.parse(inBuffer, inputStream);
61
62 Assert.assertEquals(Method.GET.name(), httprequest.getMethod());
63 Assert.assertEquals("/", httprequest.getPath());
64 final Header[] headers = httprequest.getHeaders();
65 Assert.assertEquals(3, headers.length);
66 }
67
68 @Test(expected = ConnectionClosedException.class)
69 public void testConnectionClosedException() throws Exception {
70 final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
71 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
72
73 final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
74 parser.parse(inBuffer, inputStream);
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 Assert.assertEquals(Method.GET.name(), httprequest.getMethod());
93 Assert.assertEquals("/", httprequest.getPath());
94 final Header[] headers = httprequest.getHeaders();
95 Assert.assertEquals(1, headers.length);
96 }
97
98 @Test(expected = RequestHeaderFieldsTooLargeException.class)
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 parser.parse(inBuffer, inputStream);
113 }
114
115 @Test
116 public void testMessageParsingTimeout() throws Exception {
117 final String s =
118 "GET \000/ HTTP/1.1\r\000\n" +
119 "Host: loca\000lhost\r\n" +
120 "User-Agent: whatever\r\n" +
121 "Coo\000kie: c1=stuff\r\n" +
122 "\000\r\n";
123 final TimeoutByteArrayInputStreamrrayInputStream.html#TimeoutByteArrayInputStream">TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
124 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
125
126 final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
127
128 int timeoutCount = 0;
129
130 ClassicHttpRequest httprequest = null;
131 for (int i = 0; i < 10; i++) {
132 try {
133 httprequest = parser.parse(inBuffer, inputStream);
134 break;
135 } catch (final InterruptedIOException ex) {
136 timeoutCount++;
137 }
138
139 }
140 Assert.assertNotNull(httprequest);
141 Assert.assertEquals(5, timeoutCount);
142
143 Assert.assertEquals(Method.GET.name(), httprequest.getMethod());
144 Assert.assertEquals("/", httprequest.getPath());
145 final Header[] headers = httprequest.getHeaders();
146 Assert.assertEquals(3, headers.length);
147 }
148
149 }
150