Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snippet inject support like <head lang="en"> #8736

Merged
merged 12 commits into from
Aug 9, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public boolean isContentTypeTextHtml() {
if (contentType == null) {
contentType = super.getHeader("content-type");
}
return contentType != null && contentType.startsWith("text/html");
return contentType != null
&& (contentType.startsWith("text/html") || "application/xhtml+xml".equals(contentType));
oliver-zhang marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public boolean isContentTypeTextHtml() {
if (contentType == null) {
contentType = super.getHeader("content-type");
}
return contentType != null && contentType.startsWith("text/html");
return contentType != null
&& (contentType.startsWith("text/html") || "application/xhtml+xml".equals(contentType));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// this is shared by both ServletOutputStream and PrintWriter injection
public class InjectionState {
private static final int HEAD_TAG_WRITTEN_FAKE_VALUE = -1;
private static final int HEAD_TAG_LENGTH = "<head>".length();
private static final int HEAD_TAG_PREFIX_LENGTH = 4;
oliver-zhang marked this conversation as resolved.
Show resolved Hide resolved
private final SnippetInjectingResponseWrapper wrapper;
private int headTagBytesSeen = 0;

Expand Down Expand Up @@ -40,12 +40,12 @@ public boolean processByte(int b) {
if (isHeadTagWritten()) {
return false;
}
if (inHeadTag(b)) {
if (inHeadTag(b) && headTagBytesSeen < HEAD_TAG_PREFIX_LENGTH) {
oliver-zhang marked this conversation as resolved.
Show resolved Hide resolved
headTagBytesSeen++;
} else {
} else if (headTagBytesSeen != HEAD_TAG_PREFIX_LENGTH) {
headTagBytesSeen = 0;
}
if (headTagBytesSeen == HEAD_TAG_LENGTH) {
if (headTagBytesSeen == HEAD_TAG_PREFIX_LENGTH && b == '>') {
setHeadTagWritten();
return true;
} else {
Expand All @@ -64,10 +64,9 @@ private boolean inHeadTag(int b) {
return true;
} else if (headTagBytesSeen == 4 && b == 'd') {
return true;
} else if (headTagBytesSeen == 5 && b == '>') {
return true;
} else {
return headTagBytesSeen == HEAD_TAG_PREFIX_LENGTH;
}
return false;
}

public SnippetInjectingResponseWrapper getWrapper() {
Expand Down
Loading