|
@@ -573,9 +573,8 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
{
|
|
|
EmbeddedChannel ch = new EmbeddedChannel(handler);
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("This is not a valid HTTP line"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("This is not a valid HTTP line", buf);
|
|
|
+ appendCrLf(buf);
|
|
|
ch.writeInbound(buf);
|
|
|
ch.flushInbound();
|
|
|
assertThat(dispatchThrowableReference.get().toString(), containsString("NOT A VALID HTTP LINE"));
|
|
@@ -586,9 +585,8 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
{
|
|
|
EmbeddedChannel ch = new EmbeddedChannel(handler);
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("GET /this/is/a/valid/but/too/long/initial/line HTTP/1.1"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("GET /this/is/a/valid/but/too/long/initial/line HTTP/1.1", buf);
|
|
|
+ appendCrLf(buf);
|
|
|
ch.writeInbound(buf);
|
|
|
ch.flushInbound();
|
|
|
assertThat(dispatchThrowableReference.get().toString(), containsString("HTTP line is larger than"));
|
|
@@ -599,11 +597,9 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
{
|
|
|
EmbeddedChannel ch = new EmbeddedChannel(handler);
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("GET /url HTTP/1.1"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- ByteBufUtil.copy(AsciiString.of("Host"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("GET /url HTTP/1.1", buf);
|
|
|
+ appendAsciiLine("Host", buf);
|
|
|
+ appendCrLf(buf);
|
|
|
ch.writeInbound(buf);
|
|
|
ch.flushInbound();
|
|
|
assertThat(dispatchThrowableReference.get().toString(), containsString("No colon found"));
|
|
@@ -614,11 +610,9 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
{
|
|
|
EmbeddedChannel ch = new EmbeddedChannel(handler);
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("GET /url HTTP/1.1"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- ByteBufUtil.copy(AsciiString.of("Host: this.looks.like.a.good.url.but.is.longer.than.permitted"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("GET /url HTTP/1.1", buf);
|
|
|
+ appendAsciiLine("Host: this.looks.like.a.good.url.but.is.longer.than.permitted", buf);
|
|
|
+ appendCrLf(buf);
|
|
|
ch.writeInbound(buf);
|
|
|
ch.flushInbound();
|
|
|
assertThat(dispatchThrowableReference.get().toString(), containsString("HTTP header is larger than"));
|
|
@@ -629,12 +623,11 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
{
|
|
|
EmbeddedChannel ch = new EmbeddedChannel(handler);
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("GET /url HTTP/1.1"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("GET /url HTTP/1.1", buf);
|
|
|
ByteBufUtil.copy(AsciiString.of("Host: invalid header value"), buf);
|
|
|
buf.writeByte(0x01);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendCrLf(buf);
|
|
|
+ appendCrLf(buf);
|
|
|
ch.writeInbound(buf);
|
|
|
ch.flushInbound();
|
|
|
assertThat(dispatchThrowableReference.get().toString(), containsString("Validation failed for header 'Host'"));
|
|
@@ -645,10 +638,8 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
{
|
|
|
EmbeddedChannel ch = new EmbeddedChannel(handler);
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("GET /url HTTP/1.1"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- ByteBufUtil.copy(AsciiString.of("Host: localhost"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("GET /url HTTP/1.1", buf);
|
|
|
+ appendAsciiLine("Host: localhost", buf);
|
|
|
ch.writeInbound(buf);
|
|
|
ch.flushInbound();
|
|
|
safeGet(ch.close());
|
|
@@ -702,33 +693,24 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
// OPTIONS request with fixed length content written in one chunk
|
|
|
{
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("OPTIONS /url/whatever/fixed-length-single-chunk HTTP/1.1"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("OPTIONS /url/whatever/fixed-length-single-chunk HTTP/1.1", buf);
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Host: localhost"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Host: localhost", buf);
|
|
|
}
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Accept: */*"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Accept: */*", buf);
|
|
|
}
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Content-Encoding: gzip"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Content-Encoding: gzip", buf);
|
|
|
}
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(
|
|
|
- AsciiString.of("Content-Type: " + randomFrom("text/plain; charset=utf-8", "application/json; charset=utf-8")),
|
|
|
- buf
|
|
|
- );
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Content-Type: " + randomFrom("text/plain; charset=utf-8", "application/json; charset=utf-8"), buf);
|
|
|
}
|
|
|
String content = randomAlphaOfLengthBetween(4, 1024);
|
|
|
// having a "Content-Length" request header is what makes it "fixed length"
|
|
|
- ByteBufUtil.copy(AsciiString.of("Content-Length: " + content.length()), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Content-Length: " + content.length(), buf);
|
|
|
// end of headers
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendCrLf(buf);
|
|
|
ByteBufUtil.copy(AsciiString.of(content), buf);
|
|
|
// write everything in one single chunk
|
|
|
ch.writeInbound(buf);
|
|
@@ -745,63 +727,44 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
}
|
|
|
{
|
|
|
ByteBuf buf = ch.alloc().buffer();
|
|
|
- ByteBufUtil.copy(AsciiString.of("OPTIONS /url/whatever/chunked-transfer?encoding HTTP/1.1"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("OPTIONS /url/whatever/chunked-transfer?encoding HTTP/1.1", buf);
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Host: localhost"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Host: localhost", buf);
|
|
|
}
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Accept: */*"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Accept: */*", buf);
|
|
|
}
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Content-Encoding: gzip"), buf);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Content-Encoding: gzip", buf);
|
|
|
}
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(
|
|
|
- AsciiString.of("Content-Type: " + randomFrom("text/plain; charset=utf-8", "application/json; charset=utf-8")),
|
|
|
- buf
|
|
|
- );
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("Content-Type: " + randomFrom("text/plain; charset=utf-8", "application/json; charset=utf-8"), buf);
|
|
|
}
|
|
|
// do not write a "Content-Length" header to make the request "variable length"
|
|
|
if (randomBoolean()) {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Transfer-Encoding: " + randomFrom("chunked", "gzip, chunked")), buf);
|
|
|
+ appendAsciiLine("Transfer-Encoding: " + randomFrom("chunked", "gzip, chunked"), buf);
|
|
|
} else {
|
|
|
- ByteBufUtil.copy(AsciiString.of("Transfer-Encoding: chunked"), buf);
|
|
|
+ appendAsciiLine("Transfer-Encoding: chunked", buf);
|
|
|
}
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ // End of headers
|
|
|
+ appendCrLf(buf);
|
|
|
// maybe append some chunks as well
|
|
|
String[] contentParts = randomArray(0, 4, String[]::new, () -> randomAlphaOfLengthBetween(1, 64));
|
|
|
for (String content : contentParts) {
|
|
|
- ByteBufUtil.copy(AsciiString.of(Integer.toHexString(content.length())), buf);
|
|
|
- buf.writeByte(HttpConstants.CR);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
- ByteBufUtil.copy(AsciiString.of(content), buf);
|
|
|
- buf.writeByte(HttpConstants.CR);
|
|
|
- buf.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine(Integer.toHexString(content.length()), buf);
|
|
|
+ appendAsciiLine(content, buf);
|
|
|
}
|
|
|
ch.writeInbound(buf);
|
|
|
ch.flushInbound();
|
|
|
ByteBuf buf2 = ch.alloc().buffer();
|
|
|
contentParts = randomArray(1, 4, String[]::new, () -> randomAlphaOfLengthBetween(1, 64));
|
|
|
for (String content : contentParts) {
|
|
|
- ByteBufUtil.copy(AsciiString.of(Integer.toHexString(content.length())), buf2);
|
|
|
- buf2.writeByte(HttpConstants.CR);
|
|
|
- buf2.writeByte(HttpConstants.LF);
|
|
|
- ByteBufUtil.copy(AsciiString.of(content), buf2);
|
|
|
- buf2.writeByte(HttpConstants.CR);
|
|
|
- buf2.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine(Integer.toHexString(content.length()), buf2);
|
|
|
+ appendAsciiLine(content, buf2);
|
|
|
}
|
|
|
// finish chunked request
|
|
|
- ByteBufUtil.copy(AsciiString.of("0"), buf2);
|
|
|
- buf2.writeByte(HttpConstants.CR);
|
|
|
- buf2.writeByte(HttpConstants.LF);
|
|
|
- buf2.writeByte(HttpConstants.CR);
|
|
|
- buf2.writeByte(HttpConstants.LF);
|
|
|
+ appendAsciiLine("0", buf2);
|
|
|
+ appendCrLf(buf2);
|
|
|
ch.writeInbound(buf2);
|
|
|
ch.flushInbound();
|
|
|
ch.runPendingTasks();
|
|
@@ -820,4 +783,24 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Append a string as ASCII terminated by a CR/LF newline
|
|
|
+ *
|
|
|
+ * @param string The string to append
|
|
|
+ * @param buf The buffer to append to
|
|
|
+ */
|
|
|
+ private static void appendAsciiLine(String string, ByteBuf buf) {
|
|
|
+ ByteBufUtil.copy(AsciiString.of(string), buf);
|
|
|
+ appendCrLf(buf);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Append a CR/LF newline
|
|
|
+ *
|
|
|
+ * @param buf The buffer to append to
|
|
|
+ */
|
|
|
+ private static void appendCrLf(ByteBuf buf) {
|
|
|
+ buf.writeByte(HttpConstants.CR);
|
|
|
+ buf.writeByte(HttpConstants.LF);
|
|
|
+ }
|
|
|
}
|