From 9fc5eb2af8b1bc93109ff66936fdaa6d2ae851bb Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 11 Apr 2019 07:17:00 -0500 Subject: [PATCH] Issue #3545 - NPE in HttpOutput with print("") Signed-off-by: Joakim Erdfelt --- .../org/eclipse/jetty/server/HttpOutput.java | 3 +- .../eclipse/jetty/server/ResponseTest.java | 61 ++++++++++--------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java index 30e65225eea..8d62dcfa022 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java @@ -31,7 +31,6 @@ import java.nio.charset.CodingErrorAction; import java.util.ResourceBundle; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; - import javax.servlet.RequestDispatcher; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; @@ -731,7 +730,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable else continue; } - else if (crlf.hasRemaining()) + else if (crlf != null && crlf.hasRemaining()) { result = encoder.encode(crlf, out, true); if (result.isUnderflow()) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java index ebf60e994ba..2dbdbc6a768 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java @@ -18,22 +18,6 @@ package org.eclipse.jetty.server; -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.startsWith; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; @@ -50,7 +34,6 @@ import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; import java.util.Locale; - import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; @@ -87,6 +70,22 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class ResponseTest { @@ -579,24 +578,30 @@ public class ResponseTest assertEquals("foo2/bar2;charset=utf-8", response.getContentType()); } + @Test + public void testPrint_Empty() throws Exception + { + Response response = getResponse(); + response.setCharacterEncoding(UTF_8.name()); + + try(ServletOutputStream outputStream = response.getOutputStream()) + { + outputStream.print("ABC"); + outputStream.print(""); + outputStream.println(); + outputStream.flush(); + } + + String expected = "ABC\r\n"; + assertEquals(expected,BufferUtil.toString(_content, UTF_8)); + } @Test public void testPrintln() throws Exception { Response response = getResponse(); - Request request = response.getHttpChannel().getRequest(); - - SessionHandler session_handler = new SessionHandler(); - session_handler.setServer(_server); - session_handler.setUsingCookies(true); - session_handler.start(); - request.setSessionHandler(session_handler); - HttpSession session = request.getSession(true); response.setCharacterEncoding(UTF_8.name()); - assertThat(session,not(nullValue())); - assertTrue(session.isNew()); - String expected = ""; response.getOutputStream().print("ABC"); expected += "ABC";