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

Only http/2-push real faces resource urls #5425

Merged
merged 1 commit into from Mar 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 23 additions & 24 deletions impl/src/main/java/com/sun/faces/context/ExternalContextImpl.java
Expand Up @@ -42,16 +42,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.faces.RIConstants;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.context.flash.ELFlash;
import com.sun.faces.renderkit.html_basic.ScriptRenderer;
import com.sun.faces.renderkit.html_basic.StylesheetRenderer;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.TypedCollections;
import com.sun.faces.util.Util;

import jakarta.faces.FacesException;
import jakarta.faces.FactoryFinder;
import jakarta.faces.application.ProjectStage;
Expand All @@ -74,6 +64,16 @@
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.PushBuilder;

import com.sun.faces.RIConstants;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.context.flash.ELFlash;
import com.sun.faces.renderkit.html_basic.ScriptRenderer;
import com.sun.faces.renderkit.html_basic.StylesheetRenderer;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.TypedCollections;
import com.sun.faces.util.Util;

/**
* <p>
* This implementation of {@link ExternalContext} is specific to the servlet implementation.
Expand Down Expand Up @@ -576,10 +576,10 @@ public String encodeResourceURL(String url) {
throw new NullPointerException(getExceptionMessageString(NULL_PARAMETERS_ERROR_MESSAGE_ID, "url"));
}

String result = ((HttpServletResponse) response).encodeURL(url);
pushIfPossibleAndNecessary(result);
String encodedURL = ((HttpServletResponse) response).encodeURL(url);
pushIfPossibleAndNecessary(encodedURL);

return result;
return encodedURL;
}

/**
Expand Down Expand Up @@ -1032,26 +1032,25 @@ public void release() {
flash = null;
}

private void pushIfPossibleAndNecessary(String result) {
@SuppressWarnings("unchecked")
private void pushIfPossibleAndNecessary(String encodedURL) {
FacesContext context = FacesContext.getCurrentInstance();

// 1. Don't bother trying to push if we've already pushed this URL for this request
@SuppressWarnings("unchecked")
Set<String> resourceUrls = (Set<String>) context.getAttributes().computeIfAbsent(PUSH_RESOURCE_URLS_KEY_NAME,
k -> new HashSet<>());
if (!context.getApplication().getResourceHandler().isResourceURL(encodedURL)) {
return;
}

Set<String> resourceUrls = (Set<String>) context.getAttributes().computeIfAbsent(PUSH_RESOURCE_URLS_KEY_NAME, k -> new HashSet<>());

if (resourceUrls.contains(result)) {
if (!resourceUrls.add(encodedURL)) {
return;
}
resourceUrls.add(result);

// 2. At this point we know we haven't pushed this URL for this request before
PushBuilder pushBuilder = getPushBuilder(context);

if (pushBuilder != null) {
// and now we also know there was no If-Modified-Since header
pushBuilder.path(result).push();
pushBuilder.path(encodedURL).push();
}

}

private PushBuilder getPushBuilder(FacesContext context) {
Expand Down