Skip to content

Commit

Permalink
optimization on PrintWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms committed Feb 28, 2023
1 parent 175c2f4 commit df43675
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SnippetServletOutputStreamTest {
@Test
void testInjectionForStringContainHeadTag() throws IOException {
String snippet = "\n <script type=\"text/javascript\"> Test </script>";
byte[] html = readFileBytes("beforeSnippetInjection.html", UTF_8);
byte[] html = readFileBytes("beforeSnippetInjection.html");

InjectionState obj = createInjectionStateForTesting(snippet, UTF_8);
InMemoryServletOutputStream out = new InMemoryServletOutputStream();
Expand All @@ -33,22 +33,22 @@ void testInjectionForStringContainHeadTag() throws IOException {
assertThat(obj.getHeadTagBytesSeen()).isEqualTo(-1);
assertThat(injected).isEqualTo(true);

byte[] expectedHtml = readFileBytes("afterSnippetInjection.html", UTF_8);
byte[] expectedHtml = readFileBytes("afterSnippetInjection.html");
assertThat(out.getBytes()).isEqualTo(expectedHtml);
}

@Test
void testInjectionForChinese() throws IOException {
String snippet = "\n <script type=\"text/javascript\"> Test </script>";
byte[] html = readFileBytes("beforeSnippetInjectionChinese.html", UTF_8);
byte[] html = readFileBytes("beforeSnippetInjectionChinese.html");

InjectionState obj = createInjectionStateForTesting(snippet, UTF_8);
InMemoryServletOutputStream out = new InMemoryServletOutputStream();

OutputStreamSnippetInjectionHelper helper = new OutputStreamSnippetInjectionHelper(snippet);
boolean injected = helper.handleWrite(obj, out, html, 0, html.length);

byte[] expectedHtml = readFileBytes("afterSnippetInjectionChinese.html", UTF_8);
byte[] expectedHtml = readFileBytes("afterSnippetInjectionChinese.html");
assertThat(injected).isTrue();
assertThat(obj.getHeadTagBytesSeen()).isEqualTo(-1);
assertThat(out.getBytes()).isEqualTo(expectedHtml);
Expand All @@ -57,7 +57,7 @@ void testInjectionForChinese() throws IOException {
@Test
void testInjectionForStringWithoutHeadTag() throws IOException {
String snippet = "\n <script type=\"text/javascript\"> Test </script>";
byte[] html = readFileBytes("htmlWithoutHeadTag.html", UTF_8);
byte[] html = readFileBytes("htmlWithoutHeadTag.html");

InjectionState obj = createInjectionStateForTesting(snippet, UTF_8);
InMemoryServletOutputStream out = new InMemoryServletOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class TestUtil {

public static byte[] readFileBytes(String resourceName, Charset charsetName) throws IOException {
public static byte[] readFileBytes(String resourceName) throws IOException {
InputStream in =
SnippetPrintWriterTest.class.getClassLoader().getResourceAsStream(resourceName);
ByteArrayOutputStream result = new ByteArrayOutputStream();
Expand All @@ -27,7 +26,7 @@ public static byte[] readFileBytes(String resourceName, Charset charsetName) thr
}

public static String readFile(String resourceName) throws IOException {
return new String(readFileBytes(resourceName, UTF_8), UTF_8);
return new String(readFileBytes(resourceName), UTF_8);
}

private TestUtil() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public boolean handleWrite(
if (!endOfHeadTagFound) {
return false;
}
state.setHeadTagWritten(); // set before write to avoid recursive loop
// set before write to avoid recursive loop
state.setHeadTagWritten();
if (state.getWrapper().isNotSafeToInject()) {
return false;
}
Expand All @@ -71,7 +72,8 @@ public boolean handleWrite(InjectionState state, OutputStream out, int b) throws
if (!state.processByte(b)) {
return false;
}
state.setHeadTagWritten(); // set before write to avoid recursive loop
// set before write to avoid recursive loop
state.setHeadTagWritten();

if (state.getWrapper().isNotSafeToInject()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public SnippetInjectingPrintWriter(

@Override
public void write(String s, int off, int len) {
if (state.isHeadTagWritten()) {
super.write(s, off, len);
return;
}
for (int i = off; i < s.length() && i - off < len; i++) {
write(s.charAt(i));
}
Expand All @@ -35,7 +39,8 @@ public void write(int b) {
if (!endOfHeadTagFound) {
return;
}
state.setHeadTagWritten(); // set before write to avoid recursive loop
// set before write to avoid recursive loop
state.setHeadTagWritten();
if (state.getWrapper().isNotSafeToInject()) {
return;
}
Expand All @@ -45,6 +50,10 @@ public void write(int b) {

@Override
public void write(char[] buf, int off, int len) {
if (state.isHeadTagWritten()) {
super.write(buf, off, len);
return;
}
for (int i = off; i < buf.length && i - off < len; i++) {
write(buf[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,6 @@ public boolean isNotSafeToInject() {
// if content-length was set and response was already committed (headers sent to the client),
// then it is not safe to inject because the content-length header cannot be updated to account
// for the snippet length
return isCommitted() && (contentLength != UNSET);
return contentLength != UNSET && isCommitted();
}
}

0 comments on commit df43675

Please sign in to comment.