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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8061729
* @library /test/lib
* @summary Sanity check that HttpHeaderParser works same as MessageHeader
* @modules java.base/sun.net.www java.base/sun.net.www.protocol.http:open
* @run testng/othervm HttpHeaderParserTest
*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.US_ASCII;
import jdk.test.lib.net.HttpHeaderParser;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import sun.net.www.MessageHeader;
public class HttpHeaderParserTest {
@DataProvider(name = "responses")
public Object[][] responses() {
List<String> responses = new ArrayList<>();
String[] basic =
{ "HTTP/1.1 200 OK\r\n\r\n",
"HTTP/1.1 200 OK\r\n" +
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
"Server: Apache/1.3.14 (Unix)\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html; charset=iso-8859-1\r\n" +
"Content-Length: 10\r\n\r\n" +
"123456789",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 9\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"X-Header: U\u00ffU\r\n" + // value with U+00FF - Extended Latin-1
"Content-Length: 9\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 9\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" + // more than one SP after ':'
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"Content-Length:\t10\r\n" +
"Content-Type:\ttext/html; charset=UTF-8\r\n\r\n" + // HT separator
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"Content-Length:\t\t10\r\n" +
"Content-Type:\t\ttext/html; charset=UTF-8\r\n\r\n" + // more than one HT after ':'
"XXXXX",
"HTTP/1.1 407 Proxy Authorization Required\r\n" +
"Proxy-Authenticate: Basic realm=\"a fake realm\"\r\n\r\n",
"HTTP/1.1 401 Unauthorized\r\n" +
"WWW-Authenticate: Digest realm=\"wally land\" domain=/ " +
"nonce=\"2B7F3A2B\" qop=\"auth\"\r\n\r\n",
"HTTP/1.1 200 OK\r\n" +
"X-Foo:\r\n\r\n", // no value
"HTTP/1.1 200 OK\r\n" +
"X-Foo:\r\n\r\n" + // no value, with response body
"Some Response Body",
"HTTP/1.1 200 OK\r\n" +
"X-Foo:\r\n" + // no value, followed by another header
"Content-Length: 10\r\n\r\n" +
"Some Response Body",
"HTTP/1.1 200 OK\r\n" +
"X-Foo:\r\n" + // no value, followed by another header, with response body
"Content-Length: 10\r\n\r\n",
"HTTP/1.1 200 OK\r\n" +
"X-Foo: chegar\r\n" +
"X-Foo: dfuchs\r\n" + // same header appears multiple times
"Content-Length: 0\r\n" +
"X-Foo: michaelm\r\n" +
"X-Foo: prappo\r\n\r\n",
"HTTP/1.1 200 OK\r\n" +
"X-Foo:\r\n" + // no value, same header appears multiple times
"X-Foo: dfuchs\r\n" +
"Content-Length: 0\r\n" +
"X-Foo: michaelm\r\n" +
"X-Foo: prappo\r\n\r\n",
"HTTP/1.1 200 OK\r\n" +
"Accept-Ranges: bytes\r\n" +
"Cache-control: max-age=0, no-cache=\"set-cookie\"\r\n" +
"Content-Length: 132868\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n" +
"Date: Sun, 05 Nov 2017 22:24:03 GMT\r\n" +
"Server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.1e-fips Communique/4.2.2\r\n" +
"Set-Cookie: AWSELB=AF7927F5100F4202119876ED2436B5005EE;PATH=/;MAX-AGE=900\r\n" +
"Vary: Host,Accept-Encoding,User-Agent\r\n" +
"X-Mod-Pagespeed: 1.12.34.2-0\r\n" +
"Connection: keep-alive\r\n\r\n"
};
Arrays.stream(basic).forEach(responses::add);
// add some tests where some of the CRLF are replaced
// by a single LF
Arrays.stream(basic)
.map(HttpHeaderParserTest::mixedCRLF)
.forEach(responses::add);
String[] foldingTemplate =
{ "HTTP/1.1 200 OK\r\n" +
"Content-Length: 9\r\n" +
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r'
" charset=UTF-8\r\n" + // one preceding SP
"Connection: close\r\n\r\n" +
"XXYYZZAABBCCDDEE",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 19\r\n" +
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
" charset=UTF-8\r\n" + // more than one preceding SP
"Connection: keep-alive\r\n\r\n" +
"XXYYZZAABBCCDDEEFFGG",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 999\r\n" +
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
"\tcharset=UTF-8\r\n" + // one preceding HT
"Connection: close\r\n\r\n" +
"XXYYZZAABBCCDDEE",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 54\r\n" +
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
"\t\t\tcharset=UTF-8\r\n" + // more than one preceding HT
"Connection: keep-alive\r\n\r\n" +
"XXYYZZAABBCCDDEEFFGG",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: -1\r\n" +
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
"\t \t \tcharset=UTF-8\r\n" + // mix of preceding HT and SP
"Connection: keep-alive\r\n\r\n" +
"XXYYZZAABBCCDDEEFFGGHH",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 65\r\n" +
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r
" \t \t charset=UTF-8\r\n" + // mix of preceding SP and HT
"Connection: keep-alive\r\n\r\n" +
"XXYYZZAABBCCDDEEFFGGHHII",
"HTTP/1.1 401 Unauthorized\r\n" +
"WWW-Authenticate: Digest realm=\"wally land\","
+"$NEWLINE domain=/,"
+"$NEWLINE nonce=\"2B7F3A2B\","
+"$NEWLINE\tqop=\"auth\"\r\n\r\n",
};
for (String newLineChar : new String[] { "\n", "\r", "\r\n" }) {
for (String template : foldingTemplate)
responses.add(template.replace("$NEWLINE", newLineChar));
}
// add some tests where some of the CRLF are replaced
// by a single LF
for (String newLineChar : new String[] { "\n", "\r", "\r\n" }) {
for (String template : foldingTemplate)
responses.add(mixedCRLF(template).replace("$NEWLINE", newLineChar));
}
String[] bad = // much of this is to retain parity with legacy MessageHeaders
{ "HTTP/1.1 200 OK\r\n" +
"Connection:\r\n\r\n", // empty value, no body
"HTTP/1.1 200 OK\r\n" +
"Connection:\r\n\r\n" + // empty value, with body
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
": no header\r\n\r\n", // no/empty header-name, no body, no following header
"HTTP/1.1 200 OK\r\n" +
": no; header\r\n" + // no/empty header-name, no body, following header
"Content-Length: 65\r\n\r\n",
"HTTP/1.1 200 OK\r\n" +
": no header\r\n" + // no/empty header-name
"Content-Length: 65\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"X-foo: bar\r\n" +
" : no header\r\n" + // fold, not a blank header-name
"Content-Length: 65\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"X-foo: bar\r\n" +
" \t : no header\r\n" + // fold, not a blank header-name
"Content-Length: 65\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
": no header\r\n\r\n" + // no/empty header-name, followed by header
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"Conte\r" +
"nt-Length: 9\r\n" + // fold/bad header name ??? without preceding space
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXXYYZZ",
"HTTP/1.0 404 Not Found\r\n" +
"header-without-colon\r\n\r\n",
"HTTP/1.0 404 Not Found\r\n" +
"header-without-colon\r\n\r\n" +
"SOMEBODY",
};
Arrays.stream(bad).forEach(responses::add);
return responses.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
}
static final AtomicInteger index = new AtomicInteger();
static final AtomicInteger limit = new AtomicInteger(1);
static final AtomicBoolean useCRLF = new AtomicBoolean();
// A small method to replace part of the CRLF present in a string
// with simple LF. The method uses a deterministic algorithm based
// on current values of static index/limit/useCRLF counters.
// These counters are used to produce a stream of substitutes that
// looks like this:
// LF CRLF LF LF CRLF CRLF LF LF LF CRLF CRLF CRLF (then repeat from start)
static final String mixedCRLF(String headers) {
int next;
int start = 0;
int last = headers.lastIndexOf("\r\n");
String prev = "";
StringBuilder res = new StringBuilder();
while ((next = headers.indexOf("\r\n", start)) > 0) {
res.append(headers.substring(start, next));
if ("\n".equals(prev) && next == last) {
// for some reason the legacy MessageHeader parser will
// not consume the final LF if the headers are terminated
// by <LF><CRLF> instead of <CRLF><CRLF>. It consume
// <LF><CR> but leaves the last <LF> in the stream.
// Here we just make sure to avoid using <LF><CRLF>
// as that would cause the legacy parser to consume
// 1 byte less than the Http1HeadersParser - which
// does consume the last <LF>, as it should.
// if this is the last CRLF and the previous one
// was replaced by LF then use LF.
res.append(prev);
} else {
prev = useCRLF.get() ? "\r\n" : "\n";
res.append(prev);
}
// skip CRLF
start = next + 2;
// The idea is to substitute some of the CRLF with LF.
// Rather than doing this randomly, always use the following
// sequence:
// LF CRLF LF LF CRLF CRLF LF LF LF CRLF CRLF CRLF
index.incrementAndGet();
if (index.get() == limit.get()) {
index.set(0);
if (useCRLF.get()) limit.incrementAndGet();
if (limit.get() > 3) limit.set(1);
useCRLF.set(!useCRLF.get());
}
}
res.append(headers.substring(start));
return res.toString();
}
@Test(dataProvider = "responses")
public void verifyHeaders(String respString) throws Exception {
System.out.println("\ntesting:\n\t" + respString
.replace("\r\n", "<CRLF>")
.replace("\r", "<CR>")
.replace("\n","<LF>")
.replace("LF>", "LF>\n\t"));
byte[] bytes = respString.getBytes(ISO_8859_1);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
MessageHeader m = new MessageHeader(bais);
Map<String,List<String>> messageHeaderMap = m.getHeaders();
int availableBytes = bais.available();
HttpHeaderParser decoder = new HttpHeaderParser();
ByteArrayInputStream headerStream = new ByteArrayInputStream(bytes);
int initialBytes = headerStream.available();
decoder.parse(headerStream);
System.out.printf("HttpHeaderParser parsed %d bytes out of %d%n", initialBytes - headerStream.available(), bytes.length);
Map<String,List<String>> decoderMap1 = decoder.getHeaderMap();
// assert status-line
String statusLine1 = messageHeaderMap.get(null).get(0);
String statusLine2 = decoder.getRequestDetails();
if (statusLine1.startsWith("HTTP")) {// skip the case where MH's messes up the status-line
assertEquals(statusLine2, statusLine1, "Status-line not equal");
} else {
assertTrue(statusLine2.startsWith("HTTP/1."), "Status-line not HTTP/1.");
}
// remove the null'th entry with is the status-line
Map<String,List<String>> map = new HashMap<>();
for (Map.Entry<String,List<String>> e : messageHeaderMap.entrySet()) {
if (e.getKey() != null) {
map.put(e.getKey(), e.getValue());
}
}
messageHeaderMap = map;
assertHeadersEqual(messageHeaderMap, decoderMap1,
"messageHeaderMap not equal to decoderMap1");
assertEquals(availableBytes, headerStream.available(),
String.format("stream available (%d) not equal to remaining (%d)",
availableBytes, headerStream.available()));
}
@DataProvider(name = "errors")
public Object[][] errors() {
List<String> responses = new ArrayList<>();
// These responses are parsed, somewhat, by MessageHeaders but give
// nonsensible results. They, correctly, fail with the Http1HeaderParser.
String[] bad =
{// "HTTP/1.1 402 Payment Required\r\n" +
// "Content-Length: 65\r\n\r", // missing trailing LF //TODO: incomplete
"HTTP/1.1 402 Payment Required\r\n" +
"Content-Length: 65\r\n\rT\r\n\r\nGGGGGG",
"HTTP/1.1 200OK\r\n\rT",
"HTTP/1.1 200OK\rT",
"HTTP/1.0 FOO\r\n",
"HTTP/1.1 BAR\r\n",
"HTTP/1.1 +99\r\n",
"HTTP/1.1 -22\r\n",
"HTTP/1.1 -20 \r\n",
"HTTP/1.1 200 OK\r\n" +
"X-fo\u00ffo: foo\r\n" + // invalid char in name
"Content-Length: 5\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"HTTP/1.1 200 OK\r\n" +
"X-foo : bar\r\n" + // trim space after name
"Content-Length: 5\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
" X-foo: bar\r\n" + // trim space before name
"Content-Length: 5\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"X foo: bar\r\n" + // invalid space in name
"Content-Length: 5\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"Content-Length: 5\r\n" +
"Content Type: text/html; charset=UTF-8\r\n\r\n" + // invalid space in name
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
"Conte\r" +
" nt-Length: 9\r\n" + // fold results in space in header name
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
" : no header\r\n" + // all blank header-name (not fold)
"Content-Length: 65\r\n\r\n" +
"XXXXX",
"HTTP/1.1 200 OK\r\n" +
" \t : no header\r\n" + // all blank header-name (not fold)
"Content-Length: 65\r\n\r\n" +
"XXXXX",
};
Arrays.stream(bad).forEach(responses::add);
return responses.stream().map(p -> new Object[] { p }).toArray(Object[][]::new);
}
@Test(dataProvider = "errors", expectedExceptions = IOException.class)
public void errors(String respString) throws IOException {
byte[] bytes = respString.getBytes(US_ASCII);
HttpHeaderParser decoder = new HttpHeaderParser();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
decoder.parse(bais);
}
void assertHeadersEqual(Map<String,List<String>> expected,
Map<String,List<String>> actual,
String msg) {
if (expected.equals(actual))
return;
assertEquals(expected.size(), actual.size(),
format("%s. Expected size %d, actual size %s. %nexpected= %s,%n actual=%s.",
msg, expected.size(), actual.size(), mapToString(expected), mapToString(actual)));
for (Map.Entry<String,List<String>> e : expected.entrySet()) {
String key = e.getKey();
List<String> values = e.getValue();
boolean found = false;
for (Map.Entry<String,List<String>> other: actual.entrySet()) {
if (key.equalsIgnoreCase(other.getKey())) {
found = true;
List<String> otherValues = other.getValue();
assertEquals(values.size(), otherValues.size(),
format("%s. Expected list size %d, actual size %s",
msg, values.size(), otherValues.size()));
if (!(values.containsAll(otherValues) && otherValues.containsAll(values)))
assertTrue(false, format("Lists are unequal [%s] [%s]", values, otherValues));
break;
}
}
assertTrue(found, format("header name, %s, not found in %s", key, actual));
}
}
static String mapToString(Map<String,List<String>> map) {
StringBuilder sb = new StringBuilder();
List<String> sortedKeys = new ArrayList(map.keySet());
Collections.sort(sortedKeys);
for (String key : sortedKeys) {
List<String> values = map.get(key);
sb.append("\n\t" + key + " | " + values);
}
return sb.toString();
}
}
|