55763
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package jdk.incubator.http;
|
|
25 |
|
|
26 |
import java.io.ByteArrayInputStream;
|
|
27 |
import java.net.ProtocolException;
|
|
28 |
import java.nio.ByteBuffer;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.Arrays;
|
|
31 |
import java.util.HashMap;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Map;
|
|
34 |
import java.util.stream.IntStream;
|
|
35 |
import sun.net.www.MessageHeader;
|
|
36 |
import org.testng.annotations.Test;
|
|
37 |
import org.testng.annotations.DataProvider;
|
|
38 |
import static java.lang.System.out;
|
|
39 |
import static java.lang.String.format;
|
|
40 |
import static java.nio.charset.StandardCharsets.US_ASCII;
|
|
41 |
import static java.util.stream.Collectors.toList;
|
|
42 |
import static org.testng.Assert.*;
|
|
43 |
|
|
44 |
// Mostly verifies the "new" Http1HeaderParser returns the same results as the
|
|
45 |
// tried and tested sun.net.www.MessageHeader.
|
|
46 |
|
|
47 |
public class Http1HeaderParserTest {
|
|
48 |
|
|
49 |
@DataProvider(name = "responses")
|
|
50 |
public Object[][] responses() {
|
|
51 |
List<String> responses = new ArrayList<>();
|
|
52 |
|
|
53 |
String[] basic =
|
|
54 |
{ "HTTP/1.1 200 OK\r\n\r\n",
|
|
55 |
|
|
56 |
"HTTP/1.1 200 OK\r\n" +
|
|
57 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
|
|
58 |
"Server: Apache/1.3.14 (Unix)\r\n" +
|
|
59 |
"Connection: close\r\n" +
|
|
60 |
"Content-Type: text/html; charset=iso-8859-1\r\n" +
|
|
61 |
"Content-Length: 10\r\n\r\n" +
|
|
62 |
"123456789",
|
|
63 |
|
|
64 |
"HTTP/1.1 200 OK\r\n" +
|
|
65 |
"Content-Length: 9\r\n" +
|
|
66 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
|
|
67 |
"XXXXX",
|
|
68 |
|
|
69 |
"HTTP/1.1 200 OK\r\n" +
|
|
70 |
"Content-Length: 9\r\n" +
|
|
71 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" + // more than one SP after ':'
|
|
72 |
"XXXXX",
|
|
73 |
|
|
74 |
"HTTP/1.1 200 OK\r\n" +
|
|
75 |
"Content-Length:\t10\r\n" +
|
|
76 |
"Content-Type:\ttext/html; charset=UTF-8\r\n\r\n" + // HT separator
|
|
77 |
"XXXXX",
|
|
78 |
|
|
79 |
"HTTP/1.1 200 OK\r\n" +
|
|
80 |
"Content-Length:\t\t10\r\n" +
|
|
81 |
"Content-Type:\t\ttext/html; charset=UTF-8\r\n\r\n" + // more than one HT after ':'
|
|
82 |
"XXXXX",
|
|
83 |
|
|
84 |
"HTTP/1.1 407 Proxy Authorization Required\r\n" +
|
|
85 |
"Proxy-Authenticate: Basic realm=\"a fake realm\"\r\n\r\n",
|
|
86 |
|
|
87 |
"HTTP/1.1 401 Unauthorized\r\n" +
|
|
88 |
"WWW-Authenticate: Digest realm=\"wally land\" domain=/ " +
|
|
89 |
"nonce=\"2B7F3A2B\" qop=\"auth\"\r\n\r\n",
|
|
90 |
|
|
91 |
"HTTP/1.1 200 OK\r\n" +
|
|
92 |
"X-Foo:\r\n\r\n", // no value
|
|
93 |
|
|
94 |
"HTTP/1.1 200 OK\r\n" +
|
|
95 |
"X-Foo:\r\n\r\n" + // no value, with response body
|
|
96 |
"Some Response Body",
|
|
97 |
|
|
98 |
"HTTP/1.1 200 OK\r\n" +
|
|
99 |
"X-Foo:\r\n" + // no value, followed by another header
|
|
100 |
"Content-Length: 10\r\n\r\n" +
|
|
101 |
"Some Response Body",
|
|
102 |
|
|
103 |
"HTTP/1.1 200 OK\r\n" +
|
|
104 |
"X-Foo:\r\n" + // no value, followed by another header, with response body
|
|
105 |
"Content-Length: 10\r\n\r\n",
|
|
106 |
|
|
107 |
"HTTP/1.1 200 OK\r\n" +
|
|
108 |
"X-Foo: chegar\r\n" +
|
|
109 |
"X-Foo: dfuchs\r\n" + // same header appears multiple times
|
|
110 |
"Content-Length: 0\r\n" +
|
|
111 |
"X-Foo: michaelm\r\n" +
|
|
112 |
"X-Foo: prappo\r\n\r\n",
|
|
113 |
|
|
114 |
"HTTP/1.1 200 OK\r\n" +
|
|
115 |
"X-Foo:\r\n" + // no value, same header appears multiple times
|
|
116 |
"X-Foo: dfuchs\r\n" +
|
|
117 |
"Content-Length: 0\r\n" +
|
|
118 |
"X-Foo: michaelm\r\n" +
|
|
119 |
"X-Foo: prappo\r\n\r\n",
|
|
120 |
};
|
|
121 |
Arrays.stream(basic).forEach(responses::add);
|
|
122 |
|
|
123 |
String[] foldingTemplate =
|
|
124 |
{ "HTTP/1.1 200 OK\r\n" +
|
|
125 |
"Content-Length: 9\r\n" +
|
|
126 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r'
|
|
127 |
" charset=UTF-8\r\n" + // one preceding SP
|
|
128 |
"Connection: close\r\n\r\n" +
|
|
129 |
"XXYYZZAABBCCDDEE",
|
|
130 |
|
|
131 |
"HTTP/1.1 200 OK\r\n" +
|
|
132 |
"Content-Length: 19\r\n" +
|
|
133 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
|
|
134 |
" charset=UTF-8\r\n" + // more than one preceding SP
|
|
135 |
"Connection: keep-alive\r\n\r\n" +
|
|
136 |
"XXYYZZAABBCCDDEEFFGG",
|
|
137 |
|
|
138 |
"HTTP/1.1 200 OK\r\n" +
|
|
139 |
"Content-Length: 999\r\n" +
|
|
140 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
|
|
141 |
"\tcharset=UTF-8\r\n" + // one preceding HT
|
|
142 |
"Connection: close\r\n\r\n" +
|
|
143 |
"XXYYZZAABBCCDDEE",
|
|
144 |
|
|
145 |
"HTTP/1.1 200 OK\r\n" +
|
|
146 |
"Content-Length: 54\r\n" +
|
|
147 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
|
|
148 |
"\t\t\tcharset=UTF-8\r\n" + // more than one preceding HT
|
|
149 |
"Connection: keep-alive\r\n\r\n" +
|
|
150 |
"XXYYZZAABBCCDDEEFFGG",
|
|
151 |
|
|
152 |
"HTTP/1.1 200 OK\r\n" +
|
|
153 |
"Content-Length: -1\r\n" +
|
|
154 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
|
|
155 |
"\t \t \tcharset=UTF-8\r\n" + // mix of preceding HT and SP
|
|
156 |
"Connection: keep-alive\r\n\r\n" +
|
|
157 |
"XXYYZZAABBCCDDEEFFGGHH",
|
|
158 |
|
|
159 |
"HTTP/1.1 200 OK\r\n" +
|
|
160 |
"Content-Length: 65\r\n" +
|
|
161 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
|
|
162 |
" \t \t charset=UTF-8\r\n" + // mix of preceding SP and HT
|
|
163 |
"Connection: keep-alive\r\n\r\n" +
|
|
164 |
"XXYYZZAABBCCDDEEFFGGHHII",
|
|
165 |
};
|
|
166 |
for (String newLineChar : new String[] { "\n", "\r" }) {
|
|
167 |
for (String template : foldingTemplate)
|
|
168 |
responses.add(template.replace("$NEWLINE", newLineChar));
|
|
169 |
}
|
|
170 |
|
|
171 |
String[] bad = // much of this is to retain parity with legacy MessageHeaders
|
|
172 |
{ "HTTP/1.1 200 OK\r\n" +
|
|
173 |
"Connection:\r\n\r\n", // empty value, no body
|
|
174 |
|
|
175 |
"HTTP/1.1 200 OK\r\n" +
|
|
176 |
"Connection:\r\n\r\n" + // empty value, with body
|
|
177 |
"XXXXX",
|
|
178 |
|
|
179 |
"HTTP/1.1 200 OK\r\n" +
|
|
180 |
": no header\r\n\r\n", // no/empty header-name, no body, no following header
|
|
181 |
|
|
182 |
"HTTP/1.1 200 OK\r\n" +
|
|
183 |
": no; header\r\n" + // no/empty header-name, no body, following header
|
|
184 |
"Content-Length: 65\r\n\r\n",
|
|
185 |
|
|
186 |
"HTTP/1.1 200 OK\r\n" +
|
|
187 |
": no header\r\n" + // no/empty header-name
|
|
188 |
"Content-Length: 65\r\n\r\n" +
|
|
189 |
"XXXXX",
|
|
190 |
|
|
191 |
"HTTP/1.1 200 OK\r\n" +
|
|
192 |
": no header\r\n\r\n" + // no/empty header-name, followed by header
|
|
193 |
"XXXXX",
|
|
194 |
|
|
195 |
"HTTP/1.1 200 OK\r\n" +
|
|
196 |
"Conte\r" +
|
|
197 |
" nt-Length: 9\r\n" + // fold/bad header name ???
|
|
198 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
|
|
199 |
"XXXXX",
|
|
200 |
|
|
201 |
"HTTP/1.1 200 OK\r\n" +
|
|
202 |
"Conte\r" +
|
|
203 |
"nt-Length: 9\r\n" + // fold/bad header name ??? without preceding space
|
|
204 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
|
|
205 |
"XXXXXYYZZ",
|
|
206 |
|
|
207 |
"HTTP/1.0 404 Not Found\r\n" +
|
|
208 |
"header-without-colon\r\n\r\n",
|
|
209 |
|
|
210 |
"HTTP/1.0 404 Not Found\r\n" +
|
|
211 |
"header-without-colon\r\n\r\n" +
|
|
212 |
"SOMEBODY",
|
|
213 |
|
|
214 |
};
|
|
215 |
Arrays.stream(bad).forEach(responses::add);
|
|
216 |
|
|
217 |
return responses.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
|
|
218 |
}
|
|
219 |
|
|
220 |
@Test(dataProvider = "responses")
|
|
221 |
public void verifyHeaders(String respString) throws Exception {
|
|
222 |
byte[] bytes = respString.getBytes(US_ASCII);
|
|
223 |
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
224 |
MessageHeader m = new MessageHeader(bais);
|
|
225 |
Map<String,List<String>> messageHeaderMap = m.getHeaders();
|
|
226 |
int available = bais.available();
|
|
227 |
|
|
228 |
Http1HeaderParser decoder = new Http1HeaderParser();
|
|
229 |
ByteBuffer b = ByteBuffer.wrap(bytes);
|
|
230 |
decoder.parse(b);
|
|
231 |
Map<String,List<String>> decoderMap1 = decoder.headers().map();
|
|
232 |
assertEquals(available, b.remaining(),
|
|
233 |
"stream available not equal to remaining");
|
|
234 |
|
|
235 |
// assert status-line
|
|
236 |
String statusLine1 = messageHeaderMap.get(null).get(0);
|
|
237 |
String statusLine2 = decoder.statusLine();
|
|
238 |
if (statusLine1.startsWith("HTTP")) {// skip the case where MH's messes up the status-line
|
|
239 |
assertEquals(statusLine1, statusLine2, "Status-line not equal");
|
|
240 |
} else {
|
|
241 |
assertTrue(statusLine2.startsWith("HTTP/1."), "Status-line not HTTP/1.");
|
|
242 |
}
|
|
243 |
|
|
244 |
// remove the null'th entry with is the status-line
|
|
245 |
Map<String,List<String>> map = new HashMap<>();
|
|
246 |
for (Map.Entry<String,List<String>> e : messageHeaderMap.entrySet()) {
|
|
247 |
if (e.getKey() != null) {
|
|
248 |
map.put(e.getKey(), e.getValue());
|
|
249 |
}
|
|
250 |
}
|
|
251 |
messageHeaderMap = map;
|
|
252 |
|
|
253 |
assertHeadersEqual(messageHeaderMap, decoderMap1,
|
|
254 |
"messageHeaderMap not equal to decoderMap1");
|
|
255 |
|
|
256 |
// byte at a time
|
|
257 |
decoder = new Http1HeaderParser();
|
|
258 |
List<ByteBuffer> buffers = IntStream.range(0, bytes.length)
|
|
259 |
.mapToObj(i -> ByteBuffer.wrap(bytes, i, 1))
|
|
260 |
.collect(toList());
|
|
261 |
while (decoder.parse(buffers.remove(0)) != true);
|
|
262 |
Map<String,List<String>> decoderMap2 = decoder.headers().map();
|
|
263 |
assertEquals(available, buffers.size(),
|
|
264 |
"stream available not equals to remaining buffers");
|
|
265 |
assertEquals(decoderMap1, decoderMap2, "decoder maps not equal");
|
|
266 |
}
|
|
267 |
|
|
268 |
@DataProvider(name = "errors")
|
|
269 |
public Object[][] errors() {
|
|
270 |
List<String> responses = new ArrayList<>();
|
|
271 |
|
|
272 |
// These responses are parsed, somewhat, by MessageHeaders but give
|
|
273 |
// nonsensible results. They, correctly, fail with the Http1HeaderParser.
|
|
274 |
String[] bad =
|
|
275 |
{// "HTTP/1.1 402 Payment Required\r\n" +
|
|
276 |
// "Content-Length: 65\r\n\r", // missing trailing LF //TODO: incomplete
|
|
277 |
|
|
278 |
"HTTP/1.1 402 Payment Required\r\n" +
|
|
279 |
"Content-Length: 65\r\n\rT\r\n\r\nGGGGGG",
|
|
280 |
|
|
281 |
"HTTP/1.1 200OK\r\n\rT",
|
|
282 |
|
|
283 |
"HTTP/1.1 200OK\rT",
|
|
284 |
};
|
|
285 |
Arrays.stream(bad).forEach(responses::add);
|
|
286 |
|
|
287 |
return responses.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
|
|
288 |
}
|
|
289 |
|
|
290 |
@Test(dataProvider = "errors", expectedExceptions = ProtocolException.class)
|
|
291 |
public void errors(String respString) throws ProtocolException {
|
|
292 |
byte[] bytes = respString.getBytes(US_ASCII);
|
|
293 |
Http1HeaderParser decoder = new Http1HeaderParser();
|
|
294 |
ByteBuffer b = ByteBuffer.wrap(bytes);
|
|
295 |
decoder.parse(b);
|
|
296 |
}
|
|
297 |
|
|
298 |
void assertHeadersEqual(Map<String,List<String>> expected,
|
|
299 |
Map<String,List<String>> actual,
|
|
300 |
String msg) {
|
|
301 |
|
|
302 |
if (expected.equals(actual))
|
|
303 |
return;
|
|
304 |
|
|
305 |
assertEquals(expected.size(), actual.size(),
|
|
306 |
format("%s. Expected size %d, actual size %s. expected= %s, actual=%s.",
|
|
307 |
msg, expected.size(), actual.size(), expected, actual));
|
|
308 |
|
|
309 |
for (Map.Entry<String,List<String>> e : expected.entrySet()) {
|
|
310 |
String key = e.getKey();
|
|
311 |
List<String> values = e.getValue();
|
|
312 |
|
|
313 |
boolean found = false;
|
|
314 |
for (Map.Entry<String,List<String>> other: actual.entrySet()) {
|
|
315 |
if (key.equalsIgnoreCase(other.getKey())) {
|
|
316 |
found = true;
|
|
317 |
List<String> otherValues = other.getValue();
|
|
318 |
assertEquals(values.size(), otherValues.size(),
|
|
319 |
format("%s. Expected list size %d, actual size %s",
|
|
320 |
msg, values.size(), otherValues.size()));
|
|
321 |
if (!values.containsAll(otherValues) && otherValues.containsAll(values))
|
|
322 |
assertTrue(false, format("Lists are unequal [%s] [%s]", values, otherValues));
|
|
323 |
break;
|
|
324 |
}
|
|
325 |
}
|
|
326 |
assertTrue(found, format("header name, %s, not found in %s", key, actual));
|
|
327 |
}
|
|
328 |
}
|
|
329 |
|
|
330 |
// ---
|
|
331 |
|
|
332 |
/* Main entry point for standalone testing of the main functional test. */
|
|
333 |
public static void main(String... args) throws Exception {
|
|
334 |
Http1HeaderParserTest test = new Http1HeaderParserTest();
|
|
335 |
int count = 0;
|
|
336 |
for (Object[] objs : test.responses()) {
|
|
337 |
out.println("Testing " + count++ + ", " + objs[0]);
|
|
338 |
test.verifyHeaders((String) objs[0]);
|
|
339 |
}
|
|
340 |
for (Object[] objs : test.errors()) {
|
|
341 |
out.println("Testing " + count++ + ", " + objs[0]);
|
|
342 |
try {
|
|
343 |
test.errors((String) objs[0]);
|
|
344 |
throw new RuntimeException("Expected ProtocolException for " + objs[0]);
|
|
345 |
} catch (ProtocolException expected) { /* Ok */ }
|
|
346 |
}
|
|
347 |
}
|
|
348 |
}
|