Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ private void drain() throws IOException {
bufferFilled = false;
}

@Override
public void flush() throws IOException {
downstream.flush();
}

@Override
public void close() throws IOException {
if (!found) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.Writer;

/**
* An Writer containing a circular buffer with a lookbehind buffer of n bytes. The first time that
* A Writer containing a circular buffer with a lookbehind buffer of n bytes. The first time that
* the latest n bytes matches the marker, a content is injected before.
*/
public class InjectingPipeWriter extends Writer {
Expand Down Expand Up @@ -40,24 +40,24 @@ public InjectingPipeWriter(
}

@Override
public void write(int b) throws IOException {
public void write(int c) throws IOException {
if (found) {
downstream.write(b);
downstream.write(c);
return;
}

if (bufferFilled) {
downstream.write(lookbehind[pos]);
}

lookbehind[pos] = (char) b;
lookbehind[pos] = (char) c;
pos = (pos + 1) % lookbehind.length;

if (!bufferFilled) {
bufferFilled = pos == 0;
}

if (marker[matchingPos++] == b) {
if (marker[matchingPos++] == c) {
if (matchingPos == marker.length) {
found = true;
downstream.write(contentToInject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public ServletOutputStream getOutputStream() throws IOException {
outputStream =
new WrappedServletOutputStream(
super.getOutputStream(),
rumInjector.getMarker(encoding),
rumInjector.getSnippet(encoding),
rumInjector.getMarkerBytes(encoding),
rumInjector.getSnippetBytes(encoding),
this::onInjected);
}
return outputStream;
Expand All @@ -50,7 +50,10 @@ public PrintWriter getWriter() throws IOException {
printWriter =
new PrintWriter(
new InjectingPipeWriter(
delegate, rumInjector.getMarker(), rumInjector.getSnippet(), this::onInjected));
delegate,
rumInjector.getMarkerChars(),
rumInjector.getSnippetChars(),
this::onInjected));
}
return printWriter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public ServletOutputStream getOutputStream() throws IOException {
outputStream =
new WrappedServletOutputStream(
super.getOutputStream(),
rumInjector.getMarker(encoding),
rumInjector.getSnippet(encoding),
rumInjector.getMarkerBytes(encoding),
rumInjector.getSnippetBytes(encoding),
this::onInjected);
}
return outputStream;
Expand All @@ -50,7 +50,10 @@ public PrintWriter getWriter() throws IOException {
printWriter =
new PrintWriter(
new InjectingPipeWriter(
delegate, rumInjector.getMarker(), rumInjector.getSnippet(), this::onInjected));
delegate,
rumInjector.getMarkerChars(),
rumInjector.getSnippetChars(),
this::onInjected));
}
return printWriter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ abstract class HttpServerTest<SERVER> extends WithHttpServer<SERVER> {
def response = client.newCall(request).execute()
then:
assert response.code() == 200
assert response.body().string().contains(new String(RumInjector.get().getSnippet("UTF-8"), "UTF-8")) == expected
assert response.body().string().contains(new String(RumInjector.get().getSnippetBytes("UTF-8"), "UTF-8")) == expected
assert response.header("x-datadog-rum-injected") == (expected ? "1" : null)
where:
mime | expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean isEnabled() {
* @return The HTML snippet chars to inject, {@code null} if RUM injection is disabled.
*/
@Nullable
public char[] getSnippet() {
public char[] getSnippetChars() {
if (!this.enabled) {
return null;
}
Expand All @@ -87,7 +87,7 @@ public char[] getSnippet() {
* @return The HTML snippet to inject, {@code null} if RUM injection is disabled.
*/
@Nullable
public byte[] getSnippet(String encoding) {
public byte[] getSnippetBytes(String encoding) {
if (!this.enabled) {
return null;
}
Expand All @@ -100,7 +100,7 @@ public byte[] getSnippet(String encoding) {
* @return The marker chars, {@code null} if RUM injection is disabled.
*/
@Nullable
public char[] getMarker() {
public char[] getMarkerChars() {
if (!this.enabled) {
return null;
}
Expand All @@ -114,7 +114,7 @@ public char[] getMarker() {
* @return The marker bytes, {@code null} if RUM injection is disabled.
*/
@Nullable
public byte[] getMarker(String encoding) {
public byte[] getMarkerBytes(String encoding) {
if (!this.enabled) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class RumInjectorTest extends DDSpecification {

then:
!injector.isEnabled()
injector.getMarker(UTF8) == null
injector.getSnippet(UTF8) == null
injector.getMarkerBytes(UTF8) == null
injector.getSnippetBytes(UTF8) == null
}

void 'invalid config injector'() {
Expand All @@ -36,10 +36,10 @@ class RumInjectorTest extends DDSpecification {

then:
!injector.isEnabled()
injector.getMarker(UTF8) == null
injector.getSnippet(UTF8) == null
injector.getSnippet() == null
injector.getMarker() == null
injector.getMarkerBytes(UTF8) == null
injector.getSnippetBytes(UTF8) == null
injector.getSnippetChars() == null
injector.getMarkerChars() == null
}

void 'enabled injector'() {
Expand All @@ -56,9 +56,9 @@ class RumInjectorTest extends DDSpecification {

then:
injector.isEnabled()
injector.getMarker(UTF8) != null
injector.getSnippet(UTF8) != null
injector.getSnippet() != null
injector.getMarker() != null
injector.getMarkerBytes(UTF8) != null
injector.getSnippetBytes(UTF8) != null
injector.getSnippetChars() != null
injector.getMarkerChars() != null
}
}
Loading